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

class Report_Controller4 extends CI_Controller {
        public function __construct() {
        parent::__construct();
        
         
        $this->load->model('Product_model');
        $this->load->model('Common_model');
        $this->load->model('General');
         if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
        }
        
        
        function index(){
            
        }
        
        function user_edit_log(){
            
            
             $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl >',0);
                   // $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['user'] = $sls;
            
             $data['content'] = ('report/user_edit_log'); 
		    $this->load->view('template',$data);
        }
        
         function pdf_user_edit_log(){
             
             $sdate = $this->input->post('to_date');
             
             $user_detail = array();
        
            $user_detail['frmdate']=""; 
            $user_detail['todate']=""; 
            $user_detail['rt_name']=""; 
            
        $this->db->select("*");    
        $this->db->from('user_eidt_log');
        
        if($this->input->post('to_date') !=""){
        //$this->db->where('user_id', $this->input->post('ref1'));
        $this->db->where($sdate, array('uedit_date' => date('Y-m-d')));
            
        }
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $custdata=$query->result();
        
        for($k=0; $k<count($custdata); $k++){
                                            
                                    $inv[] = array($custdata[$k]->uedit_userid,$custdata[$k]->uedit_details,$custdata[$k]->uedit_user,$custdata[$k]->uedit_date,"","");
                                        
                                    }
                                    
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = "";
        $user_detail['ref'] = "";
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
        $this->load->view('report/pdf_usereditlog',$data);
        
        
        }
        
        function chkdepo_sum_view(){
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $sls;
            
                            $this->db->select("*");
                            $this->db->from('mes_depot');
                            $all = $this->db->get();
                            $data['dpodetail'] = $all->result();
                    
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    $data['inv'] = $inv;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/chksetl_sum'); 
		        $this->load->view('template',$data);
	}
	
        function chkdepo_sumpdf(){
    
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	     $user_detail['ref']=$user_data[0]->user_fname." ".$user_data[0]->user_lname;
	    
	    $begin =$end = "";
	    
           if($this->input->post('type')<2){
               $this->db->select("*");    
                $this->db->from('mes_payment_main');
                $this->db->where('pay_type', 1);
                $this->db->where('pay_stetus', 1);
                $this->db->where('pay_chkreturn', 1);
                                    $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_payment_main.pay_cust_id', 'left');
                                    $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
                                    $this->db->join( "cities", "cities.id = mes_payment_main.pay_bbranch", 'left');
                               if($this->input->post('ref')!="0"){
                                   $this->db->where('mes_customer.cust_salseperson',$this->input->post('ref'));
                               }     
                                    
                if($this->input->post('frm_date')!=""){
                        $user_detail['fdate'] = $this->input->post('frm_date');
                        $this->db->where('pay_date>=',$this->input->post('frm_date'));
                    }else{
                        $user_detail['fdate'] = date('Y-m-d');
                    }
                    
        	    if($this->input->post('to_date')!=""){
                        $user_detail['todate'] = $this->input->post('to_date');
                        $this->db->where('pay_date<=',$this->input->post('to_date'));
                    }else{
                        $user_detail['todate'] = date('Y-m-d'); 
                        $this->db->where('pay_date<=',date('Y-m-d'));
                    }                 
               // $this->db->where('pay_chkdate',$sin_date);
                $query = $this->db->get();
                $datee = $query->result();
           
                    
                    $ret = array(); $det = "";
            	        
                     for($j=0; $j<count($datee); $j++){
                         
                            if($this->input->post('type')<1){
                                $this->db->select("*");    
                                $this->db->from('chk_collect');
                                $this->db->where('stetus', 1);
                                                    $this->db->join('mes_customer', 'mes_customer.cust_id   = chk_collect.cus_id', 'left');
                                                    $this->db->join( "mes_bank", "mes_bank.bank_id = chk_collect.chk_bak", 'left');
                                                    $this->db->join( "cities", "cities.id = chk_collect.chk_brnch", 'left');
                                $this->db->where('pay_id',$datee[$j]->pay_id);
                                $query = $this->db->get();
                                $stle = $query->result();
                                $ret1 = array();
                                        for($k=0; $k<count($stle); $k++){
                                            if($stle[$k]->pay_type<1){$dett = "CASH" ;}else{$dett = $stle[$k]->bank_name." | ".$stle[$k]->name_en ;}
                                            
                                            $ret1[] = array("STL".$stle[$k]->pay_id,$dett,$stle[$k]->amount,$stle[$k]->date,$stle[$k]->created_by,$stle[$k]->chk_id);
                     
                                        }
                            }else{ $ret1 = array();}     
                                           
                                
                               
                                    $det = $datee[$j]->bank_name." | ".$datee[$j]->name_en ;
                                    $ret[] = array($datee[$j]->pay_code.$datee[$j]->pay_id,$det,$datee[$j]->pay_amount,$datee[$j]->pay_date,$datee[$j]->pay_user,$datee[$j]->pay_chkid,$ret1);
                     
                           
                     }
           }else if($this->input->post('type')>1){
               
                                $this->db->select("*");    
                                $this->db->from('chk_collect');
                                $this->db->where('stetus', 1);
                                                    $this->db->join('mes_customer', 'mes_customer.cust_id   = chk_collect.cus_id', 'left');
                                                    $this->db->join( "mes_bank", "mes_bank.bank_id = chk_collect.chk_bak", 'left');
                                                    $this->db->join( "cities", "cities.id = chk_collect.chk_brnch", 'left');
                                        if($this->input->post('ref')!="0"){
                                            $this->db->where('ref_id',$this->input->post('ref'));
                                        }     
                                                    
                                        if($this->input->post('frm_date')!=""){
                                            $user_detail['fdate'] = $this->input->post('frm_date');
                                            $this->db->where('date>=',$this->input->post('frm_date'));
                                        }else{
                                            $user_detail['fdate'] = date('Y-m-d');
                                        }
                                        
                            	    if($this->input->post('to_date')!=""){
                                            $user_detail['todate'] = $this->input->post('to_date');
                                            $this->db->where('date<=',$this->input->post('to_date'));
                                        }else{
                                            $user_detail['todate'] = date('Y-m-d'); 
                                            $this->db->where('date<=',date('Y-m-d'));
                                        }                   
                                //$this->db->where('date',$datee[$j]->pay_id);
                                $query = $this->db->get();
                                $stle = $query->result();
                                $ret = array();
                                        for($k=0; $k<count($stle); $k++){
                                            if($stle[$k]->pay_type<1){$dett = "CASH" ;}else{$dett = $stle[$k]->bank_name." | ".$stle[$k]->name_en ;}
                                            
                                            $ret[] = array("STL".$stle[$k]->pay_id,$dett,$stle[$k]->amount,$stle[$k]->date,$stle[$k]->created_by,$stle[$k]->chk_id,$ret = array());
                     
                                        }
           }
                
                
                      
                     
            $user_detail['type'] = $this->input->post('type');
                    
        //print_r($ret1);
      
        $data['inv_itm'] = $ret;
        $data['inv'] = $user_detail;
        
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
        
         $this->load->view('report/pdf_chksetl_sum',$data);
	}
	
