<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*************************************
  * Created : May 2015
  * Creator : Fauzan Rabbani
  * Email   : jhanojan@gmail.com
  * Framework ver. : CI ver.2.0
*************************************/	

class stok_pabrikasi extends CI_Controller {
	
		var $utama ='stok_pabrikasi';
		var $title ='Proses Pabrikasi'; 
	function __construct()
	{
		parent::__construct();permissionz();
		$this->load->library('flexigrid');
                $this->load->helper('flexigrid');
                error_reporting(0);
                $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
                $this->output->set_header('Pragma: no-cache');
                $this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}
	
	function index()
	{
		$this->main();
	}
	
	function main($cat=null)
	{
		//Migrasi 1 Feb 14
		//permissionz();
		//Set Global
		//permission();
		//$data = GetHeaderFooter();
		$data['content'] = 'contents/'.$this->utama.'/view';
		
		$data['js_grid']=$this->get_column($cat);
		//$data['list']=GetAll($this->utama);
		//End Global
		
		//Attendance
		
		$this->load->view('layout/main',$data);
	}
	
	function listcol(){
		
            $colModel['idnya'] = array('ID',50,TRUE,'left',2,TRUE);
            $colModel['id'] = array('ID',100,TRUE,'left',2,TRUE);
            $colModel['kode_order'] = array('PO Number',110,TRUE,'left',2);
            $colModel['so_number'] = array('SO Number',110,TRUE,'left',2);
            $colModel['pabriks'] = array('Pabrik',110,TRUE,'left',2);
            $colModel['date_dp'] = array('Tanggal Down Payment',110,TRUE,'left',2);
            $colModel['date_bp'] = array('Tanggal Balance Payment',110,TRUE,'left',2);
            $colModel['po_date'] = array('Tanggal Order',110,TRUE,'left',2);
            $colModel['est_selesai'] = array('Estimasi Selesai',110,TRUE,'left',2);
            $colModel['status'] = array('Status',110,TRUE,'left',2);
            $colModel['tgl_selesai'] = array('Tanggal Selesai',110,TRUE,'left',2);
            $colModel['keterangan'] = array('Keterangan',110,TRUE,'left',2);
            return $colModel;
	}
	
	function get_column($cat){
            $colModel=$this->listcol(); 
	
        
            $gridParams = array(
                'rp' => 25,
                'rpOptions' => '[10,20,30,40]',
                'pagestat' => 'Displaying: {from} to {to} of {total} items.',
                'blockOpacity' => 0.5,
                'title' => '', 
                'showTableToggleBtn' => TRUE
		);
        
           $buttons[] = array('select','check','btn');
            $buttons[] = array('deselect','uncheck','btn');
            $buttons[] = array('separator');
            if($cat==null){
            if(izin('c'))$buttons[] = array('add','add','btn');
            $buttons[] = array('separator');
            if(izin('u'))$buttons[] = array('edit','edit','btn');
            if(izin('d'))$buttons[] = array('delete','delete','btn');
            $buttons[] = array('separator');
            if(izin('u'))$buttons[] = array('pengiriman','truck','btn');
            }
            $buttons[] = array('separator');
            if(izin('u'))$buttons[] = array('cetak p.o','print','btn');
		
            return $grid_js = build_grid_js('flex1',site_url($this->utama."/get_record/".$cat),$colModel,'title','asc',$gridParams,$buttons);
	}
	
	function get_flexigrid($cat)
        {

            //Build contents query
        if($cat==null){$this->db->where("sv_a.status",'proses');}else{
            $this->db->where("sv_a.status",'selesai');
        }
            $this->db->select("sv_a.*,b.title pabriks,c.title produks")->from($this->utama.' sv_a');
            $this->db->join('sv_master_pabrik b', "sv_a.pabrik=b.id", 'left');
            $this->db->join('sv_master_produk c', "sv_a.produk=c.id", 'left');
            $this->flexigrid->build_query();

            //Get contents
            $return['records'] = $this->db->get();

            //Build count query
            //$this->db->where("param",'in');
        if($cat==null){$this->db->where("status",'proses');}else{
            $this->db->where("status",'selesai');
        }
            $this->db->select("count(id) as record_count")->from($this->utama);
            $this->flexigrid->build_query(FALSE);
            $record_count = $this->db->get();
            $row = $record_count->row();

            //Get Record Count
            $return['record_count'] = $row->record_count;

            //Return all
            return $return;
        }
	
