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

class Report_Controller3 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 item_detail(){
	      $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;
                    
                     $this->db->select('*');
                    $this->db->from('mes_depot');
                    $this->db->where('dep_stetus',1);
                    $hasil=$this->db->get();
                    $depo= $hasil->result();
                    $data['dop'] = $depo;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
            
            $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');
                    $hasil=$this->db->get();
                    $dep= $hasil->result(); 
            $data['dep'] = $dep;
                
                
                $data['content'] = ('report/item_detail'); 
		        $this->load->view('template',$data);
	}
	
	
	function prnt_item_detail(){
	    $user_detail['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
	    
	    $this->db->select("item_id");    
        $this->db->from('mes_item');
        $query = $this->db->get();
        $dataa=$query->result();
    
            
                 $data = $this->Product_model->single_product($this->input->post('item'));
                 $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                 $itm_nm = $data[0]->itm_name;
                 
                    $this->db->select("*");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                    $this->db->where('invt_itemid',$this->input->post('item'));
                  $this->db->where('invt_type!=','temprem');
                  $this->db->where('invt_type!=','temp');
                    $this->db->where('invt_type!=','tmptran');
                    $this->db->where('invt_type!=','tmptran_rem');
                    $this->db->where('invt_type!=','tmptran_del');
                    $this->db->where('invt_type!=','return');
                    $this->db->where('invt_proc!=','0');
                    
                    if($this->input->post('dop')!=0){
                        $this->db->where('invt_type!=','GRN');
                    }
                    
                        if($this->input->post('frm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_date');
                            $this->db->where('invt_cdate>=', $this->input->post('frm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('to_date') . ' +1 day'));
                            $this->db->where('invt_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                             if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('to_date') . ' +1 day'));
                            $this->db->where('invt_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        
                    $query = $this->db->get();
                    $detail = $query->result();
            $inv=array(); 
            for($i=0; $i<count($detail); $i++){
                
                $bal = 0; $amp = "n";  $depo = "";
                if($detail[$i]->invt_type=="INV"){
                        $this->db->select("mes_depot.dep_name as depart,mes_depot.dep_id as depp_id");
                        $this->db->from('mes_qto');
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_qto.depo", 'left');
                        $this->db->where('po_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $dep =$query->result();
                    
                    $reason = "INVOICE (".$dep[0]->depart.")"; $type_id = "INV".$detail[$i]->invt_type_id; $invuser = $detail[$i]->user_name; $cal = "M";
                    if($dep[0]->depp_id!=0){$amp = "y";} $depo = $dep[0]->depp_id;
                    
                }else if($detail[$i]->invt_type=="GRN") {
                    $reason = "GRN"; $type_id = "GRN".$detail[$i]->invt_type_id; $cal = "P";
                    $depo = 0;
                }else if($detail[$i]->invt_type=="ADJ") {
                    
                    if($detail[$i]->cal!="plus"){$cal="M";}else{$cal="P";}
                    $reason = "ADJUSTMENT"; $type_id = $detail[$i]->invt_type_id; 
                            $date_time = $detail[$i]->invt_cdate;
                            $new_date = date("Y-m-d",strtotime($date_time));
                    
                        $this->db->select("batch_edit_depo as depp_id");
                        $this->db->from('batch_edit_log');
                        $this->db->where('batch_edit_item_id', $detail[$i]->invt_itemid);
                        $this->db->where('batch_edit_qty', $detail[$i]->invt_qty);
                        $this->db->where('batch_edit_date', $new_date);
                        $query = $this->db->get();
                        $dep =$query->result();
                        
                        $depo = $dep[0]->depp_id;
                    
                }else if($detail[$i]->invt_type=="RTN") { 
                        $this->db->select("ret_reason");
                        $this->db->from('mes_return');
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = mes_return.ret_invnum", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                        if(count($ret_res)>0){$reason = $ret_res[0]->ret_reason;}else{$reason = "";}
                     $type_id = "GRNRTN".$detail[$i]->invt_type_id; $cal = "M"; $depo=0;
                    
                }else if($detail[$i]->invt_type=="TRS") {
                        $this->db->from('mes_stock_trancfer');
                        $this->db->select("*");
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left'); 
                        $this->db->where('trncfer_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                     $type_id = "TRN".$detail[$i]->invt_type_id; 
                     
                    if($detail[$i]->cal=="min"){
                        if($ret_res[0]->trncfer__from_dpoid==$this->input->post('dop')){ $reason = "TO ".$ret_res[0]->dep_name; $depo=$this->input->post('dop'); $cal = "M";}
                    } else{
                        $this->db->from('mes_depot');
                        $this->db->select("dep_name");
                        $this->db->where('dep_id', $ret_res[0]->trncfer__from_dpoid);
                        $queryy = $this->db->get();
                            $todepo =$queryy->result();
                        
                        if($ret_res[0]->trncfer_dpoid==$this->input->post('dop')){ $reason = "FROM ".$todepo[0]->dep_name; $depo=$this->input->post('dop'); $cal = "P";}
                    }
                    
                    
                    
                    
                }else if($detail[$i]->invt_type=="INVRTN") {
                    
                        $this->db->from('inv_return');
                        $this->db->select("mes_ret_reason.ret_reason,ret_depo,ret_invnum");
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                        
                         $rtinv = $ret_res[0]->ret_invnum;
                        $this->db->from('mes_qto');
                        $this->db->select("depo");
                        $this->db->where('po_id', $rtinv);
                        $query = $this->db->get();
                        $ret_dep =$query->result();
                        
                    $reason = $ret_res[0]->ret_reason; $type_id = "INVRTN".$detail[$i]->invt_type_id; $cal = "P"; $depo = $ret_dep[0]->depo;
                    
                }else{
                    $reason = ""; $type_id = $detail[$i]->invt_type_id; $cal = $depo="";
                }
                
                if($cal=="P"){$plus=$detail[$i]->invt_qty;$min=0;$minn="";}else{$min=$detail[$i]->invt_qty;$plus=0;$pluss="";}
                $bal = $detail[$i]->invt_stock + $plus - $min;
                
                if($detail[$i]->invt_type=="INV"){$uuser = $invuser;} else{$uuser = $detail[$i]->user_name;}
                if($depo==$this->input->post('dop')){
                    $inv[] = array($itm_code,$itm_nm,$detail[$i]->invt_type,$type_id,$detail[$i]->invt_qty,$detail[$i]->invt_cdate,$detail[$i]->invt_stock,$reason,$uuser,$min,$plus,$bal);
                }
                    
                    $type = $detail[$i]->invt_type_id;
                    
            }
            
           $var = (int)$this->input->post('item');
                    $this->db->select("*");    
                    $this->db->from('temp_qto_items');
                    $this->db->where('po_itm_item_id',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                    $inv[] = array($itm_code,$itm_nm,"TMPINV","",($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),date('Y-m-d'),($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),$reason,$dataa[$i]->po_user,($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),"0","0");
                 
                }
                
                $this->db->select("*");    
                    $this->db->from('temp_trf_itms');
                    $this->db->join( "batch", "batch.batch_id = temp_trf_itms.temp_trf_batch", 'left');
                    $this->db->where('batch.product',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                    $inv[] = array($itm_code,$itm_nm,"TMPTRF","",$dataa[$i]->temp_trf_qty,date('Y-m-d'),$dataa[$i]->temp_trf_qty,$reason,$dataa[$i]->temp_trf_user,$dataa[$i]->temp_trf_qty,"0","0");
                 
                }
            
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_item_detail',$data);
        
	}
	
	function prnt_allitem_detail(){
	    
	     $user_detail['fdate'] = $this->input->post('afrm_date');
        $user_detail['todate'] = $this->input->post('ato_date');
        
	    
	    $this->db->select("item_id");    
        $this->db->from('mes_item');
        $query = $this->db->get();
        $dataa_item=$query->result();
        
        $depo_name = "";
        $inv_all=array(); 
            for($x=0; $x<count($dataa_item); $x++){
                $item_id = $dataa_item[$x]->item_id;
                $data = $this->Product_model->single_product($item_id);
                 $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                 $itm_nm = $data[0]->itm_name;
                 
                  $this->db->from('batch');
                        $this->db->select("selling_price");
                        $this->db->where('product',$item_id );
                        $queryy = $this->db->get();
                            $p_price =$queryy->result();
                            if(count($p_price)>0){
                                $prod_price = $p_price[0]->selling_price;   
                            }else{
                                $prod_price = 0;   
                            }
                    
                    
                 
                    $this->db->select("*");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                     
                    $this->db->where('invt_itemid',$item_id);
                  $this->db->where('invt_type!=','temprem');
                  $this->db->where('invt_type!=','temp');
                    $this->db->where('invt_type!=','tmptran');
                    $this->db->where('invt_type!=','tmptran_rem');
                    $this->db->where('invt_type!=','tmptran_del');
                    $this->db->where('invt_type!=','return');
                    $this->db->where('invt_type!=','INVRTN');
                    $this->db->where('invt_proc!=','0');
                    
                    if($this->input->post('adop')!=0){
                        $this->db->where('invt_type!=','GRN');
                    }
                    
                        if($this->input->post('afrm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('afrm_date');
                            $this->db->where('invt_cdate>=', $this->input->post('afrm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('ato_date')!=""){
                            $user_detail['todate'] = $this->input->post('ato_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('ato_date') . ' +1 day'));
                            $this->db->where('invt_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        
                        
                    $query = $this->db->get();
                    $detail = $query->result();
                    
            $inv=array(); 
           $linecnt = 0;
            for($i=0; $i<count($detail); $i++){
                
               
                $bal = 0; $amp = "n";  $depo = ""; $inv_price = 0; $cal = $depo="";$invuser = $detail[$i]->user_name;
                $invrtn_qty = 0;
                if($detail[$i]->invt_type=="INV"){
                        $this->db->select("mes_depot.dep_name as depart,mes_depot.dep_id as depp_id,mes_customer.cust_company");
                        $this->db->from('mes_qto');
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_qto.depo", 'left');
                        $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
                        $this->db->where('po_id', $detail[$i]->invt_type_id);
                        //$this->db->where('mes_qto.return_amnt!=mes_qto.po_final_tot');
                        //$this->db->where('po_status!=','0');
                        $query = $this->db->get();
                        $dep =$query->result();
                        
                        $this->db->select("po_itm_unitprice");
                        $this->db->from('qto_items');
                        $this->db->where('po_itm_item_id',$item_id);
                        $this->db->where('po_itm_po_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $pp =$query->result();
                        $inv_price = $pp[0]->po_itm_unitprice;
                        
                        if(count($dep)>0){
                                $depo_name = $dep[0]->depart;    
                        
                                $reason = $dep[0]->cust_company; 
                               // $reason = $dep[0]->cust_company."(".$dep[0]->depart.")"; 
                                $type_id = "INV".$detail[$i]->invt_type_id; $invuser = $detail[$i]->user_name; $cal = "M";
                                if($dep[0]->depp_id!=0){$amp = "y";} $depo = $dep[0]->depp_id;
                        }
                        
                    
                    
                }else if($detail[$i]->invt_type=="GRN") {
                    $reason = "GRN"; $type_id = "GRN".$detail[$i]->invt_type_id; $cal = "P";
                    $depo = 0;
                }else if($detail[$i]->invt_type=="ADJ") {
                    
                    if($detail[$i]->cal!="plus"){$cal="M";}else{$cal="P";}
                    $reason = "ADJUSTMENT"; $type_id = $detail[$i]->invt_type_id; 
                            $date_time = $detail[$i]->invt_cdate;
                            $new_date = date("Y-m-d",strtotime($date_time));
                    
                        $this->db->select("batch_edit_depo as depp_id");
                        $this->db->from('batch_edit_log');
                        $this->db->where('batch_edit_item_id', $detail[$i]->invt_itemid);
                        $this->db->where('batch_edit_qty', $detail[$i]->invt_qty);
                        $this->db->where('batch_edit_date', $new_date);
                        $query = $this->db->get();
                        $dep =$query->result();
                        
                        $depo = $dep[0]->depp_id;
                    
                }else if($detail[$i]->invt_type=="RTN") { 
                        $this->db->select("ret_reason");
                        $this->db->from('mes_return');
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = mes_return.ret_invnum", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                        if(count($ret_res)>0){$reason = $ret_res[0]->ret_reason;}else{$reason = "";}
                     $type_id = "GRNRTN".$detail[$i]->invt_type_id; $cal = "M"; $depo=0;
                    
                }else if($detail[$i]->invt_type=="TRS") {
                        $this->db->from('mes_stock_trancfer');
                        $this->db->select("*");
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left'); 
                        $this->db->where('trncfer_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                     $type_id = "TRN".$detail[$i]->invt_type_id; 
                     
                    if($detail[$i]->cal=="min"){
                        if($ret_res[0]->trncfer__from_dpoid==$this->input->post('adop')){ $reason = "TO ".$ret_res[0]->dep_name; $depo=$this->input->post('adop'); $cal = "M";}
                    } else{
                        $this->db->from('mes_depot');
                        $this->db->select("dep_name");
                        $this->db->where('dep_id', $ret_res[0]->trncfer__from_dpoid);
                        $queryy = $this->db->get();
                            $todepo =$queryy->result();
                        
                        if($ret_res[0]->trncfer_dpoid==$this->input->post('adop')){ $reason = "FROM ".$todepo[0]->dep_name; $depo=$this->input->post('adop'); $cal = "P";}
                    }
                    
                    
                    
                    
                
                    
                }else if($detail[$i]->invt_type=="INVRTNP") {
                    
                        $this->db->from('inv_return');
                        $this->db->select("mes_ret_reason.ret_reason,ret_depo,inv_return.ret_invnum,inv_return.ret_totqty");
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                       
                        $rtinv = $ret_res[0]->ret_invnum;
                        $this->db->from('mes_qto');
                        $this->db->select("depo");
                        $this->db->where('po_id', $rtinv);
                        //$this->db->where('po_status!=','0');
                        $query = $this->db->get();
                        $ret_dep =$query->result();
                        
                    if(count($ret_dep)>0){
                        $depo = $ret_dep[0]->depo;
                    }else{
                        $depo = "";
                    }    
                    
                    $depo = $ret_dep[0]->depo;
                    $reason = $ret_res[0]->ret_reason; 
                    $type_id = "INVRTN".$ret_res[0]->ret_invnum; 
                    $cal = "P"; 
                    $invrtn_qty = $ret_res[0]->ret_totqty;
                    
                    
                
                }else if($detail[$i]->invt_type=="CREDIT") {
                    
                        $this->db->from('inv_return_credit_main');
                        $this->db->select("*");
                        $this->db->where('retm_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                     
                        
                    
                    $depo = $ret_res[0]->retm_depo;
                    $reason = "CREDIT RETURN"; 
                    $type_id = "CRD".$ret_res[0]->retm_id; 
                    $cal = "P"; 
                    $invrtn_qty = $detail[$i]->invt_qty;
                
                    
                }else{
                    
                    $reason = ""; $type_id = $detail[$i]->invt_type_id; $cal = $depo="";
                }
                
                if($invrtn_qty!=0){
                      $invv_qty = $invrtn_qty;
                  }else{
                      $invv_qty = $detail[$i]->invt_qty;
                  }
                
                if($cal=="P"){$plus=$invv_qty;$min=0;$minn="";}else{$min=$invv_qty;$plus=0;$pluss="";}
                $bal = $detail[$i]->invt_stock + $plus - $min;
                
                if($detail[$i]->invt_type=="INV"){$uuser = $invuser;} else{$uuser = $detail[$i]->user_name;}
                
                
                if($depo==$this->input->post('adop')){
                  
                   
                    $linecnt++;
                    $inv[] = array($itm_code,$itm_nm,$detail[$i]->invt_type,$type_id,$invv_qty,$detail[$i]->invt_cdate,$detail[$i]->invt_stock,$reason,$uuser,$min,$plus,$bal,$prod_price,$inv_price);
                }
                    
                    $type = $detail[$i]->invt_type_id;
                   
            }
            
            //echo $linecnt."<br>"; 
            
            if($linecnt<1){
                
                $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                    $this->db->where('invt_itemid',$item_id);
                    $this->db->where('invt_type','INV');
                    $this->db->where('invt_proc!=','0');
                    $this->db->where('mes_qto.depo',$this->input->post('adop'));
                    $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                    $this->db->order_by("mes_inventry.invt_id", "desc");
                    $this->db->limit(1);
                       
                    $query = $this->db->get();
                    $detailbalinv = $query->result();
                    
                    if(count($detailbalinv)>0){
                        $lastinv = $detailbalinv[0]->invt_cdate;
                    }else{
                        $lastinv = "";
                    }
                    
                    
                            $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                            $this->db->from('mes_inventry');
                            $this->db->join( "inv_return", "inv_return.ret_id = mes_inventry.invt_type_id", 'left');
                            $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                            $this->db->where('invt_itemid',$item_id);
                            $this->db->where('invt_type','INVRTNP');
                            $this->db->where('invt_proc!=','0');
                            $this->db->where('inv_return.ret_depo',$this->input->post('adop'));
                            $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                            $this->db->order_by("mes_inventry.invt_id", "desc");
                            $this->db->limit(1);
                               
                            $query = $this->db->get();
                            $detailbalrtn = $query->result();
                            
                            if(count($detailbalrtn)>0){
                                $lastrtn = $detailbalrtn[0]->invt_cdate;
                            }else{
                                $lastrtn = "";
                            }
                    
                                    $this->db->select("invt_stock,invt_qty,invt_cdate,cal,invt_id");    
                                    $this->db->from('mes_inventry');
                                    $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                                    $this->db->where('invt_itemid',$item_id);
                                    $this->db->where('invt_type','TRS');
                                    $this->db->where('cal','min');
                                    $this->db->where('invt_proc!=','0');
                                    
                                    $this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                                   // $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                                   
                                    $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                                    $this->db->order_by("mes_inventry.invt_id", "desc");
                                    $this->db->limit(1);
                                    $query = $this->db->get();
                                    $detailbalfrm_trans = $query->result();
                                    
                                    if(count($detailbalfrm_trans)>0){
                                        $frmtrans = $detailbalfrm_trans[0]->invt_cdate;
                                    }else{
                                        $frmtrans = "";
                                    }
                                    
                    $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                                        $this->db->from('mes_inventry');
                                        $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                                        $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                                        $this->db->where('invt_itemid',$item_id);
                                        $this->db->where('invt_type','TRS');
                                        $this->db->where('cal!=','min');
                                        $this->db->where('invt_proc!=','0');
                                        
                                        //$this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                                        $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                                       
                                        $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                                        $this->db->order_by("mes_inventry.invt_id", "desc");
                                        $this->db->limit(1);
                                        $query = $this->db->get();
                                        $detailbalto_trans = $query->result();     
                                        
                                            if(count($detailbalto_trans)>0){
                                                $totrans = $detailbalto_trans[0]->invt_cdate;
                                            }else{
                                                $totrans = "";
                                            } 
                                   
                                            
                                    $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                                        $this->db->from('mes_inventry');
                                        $this->db->join( "inv_return_credit_main", "inv_return_credit_main.retm_id = mes_inventry.invt_type_id", 'left');
                                        $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                                        $this->db->where('invt_itemid',$item_id);
                                        $this->db->where('invt_type','CREDIT');
                                        $this->db->where('invt_proc!=','0');
                                        
                                        //$this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                                        $this->db->where('retm_depo',$this->input->post('adop'));
                                       
                                        $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                                        $this->db->order_by("mes_inventry.invt_id", "desc");
                                        $this->db->limit(1);
                                        $query = $this->db->get();
                                        $detailbalto_credt = $query->result();     
                                        
                                            if(count($detailbalto_credt)>0){
                                                $totcred = $detailbalto_credt[0]->invt_cdate;
                                            }else{
                                                $totcred = "";
                                            }
                                               
                                            
                                 // echo $totrans;          
                        $latest_date = array($lastinv, $frmtrans, $totrans, $totcred,$lastrtn); 
                        $mx_date = max($latest_date);
                        $index = array_search($mx_date, $latest_date);
                        
                        if($index==0){$detailbal = $detailbalinv;}
                        if($index==1){$detailbal = $detailbalfrm_trans;}
                        if($index==2){$detailbal = $detailbalto_trans;}
                        if($index==3){$detailbal = $detailbalto_credt;}
                        if($index==4){$detailbal = $detailbalrtn;}
                        
                   // echo $item_id."--".$mx_date."index".$index;
                    //echo "</br>";
                
                    if(count($detailbal)>0){
                        if(($detailbal[0]->cal)!="min"){
                                 $dtbal = $detailbal[0]->invt_stock+$detailbal[0]->invt_qty;
                            }else{
                                $dtbal = $detailbal[0]->invt_stock-$detailbal[0]->invt_qty;
                            }
                        if($index == 0){
                            $dtbal =  $detailbal[0]->invt_stock - $detailbal[0]->invt_qty;
                        }    
                            
                       
                        $cdt = $detailbal[0]->invt_cdate;
                    }else{
                        $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                            $this->db->from('mes_inventry');
                            $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                            $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                            $this->db->where('invt_itemid',$item_id);
                            $this->db->where('invt_type','TRS');
                            $this->db->where('cal','min');
                            $this->db->where('invt_proc!=','0');
                            
                            $this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                           // $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                           
                            $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                            $this->db->order_by("mes_inventry.invt_id", "desc");
                            $this->db->limit(1);
                            $query = $this->db->get();
                            $detailbal = $query->result();
                        if(count($detailbal)>0){
                            if(($detailbal[0]->cal)!="min"){
                                 $dtbal = $detailbal[0]->invt_stock+$detailbal[0]->invt_qty;
                            }else{
                                $dtbal = $detailbal[0]->invt_stock-$detailbal[0]->invt_qty;
                            }
                           $cdt = $detailbal[0]->invt_cdate;
                        }else{
                                        $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                                        $this->db->from('mes_inventry');
                                        $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                                        $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                                        $this->db->where('invt_itemid',$item_id);
                                        $this->db->where('invt_type','TRS');
                                        $this->db->where('cal!=','min');
                                        $this->db->where('invt_proc!=','0');
                                        
                                        //$this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                                        $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                                       
                                        $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                                        $this->db->order_by("mes_inventry.invt_id", "desc");
                                        $this->db->limit(1);
                                        $query = $this->db->get();
                                        $detailbal = $query->result();
                                                if(count($detailbal)>0){
                                                    if(($detailbal[0]->cal)!="min"){
                                                         $dtbal = $detailbal[0]->invt_stock+$detailbal[0]->invt_qty;
                                                    }else{
                                                        $dtbal = $detailbal[0]->invt_stock-$detailbal[0]->invt_qty;
                                                    }
                                                   $cdt = $detailbal[0]->invt_cdate;
                                                }else{
                                                    $dtbal = 0;
                                                    $cdt = $this->input->post('ato_date');
                                                } 
                                                
                            //$dtbal = 0;
                           // $cdt = $this->input->post('ato_date');
                            
                        }            
                            
                            
                    }
                
                $inv[] = array($itm_code,$itm_nm,"BAL","",0,$cdt,$dtbal,"","",0,"0","0",$prod_price,0);
                 
                $dtbal = 0;
            }
            
            
           $var = (int)$item_id;
                    $this->db->select("*");    
                    $this->db->from('temp_qto_items');
                    $this->db->where('po_itm_item_id',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                  //  $inv[] = array($itm_code,$itm_nm,"TMPINV","",($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),date('Y-m-d'),($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),"",$dataa[$i]->po_user,($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),"0","0");
                 
                }
                
                    $this->db->select("*");    
                    $this->db->from('temp_trf_itms');
                    $this->db->join( "batch", "batch.batch_id = temp_trf_itms.temp_trf_batch", 'left');
                    $this->db->where('batch.product',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                  //  $inv[] = array($itm_code,$itm_nm,"TMPTRF","",$dataa[$i]->temp_trf_qty,date('Y-m-d'),$dataa[$i]->temp_trf_qty,"TMPTRF",$dataa[$i]->temp_trf_user,$dataa[$i]->temp_trf_qty,"0","0");
                 
                }
                
                
                $inv_all[] = $inv; 
                
                
            }
            
                 
      // print_r($inv_all[1]);
        
       $newstk =array();      
        $this->db->select("*");    
                $this->db->from('mes_depot_stock');
                $this->db->where('dpstock_dpoid',$this->input->post('adop'));
                $query = $this->db->get();
                $newstock =$query->result(); 
                for($i=0; $i<count($newstock); $i++){
                        $data = $this->Product_model->single_product($newstock[$i]->dpstock_itemid);
                        $itm_nm = $data[0]->itm_name;
                    $newstk[] = array($itm_nm,$newstock[$i]->dpstock_qty);    
                }
                $data['tm'] =$newstk;   
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                //$data['tm'][1] =$query->result();
                
                $this->db->from('mes_depot');
                        $this->db->select("dep_name");
                        $this->db->where('dep_id', $this->input->post('adop'));
                        $queryy = $this->db->get();
                            $gt_depo =$queryy->result();
                
                $user_detail['depo_name'] = $gt_depo[0]->dep_name;
                
        $data['inv_itm'] = $inv_all;
        $data['inv'] = $user_detail;      
        
      $this->load->view('report/pdf_allitem_detail',$data);
        
	}
	
	function prnt_allitem_detail_old(){
	    
	     $user_detail['fdate'] = $this->input->post('afrm_date');
        $user_detail['todate'] = $this->input->post('ato_date');
        
	    
	    $this->db->select("item_id");    
        $this->db->from('mes_item');
        $query = $this->db->get();
        $dataa_item=$query->result();
        
        $depo_name = "";
        $inv_all=array(); 
            for($x=0; $x<count($dataa_item); $x++){
                $item_id = $dataa_item[$x]->item_id;
                $data = $this->Product_model->single_product($item_id);
                 $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                 $itm_nm = $data[0]->itm_name;
                 
                  $this->db->from('batch');
                        $this->db->select("selling_price");
                        $this->db->where('product',$item_id );
                        $queryy = $this->db->get();
                            $p_price =$queryy->result();
                            if(count($p_price)>0){
                                $prod_price = $p_price[0]->selling_price;   
                            }else{
                                $prod_price = 0;   
                            }
                    
                    
                 
                    $this->db->select("*");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                     
                    $this->db->where('invt_itemid',$item_id);
                  $this->db->where('invt_type!=','temprem');
                  $this->db->where('invt_type!=','temp');
                    $this->db->where('invt_type!=','tmptran');
                    $this->db->where('invt_type!=','tmptran_rem');
                    $this->db->where('invt_type!=','tmptran_del');
                    $this->db->where('invt_type!=','return');
                    $this->db->where('invt_proc!=','0');
                    
                    if($this->input->post('adop')!=0){
                        $this->db->where('invt_type!=','GRN');
                    }
                    
                        if($this->input->post('afrm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('afrm_date');
                            $this->db->where('invt_cdate>=', $this->input->post('afrm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('ato_date')!=""){
                            $user_detail['todate'] = $this->input->post('ato_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('ato_date') . ' +1 day'));
                            $this->db->where('invt_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        
                        
                    $query = $this->db->get();
                    $detail = $query->result();
                    
            $inv=array(); 
           $linecnt = 0;
            for($i=0; $i<count($detail); $i++){
                
               
                $bal = 0; $amp = "n";  $depo = ""; $inv_price = 0; $cal = $depo="";$invuser = $detail[$i]->user_name;
                $invrtn_qty = 0;
                if($detail[$i]->invt_type=="INV"){
                        $this->db->select("mes_depot.dep_name as depart,mes_depot.dep_id as depp_id,mes_customer.cust_company");
                        $this->db->from('mes_qto');
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_qto.depo", 'left');
                        $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
                        $this->db->where('po_id', $detail[$i]->invt_type_id);
                        //$this->db->where('mes_qto.return_amnt!=mes_qto.po_final_tot');
                        //$this->db->where('po_status!=','0');
                        $query = $this->db->get();
                        $dep =$query->result();
                        
                        $this->db->select("po_itm_unitprice");
                        $this->db->from('qto_items');
                        $this->db->where('po_itm_item_id',$item_id);
                        $this->db->where('po_itm_po_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $pp =$query->result();
                        $inv_price = $pp[0]->po_itm_unitprice;
                        
                        if(count($dep)>0){
                                $depo_name = $dep[0]->depart;    
                        
                                $reason = $dep[0]->cust_company; 
                               // $reason = $dep[0]->cust_company."(".$dep[0]->depart.")"; 
                                $type_id = "INV".$detail[$i]->invt_type_id; $invuser = $detail[$i]->user_name; $cal = "M";
                                if($dep[0]->depp_id!=0){$amp = "y";} $depo = $dep[0]->depp_id;
                        }
                        
                    
                    
                }else if($detail[$i]->invt_type=="GRN") {
                    $reason = "GRN"; $type_id = "GRN".$detail[$i]->invt_type_id; $cal = "P";
                    $depo = 0;
                }else if($detail[$i]->invt_type=="ADJ") {
                    
                    if($detail[$i]->cal!="plus"){$cal="M";}else{$cal="P";}
                    $reason = "ADJUSTMENT"; $type_id = $detail[$i]->invt_type_id; 
                            $date_time = $detail[$i]->invt_cdate;
                            $new_date = date("Y-m-d",strtotime($date_time));
                    
                        $this->db->select("batch_edit_depo as depp_id");
                        $this->db->from('batch_edit_log');
                        $this->db->where('batch_edit_item_id', $detail[$i]->invt_itemid);
                        $this->db->where('batch_edit_qty', $detail[$i]->invt_qty);
                        $this->db->where('batch_edit_date', $new_date);
                        $query = $this->db->get();
                        $dep =$query->result();
                        
                        $depo = $dep[0]->depp_id;
                    
                }else if($detail[$i]->invt_type=="RTN") { 
                        $this->db->select("ret_reason");
                        $this->db->from('mes_return');
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = mes_return.ret_invnum", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                        if(count($ret_res)>0){$reason = $ret_res[0]->ret_reason;}else{$reason = "";}
                     $type_id = "GRNRTN".$detail[$i]->invt_type_id; $cal = "M"; $depo=0;
                    
                }else if($detail[$i]->invt_type=="TRS") {
                        $this->db->from('mes_stock_trancfer');
                        $this->db->select("*");
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left'); 
                        $this->db->where('trncfer_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                     $type_id = "TRN".$detail[$i]->invt_type_id; 
                     
                    if($detail[$i]->cal=="min"){
                        if($ret_res[0]->trncfer__from_dpoid==$this->input->post('adop')){ $reason = "TO ".$ret_res[0]->dep_name; $depo=$this->input->post('adop'); $cal = "M";}
                    } else{
                        $this->db->from('mes_depot');
                        $this->db->select("dep_name");
                        $this->db->where('dep_id', $ret_res[0]->trncfer__from_dpoid);
                        $queryy = $this->db->get();
                            $todepo =$queryy->result();
                        
                        if($ret_res[0]->trncfer_dpoid==$this->input->post('adop')){ $reason = "FROM ".$todepo[0]->dep_name; $depo=$this->input->post('adop'); $cal = "P";}
                    }
                    
                    
                    
                    
                }
                else if($detail[$i]->invt_type=="INVRTN") {
                    
                        $this->db->from('inv_return');
                        $this->db->select("mes_ret_reason.ret_reason,ret_depo,inv_return.ret_invnum,inv_return.ret_totqty");
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                       
                        $rtinv = $ret_res[0]->ret_invnum;
                        $this->db->from('mes_qto');
                        $this->db->select("depo");
                        $this->db->where('po_id', $rtinv);
                        //$this->db->where('po_status!=','0');
                        $query = $this->db->get();
                        $ret_dep =$query->result();
                        
                    if(count($ret_dep)>0){
                        $depo = $ret_dep[0]->depo;
                    }else{
                        $depo = "";
                    }    
                    
                    $depo = $ret_dep[0]->depo;
                    $reason = $ret_res[0]->ret_reason; 
                    $type_id = "INVRTN".$ret_res[0]->ret_invnum; 
                    $cal = "P"; 
                    $invrtn_qty = $ret_res[0]->ret_totqty;
                    
                    
                }
                else{
                    
                    $reason = ""; $type_id = $detail[$i]->invt_type_id; $cal = $depo="";
                }
                
                if($invrtn_qty!=0){
                      $invv_qty = $invrtn_qty;
                  }else{
                      $invv_qty = $detail[$i]->invt_qty;
                  }
                
                if($cal=="P"){$plus=$invv_qty;$min=0;$minn="";}else{$min=$invv_qty;$plus=0;$pluss="";}
                $bal = $detail[$i]->invt_stock + $plus - $min;
                
                if($detail[$i]->invt_type=="INV"){$uuser = $invuser;} else{$uuser = $detail[$i]->user_name;}
                
                
                if($depo==$this->input->post('adop')){
                  
                  
                   
                    $linecnt++;
                    $inv[] = array($itm_code,$itm_nm,$detail[$i]->invt_type,$type_id,$invv_qty,$detail[$i]->invt_cdate,$detail[$i]->invt_stock,$reason,$uuser,$min,$plus,$bal,$prod_price,$inv_price);
                }
                    
                    $type = $detail[$i]->invt_type_id;
                   
            }
            
            //echo $linecnt."<br>"; 
            
            if($linecnt<1){
                
                $this->db->select("invt_stock,invt_qty,invt_cdate");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                    $this->db->where('invt_itemid',$item_id);
                    $this->db->where('invt_type','INV');
                    $this->db->where('invt_proc!=','0');
                    $this->db->where('mes_qto.depo',$this->input->post('adop'));
                    $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                    $this->db->order_by("mes_inventry.invt_id", "desc");
                    $this->db->limit(1);
                       
                    $query = $this->db->get();
                    $detailbal = $query->result();
                
                    if(count($detailbal)>0){
                        $dtbal =  $detailbal[0]->invt_stock - $detailbal[0]->invt_qty;
                        $cdt = $detailbal[0]->invt_cdate;
                    }else{
                        $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                            $this->db->from('mes_inventry');
                            $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                            $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                            $this->db->where('invt_itemid',$item_id);
                            $this->db->where('invt_type','TRS');
                            $this->db->where('cal','min');
                            $this->db->where('invt_proc!=','0');
                            
                            $this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                           // $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                           
                            $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                            $this->db->order_by("mes_inventry.invt_id", "desc");
                            $this->db->limit(1);
                            $query = $this->db->get();
                            $detailbal = $query->result();
                        if(count($detailbal)>0){
                            if(($detailbal[0]->cal)!="min"){
                                 $dtbal = $detailbal[0]->invt_stock+$detailbal[0]->invt_qty;
                            }else{
                                $dtbal = $detailbal[0]->invt_stock-$detailbal[0]->invt_qty;
                            }
                           $cdt = $detailbal[0]->invt_cdate;
                        }else{
                                        $this->db->select("invt_stock,invt_qty,invt_cdate,cal");    
                                        $this->db->from('mes_inventry');
                                        $this->db->join( "mes_stock_trancfer", "mes_stock_trancfer.trncfer_id = mes_inventry.invt_type_id", 'left');
                                        $this->db->join( "mes_user", "mes_user.user_id = mes_inventry.invt_cuser", 'left');
                                        $this->db->where('invt_itemid',$item_id);
                                        $this->db->where('invt_type','TRS');
                                        $this->db->where('cal!=','min');
                                        $this->db->where('invt_proc!=','0');
                                        
                                        //$this->db->where('trncfer__from_dpoid',$this->input->post('adop'));
                                        $this->db->where('trncfer_dpoid',$this->input->post('adop'));
                                       
                                        $this->db->where('invt_cdate<=', $this->input->post('afrm_date'));
                                        $this->db->order_by("mes_inventry.invt_id", "desc");
                                        $this->db->limit(1);
                                        $query = $this->db->get();
                                        $detailbal = $query->result();
                                                if(count($detailbal)>0){
                                                    if(($detailbal[0]->cal)!="min"){
                                                         $dtbal = $detailbal[0]->invt_stock+$detailbal[0]->invt_qty;
                                                    }else{
                                                        $dtbal = $detailbal[0]->invt_stock-$detailbal[0]->invt_qty;
                                                    }
                                                   $cdt = $detailbal[0]->invt_cdate;
                                                }else{
                                                    $dtbal = 0;
                                                    $cdt = $this->input->post('ato_date');
                                                } 
                                                
                            //$dtbal = 0;
                           // $cdt = $this->input->post('ato_date');
                            
                        }            
                            
                            
                    }
                
                $inv[] = array($itm_code,$itm_nm,"BAL","",0,$cdt,$dtbal,"","",0,"0","0",$prod_price,0);
                 
                $dtbal = 0;
            }
            
            
           $var = (int)$item_id;
                    $this->db->select("*");    
                    $this->db->from('temp_qto_items');
                    $this->db->where('po_itm_item_id',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                  //  $inv[] = array($itm_code,$itm_nm,"TMPINV","",($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),date('Y-m-d'),($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),"",$dataa[$i]->po_user,($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),"0","0");
                 
                }
                
                    $this->db->select("*");    
                    $this->db->from('temp_trf_itms');
                    $this->db->join( "batch", "batch.batch_id = temp_trf_itms.temp_trf_batch", 'left');
                    $this->db->where('batch.product',$var);
                    $query = $this->db->get();
                    $dataa=$query->result();
            
                for($i=0; $i<count($dataa); $i++){
                  //  $inv[] = array($itm_code,$itm_nm,"TMPTRF","",$dataa[$i]->temp_trf_qty,date('Y-m-d'),$dataa[$i]->temp_trf_qty,"TMPTRF",$dataa[$i]->temp_trf_user,$dataa[$i]->temp_trf_qty,"0","0");
                 
                }
                
                
                $inv_all[] = $inv; 
                
                
            }
            
                 
      // print_r($inv_all[1]);
        
       $newstk =array();      
        $this->db->select("*");    
                $this->db->from('mes_depot_stock');
                $this->db->where('dpstock_dpoid',$this->input->post('adop'));
                $query = $this->db->get();
                $newstock =$query->result(); 
                for($i=0; $i<count($newstock); $i++){
                        $data = $this->Product_model->single_product($newstock[$i]->dpstock_itemid);
                        $itm_nm = $data[0]->itm_name;
                    $newstk[] = array($itm_nm,$newstock[$i]->dpstock_qty);    
                }
                $data['tm'] =$newstk;   
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                //$data['tm'][1] =$query->result();
                
                $this->db->from('mes_depot');
                        $this->db->select("dep_name");
                        $this->db->where('dep_id', $this->input->post('adop'));
                        $queryy = $this->db->get();
                            $gt_depo =$queryy->result();
                
                $user_detail['depo_name'] = $gt_depo[0]->dep_name;
                
        $data['inv_itm'] = $inv_all;
        $data['inv'] = $user_detail;      
        
      $this->load->view('report/pdf_allitem_detail',$data);
        
	}
	
	function batch_log_pdf(){
	    $user_detail['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
	    
	    $this->db->select("item_id");    
        $this->db->from('mes_item');
        $query = $this->db->get();
        $dataa=$query->result();
    
            
                 $data = $this->Product_model->single_product($this->input->post('item'));
                 $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                 $itm_nm = $data[0]->itm_name;
                 
                    $this->db->select("*");    
                    $this->db->from('mes_inventry');
                    $this->db->join( "mes_qto", "mes_qto.po_id = mes_inventry.invt_type_id", 'left');
                    $this->db->where('invt_itemid',$this->input->post('item'));
                  $this->db->where('invt_type!=','temprem');
                  $this->db->where('invt_type!=','temp');
                    $this->db->where('invt_type!=','tmptran');
                    $this->db->where('invt_type!=','tmptran_rem');
                    $this->db->where('invt_proc!=','0');
                    
                        if($this->input->post('frm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_date');
                            $this->db->where('invt_cdate>=', $this->input->post('frm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('to_date') . ' +1 day'));
                            $this->db->where('invt_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('invt_cdate', date('Y-m-d'));
                        }
                    $query = $this->db->get();
                    $detail = $query->result();
            $inv=array(); 
            for($i=0; $i<count($detail); $i++){
                
                $bal = 0; $amp = "n";
                if($detail[$i]->invt_type=="INV"){
                        $this->db->select("mes_depot.dep_name as depart,mes_depot.dep_id as depp_id");
                        $this->db->from('mes_qto');
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_qto.depo", 'left');
                        $this->db->where('po_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $dep =$query->result();
                    
                    $reason = "INVOICE (".$dep[0]->depart.")"; $type_id = "INV".$detail[$i]->invt_type_id; $invuser = $detail[$i]->po_cuser; $cal = "M";
                    if($dep[0]->depp_id!=0){$amp = "y";}
                    
                }else if($detail[$i]->invt_type=="GRN") {
                    $reason = "GRN"; $type_id = "GRN".$detail[$i]->invt_type_id; $cal = "P";
                }else if($detail[$i]->invt_type=="SAJ") {
                    $reason = "ADJUSTMENT"; $type_id = $detail[$i]->invt_type_id; $cal = "";
                }else if($detail[$i]->invt_type=="RTN") {
                        $this->db->select("ret_reason");
                        $this->db->from('mes_return');
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = mes_return.ret_invnum", 'left');
                        $this->db->where('ret_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                    $reason = $ret_res[0]->ret_reason; $type_id = "GRNRTN".$detail[$i]->invt_type_id; $cal = "P";
                }else if($detail[$i]->invt_type=="TRS") {
                        $this->db->from('mes_stock_trancfer');
                        $this->db->select("dep_name");
                        $this->db->join( "mes_depot", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left'); 
                        $this->db->where('trncfer_id', $detail[$i]->invt_type_id);
                        $query = $this->db->get();
                        $ret_res =$query->result();
                    $reason = "TO ".$ret_res[0]->dep_name; $type_id = "TRN".$detail[$i]->invt_type_id; $cal = "M";
                }else if($detail[$i]->invt_type=="INVRTN") {
                        $this->db->from('inv_return');
                        $this->db->select("mes_ret_reason.ret_reason");
                        $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
                        $query = $this->db->get();
                        $ret_res =$query->result();
                    $reason = $ret_res[0]->ret_reason; $type_id = "INVRTN".$detail[$i]->invt_type_id; $cal = "P";
                }else{
                    $reason = ""; $type_id = $detail[$i]->invt_type_id; $cal = "";
                }
                
                if($cal=="P"){$plus=$detail[$i]->invt_qty;$min=0;$minn="";}else{$min=$detail[$i]->invt_qty;$plus=0;$pluss="";}
                $bal = $detail[$i]->invt_stock + $plus - $min;
                
                if($detail[$i]->invt_type=="INV"){$uuser = $invuser;} else{$uuser = $detail[$i]->invt_cuser;}
                    $inv[] = array($itm_code,$itm_nm,$detail[$i]->invt_type,$type_id,$detail[$i]->invt_qty,$detail[$i]->invt_cdate,$detail[$i]->invt_stock,$reason,$uuser,$min,$plus,$bal);
                 
                    $type = $detail[$i]->invt_type_id;
                    
            }
            
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_batch_log',$data);
        
	}
	
	function batch_edit_log(){
	    
	    	    
	    $user_detail['fdate'] = $this->input->post('logfrm_bdate');
        $user_detail['todate'] = $this->input->post('logto_bdate');
        $user_detail['batch'] = $this->input->post('logbatchselect');
        $batch_id = $this->input->post('logbatchselect');
        
        
                    $data = $this->Product_model->single_product($this->input->post('logbatchitem'));
                    $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    $itm_nm = $data[0]->itm_name;
        //$itm_data = $this->Product_model->single_product($this->input->post('logbatchselect'));
        
        //$user_detail['item'] = $itm_data[0]->itm_name;
        
        
	                $this->db->select("*");    
                    $this->db->from('batch_edit_log');
                    $this->db->join( "batch", "batch.batch_id = batch_edit_log.batch_edit_batch_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = batch_edit_log.batch_edit_user", 'left');
                    $this->db->where('batch_edit_log.batch_edit_batch_id',$batch_id);
                    //$this->db->where('depo',$this->input->post('depo'));
                    
                       if($this->input->post('logfrm_bdate')!=""){
                            $user_detail['fdate'] = $this->input->post('logfrm_bdate');
                            $this->db->where('batch_edit_date>=', $this->input->post('logfrm_bdate'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('batch_edit_date<=', date('Y-m-d'));
                        }
                        
                        if($this->input->post('logto_bdate')!=""){
                            $user_detail['todate'] = $this->input->post('logto_bdate');
                            //$stop_date = date('Y-m-d', strtotime($this->input->post('logto_bdate') . ' +1 day'));
                            $this->db->where('batch_edit_date<=', $this->input->post('logto_bdate'));
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('batch_edit_date<=', date('Y-m-d'));
                        }
                    $query1 = $this->db->get();
                    $detail = $query1->result();
                    
                   
                    
                    
            $inv=array(); 
            
            for($i=0; $i<count($detail); $i++){
                $user_detail['batch'] = $detail[$i]->batch;
               // if($detail[$i]->cal!="plus"){$plus="0"; $min=$detail[$i]->qty;}else{ $plus="0"; $min=$detail[$i]->qty; }
            
             $inv[] = array($detail[$i]->batch,$detail[$i]->batch_edit_buying,$detail[$i]->batch_edit_selling,$detail[$i]->batch_edit_retail,$detail[$i]->batch_edit_mafd,$detail[$i]->batch_edit_exp,$detail[$i]->batch_edit_qty,$detail[$i]->batch_edit_comment,$detail[$i]->user_name,$detail[$i]->batch_edit_date,$itm_nm);

            }
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_batch_edit_log',$data);
	}
	
	function prnt_batch_log(){
	    
	    $user_detail['fdate'] = $this->input->post('frm_bdatelog');
        $user_detail['todate'] = $this->input->post('to_bdatelog');
        $user_detail['batch'] = $this->input->post('batchselectlog');
        $batch_id = $this->input->post('batchselectlog');
        $itm_data = $this->Product_model->single_product($this->input->post('batchitemlog'));
        $user_detail['item'] = $itm_data[0]->itm_name;
        
        
	                $this->db->select("*");    
                    $this->db->from('batch_log');
                    $this->db->join( "batch", "batch.batch_id = batch_log.batch_id", 'left');
                    $this->db->join( "mes_user", "mes_user.user_id = batch_log.user", 'left');
                    $this->db->where('batch_log.batch_id',$batch_id);
                    $this->db->where('depo',$this->input->post('depo'));
                    
                       if($this->input->post('frm_bdatelog')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_bdatelog');
                            $this->db->where('batch_log.date>=', $this->input->post('frm_bdatelog'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('batch_log.date<=', date('Y-m-d'));
                        }
                        
                        if($this->input->post('to_bdatelog')!=""){
                            $user_detail['todate'] = $this->input->post('to_bdatelog');
                            //$stop_date = date('Y-m-d', strtotime($this->input->post('logto_bdate') . ' +1 day'));
                            $this->db->where('batch_log.date<=', $this->input->post('to_bdatelog'));
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('batch_log.date<=', date('Y-m-d'));
                        }
                    $query1 = $this->db->get();
                    $detail = $query1->result();
                    
                   
                    
                    
            $inv=array(); 
            
            for($i=0; $i<count($detail); $i++){
                $user_detail['batch'] = $detail[$i]->batch;
                if($detail[$i]->cal!="plus"){$plus="0"; $min=$detail[$i]->qty;}else{ $plus="0"; $min=$detail[$i]->qty; }
            
             $inv[] = array($detail[$i]->batch,$detail[$i]->type,$detail[$i]->doc_id,$detail[$i]->qty,$plus,$min,$detail[$i]->depo,$detail[$i]->date);

            }
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_batch_log',$data);
        
	    
	}
	
	function prnt_batch_detail(){
	    
	    
	    $user_detail['fdate'] = $this->input->post('frm_bdate');
        $user_detail['todate'] = $this->input->post('to_bdate');
        $user_detail['batch'] = $this->input->post('batchselect');
        $itm_data = $this->Product_model->single_product($this->input->post('batchitem'));
        $user_detail['item'] = $itm_data[0]->itm_name;
        $user_detail['rep'] = "";
        
        if($this->input->post('ref')>0){
            $this->db->select('user_fname,user_lname');
                    $this->db->from('mes_user');
                    $this->db->where('user_id',$this->input->post('ref'));
                    $hasil=$this->db->get();
                    $sls= $hasil->result();
                $user_detail['rep'] =$sls[0]->user_fname." ".$sls[0]->user_lname;     
        }
        
	    
	      if($this->input->post('frm_bdate')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_bdate');
                        }else{
                            $user_detail['fdate'] = date('Y-m-d',strtotime("-1 year"));
                        }
                        
                        if($this->input->post('to_bdate')!=""){
                            $user_detail['todate'] = $this->input->post('to_bdate');
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                        }
	    
	  //  $query = $this->db->query("SELECT `qto_items`.*,mes_qto.po_sup_id as sup FROM `qto_items` WHERE `po_itm_po_id` IN 
	    //(SELECT mes_qto.po_id FROM mes_qto WHERE mes_qto.po_cdate>".$user_detail['todate']." and mes_qto.po_cdate<".$user_detail['fdate'].") AND po_itm_batch = ".$user_detail['batch']."");
	    
	    $this->db->select("qto_items.*,mes_customer.cust_company as customer,batch.batch as batch,`mes_qto`.po_id as invid,mes_user.user_name as uname,mes_qto.po_cdate as cdate");    
        $this->db->from('qto_items');
        $this->db->join( "mes_qto", "mes_qto.po_id = qto_items.po_itm_po_id", 'left');
        $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
        $this->db->join( "batch", "batch.batch_id = qto_items.po_itm_batch", 'left');
        $this->db->join( "mes_user", "mes_user.user_id = mes_qto.po_cuser", 'left');
	    $this->db->where('mes_qto.po_cdate<=',$user_detail['todate']);
	    $this->db->where('mes_qto.po_cdate>=',$user_detail['fdate']);
	    if($user_detail['batch']!=""){
	        $this->db->where('qto_items.po_itm_batch',$user_detail['batch']);
	    }else{
	        $this->db->where('qto_items.po_itm_item_id',$this->input->post('batchitem'));
	    }
	    if($this->input->post('ref')>0){
	        $this->db->where('mes_qto.po_ref_id',$this->input->post('ref'));
	    }
	    $this->db->order_by('mes_customer.cust_id', 'asc');
	    $query = $this->db->get();
	    $dataa = $query->result();
	   
            $inv=array(); 
            
            for($i=0; $i<count($dataa); $i++){
                 $this->db->select("sum(ret_itm_qty) as retq,sum(free_item) as retfq");    
                $this->db->from('return_items');
                if($user_detail['batch']!=""){
        	        $this->db->where('ret_item_batch',$user_detail['batch']);
        	    }else{
        	        $this->db->where('ret_itm_item_id',$this->input->post('batchitem'));
        	    }
        	    $this->db->where('ret_inv_id',$dataa[$i]->invid);
        	    $queryy = $this->db->get();
	            $ret = $queryy->result();
                
                    $inv[] = array($dataa[$i]->batch,"INV".$dataa[$i]->invid,$dataa[$i]->customer,($dataa[$i]->po_itm_qty+$dataa[$i]->free_item),($ret[0]->retq + $ret[0]->retfq),$dataa[$i]->po_itm_unitprice,$dataa[$i]->cdate,$dataa[$i]->uname);
                 
                    
            }
            
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_batch_detail',$data);
        
	}
        
    function dref_salsesumpdf(){
	    
	   
	    $crdate = $this->input->get_post('frmchk_date');
	    $todate = $this->input->get_post('tochk_date');
	    $refid = $this->input->get_post('sum_repselect');
	    
	    if($this->input->get_post('frmchk_date')!=""){
	        $crdate = $this->input->get_post('frmchk_date');
	    }
	    
	    if($this->input->get_post('tochk_date')!=""){
	        $todate = $this->input->get_post('tochk_date');
	    }else{
	        $todate = date('Y-m-d');
	    }
	
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->join('mes_depot', 'mes_user.user_depoid   = mes_depot.dep_id', 'left');
        $this->db->where('user_name', $refid );
        $query = $this->db->get();
        $user_data=$query->result();
        
       
        if(isset($user_data[0]->user_id)){
            $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        }else{
            $usid = 0;
            $usname = 0;
        }
        
        
        $this->db->select("po_id");    
        $this->db->from('mes_qto');
        $this->db->where('po_cdate', $crdate );
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $cnt_data = $query->result();
        
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
            if($usid!=0){$this->db->where('po_ref_id', $usid );}
        
                 if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('po_cdate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('po_cdate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('po_cdate<=', date('Y-m-d'));
        	    }
        //$this->db->where('po_cdate', $crdate );
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dataa=$query->result();
        
          for($i=0; $i<count($cnt_data); $i++){
               $arr[]=$cnt_data[$i]->po_id;
           }
     
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             
                $city = $dataa[$i]->cust_city;
                $cusref = $dataa[$i]->cust_salseperson;
                $this->db->select('route_name');
                $this->db->from('mes_route');
                $this->db->where("find_in_set($city, `cities`)");
                $this->db->where('ref_id', $cusref);
                $query = $this->db->get();
                $rcity = $query->result();
                
                //print_r($rcity);
               //if(isset($rcity[0]->route_name)){$rtt = $rcity[0]->route_name;}else{$rtt="";}
               if($dataa[$i]->cust_route == 0){$rtt = $rcity[0]->route_name;}else{$rtt="";}
             
         $this->db->select('sum(pay_amount) as paysum');
        $this->db->from('mes_payment');
        //$this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        $this->db->where('pay_invid', $dataa[$i]->po_id );
        $this->db->where('pay_stetus', 1);
                if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('pay_date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('pay_date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('pay_date<=', date('Y-m-d'));
        	    }
        
        //$this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dat=$query->result();
        
         $ind_id = array_search($dataa[$i]->po_id, $arr);
        
        if($dataa[$i]->po_final_tot!= $dataa[$i]->return_amnt){
            
            $inv[] = array($dataa[$i]->qto_code,$dataa[$i]->po_id,$dataa[$i]->cust_company." - ".$rtt,$dataa[$i]->po_tot_qty,$dataa[$i]->po_tot_freqty,$dataa[$i]->po_tot_price,$dataa[$i]->po_discount,$dataa[$i]->po_final_tot,$dataa[$i]->po_payment,$dat[0]->paysum,$ind_id+1); 
        }
                   
  
        }
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_data;
        
        
        $this->db->select("mes_payment_main.*,mes_customer.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_chkid as chqid,mes_payment_main.pay_amount as payam");    
        $this->db->from('mes_payment_main');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
        //$this->db->join('mes_payment', 'mes_payment_main.pay_uniqid   = mes_payment.pay_uniqid', 'left');
        if($usid!=0){$this->db->where('pay_ref_id', $usid );}
        //$this->db->where('pay_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('mes_payment_main.pay_date<=', date('Y-m-d'));
        	    }
        
        
        $this->db->where('mes_payment_main.pay_stetus', 1 );
         
        $query = $this->db->get();
        $dataapay=$query->result();
        
        
            
        $invpay=array();
         for($i=0; $i<count($dataapay); $i++){
             
                $city = $dataapay[$i]->cust_city;
                $cusref = $dataapay[$i]->cust_salseperson;
                $this->db->select('route_name');
                $this->db->from('mes_route');
                $this->db->where('ref_id', $cusref);
                $this->db->where("find_in_set($city, `cities`)");
                //$this->db->like('cities', $city, 'match');
                $query = $this->db->get();
                $rcity = $query->result();
                
                //print_r($rcity);
               if(isset($rcity[0]->route_name)){$rtt = $rcity[0]->route_name;}else{$rtt="";}
             
             //if($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid;}else if($dataapay[$i]->pay_type == 3){$type = "Pay Excess";}else if($dataapay[$i]->pay_type == 4){$type = "Ret Excess";}else{$type = "Bank Deposit";}
             if($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid;}else if($dataapay[$i]->pay_type == 2){$type = "Bank Deposit";}else if($dataapay[$i]->pay_type == 3){$type = "Payment Excess";}else if($dataapay[$i]->pay_type == 4){$type = "Return Excess";}else{$type = "Old Return";}
                    $this->db->from('mes_payment');
                    $this->db->join('mes_qto', 'mes_qto.po_id   = mes_payment.pay_invid', 'left');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$dataapay[$i]->pid);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";$bal="1";
                    for($j=0; $j<count($inv_data); $j++){
                         $ball = $this->Common_model->inv_balance($inv_data[$j]->pay_invid);
                            $this->db->select("sum(pay_amount) as pd");
                            $this->db->from('mes_payment');
                            $this->db->where('pay_invid',$inv_data[$j]->pay_invid);
                            $this->db->where('pay_id>',$inv_data[$j]->pay_id);
                            $ad_pay = $this->db->get();
                            $add_pay = $ad_pay->result();
                       if($add_pay[0]->pd){$bal = $add_pay[0]->pd - $ball;}else{$bal = $ball;}     
                       
                       if($inv_!=""){$br="";}else{$br=" ";}
                       // $inv_ =  'INV'.$inv_data[$j]->pay_invid.'-'.$inv_data[$j]->pay_amount." , ".$inv_;
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." - ".$inv_data[$j]->pay_amount.$br." - ".$inv_data[$j]->po_cdate." - BAL - ".number_format($bal, 2, '.', ',').", ".$inv_;
                    }
                   
                    $invpay[] = array($dataapay[$i]->pay_code,$dataapay[$i]->pay_id,$dataapay[$i]->cust_company." - ".$rtt,$type,$dataapay[$i]->payam,$inv_ ,$dataapay[$i]->chqid,$dataapay[$i]->pay_type); 
                
        }
        
     
        
        $data['inv_pay'] = $invpay;
        
        $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_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        if($usid!=0){$this->db->where('mes_qto.po_ref_id', $usid );}
       // $this->db->where('mes_qto.po_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('inv_return.ret_cdate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('inv_return.ret_cdate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('inv_return.ret_cdate<=', date('Y-m-d'));
        	    }
        //$this->db->where('inv_return.ret_cdate', $crdate);
        $this->db->where('inv_return.ret_stetus', 1);
        
        $query = $this->db->get();
        $datatrn=$query->result();
            
        $invrtn=array();
         for($i=0; $i<count($datatrn); $i++){
             
             $invrtn[] = array($datatrn[$i]->ret_code,$datatrn[$i]->ret_id,$datatrn[$i]->ret_invnum,$datatrn[$i]->ret_totqty,$datatrn[$i]->ret_amount,$datatrn[$i]->cust_company); 
        }
        
      
        $data['inv_trn'] = $invrtn;
        
        //cheque return
        
        $this->db->select("mes_payment_main.*,mes_payment.*,mes_customer.*,mes_bank.*,cities.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_code as pcd,mes_payment_main.pay_chkid as chqid,mes_payment_main.pay_chkdate as chdt,mes_payment.pay_invid as pinv,mes_payment.pay_amount as pamu ");    
        $this->db->from('mes_payment_main');
        
        $this->db->join('mes_payment', 'mes_payment.pay_main_id   = mes_payment_main.pay_id', 'left');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
        $this->db->join('mes_bank', 'mes_payment_main.pay_chkbnk   = mes_bank.bank_id', 'left');
        $this->db->join('cities', 'mes_payment_main.pay_bbranch   = cities.id', 'left');
        if($usid!=0){$this->db->where('mes_payment_main.pay_ref_id', $usid );}
       // $this->db->where('mes_payment_main.pay_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_trndate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_trndate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('mes_payment_main.pay_trndate<=', date('Y-m-d'));
        	    }
       // $this->db->where('mes_payment_main.pay_trndate', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $querychq = $this->db->get();
        $datachqrtn = $querychq->result();
        
         $chqrtn=array();
         for($i=0; $i<count($datachqrtn); $i++){
             
   
                    $chqrtn[] = array($datachqrtn[$i]->pcd,$datachqrtn[$i]->pid,$datachqrtn[$i]->chqid,$datachqrtn[$i]->bank_name,$datachqrtn[$i]->name_en,$datachqrtn[$i]->pamu,$datachqrtn[$i]->pinv,$datachqrtn[$i]->chdt); 
                
        }
        
      
        $data['chq_trn'] = $chqrtn;
        
        // cheque collection
        
        
        $this->db->select("chk_collect.*,mes_payment_main.*,mes_customer.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_amount as pamount");    
        $this->db->from('chk_collect');
        $this->db->join('mes_payment_main', 'mes_payment_main.pay_id   = chk_collect.pay_id', 'left');
        //$this->db->join('mes_payment', 'mes_payment_main.pay_id   = mes_payment.pay_main_id', 'left');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
            if($usid!=0){$this->db->where('mes_payment_main.pay_ref_id', $usid );}
       // $this->db->where('mes_payment_main.pay_ref_id', $usid );
                 if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('chk_collect.date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('chk_collect.date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('chk_collect.date<=', date('Y-m-d'));
        	    }
        //$this->db->where('chk_collect.date', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $query = $this->db->get();
        $datachqcollection = $query->result();
        
        
        
         $chqcol=array();
         for($i=0; $i<count($datachqcollection); $i++){
             $details = $datachqcollection[$i]->cust_company;
             
                    $this->db->from('mes_payment');
                    $this->db->join('mes_qto', 'mes_qto.po_id   = mes_payment.pay_invid', 'left');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$datachqcollection[$i]->pid);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";$bal="1";
                    for($j=0; $j<count($inv_data); $j++){
                      
                       // if(count($inv_data[$j]->pay_amount)>1){$amount = $inv_data[$j]->pay_amount;}else{$amount = "";}
                       if($inv_!=""){$br="";}else{$br=" ";}
                       // $inv_ =  'INV'.$inv_data[$j]->pay_invid.'-'.$inv_data[$j]->pay_amount." , ".$inv_;
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid.", ".$inv_;
                    }
   
                    $chqcol[] = array($datachqcollection[$i]->pay_chkid,$details,$inv_,$datachqcollection[$i]->amount,$datachqcollection[$i]->pamount); 
                
        }
        
      
        $data['chq_col'] = $chqcol;
        
        
        // company profile 
        
      
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd = $query->result();
                
                $data['crdate'] = $crdate;
                 $data['tm'][0] = $crdate;
                 $data['tm'][2] = $todate;
                 $data['tm'][1] = $dd[0];
            
            $this->load->view('report/salysummrypdf',$data);
            
        }
        
    function dref_salsesumpdf_range(){
	    
	   
	    $crdate = $this->input->get_post('frmchk_date');
	    $todate = $this->input->get_post('tochk_date');
	    $refid = $this->input->get_post('sum_repselect');
	    
	    if($this->input->get_post('frmchk_date')!=""){
	        $crdate = $this->input->get_post('frmchk_date');
	    }
	    
	    if($this->input->get_post('tochk_date')!=""){
	        $todate = $this->input->get_post('tochk_date');
	    }else{
	        $todate = date('Y-m-d');
	    }
	
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->join('mes_depot', 'mes_user.user_depoid   = mes_depot.dep_id', 'left');
        $this->db->where('user_name', $refid );
        $query = $this->db->get();
        $user_data=$query->result();
        
       
        if(isset($user_data[0]->user_id)){
            $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        }else{
            $usid = 0;
            $usname = 0;
        }
        
	     
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
            if($usid!=0){$this->db->where('po_ref_id', $usid );}
        
                 if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('po_cdate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('po_cdate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('po_cdate<=', date('Y-m-d'));
        	    }
        //$this->db->where('po_cdate', $crdate );
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dataa=$query->result();
     
     
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             
                $city = $dataa[$i]->cust_city;
                $cusref = $dataa[$i]->cust_salseperson;
                
                $this->db->select('route_name');
                $this->db->from('mes_route');
                $this->db->join('customer_route', 'customer_route.route_id = mes_route.route_id','left');
                $this->db->where('customer_route.cus_id', $dataa[$i]->cust_id);
                $query = $this->db->get();
                $rcity = $query->result();
                
                //print_r($rcity);
               //if(isset($rcity[0]->route_name)){$rtt = $rcity[0]->route_name;}else{$rtt="";}
               if($rcity[0]->route_name){$rtt = $rcity[0]->route_name;}else{$rtt=$rcity[0]->route_name;}
             
         $this->db->select('sum(pay_amount) as paysum');
        $this->db->from('mes_payment');
        //$this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        $this->db->where('pay_invid', $dataa[$i]->po_id );
        $this->db->where('pay_stetus', 1);
                if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('pay_date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('pay_date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('pay_date<=', date('Y-m-d'));
        	    }
        
        //$this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dat=$query->result();
        
        $arr = array(); 
        $this->db->select("po_id");    
        $this->db->from('mes_qto');
        $this->db->where('po_cdate', $dataa[$i]->po_cdate);
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $cnt_data = $query->result();
        
         for($j=0; $j<count($cnt_data); $j++){
               $arr[]=$cnt_data[$j]->po_id;
           }
       $ind_id = array_search($dataa[$i]->po_id, $arr);
        
        if($dataa[$i]->po_final_tot!= $dataa[$i]->return_amnt){
            
            $inv[] = array($dataa[$i]->qto_code,$dataa[$i]->po_id,$dataa[$i]->cust_company." - ".$rtt,$dataa[$i]->po_tot_qty,$dataa[$i]->po_tot_freqty,$dataa[$i]->po_tot_price,$dataa[$i]->po_discount,$dataa[$i]->po_final_tot,$dataa[$i]->po_payment,$dat[0]->paysum,$ind_id+1,$dataa[$i]->po_cdate); 
        }
                  
  
        }
        $data['inv_itm'] = $inv;
        
        $data['inv'] = $user_data;
        
        
        $this->db->select("mes_payment_main.*,mes_customer.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_chkid as chqid,mes_payment_main.pay_amount as payam");    
        $this->db->from('mes_payment_main');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
        //$this->db->join('mes_payment', 'mes_payment_main.pay_uniqid   = mes_payment.pay_uniqid', 'left');
        if($usid!=0){$this->db->where('pay_ref_id', $usid );}
        //$this->db->where('pay_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('mes_payment_main.pay_date<=', date('Y-m-d'));
        	    }
        
        
        $this->db->where('mes_payment_main.pay_stetus', 1 );
         
        $query = $this->db->get();
        $dataapay=$query->result();
        
        
            
        $invpay=array();
         for($i=0; $i<count($dataapay); $i++){
             
                $city = $dataapay[$i]->cust_city;
                $cusref = $dataapay[$i]->cust_salseperson;
                $this->db->select('route_name');
                $this->db->from('mes_route');
                $this->db->where('ref_id', $cusref);
                $this->db->where("find_in_set($city, `cities`)");
               // $this->db->like('cities', $city, 'match');
                $query = $this->db->get();
                $rcity = $query->result();
                
                //print_r($rcity);
               if(isset($rcity[0]->route_name)){$rtt = $rcity[0]->route_name;}else{$rtt="";}
             
             //if($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid;}else if($dataapay[$i]->pay_type == 3){$type = "Pay Excess";}else if($dataapay[$i]->pay_type == 4){$type = "Ret Excess";}else{$type = "Bank Deposit";}
             if($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid;}else if($dataapay[$i]->pay_type == 2){$type = "Bank Deposit";}else if($dataapay[$i]->pay_type == 3){$type = "Payment Excess";}else if($dataapay[$i]->pay_type == 4){$type = "Return Excess";}else{$type = "Old Return";}
                    $this->db->from('mes_payment');
                    $this->db->join('mes_qto', 'mes_qto.po_id   = mes_payment.pay_invid', 'left');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$dataapay[$i]->pid);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";$bal="1";
                    for($j=0; $j<count($inv_data); $j++){
                         $ball = $this->Common_model->inv_balance($inv_data[$j]->pay_invid);
                            $this->db->select("sum(pay_amount) as pd");
                            $this->db->from('mes_payment');
                            $this->db->where('pay_invid',$inv_data[$j]->pay_invid);
                            $this->db->where('pay_id>',$inv_data[$j]->pay_id);
                            $ad_pay = $this->db->get();
                            $add_pay = $ad_pay->result();
                       if($add_pay[0]->pd){$bal = $add_pay[0]->pd - $ball;}else{$bal = $ball;}     
                       
                       if($inv_!=""){$br="";}else{$br=" ";}
                       // $inv_ =  'INV'.$inv_data[$j]->pay_invid.'-'.$inv_data[$j]->pay_amount." , ".$inv_;
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." - ".$inv_data[$j]->pay_amount.$br." - ".$inv_data[$j]->po_cdate." - BAL - ".number_format($bal, 2, '.', ',').", ".$inv_;
                    }
                   
                    $invpay[] = array($dataapay[$i]->pay_code,$dataapay[$i]->pay_id,$dataapay[$i]->cust_company." - ".$rtt,$type,$dataapay[$i]->payam,$inv_ ,$dataapay[$i]->chqid,$dataapay[$i]->pay_type,$dataapay[$i]->pay_date); 
                
        }
        
     
        
        $data['inv_pay'] = $invpay;
        
        $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_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        if($usid!=0){$this->db->where('mes_qto.po_ref_id', $usid );}
       // $this->db->where('mes_qto.po_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('inv_return.ret_cdate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('inv_return.ret_cdate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('inv_return.ret_cdate<=', date('Y-m-d'));
        	    }
        //$this->db->where('inv_return.ret_cdate', $crdate);
        $this->db->where('inv_return.ret_stetus', 1);
        
        $query = $this->db->get();
        $datatrn=$query->result();
            
        $invrtn=array();
         for($i=0; $i<count($datatrn); $i++){
             
             $invrtn[] = array($datatrn[$i]->ret_code,$datatrn[$i]->ret_id,$datatrn[$i]->ret_invnum,$datatrn[$i]->ret_totqty,$datatrn[$i]->ret_amount,$datatrn[$i]->cust_company,$datatrn[$i]->ret_cdate); 
        }
        
      
        $data['inv_trn'] = $invrtn;
        
        //cheque return
        
        $this->db->select("mes_payment_main.*,mes_payment.*,mes_customer.*,mes_bank.*,cities.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_code as pcd,mes_payment_main.pay_chkid as chqid,mes_payment_main.pay_chkdate as chdt,mes_payment.pay_invid as pinv,mes_payment.pay_amount as pamu ");    
        $this->db->from('mes_payment_main');
        
        $this->db->join('mes_payment', 'mes_payment.pay_main_id   = mes_payment_main.pay_id', 'left');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
        $this->db->join('mes_bank', 'mes_payment_main.pay_chkbnk   = mes_bank.bank_id', 'left');
        $this->db->join('cities', 'mes_payment_main.pay_bbranch   = cities.id', 'left');
        if($usid!=0){$this->db->where('mes_payment_main.pay_ref_id', $usid );}
       // $this->db->where('mes_payment_main.pay_ref_id', $usid );
            if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_trndate>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('mes_payment_main.pay_trndate<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('mes_payment_main.pay_trndate<=', date('Y-m-d'));
        	    }
       // $this->db->where('mes_payment_main.pay_trndate', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $querychq = $this->db->get();
        $datachqrtn = $querychq->result();
        
         $chqrtn=array();
         for($i=0; $i<count($datachqrtn); $i++){
             
   
                    $chqrtn[] = array($datachqrtn[$i]->pcd,$datachqrtn[$i]->pid,$datachqrtn[$i]->chqid,$datachqrtn[$i]->bank_name,$datachqrtn[$i]->name_en,$datachqrtn[$i]->pamu,$datachqrtn[$i]->pinv,$datachqrtn[$i]->chdt); 
                
        }
        
      
        $data['chq_trn'] = $chqrtn;
        
        // cheque collection
        
        
        $this->db->select("chk_collect.*,mes_payment_main.*,mes_customer.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_amount as pamount");    
        $this->db->from('chk_collect');
        $this->db->join('mes_payment_main', 'mes_payment_main.pay_id   = chk_collect.pay_id', 'left');
        //$this->db->join('mes_payment', 'mes_payment_main.pay_id   = mes_payment.pay_main_id', 'left');
        $this->db->join('mes_customer', 'mes_payment_main.pay_cust_id   = mes_customer.cust_id', 'left');
            if($usid!=0){$this->db->where('mes_payment_main.pay_ref_id', $usid );}
       // $this->db->where('mes_payment_main.pay_ref_id', $usid );
                 if($this->input->get_post('frmchk_date')!=""){
        	        $this->db->where('chk_collect.date>=', $this->input->get_post('frmchk_date'));
        	    }
        	    
        	    if($this->input->get_post('tochk_date')!=""){
        	        $this->db->where('chk_collect.date<=', $this->input->get_post('tochk_date'));
        	    }else{
        	        $this->db->where('chk_collect.date<=', date('Y-m-d'));
        	    }
        //$this->db->where('chk_collect.date', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $query = $this->db->get();
        $datachqcollection = $query->result();
        
        
        
         $chqcol=array();
         for($i=0; $i<count($datachqcollection); $i++){
             $details = $datachqcollection[$i]->cust_company;
             
                    $this->db->from('mes_payment');
                    $this->db->join('mes_qto', 'mes_qto.po_id   = mes_payment.pay_invid', 'left');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$datachqcollection[$i]->pid);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";$bal="1";
                    for($j=0; $j<count($inv_data); $j++){
                      
                       // if(count($inv_data[$j]->pay_amount)>1){$amount = $inv_data[$j]->pay_amount;}else{$amount = "";}
                       if($inv_!=""){$br="";}else{$br=" ";}
                       // $inv_ =  'INV'.$inv_data[$j]->pay_invid.'-'.$inv_data[$j]->pay_amount." , ".$inv_;
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid.", ".$inv_;
                    }
   
                    $chqcol[] = array($datachqcollection[$i]->pay_chkid,$details,$inv_,$datachqcollection[$i]->amount,$datachqcollection[$i]->pamount); 
                
        }
        
      
        $data['chq_col'] = $chqcol;
        
        
        // company profile 
        
      
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd = $query->result();
                
                $data['crdate'] = $crdate;
                 $data['tm'][0] = $crdate;
                 $data['tm'][2] = $todate;
                 $data['tm'][1] = $dd[0];
            
            $this->load->view('report/salsesummeryrangepdf',$data);
            
        }
        
    function petro_detail(){
	      $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_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/petro_detail'); 
		        $this->load->view('template',$data);
	}
	
	
	function prnt_petro_detail(){
	    $user_detail['fdate'] = $this->input->post('frm_date');
        $user_detail['todate'] = $this->input->post('to_date');
        $user_detail['status'] = $this->input->post('status');
	    
	    $this->db->select("user_fname,user_lname");    
        $this->db->from('mes_user');
        $this->db->where('user_id',$this->input->post('ref'));
        $query = $this->db->get();
        $dataa=$query->result();
        
                 $refname = $dataa[0]->user_fname." ".$dataa[0]->user_lname;
                 
                    $this->db->select("po_itm_po_id");    
                    $this->db->from('qto_items');
                    $this->db->join( "mes_item", "mes_item.item_id = qto_items.po_itm_item_id", 'left');
                    $this->db->where('mes_item.item_cat',3);
                    $this->db->group_by('qto_items.po_itm_po_id');
                    $query = $this->db->get();
                    $detail = $query->result();
                    
            
            $inv=array(); 
            
            for($i=0; $i<count($detail); $i++){
                    $this->db->select("*");    
                    $this->db->from('mes_qto');
                    $this->db->where('mes_qto.po_id',$detail[$i]->po_itm_po_id);
                    $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('po_cdate>=', $this->input->post('frm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('po_cdate', date('Y-m-d'));
                        }  
                        
                        if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $stop_date = date('Y-m-d H:i:s', strtotime($this->input->post('to_date') . ' +1 day'));
                            $this->db->where('po_cdate<=', $stop_date);
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('po_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('status')=="1"){
                            $this->db->where('po_payment', 1);
                        }else if($this->input->post('status')=="2"){
                             $this->db->where('po_payment', 0);
                        }
                        
                    $query = $this->db->get(); 
                    $datat = $query->result();
                    
                   if(!empty($datat)){
                       if($datat[0]->po_payment==1){$pp="PAID";}else{$pp="UNPAID";}
                       
                        $this->db->select("sum(po_itm_qty) as tot,sum(free_item) as frtot,sum(po_itm_unitprice*po_itm_qty) as totprc");    
                        $this->db->from('qto_items');
                        $this->db->join( "mes_item", "mes_item.item_id = qto_items.po_itm_item_id", 'left');
                        $this->db->where('qto_items.po_itm_po_id',$datat[0]->po_id);
                        $this->db->where('mes_item.item_cat',3);
                        $queryy = $this->db->get(); 
                        $sumdata = $queryy->result();
                        
                        $this->db->select("sum(ret_itm_qty) as tot,sum(free_item) as frtot,sum(ret_itm_unitprice*ret_itm_qty) as totprc");    
                        $this->db->from('return_items');
                        $this->db->join( "mes_item", "mes_item.item_id = return_items.ret_itm_item_id", 'left');
                        $this->db->where('return_items.ret_inv_id',$datat[0]->po_id);
                        $this->db->where('mes_item.item_cat',3);
                        $query1 = $this->db->get(); 
                        $sumrdata = $query1->result();
                       
                       $inv[] = array($datat[0]->po_id,($sumdata[0]->tot+$sumdata[0]->frtot),$datat[0]->po_discount,
                       $sumdata[0]->totprc,$datat[0]->po_cdate,$sumrdata[0]->totprc,$pp,0,0);
                   }
                
                    
            }
            
        $data['tm'][0] = $refname;
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_petro_detail',$data);
        
	}    
	
	
	function prnt_moq(){
	    
	    $this->db->select("item_id,item_moq,item.itm_name as itmname,mes_stock.stc_actqty as actstk");    
        $this->db->from('mes_item');
        $this->db->join( "mes_stock", "mes_stock.stc_itemid = mes_item.item_id", 'left');
        $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
        $this->db->where('mes_item.item_moq>=mes_stock.stc_actqty');
        $this->db->where('mes_item.item_moq!=',0);
        $query = $this->db->get();
        $dataa =$query->result();
        
        
        $user_detail['fdate'] = "";
        $user_detail['todate'] = "";
           $inv=array(); 
           
        
        for($i=0; $i<count($dataa); $i++){
            
            $inv[] = array($dataa[$i]->item_id,$dataa[$i]->itmname,$dataa[$i]->item_moq,$dataa[$i]->actstk,($dataa[$i]->item_moq-$dataa[$i]->actstk));
                 
        }
        
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_moq',$data);
	}	
	
	function prnt_exp(){
	    
	    $this->db->select("batch.batch_id,batch.batch,batch.exp_date,batch.avl_qty,item_id,item_moq,item.itm_name as itmname,mes_stock.stc_actqty as actstk");    
        $this->db->from('batch');
        $this->db->join( "mes_item", "batch.product = mes_item.item_id", 'left');
        $this->db->join( "mes_stock", "mes_stock.stc_itemid = mes_item.item_id", 'left');
        $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
        $this->db->where('batch.exp_date!=',"0000-00-00");
        if($this->input->post('within')<0){
            $this->db->where('batch.exp_date<=',date('Y-m-d'));
        }else{
            $this->db->where('batch.exp_date<',date('Y-m-d', strtotime("+".$this->input->post('within')." months")));
        }
        
        $this->db->where('batch.avl_qty>',0);
        $query = $this->db->get();
        $dataa =$query->result();
        
        
        $user_detail['fdate'] = "";
        $user_detail['todate'] = "";
           $inv=array(); 
           
        
        for($i=0; $i<count($dataa); $i++){
            
            $date1=date_create(date('Y-m-d'));
            $dat = strtotime($dataa[$i]->exp_date);
            $date2=date_create(date('Y-m-d',$dat));
            $diff=date_diff($date1,$date2);
            
            $inv[] = array($dataa[$i]->batch,$dataa[$i]->itmname,$dataa[$i]->exp_date,$dataa[$i]->avl_qty,$diff->format("%R%a days"));
                 
        }
        
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;      
        
        $this->load->view('report/pdf_exp',$data);
	}
        
        
        
        
}	