	    function custlist_view(){
	    
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                        $data['ref'] = $sls;
	    
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['cus'] = $sls;
                    
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    $data['inv'] = $inv;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/customer_list'); 
		        $this->load->view('template',$data);
	}
	
	    function cust_pdf(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref1'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_fname." ".$user_data[0]->user_lname;
       
        $user_detail = array();
        
            $user_detail['frmdate']=$this->input->post('frmm_date'); 
            $user_detail['todate']=$this->input->post('too_date'); 
            $user_detail['rt_name']=""; 
	    
	    if($this->input->post('city')!=0){
	       $this->db->select("*");    
                    $this->db->from('cities');
                    $this->db->where('id',$this->input->post('city'));
                    $query = $this->db->get();
                    $cty =$query->result();
                     
                    $cust = array(); 
                     
                    $this->db->select("*");    
                    $this->db->from('mes_customer');
                    $this->db->where('cust_city',$this->input->post('city'));
                    $this->db->where('cust_salseperson',$this->input->post('ref1'));
                    $this->db->where('cust_status',1);
                    $this->db->order_by("cust_company", "asc");
                    $query = $this->db->get();
                    $custdata=$query->result();
                    $inv = $invv =  array();
                         for($k=0; $k<count($custdata); $k++){
                          $inv[] = array($custdata[$k]->cust_company,$custdata[$k]->cust_addres."  ".$custdata[$k]->cust_contact,$cty[0]->name_en,$custdata[$k]->cust_id." ".$custdata[$k]->cust_id,"",$cty[0]->name_en);
                           
                  
                }    
	    }else{
	         $this->db->select("*");    
                                    $this->db->from('mes_route');
                                    $this->db->where('route_id',$this->input->post('route'));
                                    $query = $this->db->get();
                                    $rtname =$query->result();
	        
	        $user_detail['rt_name']=$rtname[0]->route_name; 
                         
                         $this->db->select("*");    
                                    $this->db->from('mes_customer');
                                    $this->db->where('cust_salseperson',$this->input->post('ref1'));
                                    $this->db->join('customer_route', 'customer_route.cus_id   = mes_customer.cust_id','left');
                                    $this->db->join('cities', 'cities.id   = mes_customer.cust_city','left');
                                    $this->db->where('customer_route.route_id',$this->input->post('route'));
                                    
                                    $this->db->order_by("cust_company", "asc");
                                    $query = $this->db->get();
                                    $all=$query->result();
                         
                         
                                 for($i=0; $i<count($all); $i++){
                                     
                                     $this->db->select("*");    
                                    $this->db->from('cities');
                                    $this->db->where('id',$all[$i]->cust_city);
                                    $query = $this->db->get();
                                    $cty =$query->result();
                                     
                                    $cust = array(); 
                                     
                                    $this->db->select("*");    
                                    $this->db->from('mes_customer');
                                    $this->db->where('cust_id',$all[$i]->cust_id);
                                    $this->db->where('cust_salseperson',$this->input->post('ref1'));
                                    $this->db->where('cust_status',1);
                                    $this->db->order_by("cust_company", "asc");
                                    $query = $this->db->get();
                                    $custdata=$query->result();
                                    
                                    for($k=0; $k<count($custdata); $k++){
                                            
                                    $inv[] = array($custdata[$k]->cust_company,$custdata[$k]->cust_addres."  ".$custdata[$k]->cust_contact,$cty[0]->name_en,$custdata[$k]->cust_id." ".$custdata[$k]->cust_id,"",$cty[0]->name_en);
                                        
                                    }
                                    
                                }
                
	    }        
                
        //print_r($inv);
        
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = $ret;
        $user_detail['ref'] = $usname;
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
        $this->load->view('report/pdf_cust_list',$data);
	}
}        