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

class Report_Controller2 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();
            }
        }
        
        
        
         public function index()
	{
	    
	}
	
	function view_pdf(){
        $id = preg_replace('/[^0-9.]+/', '', $this->input->post('inv'));;
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->where('po_id', $id);
        $query = $this->db->get();
        $gtid=$query->result();
        $insert_id = $gtid[0]->po_id;
        
       
        
        $this->db->select("*");    
        $this->db->from('qto_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $this->db->join('mes_item', 'qto_items.po_itm_item_id   = mes_item.item_id','left');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id','left');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id','left');
        $this->db->join('mes_unit', 'qto_items.po_itm_unit   = mes_unit.unit_id','left');
        $this->db->join('mes_currancy', 'qto_items.po_itm_currancy   = mes_currancy.curr_id','left');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
        $query = $this->db->get();
        $dataa=$query->result();
        
         for($i=0; $i<count($dataa); $i++){
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->model_name!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->item_retail_price == 0.00){$rp = "";}else{$rp = number_format(($dataa[$i]->item_retail_price),2,".",",");}
                  
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$dataa[$i]->itm_name,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id,$dataa[$i]->free_item,$dataa[$i]->brand_shordcode,$rp); 
                
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->where('po_id', $insert_id );
        $this->db->join('mes_customer', 'mes_qto.po_sup_id  = mes_customer.cust_id', 'left');
        $this->db->join('mes_currancy', 'mes_qto.po_currancy   = mes_currancy.curr_id', 'left');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id', 'left');
        //$this->db->join('mes_user', 'mes_qto.po_cuser   = mes_user.user_id','left');
        $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id','left');
        
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->po_tc);
      // sort($tc);
       $length = count($tc);
        
         for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_tmdes');
                $this->db->join('mes_termcat', 'mes_tmdes.tmdes_catid  = mes_termcat.tcat_id');
                $this->db->where('tmdes_id', $tc[$i]);
                $this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
     
     
        $this->load->view('pages/qtnpdf',$data);
    }
	
	function grn_sum_view(){
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['ref'] = $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/grn_sum'); 
		        $this->load->view('template',$data);
	}
	
	function productretn_sum_view(){
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['ref'] = $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['reson_list'] = $this->Product_model->get_trn_reason();
                
                
                $data['content'] = ('report/product_trn_itm'); 
		        $this->load->view('template',$data);
	}
	
	function product_trn_pdf(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_return');
        $this->db->where('mes_return.ret_stetus', 1);
        $this->db->where('mes_return.ret_proceed', 1);
        //$this->db->where('mes_return.invt_type', 'RTN');
        $this->db->join('stock_return_items', 'stock_return_items.stock_trn_id   = mes_return.ret_id', 'left');
        $this->db->join('mes_ret_reason', 'mes_ret_reason.ret_reason_id   = mes_return.ret_invnum', 'left');
        
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = "All";
                //$this->db->where('mes_return.ret_cdate', date('Y-m-d'));
                $this->db->where('mes_return.ret_cdate<=', $this->input->post('to_date'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_grn.grn_cdate', date('Y-m-d'));
            }
           
           $user_detail['cus']=""; 
           
           if($this->input->post('item')!="0"){
               $this->db->where('stock_return_items.stock_rtn_item',$this->input->post('item'));
           }
           
            if($this->input->post('resn')!="0"){
               $this->db->where('ret_resaon',$this->input->post('resn'));
           }
          
           
        $this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
        
        
          if($this->input->post('resn')!="0"){
                $this->db->select("*");    
                $this->db->from('mes_ret_reason');
               // $this->db->where('ret_reason_status', 1);
                $this->db->where('ret_reason_id', $this->input->post('resn'));
                //$this->db->order_by('ret_id', 'asc');
                $queryre = $this->db->get();
                $dataare=$queryre->result();
                
                $user_detail['resoan'] = $dataare[0]->ret_reason;
                
          }else{$user_detail['resoan'] = "1000";}
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             
                    $data = $this->Product_model->single_product($dataa[$i]->stock_rtn_item);
                    $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    
                    
                        $inv[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$itm_code,$dataa[$i]->stock_rtn_qty,$dataa[$i]->ret_cuser,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$data[0]->itm_name,$data[0]->item_selling_price);
        
                 
       }
      
        $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;
        
        $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_product_trn',$data);
	}
	
	
	function product_trn_pdf_old(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_return');
        $this->db->where('mes_return.ret_stetus', 1);
        $this->db->where('mes_return.ret_proceed', 1);
        //$this->db->where('mes_return.invt_type', 'RTN');
        $this->db->join('mes_inventry', 'mes_inventry.invt_type_id   = mes_return.ret_id', 'left');
        $this->db->join('mes_ret_reason', 'mes_ret_reason.ret_reason_id   = mes_return.ret_invnum', 'left');
        $this->db->where('mes_inventry.invt_type', 'return');
        
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = "All";
                //$this->db->where('mes_return.ret_cdate', date('Y-m-d'));
                $this->db->where('mes_return.ret_cdate<=', $this->input->post('to_date'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_grn.grn_cdate', date('Y-m-d'));
            }
           
           $user_detail['cus']=""; 
           
           if($this->input->post('item')!="0"){
               $this->db->where('invt_itemid',$this->input->post('item'));
           }
           
            if($this->input->post('resn')!="0"){
                
            
               $this->db->where('ret_resaon',$this->input->post('resn'));
           }
          
           
        $this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
        
        
          if($this->input->post('resn')!="0"){
                $this->db->select("*");    
                $this->db->from('mes_ret_reason');
               // $this->db->where('ret_reason_status', 1);
                $this->db->where('ret_reason_id', $this->input->post('resn'));
                //$this->db->order_by('ret_id', 'asc');
                $queryre = $this->db->get();
                $dataare=$queryre->result();
                
                $user_detail['resoan'] = $dataare[0]->ret_reason;
                
          }else{$user_detail['resoan'] = "1000";}
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             
                    $data = $this->Product_model->single_product($dataa[$i]->invt_itemid);
                    $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    
                    
                    
                    
             if($this->input->post('item')!="0"){
                    $this->db->select("*");    
                    $this->db->from('mes_inventry');
                    $this->db->where('invt_type',"return");
                    $this->db->where('invt_type_id',$dataa[$i]->grn_id);
                    $this->db->where('invt_itemid',$this->input->post('item'));
                    $query = $this->db->get();
                    $itm_cnt=$query->result();
                    
                    if($this->input->post('item')==$dataa[$i]->invt_itemid){
                        $inv[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$itm_code,$dataa[$i]->invt_qty,$dataa[$i]->ret_cuser,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$data[0]->itm_name,$data[0]->item_selling_price);
                    }
             }else{
                        $inv[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$itm_code,$dataa[$i]->invt_qty,$dataa[$i]->ret_cuser,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$data[0]->itm_name,$data[0]->item_selling_price);
        
             }
                 
       }
      
        $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;
        
        $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_product_trn',$data);
	}
	
	function grn_sumpdf(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        //$this->db->where('user_id', "0");
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usname = $usid = "";
        
        //$usid = $user_data[0]->user_id;
        //$usname = $user_data[0]->user_name;
        $user_detail['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_grn');
        $this->db->where('mes_grn.grn_stetus', 1);
        $this->db->where('mes_grn.grn_proceed', 1);
        //$this->db->join('mes_inventry', 'mes_inventry.invt_type_id   = mes_grn.grn_id', 'left');
        $this->db->join('grn_item', 'grn_item.grn_id   = mes_grn.grn_id', 'left');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_grn.grn_cuser', 'left');
        //$this->db->where('mes_inventry.invt_type', 'GRN');
        
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_grn.grn_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_grn.grn_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_grn.grn_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_grn.grn_cdate', date('Y-m-d'));
            }
           
           $user_detail['cus']=""; 
           
           if($this->input->post('item')!="0"){
               $this->db->where('grn_item.item',$this->input->post('item'));
           }
          
           
        $this->db->order_by('mes_grn.grn_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             
                   $data = $this->Product_model->single_product($dataa[$i]->item);
                    //$itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    
                    
                    
                    
             if($this->input->post('item')!="0"){
                    //$this->db->select("*");    
                   // $this->db->from('grn_item');
                    //$this->db->where('invt_type',"GRN");
                    //$this->db->where('grn_item.grn_id',$dataa[$i]->grn_id);
                    //$this->db->where('invt_itemid',$this->input->post('item'));
                   // $query = $this->db->get();
                    //$itm_cnt=$query->result();
                    
                    if($this->input->post('item')==$dataa[$i]->item){
                        $inv[] = array($dataa[$i]->grn_code.$dataa[$i]->grn_id,$dataa[$i]->batch,$dataa[$i]->grn_qty,$dataa[$i]->user_name,$dataa[$i]->grn_cdate,$dataa[$i]->grn_cdate,$data[0]->itm_name,$dataa[$i]->selling_price,$dataa[$i]->retail_price);
                    }
             }else{
                        $inv[] = array($dataa[$i]->grn_code.$dataa[$i]->grn_id,$dataa[$i]->batch,$dataa[$i]->grn_qty,$dataa[$i]->user_name,$dataa[$i]->grn_cdate,$dataa[$i]->grn_cdate,$data[0]->itm_name,$dataa[$i]->selling_price,$dataa[$i]->retail_price);
        
             }
                    
           
       }
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
      
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = $ret;
        
        $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();
                $dd =$query->result();
                
                $data['tm'][1] = $dd[0];
         
         $this->load->view('report/pdf_grn_sum',$data);
	}
	
	function ret_sum_view(){
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                      if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $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/return_sum'); 
		        $this->load->view('template',$data);
	}
	
	function ret_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;
	    
	    
                    
       $this->db->select("*");    
        $this->db->from('inv_return');
        $this->db->join('mes_qto', 'mes_qto.po_id   = inv_return.ret_invnum', 'left');
        $this->db->join('mes_user', 'mes_qto.po_sup_id   = mes_user.user_name', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        //$this->db->join('return_items', 'inv_return.ret_invnum   = return_items.ret_inv_id', 'left');
        
        $this->db->where('inv_return.ret_proceed=', 1);
        $this->db->where('inv_return.ret_stetus=', 1);
         	

            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('mes_qto.po_ref_id',$this->input->post('ref'));
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
         
            
         
        $this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
             
                  
                   if($this->input->post('item')!="0"){
                        $this->db->select("*");    
                        $this->db->from('return_items');
                        $this->db->join('mes_item', 'return_items.ret_itm_item_id   = mes_item.item_id', 'left');
                        $this->db->join('item', 'mes_item.item_name   = item.itm_id', 'left');
                        $this->db->where('ret_itm_item_id', $this->input->post('item'));
                        $this->db->where('ret_note_id', $dataa[$i]->ret_id);
                        $query = $this->db->get();
                        $item_chk = $query->result();
                        
                       //$itmname = $item_chk[0]->itm_name;
                        
                        if(count($item_chk)>0){
                                $ret[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$dataa[$i]->cust_company,$dataa[$i]->ret_totqty,$dataa[$i]->ret_amount,$dataa[$i]->ret_cuser,$dataa[$i]->ret_cdate,"INV".$dataa[$i]->ret_invnum,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$item_chk[0]->itm_name);
                        }
                        
                    }else{
                                $ret[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$dataa[$i]->cust_company,$dataa[$i]->ret_totqty,$dataa[$i]->ret_amount,$dataa[$i]->ret_cuser,$dataa[$i]->ret_cdate,"INV".$dataa[$i]->ret_invnum,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate);
                                
                    }
                  
                
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
        $user_detail['itmname'] = $item_chk[0]->itm_name;
        $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_ret_sum',$data);
	}
	
    function trans_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/transfer_sum'); 
		        $this->load->view('template',$data);
	}
	
	function trans_sumpdf(){
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "1");
       // $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['itmname'] = "";
        
        $this->db->select("*");    
        $this->db->from('mes_depot');
        $this->db->where('dep_id', $this->input->post('depo'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $querydp = $this->db->get();
        $dp_data=$querydp->result();
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_stock_trancfer');
        $this->db->join('mes_depot', 'mes_stock_trancfer.trncfer_dpoid = mes_depot.dep_id', 'left');
        $this->db->where('trncfer_prosses', 1);
        $this->db->where('trncfer_stetus', 1);
        $this->db->join('mes_trnfer_item', 'mes_trnfer_item.trf_trfid   = mes_stock_trancfer.trncfer_id', 'left');

            //$user_detail['deponame']=""; 
            if($this->input->post('depo')!="1000"){
                
                $this->db->where('trncfer_dpoid',$this->input->post('depo'));
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('trncfer_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('trncfer_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('trncfer_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('trncfer_cdate', date('Y-m-d'));
            }
            
         
        $this->db->order_by('trncfer_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
             
                    $data = $this->Product_model->single_product($dataa[$i]->trf_itmid);
                    $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                  
                   if($this->input->post('item')!="0"){
                        $this->db->select("*");    
                        $this->db->from('mes_trnfer_item');
                        $this->db->join('mes_item', 'mes_trnfer_item.trf_itmid   = mes_item.item_id', 'left');
                        $this->db->join('item', 'mes_item.item_name = item.itm_id', 'left');
                        $this->db->where('trf_itmid', $this->input->post('item'));
                        $this->db->where('trf_trfid', $dataa[$i]->trncfer_id);
                        $query = $this->db->get();
                        $item_chk = $query->result();
                        
                        if(count($item_chk)>0){
                            if($item_chk[0]->trf_itmid=$this->input->post('item')){
                            $ret[] = array($dataa[$i]->trncfer_code.$dataa[$i]->trncfer_id,$dataa[$i]->dep_name,$dataa[$i]->trncfer_titm,$dataa[$i]->trf_qty,$dataa[$i]->trncfer_cuser,$dataa[$i]->trncfer_cdate,$itm_code,$data[0]->itm_name,$item_chk[0]->itm_name);
                        
                            $user_detail['itmname'] = $item_chk[0]->itm_name;
                            }else{
                                $user_detail['itmname'] = "";
                            }
                        }else{
                            $ret[] = array($dataa[$i]->trncfer_code.$dataa[$i]->trncfer_id,$dataa[$i]->dep_name,$dataa[$i]->trncfer_titm,$dataa[$i]->trf_qty,$dataa[$i]->trncfer_cuser,$dataa[$i]->trncfer_cdate,$itm_code,$data[0]->itm_name);
                         
                            $user_detail['itmname'] = "";
                        }
                        
                        
                    }else{
                        $ret[] = array($dataa[$i]->trncfer_code.$dataa[$i]->trncfer_id,$dataa[$i]->dep_name,$dataa[$i]->trncfer_titm,$dataa[$i]->trf_qty,$dataa[$i]->trncfer_cuser,$dataa[$i]->trncfer_cdate,$itm_code,$data[0]->itm_name);
                         $user_detail['itmname'] = "";       
                    }
                  
                
        }
        
        
        $user_detail['depoid'] = $this->input->post('depo');
        
        $user_detail['deponame'] ="" ;
       // $user_detail['itmname'] = "";
       // $user_detail['deponame'] = $this->input->post('depo');
        $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();
                $dd =$query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_trans_sum',$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/chkcol_sum'); 
		        $this->load->view('template',$data);
	}
	
function chkdepo_sumpdf(){
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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']="";
	    
	    $begin =$end = "";
	    
	    if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $begin = new DateTime($this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $begin = new DateTime(date('Y-m-d'));
            }
            
	    if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $end   = new DateTime($this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $end   = new DateTime(date('Y-m-d'));
            } 
            
             
                               $chk_date= array();     
            for($i = $begin; $i <= $end; $i->modify('+1 day')){
                $sin_date =  $i->format("Y-m-d");
                
                $this->db->select("*");    
                $this->db->from('mes_payment_main');
                $this->db->where('pay_type', 1);
                $this->db->where('pay_stetus', 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');
                $this->db->where('pay_chkdate',$sin_date);
                $query = $this->db->get();
                $datee = $query->result();
           
                    
                    $ret = array(); $det = "";
            	        
                     for($j=0; $j<count($datee); $j++){
                                        
                                  
                                             $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);
                     }
                     
                     
                $this->db->select("*");    
                $this->db->from('chk_collect');
                $this->db->where('pay_type', 1);
                $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('chk_date',$sin_date);
                $query = $this->db->get();
                $dati = $query->result();   
                
              
                     for($j=0; $j<count($dati); $j++){
                                       
                                        $det = $dati[$j]->bank_name." | ".$dati[$j]->name_en ;
                                        $ret[] = array("COLL".$dati[$j]->pay_id,$det,$dati[$j]->amount,$dati[$j]->chk_date,$dati[$j]->created_by,$dati[$j]->chk_id);
                        
                     }
                     
                     
                $chk_date[] = array($sin_date,$ret);
            }
                    
        //print_r($chk_date);
      
        $data['inv_itm'] = $chk_date;
        $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_chkcol_sum',$data);
	}
	
	function chkdepo_sumpdf_old(){
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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;
	    
	    $this->db->select("*");    
        $this->db->from('mes_payment_main');
        $this->db->where('pay_type', 1);
       // $this->db->where('pay_authrize', 1);
        $this->db->where('pay_stetus', 1);

            $user_detail['ref']=""; 
            
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('pay_chkdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('pay_chkdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('pay_chkdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('pay_chkdate', date('Y-m-d'));
            }
            
         
        $this->db->order_by('pay_chkdate', 'asc');
        $this->db->group_by('pay_chkdate');
        $query = $this->db->get();
        $datee = $query->result();
	    
	    $det = "";
	        $chk_date= array();
         for($j=0; $j<count($datee); $j++){
                             $this->db->select("*");    
                        $this->db->from('mes_payment_main');
                        $this->db->where('pay_type', 1);
                       // $this->db->where('pay_authrize', 1);
                        $this->db->where('pay_stetus', 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');
                
                           $this->db->where('pay_chkdate', $datee[$j]->pay_chkdate);
                         
                        $this->db->order_by('pay_id', 'asc');
                        $query = $this->db->get();
                        $dataa = $query->result();
                            
                        $ret = array();
                         for($i=0; $i<count($dataa); $i++){
                             
                             
                                 $det = $dataa[$i]->bank_name." | ".$dataa[$i]->name_en ;
                                      $ret[] = array($dataa[$i]->pay_code.$dataa[$i]->pay_id,$det,$dataa[$i]->pay_amount,$dataa[$i]->pay_date,$dataa[$i]->pay_user,$dataa[$i]->pay_chkid);
                          
                                
                        }
         
             $chk_date[] = array($datee[$j]->pay_chkdate,$ret);
         }
	    
                    
        //print_r($chk_date);
      
        $data['inv_itm'] = $chk_date;
        $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_chkcol_sum',$data);
	}
	
	function stock_bal_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/stock_bal'); 
		        $this->load->view('template',$data);
	}
	
	function stock_bal_pdf(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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;
        
        $this->db->select("*");    
        $this->db->from('mes_depot');
        $this->db->where('dep_id', $this->input->post('depo'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $querydp = $this->db->get();
        $dp_data=$querydp->result();
	    
	    if($this->input->post('depo')=="1000"){
	         
    	        $this->db->select("mes_stock.stc_itemid as itemid,mes_stock.stc_qty as stockk");    
                $this->db->from('mes_stock');
                if($this->input->post('item')!="0"){
                        $this->db->where('stc_itemid', $this->input->post('item'));
                        }
                 $this->db->join( "mes_item", "mes_stock.stc_itemid = mes_item.item_name", 'left');       
                 $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');    
                 $this->db->order_by('brand_id', 'ASC');
    	        $query = $this->db->get();
                $dataa = $query->result();
           
            
            
            $user_detail['ref'] = $user_detail['todate'] = "";
            $user_detail['fdate'] = $this->input->post('depo');
                
            $inv = array();
             for($i=0; $i<count($dataa); $i++){
                 
                        $data = $this->Product_model->single_product($dataa[$i]->itemid);
                        $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                        
                            $this->db->select("sum(dpstock_qty) as stockk");    
                            $this->db->from('mes_depot_stock');
                            $this->db->where('dpstock_itemid', $dataa[$i]->itemid);
                            $query = $this->db->get();
                            $depdataa = $query->result();
                    
                        $allstk = $dataa[$i]->stockk+$depdataa[0]->stockk;
                            
                                if($this->input->post('cat')!="0"){
                                    if($data[0]->item_cat==$this->input->post('cat')){
                                        $inv[] = array($itm_code,$data[0]->itm_name,$allstk,$allstk,$allstk,$allstk,$data[0]->item_selling_price);
                                    }
                                }else{
                                    $inv[] = array($itm_code,$data[0]->itm_name,$allstk,$allstk,$allstk,$allstk,$data[0]->item_selling_price);
                                
                                }
            }
	    }else{
	        if($this->input->post('depo')=="0000"){
    	        $this->db->select("mes_stock.stc_itemid as itemid,mes_stock.stc_qty as stockk");    
                $this->db->from('mes_stock');
                if($this->input->post('item')!="0"){
                        $this->db->where('stc_itemid', $this->input->post('item'));
                        }
                    if($this->input->post('minval')!=""){
                        $this->db->where('stc_qty >', $this->input->post('minval'));
                        }
                        
                        if($this->input->post('maxval')!=""){
                        $this->db->where('stc_qty <', $this->input->post('maxval'));
                        }  
                        
            $this->db->join( "mes_item", "mes_stock.stc_itemid = mes_item.item_name", 'left');       
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');    
            $this->db->order_by('brand_id', 'ASC');
                        
                        
	        }else{
    	        $this->db->select("mes_depot_stock.dpstock_itemid as itemid,mes_depot_stock.dpstock_qty as stockk");    
                $this->db->from('mes_depot_stock');
                $this->db->where('dpstock_dpoid', $this->input->post('depo'));
                if($this->input->post('item')!="0"){
                        $this->db->where('dpstock_itemid', $this->input->post('item'));
                        }
                 if($this->input->post('minval')!=""){
                        $this->db->where('dpstock_qty >', $this->input->post('minval'));
                        }
                        
                        if($this->input->post('maxval')!=""){
                        $this->db->where('dpstock_qty <', $this->input->post('maxval'));
                        } 
                        
            $this->db->join( "mes_item", "mes_depot_stock.dpstock_itemid = mes_item.item_name", 'left');       
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');    
            $this->db->order_by('brand_id', 'ASC');
            
	        }
	        
	        
       
        $query = $this->db->get();
        $dataa = $query->result();
        
        $user_detail['ref'] = $user_detail['todate'] = "";
        $user_detail['fdate'] = $this->input->post('depo');
            
        $inv = array();
         for($i=0; $i<count($dataa); $i++){
             
                    $data = $this->Product_model->single_product($dataa[$i]->itemid);
                    $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    
                    if(($this->input->post('depo')>0)&&($this->input->post('depo')<1000)){
                         $this->db->from('batch_depo');
                    }else{
                         $this->db->from('batch');
                    }
                    $this->db->select("*");
                    //$this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                    $this->db->where('avl_qty >',0);
                        if(($this->input->post('depo')>0)&&($this->input->post('depo')<1000)){
                                $this->db->where('depo',$this->input->post('depo'));
                        }
                    $this->db->where('product', $dataa[$i]->itemid);
                    $price = $this->db->get();
                    $pricelist = $price->result();
                  $invdt =array();  
                     for($k=0; $k<count($pricelist); $k++){
                                                    $invdt[]=array($pricelist[$k]->batch_id,$pricelist[$k]->batch,$pricelist[$k]->avl_qty,$pricelist[$k]->selling_price);
                                                }
                     
                    //$buy = $pricelist[0]->buying;
                    //$trl = $pricelist[0]->retail;
                    //$ssel = $pricelist[0]->selling;
                    
                            if($this->input->post('cat')!="0"){
                                if($data[0]->item_cat==$this->input->post('cat')){
                                    $inv[] = array($itm_code,$data[0]->itm_name,$dataa[$i]->stockk,$dataa[$i]->stockk,$dataa[$i]->stockk,$dataa[$i]->stockk,$data[0]->item_selling_price,$invdt);
                                }
                            }else{
                                $inv[] = array($itm_code,$data[0]->itm_name,$dataa[$i]->stockk,$dataa[$i]->stockk,$dataa[$i]->stockk,$dataa[$i]->stockk,$data[0]->item_selling_price,$invdt);
                            
                            }
        }
	    }
	    
      
        $user_detail['depoid'] = $this->input->post('depo');
        if(isset($dp_data[0]->dep_name)){
            
          $user_detail['deponame'] = $dp_data[0]->dep_name ;  
        }else{$user_detail['deponame'] = "All"  ;}
        
        $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;
        
        $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_stock_bal',$data);
	}
	
	function gen_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_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['cus'] = $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/gen_sum'); 
		        $this->load->view('template',$data);
	}
	
	function gen_sum_pdf(){
	    
	        $this->db->select("*");    
            $this->db->from('mes_user');
            //$this->db->where('user_id', "0");
            $this->db->where('user_id', $this->input->post('ref'));
            $query = $this->db->get();
            $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    if($this->input->post('det')==1){
	        $user_detail['det'] = "All Customer Details";
	        $this->db->select("cust_company as detail,cust_id as id,cust_idtext as code,cust_addres as add, cust_contact as num");    
            $this->db->from('mes_customer');
            if($this->input->post('sta')=="A"){
                    $this->db->where('cust_status',1);}
                else if($this->input->post('sta')=="I"){
                    $this->db->where('cust_status',0);}
            if($this->input->post('ref')!="0"){
                
                $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
                $user_detail['det'] = $user_detail['fullname']. " Customer Details";
                $this->db->where('cust_salseperson', $this->input->post('ref'));
                if($this->input->post('sta')=="A"){
                    $this->db->where('cust_status',1);}
                else if($this->input->post('sta')=="I"){
                    $this->db->where('cust_status',0);}
                    
                    }
                    
	    }elseif($this->input->post('det')==2){
	        $user_detail['det'] = "All Item Details";
	        $this->db->select("item_id as detail");  
	        $this->db->from('mes_item');
	        $dt = $this->input->post('cate') ;
	            if(count($dt)>0){
	                $this->db->where('item_cat',$dt[0]);
	            
        	    for($i=1; $i<count($dt); $i++){
        	       // echo $dt[$i];
        	        $this->db->or_where('item_cat',$dt[$i]);
        	    }
	            }
	            
                if($this->input->post('sta')=="A"){
                    $this->db->where('item_status',1);}
                else if($this->input->post('sta')=="I"){
                    $this->db->where('item_status',0);}
            
	    }elseif($this->input->post('det')==3){
	        $user_detail['det'] = "All Ref";
	        $this->db->select("user_fname as detail1,user_lname as detail2,user_id as id,user_code as code");    
            $this->db->from('mes_user');
            $this->db->where('user_lvl',20);
            if($this->input->post('sta')=="A"){
                    $this->db->where('user_status',1);}
                else if($this->input->post('sta')=="I"){
                    $this->db->where('user_status',0);}
            
	    }else{
	        $user_detail['det'] = "All Bank";
	        $this->db->select("bank_name as detail,bank_id as id,bank_code as code");    
            $this->db->from('mes_bank');
             if($this->input->post('sta')=="A"){
                    $this->db->where('bank_stetus',1);}
                else if($this->input->post('sta')=="I"){
                    $this->db->where('bank_stetus',0);}
            
	    }
       
        $query = $this->db->get();
        $dataa = $query->result();
        
        $user_detail['ref'] = 
        $user_detail['fdate'] = "";
            
        $inv = array();
         for($i=0; $i<count($dataa); $i++){
             
             if($this->input->post('det')==2){
                    $data = $this->Product_model->single_product($dataa[$i]->detail);
                   $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount; 
                   
                    $this->db->from('batch');
                    $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                    $this->db->where('avl_qty >',0);
                    $this->db->where('product', $dataa[$i]->detail);
                    $price = $this->db->get();
                    $pricelist = $price->result();
                    
                    $buy = $pricelist[0]->buying;
                    $trl = $pricelist[0]->retail;
                    $ssel = $pricelist[0]->selling;
                    
                    if($ssel<1){
                        $this->db->from('batch');
                        $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                        $this->db->order_by('exp_date', 'DESC');
                        $this->db->where('product', $dataa[$i]->detail);
                        $price = $this->db->get();
                        $pricelist = $price->result();
                            $buy = $pricelist[0]->buying;
                            $trl = $pricelist[0]->retail;
                            $ssel = $pricelist[0]->selling;
                    }
                    
                    if($ssel<1){
                        $this->db->from('batch');
                        $this->db->select("MAX(`buying_price`) as buying, MAX(`selling_price`) as selling, MAX(`retail_price`) as retail");
                        //$this->db->order_by('batch_id', 'DESC');
                        $this->db->where('product', $dataa[$i]->detail);
                        $price = $this->db->get();
                        $pricelist = $price->result();
                            $buy = $pricelist[0]->buying;
                            $trl = $pricelist[0]->retail;
                            $ssel = $pricelist[0]->selling;
                    }
                    
                    
                   
                   
                           if($this->input->post('sta')=="A"){
                               if($data[0]->item_status==1){
                                   $inv[] = array($itm_code,$data[0]->itm_name,$ssel,$trl);
                               }
                            }
                        else if($this->input->post('sta')=="I"){
                            if($data[0]->item_status==0){
                                   $inv[] = array($itm_code,$data[0]->itm_name,$data[0]->item_selling_price,$data[0]->item_retail_price);
                               }
                        }
                   
             }elseif($this->input->post('det')==3){
                  $inv[] = array($dataa[$i]->code.$dataa[$i]->id,$dataa[$i]->detail1." ".$dataa[$i]->detail1);
             }else if($this->input->post('det')==1){
                 $contact = $dataa[$i]->detail."\n".$dataa[$i]->add;
                 $inv[] = array($dataa[$i]->code.$dataa[$i]->id,$contact,$dataa[$i]->num);
             }else{
                 $inv[] = array($dataa[$i]->code.$dataa[$i]->id,$dataa[$i]->detail);
             }
                               
                            
        }
      
      
        $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;
        
        $data['inv'] = $user_detail;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
         
         if($this->input->post('det')==2){
             
             $this->load->view('report/pdf_gen_sum_itm',$data);
         }else if($this->input->post('det')==1){
             $this->load->view('report/pdf_gen_sum_cus',$data);
        }else{
             $this->load->view('report/pdf_gen_sum',$data);
         }
          
	}
	
	function get_date_stock(){
	   $item_id = $this->input->post('item');
	   $dt = $this->input->post('dt');
	   

	   $this->db->select("invt_type_id,invt_qty,invt_stock,invt_cdate");    
        $this->db->from('mes_inventry');
        $this->db->where('invt_cdate>=', $dt);
        $this->db->where('invt_itemid',$item_id);
        $this->db->where('invt_type','INV');
        $this->db->order_by('invt_id', 'ASC');
        $this->db->limit(1);
        $query = $this->db->get();
        $data=$query->result();
        
        echo json_encode($data);
	    
	}
	
	
	function fast_move_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_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/fast_move'); 
		        $this->load->view('template',$data);
	}
	
	function fast_move_pdf(){
	    
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', "0");
       // $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;
	    
	                $this->db->select('sum(qto_items.po_itm_qty) as totall,mes_qto.*,qto_items.*,item.*,mes_depot.*,mes_item.item_selling_price');
	                $this->db->from('mes_qto');
	                $this->db->join( "qto_items", "qto_items.po_itm_po_id = mes_qto.po_id", 'left');
	                $this->db->join( "mes_item", "qto_items.po_itm_item_id = mes_item.item_id", 'left');
	                $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
	                $this->db->join( "mes_depot", "mes_depot.dep_id = mes_qto.depo", 'left');
                 
                    
           if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
            $user_detail['depo']="";
            if($this->input->post('depo')!=""){
                $user_detail['depo'] = $this->input->post('depo');
                $this->db->where('mes_qto.depo', $this->input->post('depo'));
            }
                    
        $this->db->group_by('qto_items.po_itm_item_id');
        $this->db->order_by('sum(qto_items.po_itm_qty)', 'desc');
        $query = $this->db->get();
        
        $dataa = $query->result();
        
        $user_detail['ref'] = 
       // $user_detail['fdate'] = "";
            
        $inv = array();
         for($i=0; $i<count($dataa); $i++){
                $data = $this->Product_model->single_product($dataa[$i]->po_itm_item_id);
                   $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                
                if($this->input->post('cat')!="0"){
                    if($data[0]->cat_id==$this->input->post('cat')){
                        $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->dep_name,$itm_code,$dataa[$i]->itm_name,$dataa[$i]->totall,$dataa[$i]->po_cdate,$dataa[$i]->po_cuser,$dataa[$i]->item_selling_price);
             
                    }
                }else{
                    $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->dep_name,$itm_code,$dataa[$i]->itm_name,$dataa[$i]->totall,$dataa[$i]->po_cdate,$dataa[$i]->po_cuser,$dataa[$i]->item_selling_price);
             
                }
                 
        }
      
        $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;
        
        $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_fast_move',$data);
         
          
	}
	
	
	function outstand_view(){
	    
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                     if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['ref'] = $sls;
                    
                    $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                     if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    //$this->db->where('user_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['alref'] = $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/outstand_view'); 
		        $this->load->view('template',$data);
	}
	
	function outstand_pdf(){
	     $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_fname." ".$user_data[0]->user_lname;
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_customer');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_customer.cust_salseperson', 'left');
        $this->db->join('cities', 'mes_customer.cust_city   = cities.id', 'left');
        
            $user_detail['inv']=""; 
            $user_detail['frmdate']=$this->input->post('frm_date'); 
            $user_detail['todate']=$this->input->post('to_date'); 
        
           
           $user_detail['cus']=""; 
            if($this->input->post('cus')!=0){
                $user_detail['cus'] = $this->input->post('ref')."-".$this->input->post('cus');
               $this->db->where('mes_customer.cust_id', $this->input->post('cus'));
            }
            
            if($this->input->post('ref')!=0){
                $user_detail['cus'] = $this->input->post('ref')."-".$this->input->post('cus');
               $this->db->where('mes_customer.cust_salseperson', $this->input->post('ref'));
            }
           
        $this->db->order_by('cust_company', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
        $totoutsnd = 0;
         for($i=0; $i<count($dataa); $i++){
            //$out = $this->Common_model->outstand($dataa[$i]->cust_id);
            
                    $this->db->select('*');
	                $this->db->from('mes_qto');
	                
	                if($this->input->post('ref')!=0){
                                $this->db->where('po_ref_id',$this->input->post('ref')); 
                        } 
	                
                        if($this->input->post('frm_date')!=""){
                                $this->db->where('po_cdate>=',$this->input->post('frm_date')); 
                        } 
                        if($this->input->post('to_date')!=""){
                                $this->db->where('po_cdate<=',$this->input->post('to_date')); 
                        }else{ $this->db->where('po_cdate<=', date('Y-m-d'));}  
                        
                    $this->db->where('po_payment',0);
                    $this->db->where('po_sup_id',$dataa[$i]->cust_id);
                    $hasil=$this->db->get();
                    $sls= $hasil->result();
                $det = array();
                    for($j=0; $j<count($sls); $j++){
                        $now = time(); // or your date as well
                        $your_date = strtotime($sls[$j]->po_cdate);
                        $datediff = $now - $your_date;
                        $days = round($datediff / (60 * 60 * 24));
            $inv_tot = $sls[$j]->po_final_tot - $sls[$j]->return_amnt;
                            $this->db->from('mes_payment');
                            $this->db->select("sum(pay_amount) as paid");
                            $this->db->where('pay_invid',$sls[$j]->po_id);
                            $this->db->where('pay_stetus',1);
                            //$this->db->where('pay_authrize',1);
                            $detailtax = $this->db->get();
                            $inv_data = $detailtax->result();
                    $bal_out = $inv_tot-$inv_data[0]->paid;    
                    
                                if(($sls[$j]->po_final_tot!=$sls[$j]->return_amnt)){
                                    if($bal_out>0){
                            $det[] = array($sls[$j]->qto_code.$sls[$j]->po_id,$sls[$j]->po_cdate,$days,$sls[$j]->po_final_tot,$bal_out,$inv_data[0]->paid,$sls[$j]->return_amnt);
                                    } 
                                }
                       
                        
                    }
                    
                     
            if(count($det)>0){
                //$totoutsnd = $totoutsnd + $bal_out;
                $inv[] = array($dataa[$i]->cust_idtext.$dataa[$i]->cust_id,$dataa[$i]->cust_company." - ".$dataa[$i]->name_en,$dataa[$i]->user_fname." ".$dataa[$i]->user_lname,$det,$totoutsnd);
                
            }
        }
      
        $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();
                $dd = $query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_outstand',$data);
	}
	
	function outstand_pdf_invoicevise(){ 
	     $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('invref'));
       // $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;
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->join('mes_customer', 'mes_customer.cust_id = mes_qto.po_sup_id', 'left');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_qto.po_ref_id', 'left');
        $this->db->join('cities', 'mes_customer.cust_city   = cities.id', 'left');
        
            $user_detail['inv']=""; 
            $user_detail['frmdate']=$this->input->post('invfrm_date'); 
            $user_detail['todate']=$this->input->post('invto_date'); 
        
           
           $user_detail['cus']=""; 
          
            
            if($this->input->post('invref')!=0){
                $user_detail['cus'] = $this->input->post('invref');
               $this->db->where('mes_qto.po_ref_id', $this->input->post('invref'));
            }
           
        
        $this->db->group_by('po_sup_id', 'asc');   
        $this->db->order_by('cust_company', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
        $totoutsnd = 0;
         for($i=0; $i<count($dataa); $i++){
            //$out = $this->Common_model->outstand($dataa[$i]->cust_id);
            
                    $this->db->select('*');
	                $this->db->from('mes_qto');
	                
	                if($this->input->post('invref')!=0){
                                $this->db->where('po_ref_id',$this->input->post('invref')); 
                        } 
	                
                        if($this->input->post('invfrm_date')!=""){
                                $this->db->where('po_cdate>=',$this->input->post('invfrm_date')); 
                        } 
                        if($this->input->post('invto_date')!=""){
                                $this->db->where('po_cdate<=',$this->input->post('invto_date')); 
                        }else{ $this->db->where('po_cdate<=', date('Y-m-d'));}  
                        
                    $this->db->where('po_payment',0);
                    $this->db->where('po_sup_id',$dataa[$i]->cust_id);
                    $hasil=$this->db->get();
                    $sls= $hasil->result();
                $det = array();
                    for($j=0; $j<count($sls); $j++){
                        $now = time(); // or your date as well
                        $your_date = strtotime($sls[$j]->po_cdate);
                        $datediff = $now - $your_date;
                        $days = round($datediff / (60 * 60 * 24));
            $inv_tot = $sls[$j]->po_final_tot - $sls[$j]->return_amnt;
                            $this->db->from('mes_payment');
                            $this->db->select("sum(pay_amount) as paid");
                            $this->db->where('pay_invid',$sls[$j]->po_id);
                            $this->db->where('pay_stetus',1);
                            $this->db->where('pay_authrize',1);
                            $detailtax = $this->db->get();
                            $inv_data = $detailtax->result();
                    $bal_out = $inv_tot-$inv_data[0]->paid;    
                    
                                if(($sls[$j]->po_final_tot!=$sls[$j]->return_amnt)){
                                    if($bal_out>0){
                            $det[] = array($sls[$j]->qto_code.$sls[$j]->po_id,$sls[$j]->po_cdate,$days,$sls[$j]->po_final_tot,$bal_out,$inv_data[0]->paid,$sls[$j]->return_amnt);
                                    } 
                                }
                       
                        
                    }
                    
                     
            if(count($det)>0){
                //$totoutsnd = $totoutsnd + $bal_out;
                $inv[] = array($dataa[$i]->cust_idtext.$dataa[$i]->cust_id,$dataa[$i]->cust_company." - ".$dataa[$i]->name_en,$dataa[$i]->user_fname." ".$dataa[$i]->user_lname,$det,$totoutsnd);
                
            }
        }
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
         
         
                
                
        //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();
                $dd = $query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_outstand',$data);
	}
	
	
	function rt_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->order_by("cust_company", "asc");
                    $query = $this->db->get();
                    $custdata=$query->result();
                    $inv = $invv =  array();
                         for($k=0; $k<count($custdata); $k++){
                             $this->db->select('*');
        	                $this->db->from('mes_qto');
                            $this->db->where('po_sup_id',$custdata[$k]->cust_id);
                            $this->db->where('po_payment',0);
                            
                            
                            if($this->input->post('frmm_date')!=""){
                                $this->db->where('po_cdate>=',$this->input->post('frmm_date')); 
                                //$this->db->where('po_cdate<=',$this->input->post('too_date'));
                                //$user_detail['fdate'] =$this->input->post('frmm_date'); 
                            }
                            
                           if($this->input->post('too_date')!=""){
                              $this->db->where('po_cdate<=',$this->input->post('too_date')); 
                               // $user_detail['todate'] =$this->input->post('too_date');
                            }
                            
                            $hasil=$this->db->get();
                            $sls= $hasil->result();
                        $det = array();
                            for($j=0; $j<count($sls); $j++){
                                $now = time(); // or your date as well
                                $your_date = strtotime($sls[$j]->po_cdate);
                                $datediff = $now - $your_date;
                                $days = round($datediff / (60 * 60 * 24));
                     $inv_tot = $sls[$j]->po_final_tot - $sls[$j]->return_amnt;
                                    $this->db->from('mes_payment');
                                    $this->db->select("sum(pay_amount) as paid");
                                    $this->db->where('pay_invid',$sls[$j]->po_id);
                                    $this->db->where('pay_stetus',1);
                                    $this->db->where('pay_authrize',1);
                                    $detailtax = $this->db->get();
                                    $inv_data = $detailtax->result();
                    $bal_out = $inv_tot-$inv_data[0]->paid;                
                                if(($bal_out!=0.00)){
                                    $det[] = array($sls[$j]->qto_code.$sls[$j]->po_id,$sls[$j]->po_cdate,$days,$sls[$j]->po_final_tot,$bal_out,$inv_data[0]->paid,$sls[$j]->return_amnt);
                                }
                                
                            }
                    if(count($det)>0){
                        $inv[] = array($custdata[$k]->cust_idtext.$custdata[$k]->cust_id,$custdata[$k]->cust_company,$custdata[$k]->cust_id." ".$custdata[$k]->cust_id,$det,$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();
                        $dataa=$query->result();
                        
                        $user_detail['rt_name']=$dataa[0]->route_name; 
                         
                         $inv = $invv =  array();   
                         $all1 = explode(",",$dataa[0]->cities);
                         $all = array_unique($all1);
                                 //for($i=0; $i<count($all); $i++){
                                   //  $this->db->select("*");    
                                   // $this->db->from('cities');
                                   // $this->db->where('id',$all[$i]);
                                   //// $query = $this->db->get();
                                   // $cty =$query->result();
                                     
                                    $cust = array(); 
                                     
                                    $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();
                                    $custdata=$query->result();
                                    
                                         for($k=0; $k<count($custdata); $k++){
                                             $this->db->select('*');
                        	                $this->db->from('mes_qto');
                                            $this->db->where('po_sup_id',$custdata[$k]->cust_id);
                                            $this->db->where('po_payment',0);
                                            if($this->input->post('frmm_date')!=""){$this->db->where('po_cdate>=',$this->input->post('frmm_date'));}
                                            if($this->input->post('too_date')!=""){$this->db->where('po_cdate<=',$this->input->post('too_date'));}
                                            
                                            $hasil=$this->db->get();
                                            $sls= $hasil->result();
                                        $det = array();
                                            for($j=0; $j<count($sls); $j++){
                                                $now = time(); // or your date as well
                                                $your_date = strtotime($sls[$j]->po_cdate);
                                                $datediff = $now - $your_date;
                                                $days = round($datediff / (60 * 60 * 24));
                                     $inv_tot = $sls[$j]->po_final_tot - $sls[$j]->return_amnt;
                                                    $this->db->from('mes_payment');
                                                    $this->db->select("sum(pay_amount) as paid");
                                                    $this->db->where('pay_invid',$sls[$j]->po_id);
                                                    $this->db->where('pay_stetus',1);
                                                    $this->db->where('pay_authrize',1);
                                                    $detailtax = $this->db->get();
                                                    $inv_data = $detailtax->result();
                                        $bal_out = $inv_tot-$inv_data[0]->paid;            
                                                if($inv_tot!=0){
                                                    $det[] = array($sls[$j]->qto_code.$sls[$j]->po_id,$sls[$j]->po_cdate,$days,$sls[$j]->po_final_tot,$bal_out,$inv_data[0]->paid,$sls[$j]->return_amnt);
                                                }
                                                
                                            }
                                    if(count($det)>0){
                                        $inv[] = array($custdata[$k]->cust_idtext.$custdata[$k]->cust_id,$custdata[$k]->cust_company,$custdata[$k]->cust_id." ".$custdata[$k]->cust_id,$det,$custdata[$k]->name_en);
                                    }
                                        
                                    }
                                    
                                //}
                
	    }        
                
        //print_r($inv);
        
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
              
                
                
        //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();
                $dd = $query->result();
                
                $data['tm'][1] = $dd[0];
         
        $this->load->view('report/pdf_route_outstand',$data);
	}
	
	function route_det(){
	            $this->db->select("*");    
                $this->db->from('mes_route');
                $this->db->where('ref_id', $this->input->post('ref_id'));
                $query = $this->db->get();
                $data['det'] =$query->result();
                
            echo json_encode($data);    
	}
	
	function city_det(){
	            $this->db->select("*");    
                $this->db->from('mes_route');
                $this->db->where('route_id', $this->input->post('rtid'));
                $query = $this->db->get();
                $rt=$query->result();
                
            $inv = array();   
         $all = explode(",",$rt[0]->cities);
                 for($i=0; $i<(count($all)-1); $i++){
                     $this->db->select("*");    
                    $this->db->from('cities');
                    $this->db->where('id',$all[$i]);
                    $query = $this->db->get();
                    $cty =$query->result();    
                    
                    $inv[] = array($cty[0]->id,$cty[0]->name_en);
                 } 
                 
                 $data['det'] = $inv;
                
            echo json_encode($data);    
	}
    
    
    
    
}	