	function get_record($cat){
		$colModel=$this->listcol();
		$valid_fields = array('id','code','jenis_produk','brand',);

            $this->flexigrid->validate_post('id','ASC',$valid_fields);
            $records = $this->get_flexigrid($cat);

            $this->output->set_header($this->config->item('json_header'));

            $record_items = array();
            $a=0;
            foreach ($records['records']->result() as $row)
            {/*
			if($row->status=='y'){$status='Aktif';}
			elseif($row->status=='n'){$status='Tidak Aktif';}
			elseif($row->status=='s'){$status='Suspended';}*/ 
			
				$record_items[$a][]=$row->id;
				$record_items[$a][]=$row->id;
				$record_items[$a][]=$row->id;
				$b=2;
				foreach($colModel as $key=>$cm){
					if($key=='auth'){
                                        $record_items[$a][$b]="<a href='".base_url()."master_sales/auth/$row->id'>Menu</a>";
                                        
                                        }
					elseif($key=='username'){
                                        $record_items[$a][$b]=GetValue('username','sv_admin',array('marketing'=>'where/'.$row->id));
                                        
                                        }
					elseif($key!='idnya' && $key!='id' && $key!='username'){
                                        $record_items[$a][$b]=$row->$key;}
				$b++;
				}
						$a++;
            }
            return $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));;
	}  

	function deletec()
	{		
		//return true;
		if(izin('d')){
		$countries_ids_post_array = explode(",",$this->input->post('items'));
		array_pop($countries_ids_post_array);
		foreach($countries_ids_post_array as $index => $country_id){
			/*if (is_numeric($country_id) && $country_id > 0) {
				$this->delete($country_id);}*/
				$data['status']='InAktif';
				$this->db->where('id',$country_id);
				$this->db->update($this->utama,$data);
			$this->db->delete($this->utama,array('id'=>$country_id));				
		}
			echo 'ok';
		}
		else{
			echo 'error';
		}
		//$error = "Selected countries (id's: ".$this->input->post('items').") deleted with success. Disabled for demo";
		//echo "Sukses!";
	}
	
	function form($id=null){
		$mar=  webmastergrup();
                $div=GetValue('divisi','sv_master_sales',array('id'=>'where/'.$mar));
                $jenisaktifitas=array();
                if(!diaadmin()){
                    $jenisaktifitas=array('type'=>'where/'.$div,'notes'=>'order/asc');
                }
                if($div=='4'){
                    $tipe_activity='support';
                }else{
                    $tipe_activity='sales';
                }
		if($id!=NULL){
		permissionz('u');
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('pabrikasi_id'=>'where/'.$id));
		}
		else{
			
		permissionz('c');
			$data['type']='New';
		}  
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-',array('title'=>'order/asc'));
                
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',$jenisaktifitas,'notes','id',NULL,array(),array('id'=>array(24,25,8,31)));
                if($id>0)
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',$jenisaktifitas,'notes','id',NULL,array(),array());
                
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('title'=>'order/asc'));
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array('title'=>'order/asc'));
                $data['opt_brand']=GetAll('sv_master_brand',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array('nama_lengkap'=>'order/asc'),'nama_lengkap');
                }
                
                $data['opt_produk']=GetAll('sv_master_produk',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                $data['opt_pabrik']=GetOptAll('master_pabrik','-Pabrik-');
		$data['opt_gudang']=GetOptAll('master_gudang','-Gudang-');
                $data['opt_satuan']=GetAll('sv_ref_satuan',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                $data['content'] = 'contents/'.$this->utama.'/edit';
		//End Global 
		 
		//Attendance
		
		$this->load->view('layout/main',$data);
	}  
        function deletes($id,$doc){
                $this->db->where('id',$id);
                $this->db->delete('sv_stok_pabrikasi_detail');
                redirect('stok_pabrikasi/form/'.$doc);
        }
	function form_po($id=null){
                $data['title']='Input PO';
		$mar=  webmastermarketing();
                $div=GetValue('divisi','sv_master_sales',array('id'=>'where/'.$mar));
                $jenisaktifitas=array();
                if(!diaadmin()){
                    $jenisaktifitas=array('type'=>'where/'.$div,'notes'=>'order/asc');
                }
                if($div=='4'){
                    $tipe_activity='support';
                }else{
                    $tipe_activity='sales';
                }
		if($id!=NULL){
		permissionz('u');
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('activity'=>'where/'.$id));
		}
		else{
			
		permissionz('c');
			$data['type']='New';
		} 
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-',array('title'=>'order/asc'));
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',array(),'notes');
                
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('title'=>'order/asc'));
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array('title'=>'order/asc'));
                $data['opt_brand']=GetAll('sv_master_brand',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array('nama_lengkap'=>'order/asc'),'nama_lengkap');
                }
                $data['content'] = 'contents/'.$this->utama.'/edit';
                $data['poquo']='po';
		//End Global 
		 
		//Attendance
		
		$this->load->view('layout/main',$data);
	} 
        
	function form_po_new($id=null){
                $data['title']='Input PO (New)';
		$mar=  webmastermarketing();
                $div=GetValue('divisi','sv_master_sales',array('id'=>'where/'.$mar));
                $jenisaktifitas=array();
                if(!diaadmin()){
                    $jenisaktifitas=array('type'=>'where/'.$div,'notes'=>'order/asc');
                }
                if($div=='4'){
                    $tipe_activity='support';
                }else{
                    $tipe_activity='sales';
                }
		if($id!=NULL){
		permissionz('u');
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('activity'=>'where/'.$id));
		}
		else{
			
		permissionz('c');
			$data['type']='New';
		} 
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-',array('title'=>'order/asc'));
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',array(),'notes');
                
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('title'=>'order/asc'));
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array('title'=>'order/asc'));
                $data['opt_brand']=GetAll('sv_master_brand',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array('nama_lengkap'=>'order/asc'),'nama_lengkap');
                }
                $data['content'] = 'contents/'.$this->utama.'/edit';
                $data['poquo']='po';
		//End Global 
		 
		//Attendance
		
		$this->load->view('layout/main',$data);
	} 
	function form_quotation($id=null){
                $data['title']='Input Quotation';
		$mar=  webmastermarketing();
                $div=GetValue('divisi','sv_master_sales',array('id'=>'where/'.$mar));
                $jenisaktifitas=array();
                if(!diaadmin()){
                    $jenisaktifitas=array('type'=>'where/'.$div,'notes'=>'order/asc');
                }
                if($div=='4'){
                    $tipe_activity='support';
                }else{
                    $tipe_activity='sales';
                }
		if($id!=NULL){
		permissionz('u');
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('activity'=>'where/'.$id));
		}
		else{
			
		permissionz('c');
			$data['type']='New';
		} 
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-',array('title'=>'order/asc'));
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',array(),'notes');
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('title'=>'order/asc'));
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array('title'=>'order/asc'));
                $data['opt_brand']=GetAll('sv_master_brand',array('status'=>'where/Aktif','title'=>'order/asc'))->result_array();
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array('nama_lengkap'=>'order/asc'),'nama_lengkap');
                }
                $data['content'] = 'contents/'.$this->utama.'/edit';
		//End Global 
		 
                $data['poquo']='quo';
		//Attendance
		
		$this->load->view('layout/main',$data);
	} 
        function load_sales(){
            $oi= GetValue('divisi','master_sales',array('id'=>'where/'.$_POST['s']));
            //lastq();
            if($oi==1 && $_POST['t']=='form_po'){
                echo '24';
            }
            elseif($oi==2 && $_POST['t']=='form_po'){
                echo '25';
            }
            elseif($oi==1 && $_POST['t']=='form_quotation'){
                echo '8';
            }
            elseif($oi==2 && $_POST['t']=='form_quotation'){
                echo '31';
            }
            elseif($_POST['t']=='form_po_new'){
                echo '37';
            }else{
                echo $oi;
            }
        }
	function sum($id=null){
		
		if($id!=NULL){
		permissionz('u');
                        $data['view']=TRUE;
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('activity'=>'where/'.$id));
		}
		else{
			
		permissionz('c');
			$data['type']='New';
		}
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-');
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',array(),'title','id','notes');
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array(),'code','id','title');
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array());
                $data['opt_brand']=GetAll('sv_master_brand')->result_array();
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array(),'nama_lengkap');
                }
                $data['content'] = 'contents/'.$this->utama.'/sum';
		//End Global 
		 
		//Attendance
		
		$this->load->view('layout/main',$data);
	}  
	
	function view($id=null){
		
		if($id!=NULL){
		//permissionz('u');
                    $webmaster=$this->session->userdata('webmaster_id');
                    $this->db->query("UPDATE sv_notif SET status='Y' WHERE type='1' AND prospek='$id' AND `to`='$webmaster'");
			$filter=array('id'=>'where/'.$id);
			$data['type']='Edit';
                        $data['view']=TRUE;
			$data['list']=GetAll($this->utama,$filter);
                        $data['detail']=GetAll('sv_stok_pabrikasi_detail',array('activity'=>'where/'.$id));
		
		$data['opt_pelanggan']=GetOptAll('master_pelanggan','-Pelanggan-');
		$data['opt_aktivitas']=GetOptAll('ref_activity','-Aktivitas-',array(),'title','id','notes');
		$data['opt_jenis_produk']=GetOptAll('master_jenis_produk','-Jenis Produk-',array(),'code','id','title');
		$data['opt_aktivitas_status']=GetOptAll('ref_activity_status','-Status-',array());
                if($this->session->userdata('webmaster_grup')=='3'){
                    $userdetails=GetAll('sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')))->row_array();
                    $data['opt_sales'][$userdetails['marketing']]=GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$userdetails['marketing']));
                }else{
		$data['opt_sales']=GetOptAll('master_sales','-Sales-',array(),'nama_lengkap');
                }
                $data['content'] = 'contents/'.$this->utama.'/sum';
		//End Global 
		 
		//Attendance
		
		$this->load->view('layout/main',$data);
	
                }
        }  
        
        
        
	function submit(){
            error_reporting(E_ALL);
		$webmaster_id=$this->session->userdata('webmaster_id');
		$id = $this->input->post('id');
		$GetColumns = GetColumns('sv_'.$this->utama);
		foreach($GetColumns as $r)
		{ 
			$data[$r['Field']] = $this->input->post($r['Field']);
			$data[$r['Field']."_temp"] = $this->input->post($r['Field']."_temp");
			
			if(!$data[$r['Field']] && !$data[$r['Field']."_temp"]) unset($data[$r['Field']]);
			unset($data[$r['Field']."_temp"]);
		}
                //if(!isset($data['nilai_po']))$data['nilai_po']=0;
                //if(!isset($data['nilai_quo']))$data['nilai_quo']=0;
                //$data['nilai_po']=  str_replace('.','',$data['nilai_po']);
                //$data['nilai_quo']=  str_replace('.','',$data['nilai_quo']);
                //$data['nilai_po']=  ($data['nilai_po']=='0'?null:$data['nilai_po']);
                //$data['nilai_quo']=  ($data['nilai_quo']=='0'?null:$data['nilai_quo']);
                unset($data['produk']);
		/* if(!$this->input->post('is_active')){$data['is_active']='InActive';}
		else{$data['is_active']='Active';} */
                //print_mz($this->input->post());
		
		if($id != NULL && $id != '')
		{
			/* if(!$this->input->post('password')){unset($data['password']);}
			else{$data['password']=md5($this->config->item('encryption_key').$this->input->post("password"));} */
			
			if(izin('u')){
			$data['modify_by'] = $webmaster_id;
			$data['modify_on']=date("Y-m-d");
			$this->db->where("id", $id);
			$this->db->update('sv_'.$this->utama, $data);
			
			$this->session->set_flashdata("message", 'Sukses diedit');}
			else{
				$this->session->set_flashdata("message", 'Anda Tidak Diizinkan Mengedit Data');
			}
		}
		else
		{
			//if($this->input->post('password')){$data['password']=md5($this->config->item('encryption_key').$this->input->post("password"));}
			//if(!$this->input->post('avatar')){$data['avatar']='default.png';}
			
			if(izin('c')){
			$data['created_by'] = $webmaster_id;
			$data['created_on'] = date("Y-m-d H:i:s");
			$this->db->insert('sv_'.$this->utama, $data);
			$id = $this->db->insert_id();
			$this->session->set_flashdata("message", 'Sukses ditambahkan');
                        //notif($id,'stok_pabrikasi');
                        
                        }
			else{
				$this->session->set_flashdata("message", 'Anda Tidak Diizinkan Menambah Data');
			}
		}
                $id_detail=$this->input->post('id_detail');
		$jenis=$this->input->post('jenis_produk');
		$produk=$this->input->post('produk');
		$a=0; 
                if(!empty($jenis)){
		foreach($jenis as $jen){
			if($_POST['brand'][$a]!=''){
				
					$divs['pabrikasi_id']=$id;
					$divs['brand']=$_POST['brand'][$a];
					$divs['jenis_produk']=$jen;
					$divs['produk']=$produk[$a];
                                        $divs['total']=$_POST['qty'][$a];
                                        $divs['satuan']=$_POST['satuan'][$a];
                                        $divs['amount']=$_POST['amount'][$a];
				
				if(isset($id_detail[$a]) && $id_detail[$a]!=''){
					//$divs['id']=$iddivisi[$a];
					$divs['modify_by'] = $webmaster_id;
					$divs['modify_on']=date("Y-m-d H:i:s");
					$this->db->where('id',$id_detail[$a]);
					$this->db->update('sv_stok_pabrikasi_detail',$divs);

				}
				else{
			$divs['created_by'] = $webmaster_id;
			$divs['created_on'] = date("Y-m-d H:i:s");
			$this->db->insert('sv_stok_pabrikasi_detail', $divs);}
			}
			$a++;
		}
                }
//                if($data['status']=='selesai'){
//                    if(GetValue('id', 'sv_stok_pengiriman',array('pabrikasi_id'=>'where/'.$id))==0){
//                    $pengiriman['pabrikasi_id']=$id;
//                    $pengiriman['kode_order']=$data['kode_order'];
//                    $pengiriman['so_number']=$data['so_number'];
//                    $pengiriman['pabrik']=$data['pabrik'];
//                    //$pengiriman['produk']=$data['produk'];
//			$pengiriman['created_by'] = $webmaster_id;
//			$pengiriman['created_on'] = date("Y-m-d H:i:s");
//			$this->db->insert('sv_stok_pengiriman', $pengiriman);
//                    
//                    }
//                   // lastq();
//                    
//                }
		
		redirect($this->utama);
		
	}
        function loadproduk(){
            $ids=rand(111,9999);
            echo form_dropdown('produk[]',GetOptAll('master_produk','-Produk-',array('jenis_produk'=>'where/'.$_POST['v'])),(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='select2' id='produk$ids'");
            echo"<script>
                    $(document).ready(function(e){ 
                        $('#produk$ids').css('width','200px').select2({allowClear:true});
				$('#select2-multiple-style .btn').on('click', function(e){
					var target = $(this).find('input[type=radio]');
					var which = parseInt(target.val());
					if(which === 2) $('.select2').addClass('tag-input-style');
					 else $('.select2').removeClass('tag-input-style');
				});});</script>";
        }
        function loadivisi(){
            $ids=rand(111,9999);
            echo form_dropdown('pelanggan_divisi',GetOptAll('master_pelanggan_divisi','-Divisi-',array('pelanggan'=>'where/'.$_POST['v'],'title'=>'order/asc')),(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='select2' id='pelanggan_divisi'");
            echo"<script>
                    $(document).ready(function(e){ 
                        $('#pelanggan_divisi').css('width','200px').select2({allowClear:true});
				$('#select2-multiple-style .btn').on('click', function(e){
					var target = $(this).find('input[type=radio]');
					var which = parseInt(target.val());
					if(which === 2) $('.select2').addClass('tag-input-style');
					 else $('.select2').removeClass('tag-input-style');
				});});</script>";
        }
        function carinilai(){
            $v=$_POST['v'];
            echo GetValue('nilai','sv_ref_activity',array('id'=>'where/'.$v));
        }
        function loadjenis(){
            $v=$_POST['v'];
            $id=$_POST['id'];
            $opt_brand=GetOptAll('master_jenis_produk','-Jenis Produk-',array('brand'=>'where/'.$v,'title'=>'order/asc'),'title','id');
            echo form_dropdown_('jenis_produk[]',$opt_brand,'',"id='jenis_produk-$id' onchange='gantijenis(this.value,$id)'");
        }
        function loadprodukz(){
            $v=$_POST['v'];
            $id=$_POST['id'];
            $opt_brand=GetOptAll('master_produk','-Produk-',array('jenis_produk'=>'where/'.$v,'title'=>'order/asc'),'title','id');
            echo form_dropdown_('produk[]',$opt_brand,'',"id='produk-$id'");
        }
        function to_xls(){
            error_reporting(E_ALL);
		$op = explode(",",$this->input->post('items'));
                foreach($op as $or){
                    $inz[]="$or";
                }
                $oops=implode($inz,'%');
               // print_mz($op);
               // array_pop($op); 
		//array_pop($countries_ids_post_array);
           // $from=$_POST['from_y'].'-'.$_POST['from_m'].'-'.$_POST['from_d'];
            //    $to=$_POST['to_y'].'-'.$_POST['to_m'].'-'.$_POST['to_d'];
              //  $by=$_POST['marketing'];
//		
//		$div=divisinya(); 
//                $anaknya=anaknya();
//            $this->db->select("sv_a.id id,b.nama_lengkap sales,d.notes aktivitas,c.title pelanggan,sv_a.tanggal tanggal,e.title status,sv_a.nilai nilai,sv_a.platform platform,sv_a.created_on created_on,sv_a.latitude latitude,sv_a.longitude longitude,b.divisi divisi")->from("$this->utama sv_a");
//            $this->db->join('master_sales b', "sv_a.sales=b.id", 'left');
//            $this->db->join('master_pelanggan c', "sv_a.pelanggan=c.id", 'left');
//            $this->db->join('ref_activity d', "sv_a.jenis_aktivitas=d.id", 'left');
//            $this->db->join('ref_activity_status e', "sv_a.status=e.id", 'left');
             //$this->db->select('*')->from('sv_activity_view');
             $q="SELECT * FROM sv_activity_view WHERE id IN (".$oops.")";
            //this->db->where_in('id',$op);
            //if(!diaadmin()){$this->db->where_in('b.leader',$anaknya);}
            //if(diasales()){$this->db->or_where('sv_a.created_by',$this->session->userdata('webmaster_id'));}
           //if($from!='--')$this->db->where('sv_a.tanggal >=', $from);
          //  if($to!='--')$this->db->where('sv_a.tanggal <=', $to);
           // if($by!='' )$this->db->where('sales', $by);
			//$oaaa=$this->db->query($q);
			//lastq();
		//$data['lastq']=$this->db->last_query();
                
                
                //$isisearch=array();
                //if(dialeader()){
               //                    $isisearch= array('divisi'=>'where/'.divisinya());
               // }
		//$data['opt_search']=GetOptAll('master_sales','-Sales-',$isisearch,'nama_lengkap');
		//$data['content'] = 'contents/'.$this->utama.'/view';
		
		//$data['lastq']=$oaaa->result_array();
               // lastq();
 
			// Mendefinisikan nama file ekspor "hasil-export.xls"
 
			// Tambahkan table
				//include 'table.html';
                redirect('stok_pabrikasi/render_xls/'.$oops);
            
        }
        function render_xls($lastq=null){
            
                $data['col']=$this->listcol();  
		header("Content-type: application/vnd-ms-excel");
		header("Content-Disposition: attachment; filename=hasil-export".date('YmdHis').".xls");
		$op = explode("%",$lastq);
                foreach($op as $or){
                    $inz[]="'$or'";
                }
                $oops=implode($inz,',');
             $q="SELECT * FROM sv_activity_view WHERE id IN (".$oops.")";
                
		$data['lastq']=$this->db->query($q)->result_array();
            $this->load->view('contents/'.$this->utama.'/export',$data);
        }
        function render_pdf($lastq=null){
  //die("ok");
  $this->load->helper('dompdf');
  
                $data['col']=$this->listcol();  
            error_reporting(E_ALL);
		$op = explode("%",$lastq);
                foreach($op as $or){
                    $inz[]="$or";
                }
                $oops=implode($inz,',');
             $q="SELECT * FROM sv_activity_view WHERE id IN (".$oops.")";
                
		$data['lastq']=$this->db->query($q)->result_array();
               // lastq();
    //load content html
    $html = $this->load->view('contents/stok_pabrikasi/export',$data,TRUE);
    // create pdf using dompdf
    $filename = "hasil-export";
    $paper = 'A4'; 
    $orientation = 'potrait';
   // .//ECHO "OK";
    pdf_create($html, $filename, $paper, $orientation);
 // $this->load->library('pdf');
 // $this->pdf->load_view('contents/stok_pabrikasi/export',$data);
 // $this->pdf->render();
 // $this->pdf->stream("hasil-export.pdf");
        }
        
	function encryptq($data, $size){
		$length = $size - strlen($data) % $size;
		return $data . str_repeat(chr($length), $length);
	}
	function decryptq($data){
		return substr($data, 0, -ord($data[strlen($data) - 1]));
	}
	function loadproduknumber(){
            $v=$this->input->post('v');
            echo GetValue('part_number','master_produk',array('id'=>'where/'.$v));
        }
        function pengiriman($id=null){
		
		if($id!=NULL){
		permissionz('u');
                        $data['id']=$id;
			$filter=array('pabrikasi_id'=>'where/'.$id);
			$data['type']='Edit';
			$data['val']=GetAll('sv_stok_pabrikasi',array('id'=>'where/'.$id))->row_array();
			$data['list']=GetAll('sv_stok_pabrikasi_detail',$filter)->result_array();
                        //print_mz($data['list']);
                        //lastq();
                        //$data['opt_jenis']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('brand'=>'where/'.$data['list']['brand']));
                        //$data['opt_produk']=GetOptAll('master_produk','-Produk-',array('brand'=>'where/'.$data['list']['brand'],'jenis_produk'=>'where/'.$data['list']['jenis_produk']));
                        $data['opt_produk']=GetOptAll('master_produk','-Produk-',array('status'=>'where/Aktif'));
                        $data['opt_pabrik']=GetOptAll('master_pabrik','-Pabrik-');
		}
		else{
			
		permissionz('c');
			$data['type']='New';
                        $data['opt_jenis']=GetOptAll('master_jenis_produk','-Jenis Produk-',array('status'=>'where/abcdefghijkl'));
                        $data['opt_produk']=GetOptAll('master_produk','-Produk-',array('status'=>'where/Aktif'));
                        $data['opt_pabrik']=GetOptAll('master_pabrik','-Pabrik-');
		}
		$data['opt_brand']=GetOptAll('master_brand','-Merk-',array('status'=>'Aktif'));
		$data['opt_currency']=GetOptAll('master_currency','-Currency-',array('status'=>'Aktif'),'code');
		$data['opt_metric']=GetOptAll('master_metric','-Metric-',array('status'=>'Aktif'),'code');
		$data['opt_klasifikasi']=GetOptAll('sv_ref_produk_klas','-Klasifikasi-');
		$data['opt_gudang']=GetOptAll('master_gudang','-Gudang-');
		$data['content'] = 'contents/'.$this->utama.'/masuk';
		//End Global
		
		//Attendance
		
		$this->load->view('layout/main',$data);
	}
        function submit_masuk(){
           // print_mz($this->input->post());
		$webmaster_id=$this->session->userdata('webmaster_id');
		$id = $this->input->post('id');
		$GetColumns = GetColumns('sv_'.$this->utama);
                //print_mz($this->input->post());
		foreach($GetColumns as $r)
		{
			$data[$r['Field']] = $this->input->post($r['Field']);
			$data[$r['Field']."_temp"] = $this->input->post($r['Field']."_temp");
			
			if(!$data[$r['Field']] && !$data[$r['Field']."_temp"]) unset($data[$r['Field']]);
			unset($data[$r['Field']."_temp"]);
		}
                //if($data['status']=='selesai'){
                    //if(GetValue('id', 'sv_stok_pengiriman',array('pabrikasi_id'=>'where/'.$id))==0){
                    $pengiriman['pabrikasi_id']=$id;
                    $pengiriman['kode_order']=$data['kode_order'][0];
                    $pengiriman['so_number']=$data['so_number'][0];
                    $pengiriman['keterangan']=$this->input->post('keterangan_kirim');
                    $pengiriman['forwarder']=$webmaster_id;
                    $pengiriman['pabrik']=$data['pabrik'];
                    //$pengiriman['produk']=$data['produk'];
			$pengiriman['created_by'] = $webmaster_id;
			$pengiriman['created_on'] = date("Y-m-d H:i:s");
			$this->db->insert('sv_stok_pengiriman', $pengiriman);
                        $pengiriman_id=$this->db->insert_id();
                  //  }
                   // lastq();
                    
                //}
		/* if(!$this->input->post('is_active')){$data['is_active']='InActive';}
		else{$data['is_active']='Active';} */
                $index=0;
                $pabrikasi_detail=$this->input->post('id_pabrikasi_produk');
                $kode_order=$this->input->post('kode_order');
                $so_number=$this->input->post('so_number');
                $brand=$this->input->post('brand');
                $jenis_produk=$this->input->post('jenis_produk');
                $produk=$this->input->post('produk');
                $total=$this->input->post('total');
                $satuan=$this->input->post('satuan');
                $keterangan=$this->input->post('keterangan');
                //$gudang=$this->input->post('gudang');
                //$tanggal_tiba=$this->input->post('tanggal_tiba');
                foreach($_POST['kode_order'] as $ko){
                    $this->db->where('id',$pabrikasi_detail[$index]);
                    $this->db->update('sv_stok_pabrikasi_detail',array('gudang'=>$gudang[$index],'tgl_tiba'=>$tanggal_tiba[$index]));
                    $masuk=array(
                        'pabrikasi_id'=>$id,
                        'pabrikasi_detail_id'=>$pabrikasi_detail[$index],
                        'pengiriman_id'=>$pengiriman_id,
                        'kode_order'=>$kode_order[$index],
                        'so_number'=>$so_number[$index],
                        'brand'=>$brand[$index],
                        'jenis_produk'=>$jenis_produk[$index],
                        'produk'=>$produk[$index],
                        //'total'=>$total[$index],
                        'satuan'=>$satuan[$index]
                    );
                    //$this->db->select('id')->from('sv_stok_pengiriman_detail')->where($masuk);
                    //$cari=$this->db->get();
                    //if($cari->num_rows()==0){
                    $masuk['total']=$total[$index];
                    $masuk['keterangan']=$keterangan[$index];
                    if($masuk['total']>0){
                    $this->db->insert('sv_stok_pengiriman_detail',$masuk);
                    $in_id=$this->db->insert_id();
                    }
                    //stok_trx('in', $in_id, $masuk['brand'], $masuk['jenis_produk'],$masuk['produk'], '0', $masuk['total'],  $masuk['satuan'], $masuk['tgl_tiba'],'0000-00-00', $masuk['gudang'],'ins');
                    //}
                    //else{
                    //    $dapet=$cari->row_array();
                    //    $where=$masuk;
                    //$masuk['total']=$total[$index];
                    //$masuk['keterangan']=$keterangan[$index];
			//$this->db->where($where);
			//$this->db->update('sv_stok_pengiriman_detail', $masuk);
                     //   stok_trx('in', $dapet['id'], $masuk['brand'], $masuk['jenis_produk'],$masuk['produk'], '0', $masuk['total'],  $masuk['satuan'], $masuk['tgl_tiba'],'0000-00-00', $masuk['gudang'],'upd');
                     //echo "Update data";
                     //die();
                    //}
                    
                    $index++;
                }
		
		if($id != NULL && $id != '')
		{
			/* if(!$this->input->post('password')){unset($data['password']);}
			else{$data['password']=md5($this->config->item('encryption_key').$this->input->post("password"));} */
			
			if(izin('u')){
			$data['modify_by'] = $webmaster_id;
			$data['modify_on']=date("Y-m-d");
			//$this->db->where("id", $id);
			//$this->db->update('sv_'.$this->utama, $data);
			
			$this->session->set_flashdata("message", 'Sukses diedit');}
			else{
				$this->session->set_flashdata("message", 'Anda Tidak Diizinkan Mengedit Data');
			}
		}
		else
		{
			//if($this->input->post('password')){$data['password']=md5($this->config->item('encryption_key').$this->input->post("password"));}
			//if(!$this->input->post('avatar')){$data['avatar']='default.png';}
			
			if(izin('c')){
			//$data['created_by'] = $webmaster_id;
			//$data['created_on'] = date("Y-m-d H:i:s");
			//$this->db->insert('sv_'.$this->utama, $data);
			$id = $this->db->insert_id();
			//$this->session->set_flashdata("message", 'Sukses ditambahkan');
                        }
			else{
				$this->session->set_flashdata("message", 'Anda Tidak Diizinkan Menambah Data');
			}
		}
                $this->session->set_flashdata("message", 'Sukses Dimasukkan ke Pengiriman');
		redirect($this->utama);
		
	}
        function suratpo($id,$render=false){
            
        $this->load->helper('dompdf');
        $data='';
        $q="SELECT * FROM sv_stok_pabrikasi WHERE id = '$id'";
        $data['lastq']=$this->db->query($q)->row_array();
                /*$data['col']=$this->listcol();  
            error_reporting(E_ALL);
		$op = explode("%",$lastq);
                foreach($op as $or){
                    $inz[]="$or";
                }
                $oops=implode($inz,',');
             $q="SELECT * FROM sv_activity_view WHERE id IN (".$oops.")";
                
		$data['lastq']=$this->db->query($q)->result_array();
                 * 
                 */
               // lastq();
        //load content html
        $html = $this->load->view('contents/stok_pabrikasi/surat_po',$data,TRUE);
        // create pdf using dompdf
        $filename = "surat_jalan";
        $paper = 'A4'; 
            $orientation = 'potrait';
            //die("ok");
            $f;
            $l;
        if(headers_sent($f,$l))
        {
            echo $f,'<br/>',$l,'<br/>';
            die('now detect line');
        }if($render){
            pdf_create($html, $filename, $paper, $orientation);}
         else{
          $this->load->view('contents/stok_pabrikasi/surat_po',$data);  
        }
        }
}
?>