<?php 
class Load extends CI_Controller {
		
		
		function __construct(){
				parent::__construct();
		}
		function renderdropdown($id=NULL){
				$side=GetAll('sv_menu',array('id_parents'=>'where/'.(int)$id,'sort'=>'order/ASC','is_active'=>"where/Active"));
				$data['menu']=$side->result();
				$this->load->view('layout/listside',$data);
		}
		function rendermessage(){
			error_reporting(0);
				$filter=array();
				$idwebmaster=$this->session->userdata('webmaster_grup');
				if($idwebmaster==4 || $idwebmaster==5 || $idwebmaster==6 ||  $idwebmaster==7 || webmastergrup()==8 || webmastergrup()==10){
				if($idwebmaster==4){$filter['to']='where/Export';}
				if($idwebmaster==5){$filter['to']='where/Import';}
				if($idwebmaster==7){$filter['to']='where/Finance';}
				if($idwebmaster==6 || webmastergrup()==8 || webmastergrup()==10){$filter['to']='where/Trucking';}
				
				}
                                $anaknya=anaknya();
				$filter['status']='where/N';
				$filter['create_date']='order/DESC';
                                $this->db->select('sv_a.link link, sv_a.message message,sv_a.create_date create_date,c.leader')->from('sv_notif sv_a');
                                 
                                $this->db->join('sv_admin b', "a.from=b.id", 'left');
                                $this->db->join('sv_master_sales c', "b.marketing=c.id", 'left');
                                $this->db->where('sv_a.status','N');
            if(!diaadmin()){$this->db->where_in('c.leader',$anaknya);}
            if(diasales()){$this->db->or_where('sv_a.create_user_id',$this->session->userdata('webmaster_id'));}
                                $this->db->order_by('sv_a.create_date','DESC');
				$oi=$this->db->get();
            //lastq();
				$data['badge']=$oi->num_rows();
				
				//unset($filter['status']);
				$data['isi']=$oi->result_array();
				
				$this->load->view('layout/message',$data);
		}
		function error404(){
				$this->load->view('layout/notfound');
		}
		function ambilakun(){
				$akun=GetAll('setup_account_mapping',array('id'=>'where/'.$_REQUEST['a']))->row_array();
				echo $akun['acno'].'/'.$akun['acno2'].'/'.$akun['name'].'/'.$akun['name'];
		}
		function marketingclient(){
				$id=$_REQUEST['v'];
				echo GetValue('client','marketing_form_prospect',array('id'=>'where/'.$id));
		}
		function marketingtrucking(){
				$id=$_REQUEST['v'];
				echo GetValue('client','marketing_form_prospect',array('id'=>'where/'.$id)).'/'.GetValue('from','marketing_form_prospect',array('id'=>'where/'.$id)).'/'.GetValue('to','marketing_form_prospect',array('id'=>'where/'.$id)).'/'.GetValue('service_truck','marketing_form_prospect',array('id'=>'where/'.$id)).'/'.GetValue('vehicle_no','marketing_form_prospect',array('id'=>'where/'.$id));
		}
		function vendortruck(){
				$id=$_REQUEST['v'];
				$truck=GetValue('name','master_vendor',array('id'=>'where/'.GetValue('vendor','master_truck',array('id'=>'where/'.$id))));
				echo ($truck=='0' ? 'Punya Sendiri' : $truck);
		}
		function generatequotation(){
				$c=$_REQUEST['c'];
				$s=$_REQUEST['s'];
				$f=$_REQUEST['f'];
				$to=$_REQUEST['to'];
				$ids=$_REQUEST['v'];
				
				
						if($s==1 || $s==2){
							$t=$_REQUEST['t'];
							$lempar=array();
							$item=$this->db->query("SELECT * FROM sv_master_exim")->result_array();
							$i=0;
							//print_mz($item);
							
							foreach($item as $hasil){
								$lempar[$i]['item']=$hasil['id'];
								$lempar[$i]['name']=$hasil['name'];
								$lempar[$i]['client']=$c;
								$lempar[$i]['service']=$t;
								
							if($ids==NULL){
								$q="SELECT * FROM sv_quotation_exim_custom WHERE client='$c' AND item='".$hasil['id']."' AND service='$t' ORDER BY create_date DESC LIMIT 1";
								$cari=$this->db->query($q);
								if($cari->num_rows()==0){
									$q="SELECT * FROM sv_quotation_exim_custom WHERE item='".$hasil['id']."' AND service='$t' ORDER BY create_date DESC LIMIT 1";
									$cari=$this->db->query($q);
									if($cari->num_rows()==0){
										$q="SELECT * FROM sv_quotation_exim_default WHERE client='$c' AND item='".$hasil['id']."' AND service='$t' ORDER BY create_date DESC LIMIT 1";
										$cari=$this->db->query($q);
										if($cari->num_rows()==0){
											$q="SELECT * FROM sv_quotation_exim_default WHERE item='".$hasil['id']."' AND service='$t' ORDER BY create_date DESC LIMIT 1";
											$cari=$this->db->query($q);
										}
									}
								}
							}
								else{
									$q="SELECT * FROM sv_quotation_exim_custom WHERE item='".$hasil['id']."' AND service='$t' AND prospek='$ids' ORDER BY create_date DESC LIMIT 1";
									$cari=$this->db->query($q);
								}
								
								if($cari->num_rows()==0){
									$lempar[$i]['charge_lcl']=0;
									$lempar[$i]['charge_20']=0;
									$lempar[$i]['charge_40']=0;
									$lempar[$i]['charge_45']=0;
									$lempar[$i]['remarks']='';
									$lempar[$i]['message']="Quotation Not Found";
								}
								else{
									$isi=$cari->row_array();
									$lempar[$i]['charge_lcl']=$isi['charge_lcl'];
									$lempar[$i]['charge_20']=$isi['charge_20'];
									$lempar[$i]['charge_40']=$isi['charge_40'];
									$lempar[$i]['charge_45']=$isi['charge_45'];
									$lempar[$i]['remarks']=$isi['remarks'];
									$lempar[$i]['message']="Quotation created on ".$isi['create_date'];
								}
								
							$i++;} 
							$data['val']=$lempar;
							$data['tables']="sv_quotation_exim_custom";
							$this->load->view('contents/quotation/quotation_exim.php',$data);
						}
						
						
						
						else{
							
							if($ids==NULL){
							$q="SELECT * FROM sv_quotation_trucking_custom WHERE client='$c' AND ( location='$f' OR location='$to' ) ORDER BY create_date DESC LIMIT 1";
							$cari=$this->db->query($q);
								if($cari->num_rows()==0){
								$q="SELECT * FROM sv_quotation_trucking_custom WHERE ( location='$f' OR location='$to' ) ORDER BY create_date DESC LIMIT 1";
									$cari=$this->db->query($q);
									if($cari->num_rows()==0){
										$q="SELECT * FROM sv_quotation_trucking_default WHERE client='$c' AND ( location='$f' OR location='$to' ) ORDER BY create_date DESC LIMIT 1";
										$cari=$this->db->query($q);
										if($cari->num_rows()==0){
											$q="SELECT * FROM sv_quotation_trucking_default WHERE ( location='$f' OR location='$to' ) ORDER BY create_date DESC LIMIT 1";
											$cari=$this->db->query($q);
										}
									}
								}
								
							}
							else{
								$q="SELECT * FROM sv_quotation_trucking_custom WHERE prospek='$ids' ORDER BY create_date DESC LIMIT 1";
								$cari=$this->db->query($q);
							}
								
							if($cari->num_rows()==0){
										$data['val']=array();
										$data['message']="Quotation Not Found";
								}
								else{
										$isi=$cari->row_array();
										$data['message']="Quotation created on ".$isi['create_date'];
										$data['val']=$isi;
								}
							
							$data['tables']="sv_quotation_trucking_custom";
							$this->load->view('contents/quotation/quotation_trucking.php',$data);
								
						}
				
		}
                function gantidivisidashboard(){
                    
                    $this->session->unset_userdata('divisi');
                    $this->session->unset_userdata('grups');
                    $d=$this->input->post('d');
                    $g=$this->input->post('g');
                    $this->session->set_userdata('divisi',$d);
                    $this->session->set_userdata('groups',$g);
                    if($d==NULL){
                        $this->session->unset_userdata('divisi');
                    }
                    if($g==NULL){
                        $this->session->unset_userdata('grups');
                    }
                }
                function cekcode(){
	

/* RECEIVE VALUE */
$validateValue=htmlentities($_GET['fieldValue']);
$validateId=  htmlentities($_GET['fieldId']);
$oi=  str_replace('code_', '', $validateId);

$validateError= "Data dengan no ini telah di input";
$validateSuccess= "Data dengan no ini di input";
$a=$this->db->query("SELECT id FROM sv_$oi WHERE code='$validateValue'")->num_rows();
if($a==0){$lanjut=TRUE;}
else{$lanjut=FALSE;}



	/* RETURN VALUE */
	$arrayToJs = array();
	$arrayToJs[0] = $validateId;

if($lanjut){		// validate??
	$arrayToJs[1] = true;			// RETURN TRUE
	echo json_encode($arrayToJs);			// RETURN ARRAY WITH success
}else{
	for($x=0;$x<1000000;$x++){
		if($x == 990000){
			$arrayToJs[1] = false;
			echo json_encode($arrayToJs);		// RETURN ARRAY WITH ERROR
		}
	}
	
}

	}
        function grup(){
            $gg=$this->input->post('dz');
            if($gg!=null){
                $where=array('divisi'=>'where/'.$gg);
            }else{
                $where=array('divisi'=>'where/kmzwa8awaa');
            }
            echo 'Group : '.form_dropdown('group',GetOptAll('ref_group','-All-',$where),$this->session->userdata('groups'),'onchange="" id="grupnya"');
        }
        function grupz(){
            $gg=$this->input->post('dz');
            $val=$this->input->post('v');
            if($gg!=null){
                $where=array('divisi'=>'where/'.$gg);
            }else{
                $where=array();
            }
            echo form_dropdown('groups',GetOptAll('ref_group','-Group-',$where),$val,"class='select2' id='groups'");
            echo "<script>
                $(document).ready(function(e){
            $('.select2').css('width','200px').select2({matcher: function(params,data){
                                    return matchStart(params, data);
                                },});    
            });
            </script>";
        }
         function alert_activity(){
             date_default_timezone_set('Asia/Jakarta');
             $now=date('H:i');
             $time=substr(GetValue('time','sv_alert_setting',array('id'=>'where/1')),0,5);
             $hari = date('D');
                //print_mz($sales);
             if($hari!='Sat' && $hari!='Sun'){
             if($now==$time){
                 
                $sales=GetAll('sv_master_sales',array('alert'=>'where/1'))->result_array();
                $this->load->library('email');
                foreach($sales as $s){
                 $cek=GetAll('sv_activity_sales',array('sales'=>'where/'.$s['id'],'tanggal'=>'where/'.date('Y-m-d')))->num_rows();
                 if($cek==0){
                     
                     
                     if($s['send_by']=='email' && $s['email']!='-' ){
                    //echo $s['email'].'<br>';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';
		 		
		$this->email->initialize($config); 
				
		$this->email->from('admin@majabintang.com', 'Admin CRM');
                $this->email->to($s['email']);
		$this->email->subject('Notifikasi Activity CRM');
                $content='Dear '.$s['nama_lengkap'].',';
                $content.=GetValue('content','sv_alert_setting',array('id'=>'where/1'));
                   $message='Dear '.$s['nama_lengkap'].', <br> Silakan lakukan input aktifitas anda hari ini di https://majabintang.com/crm/ <br><br> Abaikan Pesan ini bila anda telah melakukan input aktifitas <br> <br><strong>Regards</strong><br><strong>Admin CRM</strong>';
		$this->email->message($content);
		$sent=$this->email->send();
                $this->email->clear(TRUE);
                     
                 }
                 
                     }
                }
             }
             }
         }
         function load_alert(){
//             echo "siap";
//             die();
             $m=0;
             $e=0;
             $u=0;
             $min=$this->db->query("SELECT * FROM sv_ref_min_stok")->result_array();
             $exp=$this->db->query("SELECT * FROM sv_ref_exp")->result_array();
             $umur=$this->db->query("SELECT * FROM sv_ref_umur")->result_array();
             foreach($min as $mn){
                 $q="SELECT * FROM sv_stok_view WHERE produk='".$mn['produk']."' AND stok<=".$mn['min_stok']."";
                 $mdata=$this->db->query($q);
                 foreach($mdata->result_array() as $mds){
                     $min_g[]=GetValue('title','master_produk',array('id'=>'where/'.$mds['produk']))." < ".$mn['min_stok'];
                 }
                 //$min_g[]=$mdata->result_array();
                 //lastq();
                 $m=+$mdata->num_rows();
                //echo $q;
             };
             //die();
             foreach($exp as $ex){
                 $q="SELECT * FROM sv_stok_view WHERE produk='".$ex['produk']."' AND expired>='".$ex['tgl']."'";
                 $edata=$this->db->query($q);
                 foreach($edata->result_array() as $ed){
                  $exp_g[]=GetValue('title','master_produk',array('id'=>'where/'.$ed['produk']))." < ".$ex['tgl'];   
                 }
//                 //lastq();
                 $e=+$edata->num_rows();
             };
                //echo "<br>";
                //echo $q;
                //die();
//             
             foreach($umur as $um){
                 $q="SELECT * FROM sv_stok_view WHERE produk='".$um['produk']."' AND umur>='".$um['umur']."'";
                 $udata= $this->db->query($q); 
                 foreach($udata->result_array() as $ud){
                  $umur_g[]=GetValue('title','master_produk',array('id'=>'where/'.$ud['produk']))." < ".$um['umur'];   
                 }
                 $u=+$udata->num_rows();
             };
             //echo $m."<br>";
             //echo $e."<br>";
             //echo $u."<br>";
             
             $data['min_data']=$min_g;
             $data['exp_data']=$exp_g;
             $data['umur_data']=$umur_g;
             //print_mz($min_g);
             if($m!=0 || $e!=0 || $u!=0){
                 //echo "Siap";
                 $total=$m+$e+$u;
                 echo "<script>$('.popupwindow_titlebar_text').text('Alert (".$total.")');</script>";
                $this->load->view('layout/alert',$data);
             }else{
                 echo "Tidak Ada Alert";
                 echo "<script>$('.popupwindow_titlebar_text').text('Alert (0)');</script>";
             }
         }
         function barang($id){
            $data['id']=$id;
            $this->load->view('contents/stok_out/barang',$data);
        }
}