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

class Invreturn_controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
        $this->load->model('General');
        $this->load->model('Common_model');
        $this->load->library('encryption');
        
        
       //  if(!isset($this->session->userdata['login_data']['username'])) { 
                
           //             redirect('login'); 
           //             exit();
           // }s
        
        }
        
        function test(){
            $this->db->select('*');
            $this->db->from('return_items');
            $hasil=$this->db->get();
            $data= $hasil->result();
            
            for($i=0; $i<count($data); $i++){ 
                $this->db->select('po_itm_unitprice');
                $this->db->from('qto_items');
                $this->db->where('po_itm_item_id',$data[$i]->ret_itm_item_id);
                    $this->db->where('po_itm_po_id',$data[$i]->ret_inv_id);
                $hasil=$this->db->get();
                $prc= $hasil->result();
                
                $this->db->set('ret_itm_unitprice', $prc[0]->po_itm_unitprice);
                $this->db->where('ret_itm_id', $data[$i]->ret_itm_id);
                 $result=$this->db->update('return_items');
            }
            
            
            
        }
        
        function edit_invrtn($id){
            
                    $data['content'] = "pages/edit_invreturn";        
                    $data['id'] = $id;        
                    $this->load->view('template',$data);
        }
        
        function get_invrtnedt(){
            
              $id = $this->input->post('id');
            
            $this->db->select("*");
            $this->db->from('return_items');
            $this->db->join('mes_item', 'return_items.ret_itm_item_id   = mes_item.item_id','left');
            $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
           // $this->db->where('mes_inventry.invt_type', "GRN");
            $this->db->where('return_items.ret_note_id', $id);
            $hasil=$this->db->get();
            $dataa = $hasil->result();
            
             for($i=0; $i<count($dataa); $i++){
                 
                        $itm = $dataa[$i]->itm_name;
                        $data[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->ret_itm_unitprice,$dataa[$i]->ret_itm_qty,"0000-00-00",$dataa[$i]->brand_shordcode,$dataa[$i]->ret_itm_id); 
                    
            }
            if(!empty($data)){$data;}else{$data="";}
           
             echo json_encode($data);
        }
        
        function index(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
                    $this->db->from('mes_qto');
                    $this->db->where('po_auth',1);
                    $this->db->where('po_status',1);
                    if($this->session->userdata['login_data']['userlevel'] == 20){
                    $this->db->where('po_ref_id', $this->session->userdata['login_data']['user_id']);
                     }
                    $hasil=$this->db->get();
                    $resultagent= $hasil->result(); 
                    
                  // $resultagnt[] = array();
                    for($i=1; $i<count($resultagent); $i++){
                       // $resultagnt[] = array($resultagent[$i]->po_code,$resultagent[$i]->po_id);
                        $this->db->from('inv_return');
                        $this->db->where('ret_invnum',$resultagent[$i]->po_id);
            
                        $this->db->where('ret_proceed',0);
                        $hasil=$this->db->get();
                        $dt= $hasil->result();
                        
                            if(count($dt)>0){
                                
                            }else{
                                $resultagnt[] = array($resultagent[$i]->po_code,$resultagent[$i]->po_id);
                            }
                        
                    }
                    
            $data['agentdetail'] = $resultagnt;
            
                    $this->db->from('mes_ret_reason');
                    $this->db->where('ret_reason_status',1);
                    $hasil=$this->db->get();
                    $res= $hasil->result(); 
            $data['ret_res'] = $res;
            
            
            
            $data['content'] = "pages/inv_return";        
            $this->load->view('template',$data);
        }
        
        function grn_all(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
            $data['ref'] = $this->Product_model->salserep_list();
            
             $data['content'] = "pages/all_inv_return";        
             $this->load->view('template',$data); 
        }
        
        function showallret(){
              if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
             
             
            $this->db->select('*');
            $this->db->from('inv_return_main');
            $this->db->join( "mes_user", "mes_user.user_id  = inv_return_main.retm_puser", 'left');
            if($this->session->userdata['login_data']['userlevel'] == 20){
                $this->db->where('retm_cuser', $this->session->userdata['login_data']['user_id']);
            }
            
            $this->db->order_by("inv_return_main.retm_stetus","desc");
            $this->db->order_by("inv_return_main.retm_proceed","asc");
            $this->db->order_by("retm_id","desc");
            $this->db->limit(100);
            
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
         
            $link = site_url('new_inv_ret');
            $link = "#";
           
             //$det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Return Note</button></a></br>";
             $det[0]="";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Return Note ID</th>
														<th>Customer</th>
														<th>Total Qty</th>
														<th>Value</th>
														<th style='width:10%'>Date</th>
													    <th>Status</th>
														<th>Action</th>                                
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                   
                        $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_main_id',$data[$i]->retm_id);
                        $hasil=$this->db->get();
                        $inv_return= $hasil->result(); 
                    
                    
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id', $inv_return[0]->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();
            
            if($hasil1->num_rows()>0){
                $sup = $datanew[0]->cust_company;
            }else{$sup = "Direct Invoice";}
                    
                     if($this->session->userdata['login_data']['userlevel']< 2 || $this->session->userdata['login_data']['userlevel']==15){
                         
                                     $vw_btn1 = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_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]->retm_id."'  onclick='stock_assign(this.id)' >Process</button>
                                 <!--a href ='".base_url()."pdf_invrtn_edit/".$data[$i]->retm_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' >Edit</button-->
                                <a href ='".base_url()."pdf_invrtn/".$data[$i]->retm_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>";
                     }else{$vw_btn1 = "";}
                    
       
                                
                               
                    $vw_btn2 = "<a href ='".base_url()."pdf_invrtn/".$data[$i]->retm_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>
                            <!--button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_id."'  onclick='stock_assign(this.id)' >view</button-->";
                            
                            
                        
                    if($data[$i]->retm_proceed==1){
                        if($data[$i]->retm_stetus==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        
                        $this->db->select('SUM(`to_bin`) as totbin');
                        $this->db->from('ret_to_stock');
                        $this->db->where('ret_main',$data[$i]->retm_id);
                        $hasill=$this->db->get();
                        $totbin= $hasill->result(); 
                            if($totbin[0]->totbin>0){$vw_btn .= "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_id."'  onclick='bin_assign(this.id)' >Bin</button>"; }
                        $vw_btn .= "<br><b>".$data[$i]->user_name."</b>";
                        }else{
                          
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                        
                    }else{
                        
                        if($data[$i]->retm_stetus==1){
                            
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        
                            
                        }else{
                            $this->db->select('user_name');
                        $this->db->from('mes_user');
                        $this->db->where('user_id',$data[$i]->retm_canuser);
                        $cusr=$this->db->get();
                        $canu= $cusr->result(); 
                            
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;
                            $vw_btn .= "<br><b>".$canu[0]->user_name."</b>";
                        }
                        
                    }
                    
                    if($inv_return[0]->po_id!=""){$poid = $inv_return[0]->po_id;}else{$poid="N/A";}
                    
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->retm_code.$data[$i]->retm_id."</td>
                                 <td >".$sup."<br><b>".$datanew[0]->user_fname." ".$datanew[0]->user_lname."</b></td>
                                 <td style='text-align: right;'>".$data[$i]->retm_totqty."</td>
                                 <td style='text-align: right;'>".number_format($data[$i]->retm_amount,2)."</td>
                                <td style='text-align: right;width:10%'>".$data[$i]->retm_cdate."</td>
                                $sts
                                <td>$vw_btn   </td>
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function showallretsearch(){
              if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
             
             
            $this->db->select('DISTINCT(`inv_return_main`.`retm_id`),`inv_return_main`.*,`mes_qto`.*,`mes_user`.*');
            $this->db->from('inv_return_main');
            $this->db->join( "mes_user", "mes_user.user_id  = inv_return_main.retm_puser", 'left');
            $this->db->join( "inv_return", "inv_return.ret_main_id  = inv_return_main.retm_id", 'left');
            $this->db->join( "mes_qto", "mes_qto.po_id  = inv_return.ret_invnum", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
            
              if($this->input->post('rep')!="0"){
                $this->db->where('retm_cuser', $this->input->post('rep'));
            } 
           if($this->input->post('cusid')!=""){
                $this->db->like('mes_customer.cust_company',$this->input->post('cusid'));
            }
            
             if($this->input->post('invid')!=""){
              $this->db->where('inv_return.ret_invnum', $this->input->post('invid'));
            }
            
            if($this->session->userdata['login_data']['userlevel'] == 20){
                $this->db->where('retm_cuser', $this->session->userdata['login_data']['user_id']);
            }
            
            $this->db->group_by("inv_return_main.retm_id");
            $this->db->order_by("inv_return_main.retm_stetus","desc");
            $this->db->order_by("inv_return_main.retm_proceed","asc");
            $this->db->order_by("retm_id","desc");
            $this->db->limit(100);
            
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
         
            $link = site_url('new_inv_ret');
            $link = "#";
           
             //$det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Return Note</button></a></br>";
             $det[0]="";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Return Note ID</th>
														<th>Customer</th>
														<th>Total Qty</th>
														<th>Value</th>
														<th style='width:10%'>Date</th>
													    <th>Status</th>
														<th>Action</th>                                
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                    
                        $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_main_id',$data[$i]->retm_id);
                        $hasil=$this->db->get();
                        $inv_return= $hasil->result(); 
                    
                    
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id', $inv_return[0]->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();
            
            if($hasil1->num_rows()>0){
                $sup = $datanew[0]->cust_company;
            }else{$sup = "Direct Invoice";}
                    
                   $vw_btn1 = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_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]->retm_id."'  onclick='stock_assign(this.id)' >Process</button>
                                 <!--a href ='".base_url()."pdf_invrtn_edit/".$data[$i]->retm_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' >Edit</button-->
                                <a href ='".base_url()."pdf_invrtn/".$data[$i]->retm_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>";
                                
                               
                    $vw_btn2 = "<a href ='".base_url()."pdf_invrtn/".$data[$i]->retm_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>
                            <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_id."'  onclick='stock_assign(this.id)' >view</button>";
                            
                            
                        
                    if($data[$i]->retm_proceed==1){
                        if($data[$i]->retm_stetus==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        
                        $this->db->select('SUM(`to_bin`) as totbin');
                        $this->db->from('ret_to_stock');
                        $this->db->where('ret_main',$data[$i]->retm_id);
                        $hasill=$this->db->get();
                        $totbin= $hasill->result(); 
                            if($totbin[0]->totbin>0){$vw_btn .= "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->retm_id."'  onclick='bin_assign(this.id)' >Bin</button>"; }
                        $vw_btn .= "<br><b>".$data[$i]->user_name."</b>";
                        }else{
                          
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                        
                    }else{
                        
                        if($data[$i]->retm_stetus==1){
                            
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        
                            
                        }else{
                            $this->db->select('user_name');
                        $this->db->from('mes_user');
                        $this->db->where('user_id',$data[$i]->retm_canuser);
                        $cusr=$this->db->get();
                        $canu= $cusr->result(); 
                            
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;
                            $vw_btn .= "<br><b>".$canu[0]->user_name."</b>";
                        }
                        
                    }
                    
                    if($inv_return[0]->po_id!=""){$poid = $inv_return[0]->po_id;}else{$poid="N/A";}
                    
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->retm_code.$data[$i]->retm_id."</td>
                                 <td >".$sup."<br><b>".$datanew[0]->user_fname." ".$datanew[0]->user_lname."</b></td>
                                 <td style='text-align: right;'>".$data[$i]->retm_totqty."</td>
                                 <td style='text-align: right;'>".number_format($data[$i]->retm_amount,2)."</td>
                                <td style='text-align: right;width:10%'>".$data[$i]->retm_cdate."</td>
                                $sts
                                <td>$vw_btn   </td>
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function showallret_old(){
              if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        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');
            if($this->session->userdata['login_data']['userlevel'] == 20){
                $this->db->where('ret_cuser', $this->session->userdata['login_data']['username']);
            }
            
            
            $this->db->order_by("inv_return.ret_stetus","desc");
            $this->db->order_by("inv_return.ret_proceed","asc");
            $this->db->order_by("ret_id","desc");
            
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
         
            $link = site_url('new_inv_ret');
           
            // $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Return Note</button></a></br>";
             $det[0]="";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														
														<th>Return Note ID</th>
														<th>Invoice ID</th>
														
												
														<th>Reason For Return</th>
														<th>Total Qty</th>
														<th>Value</th>
														<th style='width:10%'>Date</th>
													    <th>Status</th>
														<th>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();
            
            if($hasil1->num_rows()>0){
                $sup = $datanew[0]->cust_company;
            }else{$sup = "Direct Invoice";}
            
            
                    
                   $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='stock_assign(this.id)' >Process</button>
                                 <a href ='".base_url()."pdf_invrtn_edit/".$data[$i]->ret_id."'><button type='button' class='btn btn-outline-secondary btn-sm' type='button' >Edit</button>
                                <a href ='".base_url()."pdf_invrtn_old/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>";
                                
                               
                    $vw_btn2 = "<a href ='".base_url()."pdf_invrtn_old/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Print</button></a>
                            <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->ret_id."'  onclick='stock_assign(this.id)' >view</button>
                            <br><b>".$data[$i]->retm_canuser."</b>
                                
                                ";
                        
                    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;}
                        
                        
                    }
                    if($data[$i]->po_id!=""){$poid = $data[$i]->po_id;}else{$poid="N/A";}
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->ret_code.$data[$i]->ret_id."</td>
                                 <td >INV".$data[$i]->ret_invnum."<br>".$sup."<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);
            //echo json_encode($data);
        }
    
        function getproduct(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
                    $this->db->select('*');
                    $this->db->from('qto_items');
                    $this->db->where('po_aprvl',0);
                    $this->db->where('po_itm_po_id',$this->input->post('po_id'));
                    $hasil=$this->db->get();
                    $dataa= $hasil->result(); 
                    
                    
                    $det[0]="";
                    
                    for($i=0; $i<count($dataa); $i++){ 
                        
                        $data = $this->Common_model->single_item($dataa[$i]->po_itm_item_id);
                        
                        
                            $this->db->select('sum(return_items.ret_itm_qty) as rec,sum(return_items.free_item) as free');
                            $this->db->from('return_items');
                            $this->db->where('ret_inv_id',$this->input->post('po_id'));
                            $this->db->where('ret_aprvl ',1);
                            $this->db->where('ret_itm_item_id',$dataa[$i]->po_itm_item_id);
                            $ha=$this->db->get();
                            $rec= $ha->result();
                            
                        if($rec[0]->rec){
                            $rc=$rec[0]->rec; $rcfr=$rec[0]->free;
                        }else{
                            
                            $this->db->select('sum(mes_inventry.invt_qty) as rec');
                            $this->db->from('inv_return');
                            $this->db->where('ret_invnum',$this->input->post('po_id'));
                           // $this->db->join( "inv_return", "inv_return.ret_id = mes_grn.grn_id");
                            $this->db->join( "mes_inventry", "mes_inventry.invt_type_id = inv_return.ret_id");
                            $this->db->where('mes_inventry.invt_type',"INVRTN");
                            $this->db->where('mes_inventry.invt_proc',1);
                            $this->db->where('mes_inventry.invt_itemid',$dataa[$i]->po_itm_item_id);
                            $ha=$this->db->get();
                            $rec= $ha->result();
                                if($rec[0]->rec){$rc=$rec[0]->rec; $rcfr=0;
                                }else{$rc=0.00;$rcfr=0;}
                            
                        }    
                       
                        $rem = $dataa[$i]->po_itm_qty - $rc;
                        $remfree = $dataa[$i]->free_item - $rcfr;
                        
                       
                     $x = $i+1;
                         $qtywfree = $dataa[$i]->po_itm_qty+$dataa[$i]->free_item;
                        $dat =   "
                                 <tr>
                               <td >".$x."</td>
                               <td >".$data[0][0]."-".$data[0][7]."-".$data[0][1]."</td>
                               <td >".$data[0][2]."</td>
                               <td style='text-align: center;'>".$dataa[$i]->po_itm_unitprice."</td>
                                <td style='text-align: right;'>".$dataa[$i]->po_itm_qty."</td>
                                <td style='text-align: right;'>".$rc."</td>
                               <td><input type='text' id=".$dataa[$i]->po_itm_item_id." onClick='this.select();' name='po_val[]' onkeyup='cal_price()' value='0' class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;'>
                                <input type='hidden' id=''  name='po_tot[]' value=".number_format($rem, 2).">
                               </td>
                                <td><input type='text' id=".$dataa[$i]->po_itm_item_id."  name='po_prc[]' placeholder='' value='' class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;'>
                                <input type='hidden' id=''  name='itm_prc[]' value=".number_format($dataa[$i]->po_itm_unitprice, 2).">
                               </td>
                               <td style='text-align: right;'>".$dataa[$i]->free_item."</td>
                               <td style='text-align: right;'>".$rcfr."</td>
                               <td><input type='text' id=fr".$dataa[$i]->po_itm_item_id." onClick='this.select();' name='free_val[]' onkeyup='' value='0' class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;'>
                                <input type='hidden' id=''  name='free_tot[]' value=".number_format($remfree, 2).">
                               </td>
                                </tr>";
                                
                              $det[0] =$det[0].$dat;
                    
                    }
                
                    $this->db->select('po_discount');
                            $this->db->from('mes_qto');
                            $this->db->where('po_id',$this->input->post('po_id'));
                            $ha=$this->db->get();
                            $dis= $ha->result();
                            
                
                        $det[1] = $dis[0]->po_discount;
                        $det[2] = "N/A";
                    
        
                  echo json_encode($det);
        }
    
        function pogrnsave(){
             if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
            if($this->input->post('allret')!=0){
                $inv = $this->input->post('poidval');
                $this->db->select('ret_invnum');
                $this->db->from('inv_return');
                $this->db->where('ret_invnum',$inv);
                $hasil=$this->db->get();
                $data= $hasil->result();
                    if(count($data)>0){
                        $send = "Cannot return All. Return Note have already created";
                    }else{
                        $send = $this->all_return();
                    }
                
            }else{
               $send =  $this->part_return();
            }
            
            echo json_encode($send);
        }    
        
        function part_return(){
             $id = $this->input->post('ids');
            $qt = $this->input->post('values');
            $fr = $this->input->post('free_val');
            $po_prc = $this->input->post('po_prc');
            
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id',$this->input->post('poidval'));
            $hasil=$this->db->get();
            $data= $hasil->result();
           
           $tot = 0;
            $dat = array(
                        'ret_invnum' => $this->input->post('poidval'),
                        'ret_totqty'  => $tot,
                        'ret_amount' => $this->input->post('final'),
                        'ret_reason' => $this->input->post('inv'),
                        'ret_depo' => $data[0]->depo,
                        'ret_cuser'  => $this->session->userdata['login_data']['username'], 
                        'ret_cdate'  => date('Y-m-d')
                        
                    );
            $deet = $this->db->insert('inv_return',$dat);
           $insert_id = $this->db->insert_id();
           
            for($i=0; $i<count($id); $i++){
                if(($qt[$i]!="0")||($fr[$i]!="0")){
                    
                   $un_prc = $po_prc[$i]/$qt[$i];
                    
                    $dat = array(
                        'invt_type'  => "INVRTN",
                        'invt_type_id'  => $insert_id,
                        'invt_itemid'  => $id[$i],
                        'invt_qty' => $qt[$i]+$fr[$i],
                        'cal' => "plus",
                        'invt_cuser'  => $this->session->userdata['login_data']['username'],
                        'invt_cdate'  =>date('Y-m-d')
                        
                    );
                    $detta = $this->db->insert('mes_inventry',$dat);
                    
                    $dat = array(
                        'ret_itm_item_id'  => $id[$i],
                        'depo_id'  => $this->session->userdata['login_data']['depot'],
                        'ret_note_id'  => $insert_id,
                        'ret_inv_id'  => $this->input->post('poidval'),
                        'ret_itm_unitprice'  => $un_prc,
                        'ret_itm_qty' => $qt[$i],
                        'free_item' => $fr[$i],
                        'ret_aprvl' => "0",
                        'ret_user'  => $this->session->userdata['login_data']['username']
                        
                    );
                    $dett1 = $this->db->insert('return_items',$dat); 
                    $tot = $tot+$qt[$i]+$fr[$i]; 
                }
            }
           
            $this->db->set('ret_totqty', $tot);
            $this->db->where('ret_id', $insert_id);
            $result=$this->db->update('inv_return');
            
            $this->General->cal_credit_limit($data[0]->po_sup_id);
            return $dt = "Success";
        }
        
        function all_return(){
            
            $inv = $this->input->post('poidval');
            
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id',$inv);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
           
           $tot = 0;
            $dat = array(
                        'ret_invnum' => $this->input->post('poidval'),
                        'ret_totqty'  => $data[0]->po_tot_qty + $data[0]->po_tot_freqty,
                        'ret_amount' => $data[0]->po_final_tot,
                        'ret_reason' => $this->input->post('inv'),
                        'ret_depo' => $data[0]->depo,
                        'ret_cuser'  => $this->session->userdata['login_data']['username'], 
                        'ret_cdate'  => date('Y-m-d')
                        
                    );
            $dd = $this->db->insert('inv_return',$dat);
           $insert_id = $this->db->insert_id();
           
           $this->db->select('*');
            $this->db->from('qto_items');
            $this->db->where('po_itm_po_id',$inv);
            $hasil=$this->db->get();
            $inv_itm= $hasil->result();
           
           
            for($i=0; $i<count($inv_itm); $i++){
               
                    $dat = array(
                        'invt_type'  => "INVRTN",
                        'invt_type_id'  => $insert_id,
                        'invt_itemid'  => $inv_itm[$i]->po_itm_item_id,
                        'invt_qty' => $inv_itm[$i]->po_itm_qty + $inv_itm[$i]->free_item,
                        'cal' => "plus",
                        'invt_cuser'  => $this->session->userdata['login_data']['username'],
                        'invt_cdate'  =>date('Y-m-d')
                        
                    );
                    $det = $this->db->insert('mes_inventry',$dat);
                    
                    $dat = array(
                        'ret_itm_item_id'  => $inv_itm[$i]->po_itm_item_id,
                        'depo_id'  => $data[0]->depo,
                        'ret_note_id'  => $insert_id,
                        'ret_inv_id'  => $this->input->post('poidval'),
                        'ret_itm_qty' => $inv_itm[$i]->po_itm_qty,
                        'free_item' => $inv_itm[$i]->free_item,
                        'ret_aprvl' => "0",
                        'ret_user'  => $this->session->userdata['login_data']['username']
                        
                    );
                    $dett = $this->db->insert('return_items',$dat); 
                    
            }
           
                    $this->db->set('po_payment', "2");
                    $this->db->set('return_amnt', $data[0]->po_final_tot);
                    $this->db->where('po_id',$inv);
                    $this->db->update('mes_qto');
             
             $this->General->cal_credit_limit($data[0]->po_sup_id);       
            return $dt = "Success";
        }
        
        function check_all_ret($inv){
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_id',$inv);
            $hasil=$this->db->get();
            $data= $hasil->result();
            
            $this->db->select('sum(ret_totqty) as ret');
            $this->db->from('inv_return');
            $this->db->where('ret_invnum',$inv);
            $this->db->where('ret_stetus',1);
            $hasil=$this->db->get();
            $ret_data = $hasil->result();
            
            $inv_tot =  $data[0]->po_tot_qty + $data[0]->po_tot_freqty ;
            if($ret_data[0]->ret >=$inv_tot){
                $this->db->set('po_status', "0");
                    $this->db->where('po_id',$inv);
                    $this->db->update('mes_qto');
            }
            
            return;
            
        }
        
        function grn_auth($ret_id){
             if(!isset($this->session->userdata['login_data']['username']))    { 
                        echo json_encode("Unauthorized User");
                      //  redirect('Login_controller'); 
                        exit();
            }
            
           // $dt = explode("/",$this->input->post('id'));
         //  $id = $dt[0];
          $id = $ret_id;
            
            $this->db->select('*');
            $this->db->from('mes_inventry');
            $this->db->where('invt_type',"INVRTN");
            $this->db->where('invt_type_id',$id);
            $this->db->where('invt_proc',0);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            $this->db->select('*');
                    $this->db->from('inv_return');
                    $this->db->where('ret_id',$id);
                    $hasil=$this->db->get();
                    $dep= $hasil->result();
            
            for($i=0; $i<count($data); $i++){ 
                
                    $this->db->set('invt_proc', "1");
                    $this->db->where('invt_type',"INVRTN");
                    $this->db->where('invt_id',$data[$i]->invt_id);
                    $this->db->update('mes_inventry');
                    
                    $this->Common_model->cal_stock_only_stock($dep[0]->ret_depo,"INVRTN",$data[$i]->invt_id,$data[$i]->invt_itemid,$data[$i]->invt_qty,"plus","0");
            }
            
            
            $this->db->select('*');
                    $this->db->from('return_items');
                    $this->db->where('ret_note_id',$id);
                    $hasil=$this->db->get();
                    $de= $hasil->result();
            
            for($i=0; $i<count($de); $i++){
                    $this->db->set('ret_aprvl', "1");
                    $this->db->where('ret_itm_id',$de[$i]->ret_itm_id);
                    $this->db->update('return_items');
                    
            }
            
                    $this->db->set('ret_proceed', "1");
                    $this->db->set('ret_puser', $this->session->userdata['login_data']['username']);
                    $this->db->set('ret_pdate', date("Y-m-d H:i:s"));
                    $this->db->where('ret_id',$id);
                    $this->db->update('inv_return');
                   
                    $this->db->select('*');
                    $this->db->from('mes_qto');
                    $this->db->where('po_id',$dep[0]->ret_invnum);
                    $hasil=$this->db->get();
                    $inv= $hasil->result();
                    
                    $this->db->select('sum(pay_amount) as amnt');
                    $this->db->from('mes_payment');
                    $this->db->where('pay_invid',$dep[0]->ret_invnum);
                    $this->db->where('pay_stetus',1);
                    $this->db->where('pay_authrize',1);
                    $hasil=$this->db->get();
                    $paym= $hasil->result();
                    
                if($inv[0]->po_payment < 1){
                    $new_amnt = $inv[0]->return_amnt + $dep[0]->ret_amount; 
                    
                    $tot_inv_bal = $inv[0]->po_final_tot - ($new_amnt+$paym[0]->amnt);
                    
                    
                    $this->db->set('return_amnt', $new_amnt);
                    if($tot_inv_bal<1){
                        $this->db->set('po_payment', "1");
                    }
                    $this->db->where('po_id',$dep[0]->ret_invnum);
                    $this->db->update('mes_qto');
                    
                    if($tot_inv_bal<0){
                            $datt = array(
                            'rtn_inv' => $dep[0]->ret_invnum,
                            'tot_rtn'  => $dep[0]->ret_amount,
                            'rtn_excess' => abs($tot_inv_bal)
                            
                        );
                            $data = $this->db->insert('inv_return_excess',$datt);
                            
                            $this->Common_model->return_excess($dep[0]->ret_invnum,abs($tot_inv_bal),"plus"); 
                    }
                   
                    $this->db->set('ret_to_inv', "1");
                    $this->db->where('ret_id',$id);
                    $this->db->update('inv_return');
                   
                }elseif ($inv[0]->po_payment == 1){
                    
                    $datt = array(
                        'rtn_inv' => $dep[0]->ret_invnum,
                        'tot_rtn'  => $dep[0]->ret_amount,
                        'rtn_excess' => $dep[0]->ret_amount
                        
                    );
                        $data = $this->db->insert('inv_return_excess',$datt);
                        
                        $this->Common_model->return_excess($dep[0]->ret_invnum,$dep[0]->ret_amount,"plus");   
                        
                        $new_amnt = $inv[0]->return_amnt + $dep[0]->ret_amount; 
                    
                    $tot_inv_bal = $inv[0]->po_final_tot - ($new_amnt+$paym[0]->amnt);
                    
                    
                    $this->db->set('return_amnt', $new_amnt);
                    if($tot_inv_bal<1){
                        $this->db->set('po_payment', "1");
                    }
                    $this->db->where('po_id',$dep[0]->ret_invnum);
                    $this->db->update('mes_qto');
                        
                }  
                    $this->check_all_ret($de[0]->ret_inv_id);
                        $ret = "Return Data Proceed";
                    $this->General->cal_credit_limit($inv[0]->po_sup_id);
            
           // echo json_encode($ret);
           return true;
        }
        
        function rtn_cancel(){
            $id = $this->input->post('id');
            
            $this->db->set('retm_stetus', 0);
            $this->db->set('retm_canuser', $this->session->userdata['login_data']['user_id']);
            $this->db->set('retm_candate', date('Y-m-d'));
            $this->db->where('retm_id', $id);
            $result=$this->db->update('inv_return_main');
            
            $this->db->set('ret_stetus', 0);
            $this->db->where('ret_main_id', $id);
            $result=$this->db->update('inv_return');
            
           // $this->db->set('invt_proc', 0);
           // $this->db->where('invt_type_id', $id);
           // $result1=$this->db->update('mes_inventry');
            
            if($result){
                $send = "Return Note Cancelled";
            }else{
                $send = "Please Retry";
            }
            echo json_encode($send);
        }
        
        function view_pdf($insert_id){
                    
        $this->db->select("inv_return_main.*,inv_return.*,mes_qto.*,mes_customer.*,mes_user.*,mes_ret_reason.ret_reason as reasonreturn");    
        $this->db->from('inv_return_main');
        $this->db->where('retm_id', $insert_id );
        $this->db->join('inv_return', 'inv_return.ret_main_id   = inv_return_main.retm_id', 'left');
        $this->db->join('mes_ret_reason', 'inv_return.ret_reason   = mes_ret_reason.ret_reason_id', 'left');
        $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_customer.cust_salseperson', 'left');
        $query = $this->db->get();
        $rt_data=$query->result();
        
         $data['inv']=$rt_data;
            
        $this->db->select("*");    
        $this->db->from('inv_return');
        $this->db->where('ret_main_id', $insert_id );
        $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_customer.cust_salseperson', 'left');
        $this->db->join('mes_ret_reason', 'inv_return.ret_reason   = mes_ret_reason.ret_reason_id', 'left');
        $query1 = $this->db->get();
        $rtndata = $query1->result();
        
       
         for($i=0; $i<count($rtndata); $i++){
                $this->db->select("*");    
                $this->db->from('return_items');
                //$this->db->where('invt_type',"INVRTN");
                $this->db->where('ret_note_id',$rtndata[$i]->ret_id);
                $this->db->join( "mes_item", "mes_item.item_id = return_items.ret_itm_item_id", 'left');
                $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
                $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
                $this->db->join( "item", "mes_item.item_name = item.itm_id",'left');
                $query = $this->db->get();
                $dataa = $query->result();
             
             
            $dataa[0]->ret_inv_id;
                    $this->db->select("po_itm_unitprice");    
                $this->db->from('qto_items');
                $this->db->where('po_itm_po_id', $dataa[0]->ret_inv_id);
                $this->db->where('po_itm_batch', $dataa[0]->ret_item_batch);
                $query = $this->db->get();
                $invdta = $query->result();
                
                 $this->db->select("batch");    
                $this->db->from('batch');
                $this->db->where('batch_id', $dataa[0]->ret_item_batch);
                $query3 = $this->db->get();
                $batch = $query3->result();
                
                $inv_num = $rtndata[$i]->qto_code."".$rtndata[$i]->po_id;
                    
                    $inv[] = array($inv_num,$batch[0]->batch,
                    $dataa[0]->itm_name,$dataa[0]->unit_symble,$dataa[0]->ret_itm_qty,$invdta[0]->po_itm_unitprice,
                    $dataa[0]->free_item,$dataa[0]->ret_inv_id,$dataa[0]->ret_item_batch); 
                 
        }
        
        $data['inv_itm'] = $inv;
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/invreturnpdf',$data);
            
        }
        
        function view_pdf_old($insert_id){
                    
        $this->db->select("*");    
        $this->db->from('inv_return');
        $this->db->where('ret_id', $insert_id );
        $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $query = $this->db->get();
        $rt_data=$query->result();
            
        $this->db->select("*");    
        $this->db->from('inv_return');
        $this->db->where('ret_id', $insert_id );
        $this->db->join('mes_qto', 'inv_return.ret_invnum   = mes_qto.po_id', 'left');
        $this->db->join('mes_customer', 'mes_customer.cust_id   = mes_qto.po_sup_id', 'left');
        $this->db->join('mes_user', 'mes_user.user_id   = mes_customer.cust_salseperson', 'left');
        
        $this->db->join('mes_ret_reason', 'inv_return.ret_reason   = mes_ret_reason.ret_reason_id', 'left');
        
        $query = $this->db->get();
        $data['inv']=$query->result();
        
        $this->db->select("*");    
        $this->db->from('return_items');
        //$this->db->where('invt_type',"INVRTN");
        $this->db->where('ret_note_id',$insert_id);
        $this->db->join( "mes_item", "mes_item.item_id = return_items.ret_itm_item_id", 'left');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
        $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
        $this->db->join( "item", "mes_item.item_name = item.itm_id");
        $query = $this->db->get();
        $dataa = $query->result();
        
         for($i=0; $i<count($dataa); $i++){
            $dataa[$i]->ret_inv_id;
                    $this->db->select("po_itm_unitprice");    
                $this->db->from('qto_items');
                $this->db->where('po_itm_po_id', $dataa[$i]->ret_inv_id);
                $this->db->where('po_itm_item_id', $dataa[$i]->ret_itm_item_id);
                $query = $this->db->get();
                $invdta = $query->result();
             
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} 
                    if($dataa[$i]->model_name!=""){$spece1 = "  ";}else{$spece1 = "";}
                    //if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    //$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]->ret_itm_qty,$dataa[$i]->brand_shordcode,$invdta[0]->po_itm_unitprice,$dataa[$i]->free_item); 
                
        }
        $data['inv_itm'] = $inv;
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/invreturnpdfold',$data);
            
        }
        
        function invretn_stock_main(){
             $mid = $this->input->post('id');
             
              
                $det[0] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Batch</th>
														<th>Item</th>
														<th>To Stock</th>
														<th>To Bin</th>                                             
																											
													</tr>";
             
                $this->db->select('*');
                $this->db->from('inv_return');
                $this->db->where('ret_main_id',$mid);
                $hasil=$this->db->get();
                $ret= $hasil->result();
            
            $det[1]="";
                for($i=0; $i<count($ret); $i++){
                    $retdt = $this->invretn_stock($ret[$i]->ret_id);
                    $det[1] = $det[1].$retdt;
                }
            
            
            
            echo json_encode($det); 
        }
            
            
            
        function invretn_stock($rid){
           //$id = $this->input->post('id');
            $id = $rid;
            
            $this->db->select('*');
            $this->db->from('inv_return');
            $this->db->where('ret_id',$id);
            $hasil=$this->db->get();
            $ret= $hasil->result();
                
                $this->db->select('*');
                $this->db->from('return_items');
                $this->db->join('mes_item', 'return_items.ret_itm_item_id   = mes_item.item_id','left');
                $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                $this->db->join('batch', 'return_items.ret_item_batch   = batch.batch_id','left');
                $this->db->where('ret_note_id',$id);
                $hasil=$this->db->get();
                $ret_item= $hasil->result();
               
            
                
                 $det="";
                     
                     for($i=0; $i<count($ret_item); $i++){
                       $x = $i+1;
                       $tot_itm = $ret_item[$i]->ret_itm_qty + $ret_item[$i]->free_item;
                       
                        if($ret[0]->ret_to_stock_main < 1){
                                $stkk = "<input type='text' id=''  name='to_stk[]' value=".$tot_itm."> <input type='hidden' id='retstk_id'  name='retstk_id[]' value=".$ret_item[$i]->ret_itm_id.">";
                                $binn = "<input type='text' id=''  name='to_bin[]' value='0'>";
                        }else{
                                     $this->db->select('*');
                                    $this->db->from('ret_to_stock');
                                    $this->db->where('ret_item_row',$ret_item[$i]->ret_itm_id);
                                    $hasil=$this->db->get();
                                    $rett= $hasil->result();
                                $stkk =  $rett[0]->to_stock;
                                $binn = $rett[0]->to_bin;
                                    
                        }
                       
                        $dat =  "
                                     <tr scope='row'>
                                     <td >".$x."</td>
                                     <td >".$ret_item[$i]->batch."</td>
                                     <td >".$ret_item[$i]->itm_name."</td>
                                     <td style='text-align: right;'>".$stkk."</td>
                                     <td style='text-align: right;'>".$binn."</td> 
                                  
                                  </tr>";
                                  $det =$det.$dat;
                        
                    }
                     
               
                    
            return($det);
        }
        
        function invretn_bin_main(){
             $mid = $this->input->post('id');
             
              
                $det[0] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Batch</th>
														<th>Item</th>
														<th>Bin</th>
														<th>To Stock</th>                                             
																											
													</tr>";
             
                $this->db->select('*');
                $this->db->from('ret_to_stock');
                $this->db->where('ret_main',$mid);
                $hasil=$this->db->get();
                $ret= $hasil->result();
            
            $det[1]="";
                for($i=0; $i<count($ret); $i++){
                            $this->db->select('*');
                            $this->db->from('batch');
                            $this->db->join('mes_item', 'batch.product   = mes_item.item_id','left');
                            $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                            $this->db->where('batch_id',$ret[$i]->batch);
                            $hasil=$this->db->get();
                            $bin_item= $hasil->result();
                            
                            if($ret[$i]->to_bin>0){
                                $tot_itm = $ret[$i]->to_bin;
                                $x = $i+1;
                                $dat =  "
                                     <tr scope='row'>
                                     <td >".$x."</td>
                                     <td >".$bin_item[0]->batch."</td>
                                     <td >".$bin_item[0]->itm_name."</td>
                                     <td style='text-align: right;'><input type='hidden' id='binn'  name='binn[]' value=".$ret[$i]->to_bin.">".$ret[$i]->to_bin."</td>
                                     <td style='text-align: right;'><input type='hidden' id='rebin_id'  name='rebin_id[]' value=".$ret[$i]->to_stock_id."><input type='text' id=''  name='rebin_val[]' value='0'></td> 
                                  
                                  </tr>";
                                  $det[1] =$det[1].$dat;
                            }
                
                    
                }
            
            
            
            echo json_encode($det); 
        }
        
        function invretn_stock_old($rid){
           $id = $this->input->post('id');
            
            $this->db->select('*');
            $this->db->from('inv_return');
            $this->db->where('ret_id',$id);
            $hasil=$this->db->get();
            $ret= $hasil->result();
                
                $this->db->select('*');
                $this->db->from('return_items');
                $this->db->join('mes_item', 'return_items.ret_itm_item_id   = mes_item.item_id','left');
                $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                $this->db->join('batch', 'return_items.ret_item_batch   = batch.batch_id','left');
                $this->db->where('ret_note_id',$id);
                $hasil=$this->db->get();
                $ret_item= $hasil->result();
                
                $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Batch</th>
														<th>Item</th>
														<th>To Stock</th>
														<th>To Bin</th>                                             
																											
													</tr>";
            
                
                 $det[2]="";
                 
               
                     $det[0]="";
                     
                     for($i=0; $i<count($ret_item); $i++){
                       $x = $i+1;
                       $tot_itm = $ret_item[$i]->ret_itm_qty + $ret_item[$i]->free_item;
                       
                        if($ret[0]->ret_to_stock_main < 1){
                                $stkk = "<input type='text' id=''  name='to_stk[]' value=".$tot_itm."> <input type='hidden' id='retstk_id'  name='retstk_id[]' value=".$ret_item[$i]->ret_itm_id.">";
                                $binn = "<input type='text' id=''  name='to_bin[]' value='0'>";
                        }else{
                                     $this->db->select('*');
                                    $this->db->from('ret_to_stock');
                                    $this->db->where('ret_item_row',$ret_item[$i]->ret_itm_id);
                                    $hasil=$this->db->get();
                                    $rett= $hasil->result();
                                $stkk =  $rett[0]->to_stock;
                                $binn = $rett[0]->to_bin;
                                    
                        }
                       
                        $dat =  "
                                     <tr scope='row'>
                                     <td >".$x."</td>
                                     <td >".$ret_item[$i]->batch."</td>
                                     <td >".$ret_item[$i]->itm_name."</td>
                                     <td style='text-align: right;'>".$stkk."</td>
                                     <td style='text-align: right;'>".$binn."</td> 
                                  
                                  </tr>";
                                  $det[2] =$det[2].$dat;
                        
                    }
                     
                
                    $det[0] = "SAVE";
               
                    
            return($det);
        }
        
        function stock_update(){
            
            $id = $this->input->post('id');
            $j = count($id);
            $to_stock = $this->input->post('to_stock');
            $to_bin = $this->input->post('to_bin');
            
            
            
            for($i=0; $i<$j; $i++){
                
                
                $this->db->select('*');
                $this->db->from('return_items');
                $this->db->join('mes_item', 'return_items.ret_itm_item_id   = mes_item.item_id','left');
                $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                $this->db->join('batch', 'return_items.ret_item_batch   = batch.batch_id','left');
                $this->db->where('ret_itm_id',$id[$i]);
                $hasil=$this->db->get();
                $ret_item= $hasil->result();
                
                $tot_qty = $to_stock[$i]+$to_bin[$i]; $totrtn = $ret_item[0]->ret_itm_qty + $ret_item[0]->free_item;
                if($tot_qty!=$totrtn){
                    break; 
                    echo json_encode("Incorrect Quantities");
                }else{
                    
                    $this->db->select('*');
                                        $this->db->from('return_items');
                                        $this->db->where('ret_itm_id',$id[$i]);
                                        $hasil=$this->db->get();
                                        $ret_item_main= $hasil->result();
                      
                    $dat = array(
                        'ret_item_row' => $id[$i],
                        'ret_main'  => $ret_item_main[0]->ret_note_id,
                        'batch' =>  $ret_item[0]->batch_id,
                        'to_stock' => $to_stock[$i],
                        'to_bin' => $to_bin[$i],
                        'proc_by'  => $this->session->userdata['login_data']['user_id'], 
                        'date'  => date('Y-m-d')
                        
                    );
                        $deet = $this->db->insert('ret_to_stock',$dat);
                       $insert_id = $this->db->insert_id(); 
                       
                       
                       if($deet){
                           $this->db->select('depo');
                                        $this->db->from('mes_qto');
                                        $this->db->where('po_id',$ret_item[0]->ret_inv_id);
                                        $hasil=$this->db->get();
                                        $inv_depo= $hasil->result();
                                        
                          $stock = $this->Common_model->cal_stock($inv_depo[0]->depo,"INVRTNP",$id[$i],$ret_item[0]->ret_itm_item_id,$to_stock[$i],"plus",0,$ret_item[0]->batch_id);
                       
                            $this->db->set('ret_to_stock', 1);
                            $this->db->set('ret_to_stock_auth', $this->session->userdata['login_data']['user_id']);
                            $this->db->where('ret_itm_id', $id[$i]);
                            $result = $this->db->update('return_items');
                       
                                       $this->db->select('*');
                                        $this->db->from('return_items');
                                        $this->db->where('ret_itm_id',$id[$i]);
                                        $hasil=$this->db->get();
                                        $ret_item_main= $hasil->result();
                            
                                    $this->db->select('*');
                                    $this->db->from('return_items');
                                    $this->db->where('ret_note_id',$ret_item_main[0]->ret_note_id);
                                    $this->db->where('ret_to_stock>',0);
                                    $hasil=$this->db->get();
                                    $ret_item= $hasil->result();
                                    $auth_count = $hasil->num_rows();
                                    
                                    $this->db->select('*');
                                    $this->db->from('return_items');
                                    $this->db->where('ret_note_id',$ret_item_main[0]->ret_note_id);
                                    $hasill=$this->db->get();
                                    $all_item= $hasill->result();
                                    $all_count = $hasill->num_rows();
                                    
                                    if($auth_count = $all_count){
                                        $this->db->set('ret_to_stock_main', 1);
                                        $this->db->where('ret_id', $ret_item_main[0]->ret_note_id);
                                        $result = $this->db->update('inv_return');
                                        
                                        if($result){
                                           $this->grn_auth($ret_item_main[0]->ret_note_id);
                                        }
                                        
                                        $msg = "Stock Updated. Return Note Processed";
                                    }else{
                                        $msg = "Incorrect Quantities";
                                    }
                           
                           
                       }
                      
                      
                }
                
            }
            
                
                
                $this->db->select('ret_main_id');
                $this->db->from('inv_return');
                $this->db->where('ret_id',$ret_item_main[0]->ret_note_id);
                $hasil=$this->db->get();
                $main_ret_item_id= $hasil->result();
                
                $this->db->select('*');
                $this->db->from('inv_return');
                $this->db->where('ret_main_id',$main_ret_item_id[0]->ret_main_id);
                $hasill=$this->db->get();
                $ret_item= $hasill->result();
                $all_maincount = $hasill->num_rows();
                
                $this->db->select('*');
                $this->db->from('inv_return');
                $this->db->where('ret_main_id',$main_ret_item_id[0]->ret_main_id);
                $this->db->where('ret_to_stock_main>',0);
                $hasill=$this->db->get();
                $ret_item= $hasill->result();
                $all_authmaincount = $hasill->num_rows();
                
                if($all_authmaincount=$all_maincount){
                    $this->db->set('retm_proceed', 1);
                    $this->db->set('retm_puser', $this->session->userdata['login_data']['user_id']);
                    $this->db->set('retm_pdate', date('Y-m-d'));
                    $this->db->where('retm_id', $main_ret_item_id[0]->ret_main_id);
                    $result = $this->db->update('inv_return_main');
                }
                
                
            echo json_encode($msg);    
        }
        
        
        function invretn_bin_updt(){
            
            $id = $this->input->post('id');
            $j = count($id);
            $to_stock = $this->input->post('to_stock');
            
            for($i=0; $i<$j; $i++){
                
                $this->db->select('*');
                $this->db->from('ret_to_stock');
                $this->db->join('batch', 'ret_to_stock.batch   = batch.batch_id','left');
                $this->db->where('to_stock_id',$id[$i]);
                $hasil=$this->db->get();
                $ret_item= $hasil->result();
                
                if($to_stock[$i] > $ret_item[0]->to_bin){
                    echo json_encode("Incorrect Quantities");
                }else{
                    
                    $newstk = $to_stock[$i] + $ret_item[0]->to_stock;
                    $newbin =  ($ret_item[0]->to_bin - $to_stock[$i]); 
                    
                                    $this->db->set('to_stock', $newstk);
                                    $this->db->set('to_bin', $newbin);
                                    $this->db->set('bin_auth', 1);
                                        $this->db->where('to_stock_id', $id[$i]);
                                        $result = $this->db->update('ret_to_stock');
                                        
                    $this->db->select('*');
                    $this->db->from('return_items');
                    $this->db->join('mes_qto', 'mes_qto.po_id   = return_items.ret_inv_id','left');
                    $this->db->where('ret_itm_id',$ret_item[0]->ret_item_row);
                    $hasil=$this->db->get();
                    $ret_dep= $hasil->result();                    
                                        
                                        
                        $stock = $this->Common_model->cal_stock($ret_dep[0]->depo,"BinToStock",$id[$i],$ret_item[0]->product,$to_stock[$i],"plus",0,$ret_item[0]->batch_id);
                       $msg = "done";
                }
                
               
                
            }
            
                
                
            echo json_encode($msg);    
        }
        
        
        
        
}        