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

class Report_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        
         
         $this->load->model('Product_model');
        $this->load->model('Common_model');
        $this->load->model('General');
        $this->load->model('Report_model');
         if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
        }
        
        
        
         public function index()
	{
	        $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;
                
                
                $data['content'] = ('report/report'); 
		        $this->load->view('template',$data);
	}  
	
	public function test()
	{
	        $this->db->select('*');
                    $this->db->from('return_items');
                    $this->db->where('ret_itm_unitprice<',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    
                for($i=0; $i<count($sls); $i++){
                     $this->db->select('po_itm_unitprice');
                    $this->db->from('qto_items');
                    $this->db->where('po_itm_item_id',$sls[$i]->ret_itm_item_id);
                    $this->db->where('po_itm_po_id',$sls[$i]->ret_inv_id);
                    $hasil=$this->db->get();
                    $unp= $hasil->result(); 
                
                    $this->db->set('ret_itm_unitprice', $unp[0]->po_itm_unitprice);
                        $this->db->where('ret_itm_id', $sls[$i]->ret_itm_id);
                        $this->db->update('return_items');
                }    
	}
	
	
	function printcsv(){
	    $ref = $_GET['id'];
	    $frm = $_GET['frm'];
	    $to = $_GET['to'];
	    
	    if($ref!=0){
	                $this->db->select('mes_item.item_id as itemid,item.itm_name as item,sum(qto_items.free_item) as frqty,sum(qto_items.po_itm_qty) as qty,SUM(qto_items.po_itm_unitprice*qto_items.po_itm_qty) as prc,mes_user.user_fname as fnm,mes_user.user_lname as lnm');
	                $this->db->from('mes_qto');
	                $this->db->join( "qto_items", "qto_items.po_itm_po_id = mes_qto.po_id", 'left');
	                $this->db->join( "mes_item", "qto_items.po_itm_item_id = mes_item.item_id", 'left');
	                $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
	                $this->db->join( "mes_user", "mes_qto.po_ref_id = mes_user.user_id", 'left');
                    $this->db->where('po_ref_id',$ref);
                    $this->db->where('po_cdate>=',$frm);
                    $this->db->where('po_cdate<=',$to);
                    $this->db->group_by('mes_item.item_id');
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
	    }else{
	        $this->db->select('mes_item.item_id as itemid,item.itm_name as item,sum(qto_items.free_item) as frqty,sum(qto_items.po_itm_qty) as qty,SUM(qto_items.po_itm_unitprice*qto_items.po_itm_qty) as prc');
	                $this->db->from('mes_qto');
	                $this->db->join( "qto_items", "qto_items.po_itm_po_id = mes_qto.po_id", 'left');
	                $this->db->join( "mes_item", "qto_items.po_itm_item_id = mes_item.item_id", 'left');
	                $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
	                //$this->db->join( "mes_user", "mes_qto.po_ref_id = mes_user.user_id", 'left');
                    //$this->db->where('po_ref_id',$ref);
                    $this->db->where('mes_qto.po_cdate>=',$frm);
                    $this->db->where('mes_qto.po_cdate<=',$to);
                    $this->db->group_by('mes_item.item_id');
                    $hasil=$this->db->get();
                    $sls= $hasil->result();
	    }
	    
	                
                    
	   // echo count($sls);
	    
                	    if(count($sls) > 0){ 
                	        
                	        
                    $delimiter = ","; 
                    if($ref!=0){
                         $filename = $sls[0]->fnm." " .$sls[0]->lnm." ".date('Y-m-d') . ".csv"; 
                    }else{
                         $filename = "All Item Data ".date('Y-m-d') . ".csv"; 
                    }
                   
                     
                    // Create a file pointer 
                    $f = fopen('php://memory', 'w'); 
                     
                    // Set column headers 
                    $fields = array('CODE','ITEM', 'QTY', 'SALE VALUE','RETURN','FREE'); 
                    fputcsv($f, $fields, $delimiter); 
                     
                    // Output each row of the data, format line as csv and write to file pointer 
                   // while($row = $sls->fetch_assoc()){
                    for($i=0; $i<count($sls); $i++){
                            $this->db->select('sum(invt_qty) as rtn');
	                        $this->db->from('mes_inventry');
                            $this->db->where('invt_itemid',$sls[$i]->itemid);
                            $this->db->where('invt_type','INVRTN');
                            $this->db->where('invt_proc',1);
                            $this->db->where('invt_cdate>',$frm);
                            $this->db->where('invt_cdate<',$to);
                            $hasil=$this->db->get();
                            $det= $hasil->result();
                        
                        $itm_dt = $this->Product_model->single_product($sls[$i]->itemid);
                        $nm = $itm_dt[0]->cat_shortcode.'-'.$itm_dt[0]->brand_shordcode.'-'.$itm_dt[0]->item_catcount;
                        
                        $val = $sls[$i]->prc*$sls[$i]->qty;
                        $lineData = array($nm,$sls[$i]->item, $sls[$i]->qty, $sls[$i]->prc,$det[0]->rtn,$sls[$i]->frqty,$sls[$i]->frqty+$sls[$i]->qty); 
                        fputcsv($f, $lineData, $delimiter); 
                    } 
                     
                    // Move back to beginning of file 
                    fseek($f, 0); 
                     
                   header('Content-Type: text/csv');
                    header('Content-Disposition: attachment; filename="' . $filename . '";');

                    //output all remaining data on a file pointer
                    fpassthru($f);
                } 
	    
	    
	}
	
	function d_sales_item(){
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                     if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $sls;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
            
            $data['brnd_list'] = $this->Product_model->brand_list();
            
                
                
                $data['content'] = ('report/daily_sales_item'); 
		        $this->load->view('template',$data);
	}
	
	function daily_salse_sumry(){
	    
	     $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                    if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $sls;
            
                $data['content'] = ('report/salsesummery'); 
		        $this->load->view('template',$data);
            
	}
	
	function prnt_d_sales_item_old(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    //
	    
	                $this->db->select('mes_item.item_id as itemid,item.itm_name as item,sum(qto_items.free_item) as frqty,sum(qto_items.po_itm_qty) as qty,SUM(qto_items.po_itm_unitprice*qto_items.po_itm_qty) as prc,mes_user.user_fname as fnm,mes_user.user_lname as lnm');
	                $this->db->from('mes_qto');
	                $this->db->join( "qto_items", "qto_items.po_itm_po_id = mes_qto.po_id", 'left');
	                $this->db->join( "mes_item", "qto_items.po_itm_item_id = mes_item.item_id", 'left');
	                $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
	                $this->db->join( "mes_user", "mes_qto.po_ref_id = mes_user.user_id", 'left');
                   
            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('mes_qto.po_ref_id', $this->input->post('ref'));
                //$this->db->where('return_items.ret_user', $user_data[0]->user_name);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
               // $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
               // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            if($this->input->post('item')!="0"){
                $this->db->where('qto_items.po_itm_item_id=', $this->input->post('item'));
               // $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
            }
            
            
        
        $this->db->group_by('item_id'); 
        $this->db->order_by('item_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
         
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
                   $data = $this->Product_model->single_product($dataa[$i]->itemid);
                   $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                   $totqty = $dataa[$i]->qty +$dataa[$i]->frqty;
                   
                        $this->db->select('sum(invt_qty) as rtn');
	                        $this->db->from('mes_inventry');
	                        $this->db->join('inv_return', 'inv_return.ret_id = mes_inventry.invt_type_id', 'left');
                            $this->db->where('invt_itemid',$dataa[$i]->itemid);
                            $this->db->where('invt_type','INVRTN');
                            $this->db->where('invt_proc',1);
                            $this->db->where('ret_cdate>=',$this->input->post('frm_date'));
                            $this->db->where('ret_cdate<=',$this->input->post('to_date'));
                            $hasil=$this->db->get();
                            $det= $hasil->result();
                         
                       
                        
                        $sin_val = $dataa[$i]->prc/$dataa[$i]->qty;
                       // $lineData = array($nm,$sls[$i]->item, $sls[$i]->qty, $sls[$i]->prc,$det[0]->rtn,$sls[$i]->frqty,$sls[$i]->frqty+$sls[$i]->qty); 
                   
                            //if($this->input->post('cat')!="0"){
                                //if($data[0]->item_cat==$this->input->post('cat')){
                                  //  $inv[] = array($itm_code,$data[0]->itm_name,$dataa[$i]->itm,$dataa[$i]->user,$dataa[$i]->itm,$dataa[$i]->free,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$totqty);
                               // }
                           // }else{
                                $inv[] = array($itm_code,$dataa[$i]->item,$dataa[$i]->qty,$dataa[$i]->prc,$dataa[$i]->frqty,$det[0]->rtn,$det[0]->rtn*$sin_val,$totqty,$totqty);
                           // }
                    
                
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
       
       $this->db->select("return_items.*,inv_return.*,mes_user.user_name as user,SUM(return_items.ret_itm_qty) as itm,SUM(return_items.free_item) as free");    
        $this->db->from('return_items');
        $this->db->join('inv_return', 'inv_return.ret_id = return_items.ret_note_id', 'left');
        $this->db->join('mes_user', 'return_items.ret_user   = mes_user.user_name', 'left');
        $this->db->where('return_items.ret_aprvl=', 1);

            //$user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('return_items.ret_user', $user_data[0]->user_name);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            if($this->input->post('item')!="0"){
                $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
            }
            
            
        
        $this->db->group_by('ret_itm_item_id'); 
        $this->db->order_by('ret_itm_item_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
                   $data = $this->Product_model->single_product($dataa[$i]->ret_itm_item_id);
                   $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                   $totqty = $dataa[$i]->itm +$dataa[$i]->free;
                            if($this->input->post('cat')!="0"){
                                if($data[0]->item_cat==$this->input->post('cat')){
                                    $ret[] = array($itm_code,$data[0]->itm_name,$totqty,$dataa[$i]->user,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->free);
                                }
                            }else{
                                $ret[] = array($itm_code,$data[0]->itm_name,$totqty,$dataa[$i]->user,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->free);
                            }
                
        }
      
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
          $this->load->view('report/pdf_dsales_item',$data);
	}
	
	function prnt_d_sales_item(){ 
	    
	    if($this->input->post('ref')!=0){
	        
	         $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        $user_detail['ref'] =$user_data[0]->user_name;
	    }
	    
	   
        
        $this->db->select("SUM(po_tot_price-po_final_tot) as disprc");    
        $this->db->from('mes_qto');
        
            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_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('mes_qto.po_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate>=', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate<=', date('Y-m-d'));
            }
        $this->db->order_by('po_id', 'asc');
        $query = $this->db->get();
        $disdata =$query->result();
        
        $this->db->select("SUM(inv_return.ret_amount) as retsum");    
        $this->db->from('inv_return');
        $this->db->join('mes_user', 'inv_return.ret_cuser   = mes_user.user_id', 'left');
        $this->db->where('inv_return.ret_proceed=', 1);

            //$user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_id;
                $this->db->where('ret_cuser', $user_data[0]->user_id);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
        
       // $this->db->group_by('ret_itm_item_id'); 
        $this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $ret_tt = $query->result();
                
                
	    
	                $this->db->select('mes_item.item_id as itemid');
	                $this->db->from('mes_item');
	                
	                if($this->input->post('cat')!=0){
                            $this->db->where('item_cat', $this->input->post('cat'));
                        }
                        
                    if($this->input->post('brnd')!=0){
                            $this->db->where('item_brand', $this->input->post('brnd'));
                        }
                        
	                if($this->input->post('item')!="0"){
                            $this->db->where('item_id', $this->input->post('item'));
                           // $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
                        } 
	                
        $this->db->group_by('item_id'); 
        $this->db->order_by('item_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
         
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
                   $data = $this->Product_model->single_product($dataa[$i]->itemid);
                   $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                   $str = ltrim($dataa[$i]->itemid, "0");
                   
                                $this->db->select('sum(qto_items.free_item) as frqty,sum(qto_items.po_itm_qty) as qty,SUM(qto_items.po_itm_unitprice*qto_items.po_itm_qty) as prc,
                                sum(qto_items.po_itm_unitprice*mes_qto.po_discount*qto_items.po_itm_qty) as discnt');
                                $this->db->from('mes_qto');
            	                $this->db->join("qto_items", "qto_items.po_itm_po_id = mes_qto.po_id", 'left');
            	                $this->db->join( "mes_user", "mes_qto.po_ref_id = mes_user.user_id", 'left');
                                $this->db->where("qto_items.po_itm_item_id",$str);
                             
                               
                        $user_detail['ref']=""; 
                        if($this->input->post('ref')!=0){
                            $user_detail['ref'] =$user_data[0]->user_id;
                            $this->db->where('mes_qto.po_ref_id', $this->input->post('ref'));
                            //$this->db->where('return_items.ret_user', $user_data[0]->user_id);
                        }
                        
                        if($this->input->post('frm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_date');
                            $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
                           // $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
                        }else{
                            $user_detail['fdate'] = date('Y-m-d');
                            $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
                           // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
                        }
                        
                        if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
                           // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
                        }else{
                            $user_detail['todate'] = date('Y-m-d');
                            $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
                           // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
                        }
                        
                        
                        
                   
                    $query = $this->db->get();
                    $invdata = $query->result();
         
                   $totqty = $invdata[0]->qty +$invdata[0]->frqty;
                   
                   
                            $this->db->select('sum(ret_itm_qty) as rtn,sum(ret_itm_qty*ret_itm_unitprice) as rtnval,
                            sum(return_items.ret_itm_unitprice*mes_qto.po_discount*return_items.ret_itm_qty) as retdis');
	                        $this->db->from('return_items');
	                        $this->db->join('inv_return', 'inv_return.ret_id = return_items.ret_note_id', 'left');
	                        $this->db->join('mes_qto', 'mes_qto.po_id   = inv_return.ret_invnum', 'left');
                            $this->db->where('return_items.ret_itm_item_id',$dataa[$i]->itemid);
                            $this->db->where('inv_return.ret_proceed=', 1);
                                if($this->input->post('ref')!=0){
                                $this->db->where('ret_user', $this->input->post('ref'));
                                }
                            $this->db->where('ret_cdate>=',$this->input->post('frm_date'));
                            $this->db->where('ret_cdate<=',$this->input->post('to_date'));
                            $hasil=$this->db->get();
                            $det= $hasil->result();
                        
                        if($invdata[0]->qty!=0){
                        $sin_val = $invdata[0]->prc/$invdata[0]->qty;    
                        }else{
                        $sin_val = 0.00;    
                        }
                        
                       
                   
                           if(($invdata[0]->qty=="") && ($det[0]->rtn=="")){
                            }else{
                                $dis_val =  $disdata[0]->disprc;
                                 $inv[] = array($itm_code,$data[0]->itm_name,$invdata[0]->qty,$invdata[0]->prc,$invdata[0]->frqty,$det[0]->rtn,$det[0]->rtnval,$totqty,($invdata[0]->discnt/100),($det[0]->retdis/100));
                          }
                                // }
                    
                
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
       
       $this->db->select("return_items.*,inv_return.*,mes_user.user_name as user,SUM(return_items.ret_itm_qty) as itm,SUM(return_items.free_item) as free");    
        $this->db->from('return_items');
        $this->db->join('inv_return', 'inv_return.ret_id = return_items.ret_note_id', 'left');
        $this->db->join('mes_user', 'return_items.ret_user   = mes_user.user_id', 'left');
        $this->db->where('return_items.ret_aprvl=', 1);

            //$user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_id;
                $this->db->where('return_items.ret_user', $user_data[0]->user_id);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            if($this->input->post('item')!="0"){
                $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
            }
            
            
        
       // $this->db->group_by('ret_itm_item_id'); 
       // $this->db->order_by('ret_itm_item_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
        
        
        
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
                   $data = $this->Product_model->single_product($dataa[$i]->ret_itm_item_id);
                   if(isset($data[0]->cat_shortcode)){
                        $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                   $totqty = $dataa[$i]->itm +$dataa[$i]->free;
                            if($this->input->post('cat')!="0"){
                                if($data[0]->item_cat==$this->input->post('cat')){
                                    $ret[] = array($itm_code,$data[0]->itm_name,$totqty,$dataa[$i]->user,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->free);
                                }
                            }else{
                                $ret[] = array($itm_code,$data[0]->itm_name,$totqty,$dataa[$i]->user,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->itm,$dataa[$i]->free);
                            }
                   }
                  
                
        }
      
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd =$query->result();
                
                 $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_dsales_item',$data);
	}
	
	
	function sales_inv(){
	    
	    
	    $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                      if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $sls;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/sales_invoice'); 
		        $this->load->view('template',$data);
	}
	
	function prnt_sales_inv(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        if($this->input->post('ref')!=0){
             $this->db->where('user_id', $this->input->post('ref'));
        }else{
             $this->db->where('user_id', 2);
        }
       
        //$this->db->where('user_departmentid', 'Marketing');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    
                    
        $this->db->select("mes_qto.*,mes_user.user_name as user,mes_customer.cust_company as customer");    
        $this->db->from('mes_qto');
        $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        
            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('mes_qto.po_ref_id', $this->input->post('ref'));
                //$this->db->where('return_items.ret_user', $user_data[0]->user_name);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
               // $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate>=', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
               // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate<=', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
           
         
        $this->db->order_by('po_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
               //$tott = $dataa[$i]->po_tot_qty+$dataa[$i]->po_tot_qty;
               $tott = $dataa[$i]->po_tot_qty;
               
               $distott = $dataa[$i]->po_tot_price - $dataa[$i]->po_final_tot;
               
               $this->db->select("uniq_id");    
                $this->db->from('mes_qto');
                $this->db->where('po_cdate', $dataa[$i]->po_cdate);
                $queryinv = $this->db->get();
                $daily_count =$queryinv->result();
        $dailycnt = 0;
            for($j=0; $j<count($daily_count); $j++){
                if($daily_count[$j]->uniq_id==$dataa[$i]->uniq_id){
                    $dailycnt = ($j+1);
                    break;
                }
            }
               
             if($this->input->post('item')!="0"){
                 $this->db->select("*");    
                    $this->db->from('qto_items');
                    $this->db->where('po_itm_po_id',$dataa[$i]->po_id);
                    $this->db->where('po_itm_item_id',$this->input->post('item'));
                    $query = $this->db->get();
                    $itm_cnt=$query->result();
                   // $tottitm = $itm_cnt[0]->po_itm_qty+$itm_cnt[0]->po_itm_qty;
                   
                   
                   
                    if(count($itm_cnt)>0){
                        $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->customer,$itm_cnt[0]->po_itm_qty,$dataa[$i]->po_final_tot,$dataa[$i]->user,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$distott,$dataa[$i]->po_tot_price);
                    }
             }else{
                        $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->customer,$tott,$dataa[$i]->po_final_tot,$dataa[$i]->user,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$distott,$dataa[$i]->po_tot_price);
                          
             }       
                
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
       
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
       
       $this->db->select("*");    
        $this->db->from('inv_return_main');
        $this->db->where('retm_proceed=', 1);
        
        //$this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id', 'left');

            //$user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('retm_cuser', $user_data[0]->user_id);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('retm_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('retm_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('retm_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('retm_cdate', date('Y-m-d'));
            }
           
            
            
        
       // $this->db->group_by('ret_itm_item_id'); 
        //$this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
                
                  // $totqty = $dataa[$i]->itm ;
                   
                   
            $this->db->select("inv_return.*,mes_user.user_name as user,ret_totqty as itmcnt,mes_customer.cust_company as customer");    
        $this->db->from('inv_return');
        $this->db->join('mes_user', 'inv_return.ret_cuser   = mes_user.user_id', 'left');
        $this->db->join('mes_qto', 'mes_qto.po_id   = inv_return.ret_invnum', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        $this->db->where('inv_return.ret_proceed=', 1);
        $this->db->where('inv_return.ret_main_id=', $dataa[$i]->retm_id);
        
        $query = $this->db->get();
        $datau = $query->result(); 
            
        $inv_ = "";    $totqtty = 0; $totamnt = 0; $totammnt=0;
            for($y=0; $y<count($datau); $y++){
                if(strpos($inv_, $datau[$y]->ret_invnum)){
                }else{
                    $inv_ = $datau[$y]->ret_invnum.",".$inv_; 
                }
                
                $totqtty = $datau[$y]->ret_totqty +$totqtty;
                $totammnt = $datau[$y]->ret_amount +$totammnt;
            }
        
                   
                   
                       // if($this->input->post('item')!="0"){
                        //    $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
                        //            $this->db->select("*");    
                        //            $this->db->from('return_items');
                       //             $this->db->where('ret_note_id',$dataa[$i]->ret_id);
                       //             $this->db->where('ret_itm_item_id',$this->input->post('item'));
                      //              $query = $this->db->get();
                      //              $itm_cnt=$query->result();
                       //            // $tottitm = $itm_cnt[0]->po_itm_qty+$itm_cnt[0]->po_itm_qty;
                       //             if(count($itm_cnt)>0){
                       //                 $ret[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$dataa[$i]->customer,$dataa[$i]->ret_totqty,$dataa[$i]->ret_amount,$dataa[$i]->user,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$dataa[$i]->mamont,$dataa[$i]->mcode,$dataa[$i]->mid);
                        //            }
                       // }else{
                          
                                 $ret[] = array($dataa[$i]->retm_code.$dataa[$i]->retm_id.$inv_,$datau[0]->customer." (".$inv_.")",$totqtty,$totammnt,$datau[0]->user,$datau[0]->ret_cdate,$datau[0]->ret_cdate,$totammnt,$dataa[$i]->retm_code,$dataa[$i]->retm_id);
                        //}
                
        }
      
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd =$query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_sales_invoice',$data);
	}	
	
	
	function prnt_sales_inv_old(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref'));
        //$this->db->where('user_departmentid', 'Marketing');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    
                    
        $this->db->select("mes_qto.*,mes_user.user_name as user,mes_customer.cust_company as customer");    
        $this->db->from('mes_qto');
        $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        
            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('mes_qto.po_ref_id', $this->input->post('ref'));
                //$this->db->where('return_items.ret_user', $user_data[0]->user_name);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
               // $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate>=', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
               // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate<=', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
           
         
        $this->db->order_by('po_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
               //$tott = $dataa[$i]->po_tot_qty+$dataa[$i]->po_tot_qty;
               $tott = $dataa[$i]->po_tot_qty;
               
               $distott = $dataa[$i]->po_tot_price - $dataa[$i]->po_final_tot;
               
               $this->db->select("uniq_id");    
                $this->db->from('mes_qto');
                $this->db->where('po_cdate', $dataa[$i]->po_cdate);
                $queryinv = $this->db->get();
                $daily_count =$queryinv->result();
        $dailycnt = 0;
            for($j=0; $j<count($daily_count); $j++){
                if($daily_count[$j]->uniq_id==$dataa[$i]->uniq_id){
                    $dailycnt = ($j+1);
                    break;
                }
            }
               
             if($this->input->post('item')!="0"){
                 $this->db->select("*");    
                    $this->db->from('qto_items');
                    $this->db->where('po_itm_po_id',$dataa[$i]->po_id);
                    $this->db->where('po_itm_item_id',$this->input->post('item'));
                    $query = $this->db->get();
                    $itm_cnt=$query->result();
                   // $tottitm = $itm_cnt[0]->po_itm_qty+$itm_cnt[0]->po_itm_qty;
                   
                   
                   
                    if(count($itm_cnt)>0){
                        $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->customer,$itm_cnt[0]->po_itm_qty,$dataa[$i]->po_final_tot,$dataa[$i]->user,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$distott,$dataa[$i]->po_tot_price);
                    }
             }else{
                        $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->customer,$tott,$dataa[$i]->po_final_tot,$dataa[$i]->user,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$dataa[$i]->po_cdate,$distott,$dataa[$i]->po_tot_price);
                          
             }       
                
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
       
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
       
       $this->db->select("inv_return.*,mes_user.user_name as user,ret_totqty as itm,mes_customer.cust_company as customer,
       inv_return_main.retm_id as mid,inv_return_main.retm_code as mcode,inv_return_main.retm_amount as mamont");    
        $this->db->from('inv_return_main');
        $this->db->join('mes_user', 'inv_return.ret_cuser   = mes_user.user_id', 'left');
        $this->db->join('mes_qto', 'mes_qto.po_id   = inv_return.ret_invnum', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        $this->db->join('inv_return', 'inv_return_main.retm_id    = inv_return.ret_main_id', 'left');
        $this->db->where('inv_return.ret_proceed=', 1);
        
        //$this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id', 'left');

            //$user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('ret_cuser', $user_data[0]->user_id);
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
           
            
            
        
       // $this->db->group_by('ret_itm_item_id'); 
        //$this->db->order_by('ret_id', 'asc');
        $query = $this->db->get();
        $dataa = $query->result();
            
        $ret = array();
         for($i=0; $i<count($dataa); $i++){
                
                   $totqty = $dataa[$i]->itm ;
                   
                   
                        if($this->input->post('item')!="0"){
                            $this->db->where('return_items.ret_itm_item_id', $this->input->post('item'));
                                    $this->db->select("*");    
                                    $this->db->from('return_items');
                                    $this->db->where('ret_note_id',$dataa[$i]->ret_id);
                                    $this->db->where('ret_itm_item_id',$this->input->post('item'));
                                    $query = $this->db->get();
                                    $itm_cnt=$query->result();
                                   // $tottitm = $itm_cnt[0]->po_itm_qty+$itm_cnt[0]->po_itm_qty;
                                    if(count($itm_cnt)>0){
                                        $ret[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$dataa[$i]->customer,$dataa[$i]->ret_totqty,$dataa[$i]->ret_amount,$dataa[$i]->user,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$dataa[$i]->mamont,$dataa[$i]->mcode,$dataa[$i]->mid);
                                    }
                        }else{
                          
                                 $ret[] = array($dataa[$i]->ret_code.$dataa[$i]->ret_id,$dataa[$i]->customer,$dataa[$i]->ret_totqty,$dataa[$i]->ret_amount,$dataa[$i]->user,$dataa[$i]->ret_cdate,$dataa[$i]->ret_cdate,$dataa[$i]->mamont,$dataa[$i]->mcode,$dataa[$i]->mid);
                        }
                
        }
      
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd =$query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_sales_invoice',$data);
	}
	
	function collection_sum_view(){
	    
	    $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                    if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
            $data['ref'] = $sls;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/daily_collection'); 
		        $this->load->view('template',$data);
	}
	
	function collection_sumpdf(){
	    
	    if($this->input->post('ref')!=0){
	          $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        $ufname = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
	    }else{
	        $usid = 0;
            $usname = "All";
            $ufname = "All";
	    }
	   
	    
	    $auth = "All Payments";
                    
        $this->db->select("mes_payment_main.*,mes_customer.*,mes_bank.*,mes_payment_main.pay_amount as mainpay");    
        $this->db->from('mes_payment_main');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_payment_main.pay_cust_id', 'left');
        $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
        $this->db->where('pay_stetus', 1);
       // $this->db->where('pay_authrize', 1);
        
            $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$usname;
               $this->db->where('pay_ref_id', $this->input->post('ref'));
            }
            
            if($this->input->post('p_status')!=""){
                $this->db->where('mes_payment_main.pay_authrize', $this->input->post('p_status'));
                //if($this->input->post('p_status')>0){$auth = "Authozied Payments";}else{$auth = "Pending Payments";}
                if($this->input->post('p_status')>0){$auth = "Authozied Payments";}else{$auth = "Pending Authorisations";}
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_payment_main.pay_date>=', $this->input->post('frm_date'));
               // $this->db->where('inv_return.ret_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_payment_main.pay_date', date('Y-m-d'));
               // $this->db->where('inv_return.ret_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_payment_main.pay_date<=', $this->input->post('to_date'));
               // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_payment_main.pay_date', date('Y-m-d'));
            }
           
           if($this->input->post('paytype')!=""){
               
               if($this->input->post('paytype') == 1){
                   
                   $this->db->where('mes_payment_main.pay_type', $this->input->post('paytype'));
               }else{
                   
                   $this->db->where('mes_payment_main.pay_type!=', 1);
               }
                
            }
           
        $this->db->order_by('pay_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             if($dataa[$i]->pay_type==1){$ptype = "CHQ";}else if($dataa[$i]->pay_type==2){$ptype = "BNK";}else if($dataa[$i]->pay_type==5){$ptype = "PRM";}else{$ptype = "CSH";}
                    $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',$dataa[$i]->pay_id);
                    $this->db->where('pay_stetus',1);
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    $mainpay=0;
                    
                    for($j=0; $j<count($inv_data); $j++){
                        if($inv_!=""){$br="";}else{$br=" ";}
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." - ".$inv_data[$j]->pay_amount.$br." - ".$inv_data[$j]->po_cdate."  ".$inv_;
                        //$inv_ =  'INV'.$inv_data[$j]->pay_invid." - ".$inv_data[$j]->pay_amount.$br." - ".$inv_data[$j]->pay_date."  ".$inv_;
                        $mainpay = $inv_data[$j]->pay_amount + $mainpay;
                    }
                        $inv[] = array($dataa[$i]->pay_code.$dataa[$i]->pay_id,$dataa[$i]->cust_company,$inv_,$mainpay,$dataa[$i]->pay_user,$ptype,0,$dataa[$i]->pay_date);
                        //$inv[] = array($dataa[$i]->pay_code.$dataa[$i]->pay_id,$dataa[$i]->cust_company,$inv_,$dataa[$i]->pay_amount,$dataa[$i]->pay_user,$ptype,$dataa[$i]->pay_amount);
        }
      
      $user_detail['fullname'] = $ufname;
      $user_detail['auth'] = $auth;
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd = $query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_daily_collection',$data);
	}
	
	function inv_sum_view(){
	    
	    
	                $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                      if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                        $data['ref'] = $sls;
	    
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['cus'] = $sls;
                    
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    $data['inv'] = $inv;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/invoice'); 
		        $this->load->view('template',$data);
	}
	
	function inv_sumpdf(){
	    
	   // $this->db->select("*");    
       // $this->db->from('mes_user');
       // $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
       // $query = $this->db->get();
      //  $user_data=$query->result();
        
      //  $usid = $user_data[0]->user_id;
       // $usname = $user_data[0]->user_name;
	    
	    
                    
        $this->db->select("mes_qto.*,mes_customer.*,mes_user.*");    
        $this->db->from('mes_qto');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        //$this->db->join('customer_route', 'customer_route.cus_id   = mes_customer.cust_id', 'left');
        //$this->db->join('mes_route', 'customer_route.route_id   = mes_route.route_id', 'left');
        $this->db->join('mes_user', 'mes_qto.po_cuser   = mes_user.user_id ', 'left');
        $this->db->where('po_status', 1);
        
            $user_detail['inv']=""; 
            
              $user_detail['ref']=""; 
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
               $this->db->where('po_ref_id', $this->input->post('ref'));
            }
           
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
           
           $user_detail['cus']=""; 
            if($this->input->post('cus')!=0){
                $user_detail['cus'] = $this->input->post('cus');
               $this->db->where('mes_qto.po_sup_id', $this->input->post('cus'));
            }
            
              $user_detail['tout']=""; 
            if($this->input->post('rot')!=0){
                $user_detail['tout'] = $this->input->post('rot');
               $this->db->where('customer_route.route_id', $this->input->post('rot'));
            }
           
        $this->db->order_by('po_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
             if($dataa[$i]->po_payment==1){$paid = "PAID";}else{$paid = "PEND";}
             $sumdet = $this->Report_model->inv_sum($dataa[$i]->po_id);
                                        $invpaych = $sumdet['invpaych'];
                                        $invpay = $sumdet['invpay'];
                                        $invtrnusm = $sumdet['invtrnusm'];
                                        $pay_chkid =$sumdet['pay_chkid'];
                   
               $ball = ($dataa[$i]->po_final_tot-$invtrnusm);   
                   
                 if($this->input->post('cus')!="0"){$user_detail['cus']=$dataa[$i]->cust_company;}
                           $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$dataa[$i]->cust_company,$paid,$dataa[$i]->po_tot_qty,number_format((float)$ball, 2, '.', ''),$dataa[$i]->user_name,$dataa[$i]->po_cdate);
        }
      
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
        //$data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
       // $data['tm'][0] = "";
        
              //  $this->db->select("*");    
               // $this->db->from('mes_profile');
               // $query = $this->db->get();
              //  $data['tm'][1] =$query->result();
                
                
        //get return
      $ret = array();
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $dd =$query->result();
                
                $data['tm'][1] = $dd[0];
         
          $this->load->view('report/pdf_invoice',$data);
	}
	
	function invoice_settlement(){
	    
	     $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                      if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                        $data['ref'] = $sls;
	    
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['cus'] = $sls;
                    
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    $data['inv'] = $inv;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/invoice_settlemnt'); 
		        $this->load->view('template',$data);
	}
	
	function invoice_settlement_pdf(){
	    
	    $this->db->select("*");    
        $this->db->from('mes_user');
         $this->db->where('user_id', $this->input->post('ref'));
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->join('mes_qto', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
                    $this->db->where('po_status',1);
                    
                     $user_detail['ref'] = $this->input->post('ref');
                    
                    if($this->input->post('ref')!=0){
                       
                        $this->db->where('po_ref_id', $this->input->post('ref'));
                    }
                    
                    if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
            }else if($this->input->post('invselect')!=0){
                $user_detail['fdate'] = "All";
            }else{$user_detail['fdate'] = "All";
                //$this->db->where('mes_qto.po_cdate', date('Y-m-d'));
                 $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }else if($this->input->post('invselect')!=0){
                $user_detail['todate'] = date('Y-m-d');
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
                    
               if($this->input->post('cus')!=0){
                        $this->db->where('po_sup_id', $this->input->post('cus'));
                        
                    }else{
                        $this->db->order_by('po_sup_id','asc');
                    }
                    
                    $this->db->group_by('po_sup_id');
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    //$data['inv'] = $inv;
                    
                   
                    
                    $user_detail['fdate'] = $this->input->post('frm_date');
                    $user_detail['todate'] = $this->input->post('to_date');
                    $user_detail['cus'] = $this->input->post('cus');
                    
                    
                    $invstl = $invdet =array();
                    
         for($i=0; $i<count($inv); $i++){
           
             
                                        $this->db->select('*');
                                        $this->db->from('mes_qto');
                                        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
                                        $this->db->where('po_status',1);
                                        
                                        if($this->input->post('ref')!=0){
                                            $this->db->where('po_ref_id', $this->input->post('ref'));
                                        }
                                        
                                        if($this->input->post('frm_date')!=""){
                                                $user_detail['fdate'] = $this->input->post('frm_date');
                                                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
                                        }else{
                                                $user_detail['fdate'] = date('Y-m-d');
                                                //$this->db->where('mes_qto.po_cdate', date('Y-m-d'));
                                                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
                                        }
                                
                                if($this->input->post('to_date')!=""){
                                    $user_detail['todate'] = $this->input->post('to_date');
                                    $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
                                }else{
                                    $user_detail['todate'] = date('Y-m-d');
                                    $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
                                }
                                        
                                        $this->db->where('po_sup_id', $inv[$i]->cust_id);
                                        $hasil=$this->db->get();
                                        $inv1= $hasil->result(); 
                                       
                                    $invdet =array();
                                    for($j=0; $j<count($inv1); $j++){
                                        $sumdet = $this->Report_model->inv_sum($inv1[$j]->po_id);
                                        $invpaych = $sumdet['invpaych'];
                                        $invpay = $sumdet['invpay'];
                                        $invtrnusm = $sumdet['invtrnusm'];
                                        $pay_chkid =$sumdet['pay_chkid'];
                                        
                                            $this->db->from('mes_payment');
                                            $this->db->select("*");
                                            $this->db->where('pay_invid',$inv1[$j]->po_id);
                                            $this->db->where('pay_stetus',1);
                                            $this->db->where('pay_authrize',1);
                                            $detailtax = $this->db->get();
                                            $pay_data = $detailtax->result();
                                        
                                            $invdt =array();
                                                for($k=0; $k<count($pay_data); $k++){
                                                    if($pay_data[$k]->pay_type==1){
                                                        $chk = $pay_data[$k]->pay_amount; $cash = "0.00"; $chk_id = $pay_data[$k]->pay_chkid;
                                                    }else{
                                                        $chk = "0.00"; $cash = $pay_data[$k]->pay_amount; $chk_id = "";
                                                    }
                                                    $invdt[]=array($pay_data[$k]->pay_code.$pay_data[$k]->pay_main_id,$chk_id,$cash,$chk,"0.00","0.00",$pay_data[$k]->pay_date,$pay_data[$k]->pay_user);
                                                }
                                                
                                            $this->db->from('inv_return');
                                            $this->db->select("*");
                                            $this->db->where('ret_invnum',$inv1[$j]->po_id);
                                            $this->db->where('ret_stetus',1);
                                            $this->db->where('ret_proceed',1);
                                            $detailtax = $this->db->get();
                                            $ret_data = $detailtax->result();    
                                        
                                            for($k=0; $k<count($ret_data); $k++){
                                                    $invdt[]=array($ret_data[$k]->ret_code.$ret_data[$k]->ret_id,"","0.00","0.00",$ret_data[$k]->ret_amount,"0.00",$ret_data[$k]->ret_cdate,$ret_data[$k]->ret_cuser);
                                                }
                                                
                                      if(!empty($invdt)){
                                          if($this->input->post('invselect')!=0){
                                              if($this->input->post('invselect')==$inv1[$j]->po_id){
                                                 $invdet[] = array($inv1[$j]->qto_code.$inv1[$j]->po_id,$inv1[$j]->cust_company,"0.00","0.00","0.00",$inv1[$j]->po_final_tot,$inv1[$j]->po_cdate,$inv1[$j]->po_cuser,$invdt);
                                              }else{}
                                            }else{
                                              $invdet[] = array($inv1[$j]->qto_code.$inv1[$j]->po_id,$inv1[$j]->cust_company,"0.00","0.00","0.00",$inv1[$j]->po_final_tot,$inv1[$j]->po_cdate,$inv1[$j]->po_cuser,$invdt);
                                          }
                                        }
                                    }
                             
                if(!empty($invdet)){
                   // print_r($invdet[0][8][0][6]);
                   // echo "br";
                   // print_r(sort($invdet[0][8][0][6]));
                    $invstl[] = array($inv[$i]->cust_company,$invdet);
                }
                       
        }
      
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
        $data['inv_itm'] = $invstl;
        $data['inv'] = $user_detail;
	    
	    $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
        $this->load->view('report/pdf_invoice_settlemnt',$data);
	}
	
	function invoice_settlement_pdf_old(){
	    
	                $this->db->select('*');
                    $this->db->from('mes_qto');
                   // $this->db->join('inv_return', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
                    $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
                    //$this->db->join('mes_payment', 'mes_payment.pay_invid   = mes_qto.po_id', 'left');
                    $this->db->where('po_status',1);
                   // $this->db->where('pay_stetus',1);
                    //$this->db->where('ret_stetus',1);
                    
                    
                    
                    if($this->input->post('ref')!=0){
                        $this->db->where('po_ref_id', $this->input->post('ref'));
                    }
                    
                    if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_qto.po_cdate>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_qto.po_cdate<=', $this->input->post('to_date'));
            }else{
                $user_detail['todate'] = date('Y-m-d');
                $this->db->where('mes_qto.po_cdate', date('Y-m-d'));
            }
                    
               if($this->input->post('cus')!=0){
                        $this->db->where('po_sup_id', $this->input->post('cus'));
                        
                    }else{
                        $this->db->order_by('po_sup_id','asc');
                    }
                    
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    //$data['inv'] = $inv;
                    
                   
                    
                    $user_detail['fdate'] = $this->input->post('frm_date');
                    $user_detail['todate'] = $this->input->post('to_date');
                    $user_detail['cus'] = $this->input->post('cus');
                    
                    
                    $invstl=array();
         for($i=0; $i<count($inv); $i++){
             if($inv[$i]->po_payment==1){$paid = "PAID";}else{$paid = "PEND";}
             
             
                   
               //  if($this->input->post('cus')!="0"){$user_detail['cus']=$dataa[$i]->cust_company;}
               
                    $this->db->select('sum(pay_amount) as chqsum,pay_chkid');
                    $this->db->from('mes_payment');
                    $this->db->where('pay_stetus',1);
                    $this->db->where('pay_invid', $inv[$i]->po_id);
                    $this->db->where('pay_type', 1);
                    $hasilpaychq=$this->db->get();
                    $invhq= $hasilpaychq->result();
                    $invpay = $invhq[0]->chqsum; 
                    
                    $this->db->select('sum(pay_amount) as cashsum');
                    $this->db->from('mes_payment');
                    $this->db->where('pay_stetus',1);
                    $this->db->where('pay_invid', $inv[$i]->po_id);
                    $this->db->where('pay_type', 0);
                    $hasilpaych=$this->db->get();
                    $invch= $hasilpaych->result();
                    $invpaych = $invch[0]->cashsum; 
                    
                    $this->db->select('sum(ret_amount) as trnsum');
                    $this->db->from('inv_return');
                    $this->db->where('ret_stetus',1);
                    $this->db->where('ret_invnum', $inv[$i]->po_id);
                    //$this->db->join('inv_return', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
                    //$this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
                    $hasiltrn = $this->db->get();
                    $invtrn = $hasiltrn->result();
                    $invtrnusm = $invtrn[0]->trnsum; 
                    $new="";
                    
                    if($inv[$i]->cust_company!=$new){
                       // $invstl[] = array("",$inv[$i]->cust_company,"1","1","1","1","1","1","1","1");
        
                    }$new = $inv[$i]->cust_company;
                  $invstl[] = array($inv[$i]->qto_code.$inv[$i]->po_id,$inv[$i]->cust_company,$invpay,$inv[$i]->po_tot_qty,$inv[$i]->po_tot_price,$inv[$i]->po_cuser,$inv[$i]->po_cdate,$invpaych,$invtrnusm,$invhq[0]->pay_chkid);
        }
      
        $data['inv_itm'] = $invstl;
        $data['inv'] = $user_detail;
	    
	    $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
          $this->load->view('report/pdf_invoice_settlemnt',$data);
	}
	
	function can_invoice(){
	    
	     $this->db->select('*');
                    $this->db->from('mes_user');
                    $this->db->where('user_lvl',20);
                    $this->db->where('user_status',1);
                      if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                        $data['ref'] = $sls;
	    
	    
	                $this->db->select('*');
                    $this->db->from('mes_customer');
                    $this->db->where('cust_status',1);
                    $hasil=$this->db->get();
                    $sls= $hasil->result(); 
                    $data['cus'] = $sls;
                    
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $hasil=$this->db->get();
                    $inv= $hasil->result(); 
                    $data['inv'] = $inv;
            
            $data['item_list'] = $this->Product_model->product_list();
            
            $data['cat_list'] = $this->Product_model->cat_list();
                
                
                $data['content'] = ('report/can_invoice'); 
		        $this->load->view('template',$data);
	}
	
	function prnt_cancel_inv(){
	    if($this->input->post('ref')!=""){
	        $ref =  $this->input->post('ref');
	    }else{ $ref = "0";}
	    $this->db->select("*");    
        $this->db->from('mes_user');
        $this->db->where('user_id',$ref);
          if($this->session->userdata['login_data']['userlevel']==20){
                        
                       $this->db->where('user_name',$this->session->userdata['login_data']['username']); 
                    }
       // $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $user_data=$query->result();
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        $user_detail['ref'] =$user_data[0]->user_name;
        $user_detail['fdate'] = "";
        $user_detail['todate'] = "";
        
        
        $this->db->select('*');
	    $this->db->from('mes_qto');
	    $this->db->join('mes_user', 'mes_qto.po_cuser   = mes_user.user_id ', 'left');
	    //$this->db->where("po_auth",1);
	    $this->db->where("return_amnt!=",0.00);
	    
	    if($this->input->post('ref')!=0){
                            $user_detail['ref'] =$user_data[0]->user_name;
                            $this->db->where('po_ref_id', $this->input->post('ref'));
                            //$this->db->where('return_items.ret_user', $user_data[0]->user_name);
                        }
                        
        if($this->input->post('frm_date')!=""){
                            $user_detail['fdate'] = $this->input->post('frm_date');
                            $this->db->where('po_cdate>=', $this->input->post('frm_date'));
                           
                        }
                        
                        if($this->input->post('to_date')!=""){
                            $user_detail['todate'] = $this->input->post('to_date');
                            $this->db->where('po_cdate<=', $this->input->post('to_date'));
                           // $this->db->where('inv_return.ret_cdate<=', $this->input->post('to_date'));
                        }               
	   
        $this->db->order_by('po_id', 'asc');
        $query = $this->db->get();
        $dataa=$query->result();
         
            
        $inv=array();
         for($i=0; $i<count($dataa); $i++){
                   
                           if(($dataa[$i]->po_final_tot == $dataa[$i]->return_amnt)){
                               $this->db->select('cust_company');
                                $this->db->from('mes_customer');
                                $this->db->where('cust_id',$dataa[$i]->po_sup_id);
                                $hasil=$this->db->get();
                                $cus= $hasil->result(); 
                    
                               $inv[] = array($dataa[$i]->qto_code.$dataa[$i]->po_id,$cus[0]->cust_company,$dataa[$i]->po_tot_qty+$dataa[$i]->po_tot_freqty,$dataa[$i]->po_final_tot,$dataa[$i]->po_cdate,$dataa[$i]->user_name,$dataa[$i]->user_name,$dataa[$i]->user_name);
                            
                            }else{
                                 }
                         
        }
        
        $user_detail['fullname'] = $user_data[0]->user_fname.' '.$user_data[0]->user_lname;
      
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_detail;
       
        
        // company profile 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
     
      
        $data['ret_itm'] = array();
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       // $data['ret_itm'] = $inv;
        $data['tm'][0] = "";
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
          $this->load->view('report/pdf_cancel_inv',$data);
	}
}        