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

class Creditreturn_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();
           // }
        
        }
        
        function crd_all(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
            $data['ref'] = $this->Product_model->salserep_list();
            
             $data['content'] = "pages/all_credit_note";        
             $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_credit_main');
            $this->db->join( "mes_user", "mes_user.user_id  = inv_return_credit_main.retm_cuser", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id   = inv_return_credit_main.cust_id", 'left');
           // $this->db->join('mes_user', 'mes_user.user_id   = mes_customer.cust_salseperson', '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_credit_main.retm_stetus","desc");
            $this->db->order_by("inv_return_credit_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]="";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														<th>Credit 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++){
                    

            
           $sup = $data[$i]->cust_company; 
                    
                     if($this->session->userdata['login_data']['userlevel']< 2 || $this->session->userdata['login_data']['userlevel']==9){
                         
                                     $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='rtn_proceed(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_invcrd/".$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_invcrd/".$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='invcrd_proceed(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>".$data[$i]->user_fname." ".$data[$i]->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 view_pdf($insert_id){
                    
        $this->db->select("*");    
        $this->db->from('inv_return_credit_main');
        $this->db->where('retm_id', $insert_id );
        $this->db->join( "mes_user", "mes_user.user_id  = inv_return_credit_main.retm_puser", 'left');
        $this->db->join( "mes_customer", "mes_customer.cust_id   = inv_return_credit_main.cust_id", 'left');
        $query = $this->db->get();
        $rt_data=$query->result();
        
         $data['inv']=$rt_data;
            
        $this->db->select("*");    
                $this->db->from('credit_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 = credit_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_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();
                $rtndata = $query->result();
        
       
         for($i=0; $i<count($rtndata); $i++){
             
             
            $rtndata[$i]->ret_inv_id;
                    $this->db->select("po_itm_unitprice");    
                $this->db->from('qto_items');
                $this->db->where('po_itm_po_id', $rtndata[$i]->ret_inv_id);
                $this->db->where('po_itm_batch', $rtndata[$i]->ret_item_batch); 
                $query = $this->db->get();
                $invdta = $query->result();
                
                 $this->db->select("batch");    
                $this->db->from('batch');
                $this->db->where('batch_id', $rtndata[$i]->ret_item_batch);
                $query3 = $this->db->get();
                $batch = $query3->result();
                
                $inv_num = "0";
                    
                    $inv[] = array($inv_num,$batch[0]->batch,
                    $rtndata[$i]->itm_name,$rtndata[$i]->unit_symble,$rtndata[$i]->ret_itm_qty,$rtndata[$i]->ret_itm_unitprice,
                    $rtndata[$i]->free_item,$rtndata[$i]->ret_inv_id,$rtndata[$i]->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/invreturncrdpdf',$data);
            
        }
        
        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_no_inventry($dep[0]->ret_depo,"INVRTN",$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_credit_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 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 invcrd_proceed(){
            $id = $this->input->post('id');
            
            $this->db->select('*');
            $this->db->from('inv_return_credit_main');
            $this->db->join( "mes_user", "mes_user.user_id  = inv_return_credit_main.retm_cuser", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id   = inv_return_credit_main.cust_id", 'left');
            $this->db->where('retm_id',$id); 
            $hasil=$this->db->get();
                $ret_main= $hasil->result();
            
            
            $this->db->select('*');
                $this->db->from('credit_return_items');
                $this->db->join('mes_item', 'credit_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', 'credit_return_items.ret_item_batch   = batch.batch_id','left');
                $this->db->where('ret_note_id',$id); 
                $hasil=$this->db->get();
                $ret_item= $hasil->result();
            
             for($i=0; $i<count($ret_item); $i++){
                 
                 if($ret_main[0]->retm_depo!=0){
                      $this->db->select("*");    
                            $this->db->from('mes_depot_stock');
                            $this->db->where('dpstock_itemid', $ret_item[$i]->ret_itm_item_id);
                            $this->db->where('dpstock_dpoid', $ret_main[0]->retm_depo);
                            $query = $this->db->get();
                            $sdata = $query->result();
                    if($query->num_rows()<1){
                          $dat = array(
                                        'dpstock_itemid'  => $ret_item[$i]->ret_itm_item_id,
                                        'dpstock_dpoid'  => $ret_main[0]->retm_depo,
                                        'dpstock_qty'  => 0
                                    );
                                    $data = $this->db->insert('mes_depot_stock',$dat);
                                    
                                                                $this->db->select("*");    
                                                                $this->db->from('batch');
                                                                $this->db->where('batch_id', $ret_item[$i]->ret_item_batch);
                                                                $query = $this->db->get();
                                                                $datab = $query->result(); 
                                    
                                                                $dat = array(
                                                                        'batch_id'  => $ret_item[$i]->ret_item_batch,
                                                                        'batch'  => $datab[0]->batch,
                                                                        'depo'  => $ret_main[0]->retm_depo,
                                                                        'product'  => $datab[0]->product,
                                                                        'tot_qty'  => 0,
                                                                        'avl_qty'  => 0,
                                                                        'buying_price'  => $datab[0]->buying_price,
                                                                        'selling_price'  => $datab[0]->selling_price,
                                                                        'retail_price'  => $datab[0]->retail_price,
                                                                        'man_date'  => $datab[0]->man_date,
                                                                        'exp_date'  => $datab[0]->exp_date
                                                                    );
                                                                    
                                                                    $this->db->insert('batch_depo',$dat);     
                                    
                    }
                 }
                 
                 
                 $stock = $this->Common_model->cal_stock_no_inventry($ret_main[0]->retm_depo,"CREDIT",$id,$ret_item[$i]->ret_itm_item_id,$ret_item[$i]->ret_itm_qty,"plus",0,$ret_item[$i]->batch_id);
                       
            }
            
            $this->db->select('*');
            $this->db->from('mes_inventry');
            $this->db->where('invt_type',"CREDIT");
            $this->db->where('invt_type_id',$id);
            $this->db->where('invt_proc',0);
            $hasil=$this->db->get();
            $datainv= $hasil->result(); 
            
        
            
            for($i=0; $i<count($datainv); $i++){ 
                    $this->db->set('invt_proc', "1");
                    $this->db->where('invt_type',"CREDIT");
                    $this->db->where('invt_id',$datainv[$i]->invt_id);
                    $this->db->update('mes_inventry');
                    
                   // $this->Common_model->cal_stock_no_inventry($dep[0]->ret_depo,"INVRTN",$id,$data[$i]->invt_itemid,$data[$i]->invt_qty,"plus","0");
            }
            
            $datt = array(
                            'rtn_inv' => $id,
                            'tot_rtn'  => $ret_main[0]->retm_amount,
                            'rtn_excess' => $ret_main[0]->retm_amount
                            
                        );
                            $data = $this->db->insert('inv_return_excess',$datt);
                            
                            $this->Common_model->return_excess_cust($ret_main[0]->cust_id,$ret_main[0]->retm_amount,"plus"); 
                            
            
                    $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', $id);
                    $result = $this->db->update('inv_return_credit_main');
             
                
                
            echo json_encode("Done");    
            
            
        }
        
        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,"return",$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_credit_main');
                }
                
                
            echo json_encode($msg);    
        }
        
        
        

    
    
}        