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

class Sales_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        
         
         $this->load->model('Product_model');
        $this->load->model('Common_model');
        $this->load->model('General');
         
        }
        
    public function index()
	{
		//$er['ss'] = "";
		       // $data['content'] = ('login');                       
                //$this->load->view('login',$er);
                if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
                
                $data['content'] = ('pages/salse'); 
		        $this->load->view('template',$data);
	}
	
	function show_all_qtn() {
	    
        if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('logout'); 
                        exit();
            }
            
            $this->db->select('mes_qto.*,mes_customer.*,mes_currancy.*,mes_country.*,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( "mes_currancy", "mes_currancy.curr_id = mes_qto.po_currancy", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id','left');
            $this->db->order_by("mes_qto.po_id","desc");
            $this->db->where('po_dispatch',0);
            $this->db->where('depo',$this->session->userdata['login_data']['depot']);
         
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('qto');
            
            if($this->session->userdata['login_data']['userlevel'] != 9){
                
                $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Invoice</button></a></br>";
            }
            
             
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>INV Number</th>
														<th>Customer</th>
														<th>Item</th>
														<th>Qty</th>
														<th>Free</th>
														<th style='text-align: right;'>Total</th>
														<th style='text-align: right;'>Payment</th>
														<th>Date</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                    if($data[$i]->po_payment==1){
                        $pd = 'Paid';$pdd = ""; 
                        }else{
                        $pd = "Unpaid"; 
                            
                            $this->db->select('SUM(pay_amount) AS pay_amount');
                            $this->db->from('mes_payment');
                            $this->db->where('pay_invid',$data[$i]->po_id);
                            $hasil=$this->db->get();
                            $paid= $hasil->result();
                            $pdd = $paid[0]->pay_amount;
                            $float = (float)$pdd;
                        } 
                        
                        if($data[$i]->po_dispatch==1){$disbtn ="Restock";}else{$disbtn ="Dispatch";}
                        
                        $vw_btn10 = "<a href ='".base_url()."storseinvpdf/?id=".$data[$i]->uniq_id."/101' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                        
                                ";
                    
                    if($this->session->userdata['login_data']['userlevel'] != 9){
                        $vw_btn1 = " <a href ='".base_url()."authorize_inv/".$data[$i]->po_id."'> <button type='button' class='btn btn-outline-secondary btn-sm' >Authorize</button></a>";
                    }else{$vw_btn1 ="";}
                    
                     if($data[$i]->po_print == 1){
                        $vw_btn2 = "<a href ='".base_url()."storseinvpdf/?id=".$data[$i]->uniq_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>
                        
                                ";
                                $trstyle = '';
                    }else{
                         $vw_btn2 = "<button type='button' class='btn btn btn-secondary btn-sm'  id='".$data[$i]->po_id."' onclick='edit_dispatch(this.id)' >$disbtn</button>
                                ";
                                $trstyle = 'style="background-color: #f9edc0;"';
                    }
                    
                   
                   
                        
                    if($data[$i]->po_auth==1 ){
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn10.$vw_btn2;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn10;}
                        
                    }else{
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn1;}
                        
                    }
                    if($data[$i]->po_recieved==1){
                        $rec = 'Invoiced';
                        }else{
                        $rec = 'No invoice';}
                        
                       
                        
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row' $trstyle><td >".$x."</td>
                                 <td >".$data[$i]->po_code.$data[$i]->po_id."</td>
                                 
                               
                                 <td>".$data[$i]->cust_company."<br>".$data[$i]->cust_addres."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                                 <td style='text-align: center;'>".($data[$i]->po_tot_items)."</td>
                                <td style='text-align: right;'>".$data[$i]->po_tot_qty."</td>
                                <td style='text-align: right;'>".$data[$i]->po_tot_freqty."</td>
                             
                                <td style='text-align: right;' width='100px'>".number_format($data[$i]->po_final_tot, 2, '.', ',')."</td>
                                 <td style='text-align: right;' width='100px'>".$pd."<br>".number_format($float, 2, '.', ',')."</td>
                            
                                <td width='70px'>".$data[$i]->po_cdate."</td>
                                
                                $sts
                               
                                
                                
                                <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                              $det[3] = $data[0]->po_id;
                }
                
           echo json_encode($det);
            //echo json_encode($data);
        }
        
    function view_pdf(){
            
        //$id = $_GET['id'];
        
        $iparr = explode ("/", $_GET['id']); 
        
        $id = $iparr[0];
        $sto = $iparr[1];
        
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->where('uniq_id', $id);
        $query = $this->db->get();
        $gtid=$query->result();
        $insert_id = $gtid[0]->po_id; 
        $cus_id = $gtid[0]->po_sup_id; 
        
        $this->db->select("*");    
        $this->db->from('qto_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $this->db->join('mes_item', 'qto_items.po_itm_item_id   = mes_item.item_id','left');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id','left');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id','left');
        $this->db->join('mes_unit', 'qto_items.po_itm_unit   = mes_unit.unit_id','left');
        $this->db->join('mes_currancy', 'qto_items.po_itm_currancy   = mes_currancy.curr_id','left');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
        $this->db->join( "batch", "batch.batch_id  = qto_items.po_itm_batch", 'left');
        
        $query = $this->db->get();
        $dataa=$query->result();
        
         for($i=0; $i<count($dataa); $i++){
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->model_name!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->retail_price == 0.00){$rp = "";}else{$rp = number_format(($dataa[$i]->retail_price),2,".",",");}
                  
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$dataa[$i]->itm_name,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id,$dataa[$i]->free_item,$dataa[$i]->brand_shordcode,$rp,$dataa[$i]->batch,$dataa[$i]->exp_date); 
                
        }
        $data['inv_itm'] = $inv;
        
         $this->db->select("*");    
                $this->db->from('mes_inventry');
               // $this->db->join('mes_termcat', 'mes_tmdes.tmdes_catid  = mes_termcat.tcat_id');
                $this->db->where('invt_type', "INV");
                $this->db->where('invt_type_id', $insert_id);
                //$this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $querynew = $this->db->get();
                $dateinv =$querynew->result();
        
        
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->where('po_id', $insert_id );
        $this->db->join('mes_customer', 'mes_qto.po_sup_id  = mes_customer.cust_id', 'left');
        $this->db->join('mes_currancy', 'mes_qto.po_currancy   = mes_currancy.curr_id', 'left');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id', 'left');
        $this->db->join('mes_depot', 'mes_depot.dep_id  = mes_qto.depo');
        //$this->db->join('mes_user', 'mes_qto.po_cuser   = mes_user.user_id','left');
        $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id','left');
        $query = $this->db->get();
        $rr = $query->result();
        
          $this->db->select("*");    
                $this->db->from('mes_qto');
                $this->db->join('mes_depot', 'mes_depot.dep_id  = mes_qto.depo');
                $this->db->where('po_sup_id', $cus_id);
                $this->db->where('po_payment', 0);
                $this->db->order_by("po_cdate", "asc");
                $queryinv = $this->db->get();
                $dateinvd =$queryinv->result();
                
                $invout="";
                $incount=0;
                  for($i=0; $i<count($dateinvd); $i++){
                      
                    $dataout = $this->Common_model->outstandqto($dateinvd[$i]->po_id);
                    
                    if($dataout!=0){
                        
                        $invout = $invout." | ".$dateinvd[$i]->po_cdate." - ".$dateinvd[$i]->qto_code.$dateinvd[$i]->po_id." - ".number_format($dataout,2); 
                        
                        $incount = $incount +1;
                    }
                    
                
                    }
        
        
              
        
        $dataout = $this->Common_model->outstand($cus_id);
        
       // $data['invcount'] = $incount;
       
           $this->db->select("*");    
                $this->db->from('mes_qto');
                $this->db->where('po_cdate', $rr[0]->po_cdate);
                $queryinv = $this->db->get();
                $daily_count =$queryinv->result();
        $dailycnt = 0;
            for($i=0; $i<count($daily_count); $i++){
                if($daily_count[$i]->uniq_id==$id){
                    $dailycnt = ($i+1);
                    break;
                }
            } 
        
            $time = new DateTime($dateinv[0]->invt_cdate);
            $date = $time->format('n.j.Y');
            $time = $time->format('h:i:sa');
        
        $data['inv']=array($rr,$dateinv[0]->invt_cdate,$dataout,$invout,$rr[0]->dep_name,$incount,$dailycnt,$time);
        
       $tc = explode(',',$rr[0]->po_tc);
      // sort($tc);
       $length = count($tc);
        
         for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_tmdes');
                $this->db->join('mes_termcat', 'mes_tmdes.tmdes_catid  = mes_termcat.tcat_id');
                $this->db->where('tmdes_id', $tc[$i]);
                $this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
               
                
                if($sto == 101){
                    
                    $this->load->view('pages/qtnpdfsto',$data);
                }else{
                    
                $this->db->set('po_print', 0);
                $this->db->where('uniq_id',  $id);
                $this->db->update('mes_qto');
                    
                    $this->load->view('pages/qtnpdf',$data);
                }
     
     
        
    }
	
	function ref_salsesumpdf($insert_id){
	    
	    if(null !==$this->input->post('chk_date')){
                $newdate = $this->input->post('chk_date');
                $ref = $this->input->post('repselect');
	    }
	    
	    
	    
	    if($insert_id == "xx"){
	        
	        $refid = $ref; $crdate = $newdate;
	    }else{
	      
	      $refid = $insert_id; $crdate = 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();
        
       
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
	    
	    
                    
        $this->db->select("*");    
        $this->db->from('mes_qto');
        $this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        $this->db->where('po_ref_id', $usid );
        $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->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="";}
             
         $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);
        $this->db->where('pay_date', $crdate);
        //$this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dat=$query->result();
        
        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); 
        }
                   
  
        }
        $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');
        $this->db->where('pay_ref_id', $usid );
        $this->db->where('mes_payment_main.pay_date', $crdate );
        $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("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($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 if($dataapay[$i]->pay_type == 5){$type = "Bank Deposit";}else{$type = "Old Return";}
             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 if($dataapay[$i]->pay_type == 5){$type = "Promo Discount";
             }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');
        $this->db->where('mes_qto.po_ref_id', $usid );
        $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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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();
                $data['tm'][1] =$query->result();
                
                $data['crdate'] = $crdate;
                 $data['tm'][0] = $crdate;
            
            $this->load->view('pages/salysummrypdf',$data);
            
        }
    
    function dref_salsesumpdf_lumen(){
	    
	    
	        $crdate = $_GET['id'];
	        $refid = $_GET['ref'];
	   
	        //$crdate = 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_id', $refid );
        $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_qto');
        $this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id', 'left');
        $this->db->join('cities', 'mes_customer.cust_city   = cities.id', 'left');
        $this->db->where('po_ref_id', $usid );
        $this->db->where('po_cdate', $crdate );
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dataa=$query->result();
     
            
        $inv=[];
         for($i=0; $i<count($dataa); $i++){
             
                $city = $dataa[$i]->cust_city;
                $cusref = $dataa[$i]->cust_salseperson;
              $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);
        $this->db->where('pay_date', $crdate);
        //$this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dat=$query->result();
        
        if($dataa[$i]->po_final_tot!= $dataa[$i]->return_amnt){
            
           // $inv[] = array($dataa[$i]->qto_code,$dataa[$i]->po_tax_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); 
        
            $inv[] = array($dataa[$i]->qto_code,$dataa[$i]->po_id,$dataa[$i]->cust_company."-".$dataa[$i]->name_en,$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,""); 
        }
        
                   
  
        }
        
        $data['inv_itm'] = $inv;
        $data['inv'] = $user_data;
        
        
        $this->db->select("mes_payment_main.*,mes_customer.*,cities.*,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('cities', 'mes_customer.cust_city   = cities.id', 'left');
        //$this->db->join('mes_payment', 'mes_payment_main.pay_uniqid   = mes_payment.pay_uniqid', 'left');
        $this->db->where('pay_ref_id', $usid );
        $this->db->where('mes_payment_main.pay_date', $crdate );
        $this->db->where('mes_payment_main.pay_stetus', 1 );
         
        $query = $this->db->get();
        $dataapay=$query->result();
        
        
            
        $invpay=[];
         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";}
               if($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid." - ".$dataapay[$i]->pay_chkdate;
               }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 if($dataapay[$i]->pay_type == 5){$type = "Promo Discount";
                }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', 'asc');
                    $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(abs($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); 
                     $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]->name_en); 
                
        }
        
     
        
        $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');
        $this->db->where('mes_qto.po_ref_id', $usid );
        $this->db->where('inv_return.ret_cdate', $crdate);
        $this->db->where('inv_return.ret_stetus', 1);
        
        $query = $this->db->get();
        $datatrn=$query->result();
            
        $invrtn=[];
         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;
        
        
          //expenses
        
        $this->db->select("*");    
        $this->db->from('daily_exp');
        
        $this->db->join('mes_expreson', 'mes_expreson.exp_id    = daily_exp.reason', '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');
        $this->db->where('daily_exp.ref_id', $usid );
        $this->db->where('daily_exp.dexp_date', $crdate);
        $this->db->where('daily_exp.dexp_stetus', 0);
        
        $queryexpn = $this->db->get();
        $dataexpn = $queryexpn->result();
        
         $expns=[];
         for($i=0; $i<count($dataexpn); $i++){
             
   
                    $expns[] = array($dataexpn[$i]->dexp_code.$dataexpn[$i]->dailyexp_id,$dataexpn[$i]->exp_reson,$dataexpn[$i]->amount); 
                
        }
        
      
        $data['tm'][2] = $expns;
        
        //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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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=[];
         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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $this->db->where('chk_collect.date', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $query = $this->db->get();
        $datachqcollection = $query->result();
        
        
        
         $chqcol=[];
         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();
                $dat =$query->result();
                $dd =$query->result();
                
                $data['crdate'] = $crdate;
                 $data['tm'][0] = $crdate;
                 $data['tm'][1] = $dd;
            
            $this->load->view('pages/salysummrypdf',$data);
            
        }    
        
        
    function dref_salsesumpdf(){
        
         if($this->input->get_post('repselect')!=null){
	        $refid = $this->input->get_post('repselect');
	    }else{
	        $refid = 0;
	    }
	    
	    if($this->input->get_post('chk_date')!=null){
	       $crdate = $this->input->get_post('chk_date');
	    }else{
	        $crdate = date("Y-m-d");
	    }
	    
	    
	   // if(null !==$this->input->post('chk_date')){
       //         $newdate = $this->input->get_post('chk_date'); 
       //         $ref = $this->input->get_post('repselect');
	   // }
	    
	   // $crdate = $this->input->get_post('chk_date');
	   // $refid = $this->input->get_post('repselect');
	
	    
	    $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();
        
       
        
        $usid = $user_data[0]->user_id;
         $usname = $user_data[0]->user_name;
	    
                    
        $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');
        $this->db->join('mes_route', 'mes_route.route_id   = mes_customer.cust_route', 'left');
        $this->db->where('po_ref_id', $usid );
        $this->db->where('po_cdate', $crdate );
        $this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dataa = $query->result();
        
       $arr=[];
           for($i=0; $i<count($cnt_data); $i++){
               $arr[]=$cnt_data[$i]->po_id;
           }
        
            
        $inv=[];
         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();
                        
                       
                       if($dataa[$i]->cust_route == 0){$rtt = $rcity[0]->route_name;}else{$rtt=$dataa[$i]->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);
                $this->db->where('pay_date', $crdate);
                //$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');
        $this->db->where('pay_ref_id', $usid );
        $this->db->where('mes_payment_main.pay_date', $crdate );
        $this->db->where('mes_payment_main.pay_stetus', 1 );
         
        $query = $this->db->get();
        $dataapay=$query->result();
        
        
            
        $invpay=[];
         for($i=0; $i<count($dataapay); $i++){
             
                $city = $dataapay[$i]->cust_city;
                $cusref = $dataapay[$i]->cust_salseperson;
                
                $this->db->select('*');
                    $this->db->from('customer_route');
                    $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
                    $this->db->where('customer_route.cus_id', $dataapay[$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($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', 'asc');
                    $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(abs($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');
        $this->db->where('mes_qto.po_ref_id', $usid );
        $this->db->where('inv_return.ret_cdate', $crdate);
        $this->db->where('inv_return.ret_stetus', 1);
        
        $query = $this->db->get();
        $datatrn=$query->result();
            
        $invrtn=[];
         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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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=[];
         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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $this->db->where('chk_collect.date', $crdate);
        $this->db->where('mes_payment_main.pay_stetus', 1);
        
        $query = $this->db->get();
        $datachqcollection = $query->result();
        
       // $details = $datachqcollection[$i]->cust_company;
        $details = "";
        
         $chqcol=[];
         for($i=0; $i<count($datachqcollection); $i++){
             
                    $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();
               // print_r($dd);
                
                $data['crdate'] = $crdate;
                 $data['tm'][0] = $crdate;
                 $data['tm'][1] = $dd;
                 
            
        $this->load->view('pages/salysummrypdf',$data);
            
        }
        
    
    function ref_salsesumpdf_lumen($ref_id,$dte){
	    
	   $refid = $ref_id;
	    //$repl = str_replace('w', '-', $dte);
	    $newdate = $dte;
	    
	   
	    
	    if($newdate != ""){
	        
	        $crdate = $newdate;
	    }else{
	      
	       $crdate = 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_id', $refid );
        $query = $this->db->get();
        $user_data=$query->result();
       
        
        $usid = $user_data[0]->user_id;
        $usname = $user_data[0]->user_name;
        
        $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("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');
        $this->db->where('po_ref_id', $usid );
        $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++){
             
         $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);
        $this->db->where('pay_date', $crdate);
        //$this->db->where('po_auth', 1 );
        $query = $this->db->get();
        $dat=$query->result();
        
        $this->db->select('*');
                    $this->db->from('customer_route');
                    $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
                    $this->db->where('customer_route.cus_id', $dataa[$i]->po_sup_id);
                $query = $this->db->get();
                $rcity = $query->result();
                
                //print_r($rcity);
               if(isset($rcity[0]->route_name)){$rtt1 = $rcity[0]->route_name;}else{$rtt1="";}
               
               $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."-".$rtt1,$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');
        $this->db->where('pay_ref_id', $usid );
        $this->db->where('mes_payment_main.pay_date', $crdate );
        $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++){
             
             $this->db->select('*');
                    $this->db->from('customer_route');
                    $this->db->join( "mes_route", "mes_route.route_id = customer_route.route_id", 'left');
                    $this->db->where('customer_route.cus_id', $dataapay[$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($dataapay[$i]->pay_type == 0)  {$type = "Cash";} else if($dataapay[$i]->pay_type == 1){$type = "Cheque - ".$dataapay[$i]->chqid;}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 == 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->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_="";
                    for($j=0; $j<count($inv_data); $j++){
                        
                       // if(count($inv_data[$j]->pay_amount)>1){$amount = $inv_data[$j]->pay_amount;}else{$amount = "";}
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid.'-'.$inv_data[$j]->pay_amount." , ".$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');
        $this->db->where('mes_qto.po_ref_id', $usid );
        $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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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("*");    
        $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');
        $this->db->where('mes_payment_main.pay_ref_id', $usid );
        $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++){
             
              $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_="";
             
   
                    $chqcol[] = array($datachqcollection[$i]->pay_chkid,$datachqcollection[$i]->details,$datachqcollection[$i]->pay_invid,$datachqcollection[$i]->amount); 
                
        }
        
      
        $data['chq_col'] = $chqcol;
        
        
        // company profile 
        
      
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                $data['crdate'] = $crdate;
        $data['tm'][0] = $crdate;
            
            $this->load->view('pages/salysummrypdf',$data);
            
        }

	
	function ref_getinv(){
	    
	    if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
    	            
            $this->db->from('mes_qto');
            $this->db->select("*");
            $this->db->where('po_ref_id',$this->input->post('id'));
          //  $this->db->where('po_auth',1);
            $this->db->order_by("po_id","desc");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Invoice ID</th>
														<th>Item</th>
														<th>Qty</th>
														<th>Free</th>
														<th>Total</th>
														<th>Discount</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                     $x = $i+1;
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td>$x</td>
                              <td>".$data[$i]->po_code.$data[$i]->po_id."</td>
                                <td>".$data[$i]->po_tot_items."</td>
                                <td >".$data[$i]->po_tot_qty."</td>
                                <td >".$data[$i]->po_tot_freqty."</td>
                                <td >".$data[$i]->po_final_tot."</td>
                                <td >".$data[$i]->po_discount."</td>
                                
                                <td style='text-align: right;'>
                                 <a href = '".base_url()."inv_view_pdf/?id=".$data[$i]->uniq_id."' target='_blank'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tx_vw/".$data[$i]->po_id."'  data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
    function ref_gettrn(){
            if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
          
            
            $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_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
             $this->db->where('mes_qto.po_ref_id',$this->input->post('id'));
             $this->db->where('inv_return.ret_proceed',1);
            $this->db->order_by("ret_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Return Number</th>
														<th>Invoice Number</th>
														<th>Reason</th>
														<th <td style='text-align: right;'>Qty</th>
														<th <td style='text-align: right;'>Amount</th>
														<th <td style='text-align: right;'>Date</th>
														<th <td style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
           
                for($i=0; $i<count($data); $i++){
                    
                     $x = $i+1;
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td >".$x."</td>
                              <td>".$data[$i]->ret_code.$data[$i]->ret_id."</td>
                                <td>INV".$data[$i]->ret_invnum."</td>
                                <td >".$data[$i]->ret_reason."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_totqty."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_amount."</td>
                                <td style='text-align: right;'>".$data[$i]->ret_cdate."</td>
                               
                                
                                <td style='text-align: right;width:10%;'>
                                 <a href ='".base_url()."Return_controller/view_pdf/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
    function ref_getpay(){
            if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
            
           
          
            
            $this->db->select('*');
            //$this->db->from('mes_payment');
            $this->db->from('mes_payment_main');
             $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            // $this->db->join( "mes_qto", "mes_qto.po_id = mes_payment.pay_invid", 'left');
             //$this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
             if($this->session->userdata['login_data']['userlevel']==20){
              $this->db->where('pay_user',$this->session->userdata['login_data']['username']);  
             }
            $this->db->where('pay_ref_id',$this->input->post('id'));
             $this->db->where('mes_payment_main.pay_authrize',1);
            $this->db->order_by("pay_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Slip ID</th>
														<th>Invoice ID</th>
														<th>Type</th>
														<th <td style='text-align: right;'>Value</th>
														<th <td style='text-align: right;'>Date</th>
														<th <td style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
           
                for($i=0; $i<count($data); $i++){
                    
                      $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                    
                     $x = $i+1;
                     if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }
                     else if ($data[$i]->pay_type==2){$ptype = "Bank Deposit";}
                     else if ($data[$i]->pay_type==3){$ptype = "Payment Excess";}
                     else if ($data[$i]->pay_type==4){$ptype = "Return Excess";}
                     else{$ptype = "Cash";}
                     
                     $bal = ""; 
                        if($data[$i]->pay_balance!=0.00){
                            $bal = "EXCESS - ".$data[$i]->pay_balance;
                        }
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td >".$x."</td>
                              <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                                <td>$inv_</td>
                                 <td>$ptype</td>
                                
                                <td style='text-align: right;'>".$data[$i]->pay_amount."<br><b>".$bal."<b></td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                               
                                
                                <td style='text-align: right;width:10%;'>
                                <a href ='".base_url()."payslip/".$data[$i]->pay_uniqid."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_uniqid."'   data-backdrop='static' data-keyboard='false'>View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
    function ref_getchq(){
            if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
          
            
            $this->db->select('*');
            $this->db->from(' mes_payment_main');
            $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            //$this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            //$this->db->join( "mes_qto", "mes_qto.po_id = mes_payment_main.pay_invid", 'left');
             //$this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
             $this->db->where('pay_ref_id',$this->input->post('id'));
             $this->db->where('pay_chkreturn',1);
             $this->db->where('pay_stetus',1);
            //$this->db->order_by("pay_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th>ID</th>
														<th>Slip ID</th>
														<th>Invoice ID</th>
														<th>Type</th>
														<th <td style='text-align: right;'>Value</th>
														<th <td style='text-align: right;'>Date</th>
														<th <td style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
           
                for($i=0; $i<count($data); $i++){
                    
                    $this->db->from('mes_payment');
                    //$this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment.pay_chkbnk", 'left');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                    
                     $x = $i+1;
                    // if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }else{$ptype = "Cash";}
                      if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }
                     else if ($data[$i]->pay_type==2){$ptype = "Bank Deposit";}
                     else if ($data[$i]->pay_type==3){$ptype = "Payment Excess";}
                     else if ($data[$i]->pay_type==4){$ptype = "Return Excess";}
                     else{$ptype = "Cash";}
                    
                   // if($data[$i]->tax_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr>
                              <td >".$x."</td>
                              <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                                <td>".$inv_."</td>
                                 <td>$ptype</td>
                                <td style='text-align: right;'>".$data[$i]->pay_amount."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                               
                                
                                <td style='text-align: right;width:10%;'>
                                <a href ='".base_url()."payslip/".$data[$i]->pay_uniqid."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_uniqid."'   data-backdrop='static' data-keyboard='false'>View</button></a>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
    function ref_getcerdit(){
        
        //$this->db->select_sum('po_final_tot');
    $this->db->select("(`po_final_tot`) as tot,(`return_amnt`) as ret,po_id");
    $this->db->from('mes_qto');
    $this->db->where('po_ref_id',$this->input->post('id'));
    $this->db->where('po_status',1);
    $this->db->where('po_auth',1);
    //$this->db->limit(75);
    $query = $this->db->get();
    $data= $query->result();
    
    
    
    
            
                        
           // $det[0]="";
            $det[1] ="<tr>
													
														<th>Invoice</th>
														<th>Invoice Total</th>
														<th>Payment Total</th>
														<th>Return Total</th>
														<th>Outstanding Total</th>
													
														
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
              
                    
                     $tbal = 0;
                    
              for($i=0; $i<count($data); $i++){
                  $this->db->select_sum('pay_amount');
                    $this->db->from('mes_payment');
                            $this->db->where('pay_invid', $data[$i]->po_id);
                            $this->db->where('pay_stetus', 1);
                    $query = $this->db->get();
                    $data2= $query->result();
                    
                    
                    $bal = $data[$i]->tot - ($data[$i]->ret +  $data2[0]->pay_amount);
                    
                    $tbal = $tbal + $bal;
                    
                    if($bal!=0){
                        
                         $dat =  "
                              <tr>
                              
                                <td>INV".$data[$i]->po_id."</td>
                                <td>".$data[$i]->tot."</td>
                                <td>".$data2[0]->pay_amount."</td>
                                <td>".$data[$i]->ret."</td>
                                <td >$bal</td>
                             
                              </tr>";
                              $det[2] =$det[2].$dat;
                    }
                  
                  $det[0]="<button type='button' class='btn btn-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>".number_format($tbal,2)."</button></br>";
             
              }
              
              
                              echo json_encode($det);
        
    }
	
	function prof_ref($name){
	   if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
	   
	   
	    $this->db->select("mes_user.*,mes_user.user_fname as fnme,mes_user.user_lname as lnme");
                    $this->db->from('mes_user');
                    //$this->db->join( "mes_user", "mes_user.user_id = mes_customer.cust_salseperson", 'left');
                    $this->db->where('user_name',$name);
                   // $this->db->where('cust_country',$this->input->post('esup_country'));
                    $detail = $this->db->get();
                    $custd = $detail->result();
                    
                   $data["custdata"] = $custd;
    	 
    	    
    	$data['content'] = ('pages/refprof'); 
		$this->load->view('template',$data);
	}
	
	function get_saleperson(){
	    if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
	    
	    
	    $this->db->from('mes_user');
            $this->db->select("mes_user.*, mes_user_lvl.*,mes_depot.*");
            $this->db->join( "mes_user_lvl", "mes_user_lvl.user_lvl_val = mes_user.user_lvl", 'left');
            $this->db->join( "mes_depot", "mes_depot.dep_id = mes_user.user_depoid", 'left');
            $this->db->where('user_departmentid','Marketing');
             //$this->db->from('mes_country');
           // $this->db->select("*");
            $detail = $this->db->get();
            $data = $detail->result();
            
             $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_user' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Sales Person</button></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Full Name</th>
														<th>User Name</th>
														<th>Depot</th>
														<th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->user_status==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                    $dat =  "
                              <tr scope='row'>
                                <td>".$data[$i]->user_code.$data[$i]->user_id."</td>
                                <td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td>
                                <td>".$data[$i]->user_name."</td>
                                <td>".$data[$i]->dep_name."</td>
                                $sts
                                
                               <td style='text-align: right;'>
                               
                               
                        
                               <a href = '".base_url()."ref_prof/".$data[$i]->user_name."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button'  >View</button></a>
                               <a href = '".base_url()."salsesumry/".$data[$i]->user_name."' target='_blank'><button type='button' class='btn btn-outline-secondary btn-sm' type='button'  >Daily Summery</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }      
            	
           echo json_encode($det);
	}
	
	function get_todaysalse() {
            $this->db->select('mes_qto.*,mes_customer.*,mes_currancy.*,mes_country.*');
            $this->db->from('mes_qto');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_qto.po_currancy", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->where("po_cdate", date("Y-m-d"));
            $this->db->order_by("mes_qto.po_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('qto');
            
            // $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Invoice</button></a></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>INV Number</th>
														<th>Customer</th>
														<th>Item</th>
														<th>Qty</th>
														<th>Free</th>
														<th style='text-align: right;'>Total</th>
														<th>Date</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                   $vw_btn1 = " <a href ='".base_url()."authorize_inv/".$data[$i]->po_id."'> <button type='button' class='btn btn-outline-secondary btn-sm' >Authorize</button></a>
                                
                                ";
                    $vw_btn2 = "<a href ='".base_url()."inv_view_pdf/?id=".$data[$i]->uniq_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                ";
                        
                    if($data[$i]->po_auth==1){
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                    }else{
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn1;}
                        
                    }
                    if($data[$i]->po_recieved==1){
                        $rec = 'Invoiced';
                        }else{
                        $rec = 'No invoice';}
                        
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'><td >".$x."</td>
                                 <td >".$data[$i]->po_code.$data[$i]->po_id."</td>
                                 
                               
                                 <td>".$data[$i]->cust_company."<br>".$data[$i]->cust_addres."<br>".$data[$i]->cust_contact."<br>".$data[$i]->cust_conperson."</td>
                                 <td style='text-align: center;'>".($data[$i]->po_tot_items)."</td>
                                <td style='text-align: right;'>".$data[$i]->po_tot_qty."</td>
                                <td style='text-align: right;'>".$data[$i]->po_tot_freqty."</td>
                             
                                <td style='text-align: right;' width='100px'>".number_format($data[$i]->po_final_tot, 2, '.', ',')."</td>
                            
                                <td width='70px'>".$data[$i]->po_cdate."</td>
                                
                                $sts
                               
                                
                                
                                <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
    function get_todayspay(){
        
            $this->db->select('mes_payment_main.*,mes_payment.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_code as pcode');
            $this->db->join( "mes_payment", "mes_payment.pay_main_id = mes_payment_main.pay_id", 'left');
            $this->db->from("mes_payment_main");
            $this->db->where("mes_payment_main.pay_date", date("Y-m-d"));
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
           // $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th style='text-align: right;'>Type</th>
														<th style='text-align: right;'>Value</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->pay_chkbnk."<br>Date : ".$data[$i]->pay_chkdate;
                    
                    
                   // if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }else{$ptype = "Cash";}
                     if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }
                     else if ($data[$i]->pay_type==2){$ptype = "Bank Deposit";}
                     else if ($data[$i]->pay_type==3){$ptype = "Payment Excess";}
                     else if ($data[$i]->pay_type==4){$ptype = "Return Excess";}
                     else{$ptype = "Cash";}
                    
                    
                   
                    if($data[$i]->pay_authrize==0){$authbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='authpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Authorize</button>
                   <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>";}
                    else{$authbtn = "";}
                    
                   
                    
                    if($data[$i]->pay_stetus==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";  $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";}
                    else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";$canbtn ="";}
                    $x = $i+1;
                    $dat =  "
                              <tr>
                              <td>".$x."</td>
                               <td>".$data[$i]->pcode.$data[$i]->pid."</td>
                              <td>INV".$data[$i]->pay_invid."</td>
                                <td>$ptype</td>
                                <td style='text-align: right;'>".$data[$i]->pay_amount."<br>Excess - ".$data[$i]->pay_balance."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                
                                 $canbtn
                                   <a href ='".base_url()."payslip/".$data[$i]->pay_id."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."'   data-backdrop='static' data-keyboard='false'>View</button></a>
                                 $authbtn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
    function get_todayrtn(){
        
            $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_ret_reason", "mes_ret_reason.ret_reason_id = inv_return.ret_reason", 'left');
            $this->db->where("ret_cdate", date("Y-m-d"));
            $this->db->order_by("ret_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
               $det[1] ="<tr>
														<th >ID</th>
														<th>Return Note ID</th>
														<th>Invoice</th>
														<th style='text-align: right;'>Reason For Return</th>
														<th>Total Qty</th>
														<th style='text-align: right;'>Value</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                    
                    $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id', $data[$i]->po_id);
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_qto.po_ref_id", 'left');
            
            $hasil1=$this->db->get();
            $datanew= $hasil1->result();
            
            
                    // $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->pay_chkbnk."<br>Date : ".$data[$i]->pay_chkdate;
                    
                    
                    //if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }else{$ptype = "Cash";}
                    
                     $vw_btn1 = "
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->ret_id."' name='edtbutton' onclick='rtn_cancel(this.id)' data-target='#Modal_edit'>Cancel</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->ret_id."/".$data[$i]->po_id."'  onclick='po_proceed(this.id)' >Process</button>
                                <a href ='".base_url()."pdf_invrtn/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>";
                    $vw_btn2 = "<a href ='".base_url()."pdf_invrtn/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                
                                ";
                   
                    
                    
                      if($data[$i]->ret_proceed==1){
                        if($data[$i]->ret_stetus==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                    }else{
                        if($data[$i]->ret_stetus==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                        
                    }
                    $x = $i+1;
                    $dat =  "
                              <tr>
                              <td>".$x."</td>
                               <td>".$data[$i]->ret_code.$data[$i]->ret_id."</td>
                              <td >INV".$data[$i]->ret_invnum."<br>".$datanew[0]->cust_company."<br><b>".$datanew[0]->user_fname." ".$datanew[0]->user_lname."</b></td>
                                <td>".$data[$i]->ret_reason."</td>
                                 <td style='text-align: right;'>".$data[$i]->ret_totqty."</td>
                                 <td style='text-align: right;'>".number_format($data[$i]->ret_amount,2)."</td>
                                <td style='text-align: right;width:10%'>".$data[$i]->ret_cdate."</td>
                                $sts
                                
                              <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
    }
    
    function get_todayrtnchq(){
        
            $this->db->select('mes_payment_main.*,mes_payment.*,mes_payment_main.pay_id as pid,mes_payment_main.pay_code as pcode');
            $this->db->join( "mes_payment", "mes_payment.pay_main_id = mes_payment_main.pay_id", 'left');
            $this->db->from("mes_payment_main");
            $this->db->where("mes_payment_main.pay_trndate", date("Y-m-d"));
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
           // $det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_tax' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel' data-backdrop='static' data-keyboard='false'>Add New Tax</button></br>";
            
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th style='text-align: right;'>Cheque</th>
														<th style='text-align: right;'>Value</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->pay_chkbnk."<br>Date : ".$data[$i]->pay_chkdate;
                    
                    
                    //if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }else{$ptype = "Cash";}
                     if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; }
                     else if ($data[$i]->pay_type==2){$ptype = "Bank Deposit";}
                     else if ($data[$i]->pay_type==3){$ptype = "Payment Excess";}
                     else if ($data[$i]->pay_type==4){$ptype = "Return Excess";}
                     else{$ptype = "Cash";}
                    
                    
                   
                    if($data[$i]->pay_authrize==0){$authbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='authpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Authorize</button>
                   <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>";}
                    else{$authbtn = "";}
                    
                   
                    
                    if($data[$i]->pay_stetus==1){$sts = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";  $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";}
                    else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";$canbtn ="";}
                    $x = $i+1;
                    $dat =  "
                              <tr>
                              <td>".$x."</td>
                               <td>".$data[$i]->pcode.$data[$i]->pid."</td>
                              <td>INV".$data[$i]->pay_invid."</td>
                                <td>$checkdel</td>
                                <td style='text-align: right;'>".$data[$i]->pay_amount."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                
                                 $canbtn
                                   <a href ='".base_url()."payslip/".$data[$i]->pay_id."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."'   data-backdrop='static' data-keyboard='false'>View</button></a>
                                 $authbtn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
    }
    
  
        
}