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

class Return_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('logout'); 
                        exit();
            }
        }
        
        function test(){
            $plain_text = 'This is a plain-text message!';
            echo $ciphertext = $this->encryption->encrypt($plain_text);
            echo "</br>";
             echo $this->encryption->decrypt($ciphertext);
        }
        
        function index(){
            if($this->session->userdata['login_data']['depot'] != 0){
                $data['content'] = "pages/allret";        
                $this->load->view('template',$data); 
            }else{
                
                
                
                    $this->db->from('mes_depot');
                    $this->db->where('dep_stetus',1);
                   // $this->db->where('grn_proceed',1);
                    $hasil=$this->db->get();
                    $resultdepot= $hasil->result(); 
                $data['depotdetail'] = $resultdepot;
           
                    $this->db->from('mes_grn');
                    $this->db->where('grn_stetus',1);
                   // $this->db->where('grn_proceed',1);
                    $hasil=$this->db->get();
                    $resultagent= $hasil->result(); 
            $data['agentdetail'] = $resultagent;
            
            $data['itemdetail'] =  $this->Product_model->batch_list();
            
                    $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/newreturn_single";        
            $this->load->view('template',$data);
            }
        }
        
        
        function getdepotbatch(){
            
           $poid =  $this->input->post('id');
            
            if($poid< 1){
                 $this->db->select('*');
                $this->db->from('batch');
                $this->db->join( "mes_item", "mes_item.item_id = batch.product", 'left');
                $this->db->join( "item", "mes_item.item_name = item.itm_id");
                $this->db->where('avl_qty!=',0);
                $hasil=$this->db->get();
                $det= $hasil->result(); 
            }else{
                 $this->db->select('*');
            $this->db->from('batch_depo');
            $this->db->join( "mes_item", "mes_item.item_id = batch_depo.product", 'left');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->where('depo', $poid);
            $this->db->where('avl_qty!=',0);
            $hasil=$this->db->get();
            $det= $hasil->result(); 
            }
            
            
           // $det = $sup[0]->sup_company;
            
            
            echo json_encode($det);
            
            
            
        }
        
        function index_old(){
            
                    $this->db->from('mes_grn');
                    $this->db->where('grn_stetus',1);
                   // $this->db->where('grn_proceed',1);
                    $hasil=$this->db->get();
                    $resultagent= $hasil->result(); 
            $data['agentdetail'] = $resultagent;
            
                    $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/newreturn";        
            $this->load->view('template',$data);
        }
        
        function getproduct(){
                    $this->db->select('*');
                    $this->db->from('mes_grn');
                    $this->db->where('grn_stetus',1);
                    //$this->db->where('grn_proceed',1);
                    $this->db->where('grn_id',$this->input->post('po_id'));
                    $hasil=$this->db->get();
                    $resultagent= $hasil->result(); 
                    
                    $this->db->select('*');
                    $this->db->from('mes_inventry');
                        $this->db->where('invt_type',"GRN");
                        $this->db->where('invt_type_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]->invt_itemid);
                       
                        $this->db->select('sum(mes_inventry.invt_qty) as rec');
                        $this->db->from('mes_grn');
                        $this->db->where('grn_id',$this->input->post('po_id'));
                        $this->db->join( "mes_return", "mes_return.ret_grnid = mes_grn.grn_id");
                        $this->db->join( "mes_inventry", "mes_inventry.invt_type_id = mes_return.ret_id");
                        $this->db->where('mes_inventry.invt_type',"RTN");
                        $this->db->where('mes_inventry.invt_itemid',$dataa[$i]->invt_itemid);
                        $ha=$this->db->get();
                        $rec= $ha->result();
                        
                        
                        if($rec[0]->rec){$rc=$rec[0]->rec;}else{$rc=0.00;}
                        $rem = $dataa[$i]->invt_qty - $rc;
                       
                     $x = $i+1;
                         
                        $dat =  "
                                 <tr>
                               <td >".$x."</td>
                               <td >".$data[0][0]."-".$data[0][1]."</td>
                               <td >".$data[0][2]."</td>
                               <td style='text-align: center;'>".$data[0][3]."</td>
                                <td style='text-align: right;'>".$dataa[$i]->invt_qty."</td>
                                <td style='text-align: right;'>".$rc."</td>
                               <td><input type='text' id=".$dataa[$i]->invt_itemid."  name='po_val[]' 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='po_tot[]' value=".number_format($rem, 2).">
                               </td>
                              
                               
                             <!--td style='text-align: right;'>
                                <div class='hidden-sm hidden-xs btn-group'>
                                 <button class='btn btn-outline-danger btn-sm' name='tax' id=".$data[0][6]." onclick ='del_data(this.id);'>X</button>
                   
                               </div>
                                </td-->
                                </tr>";
                                
                              $det[0] =$det[0].$dat;
                    
                    }
                    
                    if($resultagent[0]->grn_poidcode) {
                        $poid = $resultagent[0]->grn_poidcode;
                        $det[1] = $resultagent[0]->grn_poid;
                            $this->db->select('*');
                          $this->db->from('mes_po');
                          $this->db->join( "mes_suppliers", "mes_suppliers.sup_id = mes_po.po_sup_id", 'inner');
                          $this->db->where('po_id', $poid);
                          $hasil=$this->db->get();
                          $sup= $hasil->result(); 
                          $det[2] = $sup[0]->sup_company;
                    }else{
                        $det[1] = "N/A";
                        $det[2] = "N/A";
                    }
        
                  echo json_encode($det);
        }
        
        function pogrnsave_old(){
            $id = $this->input->post('ids');
            $qt = $this->input->post('values');
           
           $tot = 0;
            $dat = array(
                        'ret_grn'  => $this->input->post('poid'),
                        'ret_invnum' => $this->input->post('inv'),
                        'ret_donum' => $this->input->post('podo'),
                        'ret_grnid' => $this->input->post('poidval'),
                        'ret_totqty'  => $tot,
                        'ret_cuser'  => "admin", 
                        'ret_cdate'  => date('Y-m-d')
                        
                    );
            $data = $this->db->insert('mes_return',$dat);
           $insert_id = $this->db->insert_id();
           
            for($i=0; $i<count($id); $i++){
                if($qt[$i]!=""){
                    $dat = array(
                        'invt_type'  => "RTN",
                        'invt_type_id'  => $insert_id,
                        'invt_itemid'  => $id[$i],
                        'invt_qty' => $qt[$i],
                        'invt_cuser'  => "admin",
                        'invt_cdate'  =>date('Y-m-d')
                        
                    );
                    $data = $this->db->insert('mes_inventry',$dat);
                    $tot = $tot+$qt[$i];
                }
            }
           
            $this->db->set('ret_totqty', $tot);
            $this->db->where('ret_id', $insert_id);
            $result=$this->db->update('mes_return');
           
      
            $send = "New GRN Created";
            echo json_encode($send);
            
        }
        
        function pogrnsave(){
           
           $tot = $depo = 0;
            $dat = array(
                        'ret_resaon' => $this->input->post('inv'),
                        'ret_totqty'  => 0,
                        'ret_depo'  => 0,
                        'ret_cuser'  => $this->session->userdata['login_data']['user_id'], 
                        'ret_cdate'  => date('Y-m-d')
                        
                    );
           $data = $this->db->insert('mes_return',$dat);
           $insert_id = $this->db->insert_id();
           
           
            $this->db->select('*');
            $this->db->from('temp_rtn_itms');
            $this->db->where('temp_rtn_user',$this->session->userdata['login_data']['user_id']);
            $hasil=$this->db->get();
            $id= $hasil->result(); 
           
            for($i=0; $i<count($id); $i++){
               
                    $dat = array(
                        'stock_rtn_item'  => $id[$i]->temp_rtn_item,
                        'stock_rtn_batch'  => $id[$i]->temp_rtn_batch,
                        'stock_trn_id'  => $insert_id,
                        'stock_rtn_depo'  => $id[$i]->temp_rtn_depo,
                        'stock_rtn_itmprice'  => $id[$i]->temp_rtn_itmprice,
                        'stock_rtn_qty' => $id[$i]->temp_rtn_qty,
                        'stock_rtn_uniqid'  => $id[$i]->temp_rtn_uniqid,
                        'stock_rtn_user'  =>$this->session->userdata['login_data']['user_id'],
                        'stock_rtn_date'  =>date('Y-m-d')
                        
                    );
                    $this->db->insert('stock_return_items',$dat);
                    $tot = $tot+$id[$i]->temp_rtn_qty;
                    $depo = $id[$i]->temp_rtn_depo;
                
                $this->db->where('temp_rtn_auid', $id[$i]->temp_rtn_auid);
                $this->db->where('temp_rtn_user', $this->session->userdata['login_data']['user_id']);
                $del=$this->db->delete('temp_rtn_itms');
            }
           
            $this->db->set('ret_totqty', $tot);
            $this->db->set('ret_depo', $depo);
            $this->db->where('ret_id', $insert_id);
            $result=$this->db->update('mes_return');
      
            $send = "New Return Created";
            echo json_encode($send);
            
        }
        
        function grn_all(){
            
             $data['content'] = "pages/allret";        
             $this->load->view('template',$data); 
        }
        
        function showallgrn(){
             
            $this->db->select('*');
            $this->db->from('mes_return');
             $this->db->join( "mes_grn", "mes_grn.grn_id = mes_return.ret_grnid", 'left');
             $this->db->join( "mes_ret_reason", "mes_ret_reason.ret_reason_id = mes_return.ret_resaon", 'left');
            $this->db->order_by("ret_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
         
            $link = site_url('new_ret');
           
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create Stock Deduction Note</button></a></br>";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														
														<th>Return Note Number</th>
														<th>GRN Number</th>
														
														<th>PO Number</th>
														<th>Supplier</th>
														<th>Reason For Return</th>
														<th>Total Qty</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_po');
            $this->db->where('po_id', $data[$i]->grn_poidcode);
            $this->db->join( "mes_suppliers", "mes_suppliers.sup_id = mes_po.po_sup_id", 'inner');
            $hasil1=$this->db->get();
            $datanew= $hasil1->result();
            
            if($hasil1->num_rows()>0){
                $sup = $datanew[0]->sup_company;
            }else{$sup = "Direct GRN";}
            
            if($this->session->userdata['login_data']['userlevel']<11 ){
                
                $vw_btn1 = "
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->ret_id."' name='edtbutton' onclick='po_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]->ret_grnid."'  onclick='po_proceed(this.id)' >Process</button>
                                <a href ='".base_url()."pdf_trn/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                ";
                
            }else{$vw_btn1 = "";}
                    
                   
                    $vw_btn2 = "<a href ='".base_url()."pdf_trn/".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                <!--a href ='".base_url()."Return_controller/view_regrn/?return=".$data[$i]->ret_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >ReGRN</button></a-->
                                
                                ";
                        
                    if($data[$i]->ret_proceed==1){
                        if($data[$i]->ret_stetus==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                    }else{
                        if($data[$i]->ret_stetus==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                        
                    }
                    if($data[$i]->grn_poid!=""){$poid = $data[$i]->grn_poid;}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 >".$data[$i]->ret_grn."</td>
                                 <td>".$poid."</td>
                                 <td>".$sup."</td>
                                 <td>".$data[$i]->ret_reason."</td>
                                 <td style='text-align: right;'>".$data[$i]->ret_totqty."</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 view_pdf($insert_id){
                    
        $this->db->select("*");    
        $this->db->from('mes_return');
        $this->db->where('ret_id', $insert_id);
        $this->db->join('mes_grn', 'mes_return.ret_grnid   = mes_grn.grn_id', 'left');
        
        $query = $this->db->get();
        $rt_data=$query->result();
            
        $this->db->select("*");    
        $this->db->from('mes_return');
        $this->db->where('ret_id', $insert_id );
        $this->db->join('mes_grn', 'mes_return.ret_grnid   = mes_grn.grn_id', 'left');
        
        if((isset($rt_data[0]->grn_poidcode)) && ($rt_data[0]->grn_poidcode=0)){
         $this->db->join('mes_po', 'mes_po.po_id  = mes_grn.grn_poidcode');   
         $this->db->join('mes_suppliers', 'mes_po.po_sup_id  = mes_suppliers.sup_id');
        $this->db->join('mes_country', 'mes_suppliers.sup_country   = mes_country.cnty_id');
        }
        
        $this->db->join('mes_ret_reason', 'mes_return.ret_resaon   = mes_ret_reason.ret_reason_id');
        $this->db->join( "mes_user", "mes_user.user_id = mes_return.ret_cuser", 'left');
        $query = $this->db->get();
        $data['inv']=$query->result();
        
        $this->db->select("*");    
        $this->db->from('stock_return_items');
        $this->db->where('stock_trn_id',$insert_id);
        //$this->db->where('invt_type_id',$insert_id);
        $this->db->join( "batch", "stock_return_items.stock_rtn_batch = batch.batch_id", 'left');
        $this->db->join( "mes_item", "mes_item.item_id = batch.product", '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');
        $this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'left');
        $this->db->join( "mes_depot", "mes_depot.dep_id = stock_return_items.stock_rtn_depo", 'left');
        $query = $this->db->get();
        $dataa = $query->result();
        
         for($i=0; $i<count($dataa); $i++){
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} 
                    if($dataa[$i]->model_name!=""){$spece1 = "  ";}else{$spece1 = "";}
                    //if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$dataa[$i]->itm_name,
                    $dataa[$i]->unit_symble,$dataa[$i]->stock_rtn_qty,$dataa[$i]->brand_shordcode,$dataa[$i]->stock_rtn_item,$dataa[$i]->batch,$dataa[$i]->dep_name); 
                
        }
        $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/returnpdf',$data);
            
        }
        
        function grn_auth(){
            $dt = explode("/",$this->input->post('id'));
            $id = $dt[0];
            
            $this->db->select('*');
            $this->db->from('stock_return_items');
            $this->db->where('stock_trn_id',$id);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
                    $this->db->set('ret_proceed', "1");
                    $this->db->where('ret_id',$id);
                   $this->db->update('mes_return');
                    
                    
                        $ret = "Return Data Proceed";
            echo json_encode($ret);
        }
        
        function view_regrn(){
            $enret_id=$_GET['return'];
                //$ret_id = $this->encryption->decrypt($enret_id);
                $ret_id = $enret_id;
                   $this->db->select('*');
                    $this->db->from('mes_inventry');
                        $this->db->where('invt_type',"RTN");
                        $this->db->where('invt_type_id',$ret_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]->invt_itemid);
                       
                        $this->db->select('sum(invt_qty) as trtn');
                        $this->db->from('mes_inventry');
                        $this->db->where('invt_type',"RGR");
                        $this->db->where('invt_type_id',$ret_id);
                        $this->db->where('invt_itemid',$dataa[$i]->invt_itemid);
                        $hasill=$this->db->get();
                        $pre= $hasill->result();
                       
                     $x = $i+1;
                         $bal = $dataa[$i]->invt_qty-$pre[0]->trtn;
                        $dat =  "
                                 <tr>
                               <td >".$x."</td>
                               <td >".$data[0][0]."-".$data[0][1]."</td>
                               <td >".$data[0][2]."</td>
                               <td style='text-align: center;'>".$data[0][3]."</td>
                                <td style='text-align: right;'>".$dataa[$i]->invt_qty."</td>
                                <td style='text-align: right;'>".$pre[0]->trtn."</td>
                               <td><input type='text' id=".$dataa[$i]->invt_itemid."  name='po_val[]' 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='po_tot[]' value=".number_format($bal, 2).">
                               </td>
                             <!--td style='text-align: right;'>
                                <div class='hidden-sm hidden-xs btn-group'>
                                 <button class='btn btn-outline-danger btn-sm' name='tax' id=".$data[0][6]." onclick ='del_data(this.id);'>X</button>
                   
                               </div>
                                </td-->
                                </tr>";
                                
                              $det[0] =$det[0].$dat;
                    
                    }
                    
                    $this->db->select("*");    
        $this->db->from('mes_return');
        $this->db->where('ret_id', $ret_id);
        $this->db->join('mes_grn', 'mes_return.ret_grnid   = mes_grn.grn_id');
        $query = $this->db->get();
        $resultagent=$query->result();
                    
                    if($resultagent[0]->grn_poidcode) {
                        $poid = $resultagent[0]->grn_poidcode;
                        $det[1] = $resultagent[0]->grn_poid;
                            $this->db->select('*');
                          $this->db->from('mes_po');
                          $this->db->join( "mes_suppliers", "mes_suppliers.sup_id = mes_po.po_sup_id", 'inner');
                          $this->db->where('po_id', $poid);
                          $hasil=$this->db->get();
                          $sup= $hasil->result(); 
                         $det[2] = $sup[0]->sup_company;  
                    }else{
                        $det[1] = "N/A";
                        $det[2] = "N/A";
                    }
                    $det[3] = $resultagent[0]->ret_grn;
                    $det[4] = $ret_id;
        
        $data['det'] = $det;  
        $data['content'] = "pages/regrn";        
            $this->load->view('template',$data);
           
                    
        
        }
        
        function save_rgrn(){
             $id = $this->input->post('ids');
            $qt = $this->input->post('values');
           
           $tot = 0;
           
           $insert_id = $this->input->post('ret_id');
           
            for($i=0; $i<count($id); $i++){
                if($qt[$i]!=""){
                    $dat = array(
                        'invt_type'  => "RGR",
                        'invt_type_id'  => $insert_id,
                        'invt_itemid'  => $id[$i],
                        'invt_qty' => $qt[$i],
                        'invt_cuser'  => "admin",
                        'invt_cdate'  =>date('Y-m-d')
                        
                    );
                    $data = $this->db->insert('mes_inventry',$dat);
                    $tot = $tot+$qt[$i];
                    
                    $this->db->select('*');
                    $this->db->from('mes_stock');
                    $this->db->where('stc_itemid',$id[$i]);
                    $hasil=$this->db->get();
                    $stk= $hasil->result();
                    
                    $nw = $stk[0]->stc_actqty+$qt[$i];
                     $nw1 = $stk[0]->stc_qty+$qt[$i];
                     
                    $this->db->set('stc_actqty', $nw);
                    $this->db->set('stc_qty', $nw1);
                    $this->db->where('stc_id',$stk[0]->stc_id);
                    $this->db->update('mes_stock');
                    
                }
            }
           
            $this->db->set('ret_regrn', $tot);
            $this->db->where('ret_id', $insert_id);
            $result=$this->db->update('mes_return');
            
            
           
      
            $send = "New ReGRN Created";
            echo json_encode($send);
        }
        
        function cancel_grn(){
            
            $id = $this->input->post('id');
            
            $this->db->select('*');
            $this->db->from('stock_return_items');
            $this->db->where('stock_trn_id',$id);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            for($i=0; $i<count($data); $i++){
                    $this->db->set('stock_trn_stetus', "0");
                    $this->db->where('stock_trn_id',$id);
                    $this->db->where('stock_rtn_auid',$data[$i]->stock_rtn_auid);
                    $this->db->update('stock_return_items');
                    
                    //$this->Common_model->cal_stock_no_inventry(0,"RTNcan",0,$data[$i]->invt_itemid,$data[$i]->invt_qty,"plus","0");
            $rett = $this->Common_model->cal_stock($data[$i]->stock_rtn_depo,"RTNcan",$id,$data[$i]->stock_rtn_item,$data[$i]->stock_rtn_qty,"plus","0",$data[$i]->stock_rtn_batch);
                    
            }
            
                    $this->db->set('ret_proceed', "0");
                    $this->db->set('ret_stetus', "0");
                    $this->db->where('ret_id',$id);
                   $this->db->update('mes_return');
                    
                    
                        $ret = "Return Data Canceled";
            
            echo json_encode($ret);
        }
        
        function remove_temp_rtn(){
            
           $this->db->select('*');
            $this->db->from('temp_rtn_itms');
            $this->db->where('temp_rtn_user',$this->session->userdata['login_data']['username']);
            $hasil=$this->db->get();
            $id= $hasil->result(); 
           
            for($i=0; $i<count($id); $i++){
                
                $rett = $this->Common_model->cal_stock(0,"RTNrem",0,$id[$i]->temp_rtn_item,$id[$i]->temp_rtn_qty,"plus","0",$id[$i]->temp_rtn_batch);
               
                $this->db->where('temp_rtn_auid', $id[$i]->temp_rtn_auid);
                $this->db->where('temp_rtn_user', $this->session->userdata['login_data']['username']);
                $del=$this->db->delete('temp_rtn_itms');
            }
            
            
            echo json_encode($ret);
        }
        
        function gt_stk_detail(){
            $itm = $this->input->post('item');
            
            if($this->input->post('depo')< 1){
                 $this->db->select('*');
                $this->db->from('batch');
                $this->db->where('batch_id', $itm);
                $hasil=$this->db->get();
                $datab= $hasil->result(); 
            }else{
                $this->db->select('*');
                $this->db->from('batch_depo');
                $this->db->where('batch_id', $itm);
                $this->db->where('depo', $this->input->post('depo'));
                $hasil=$this->db->get();
                $datab= $hasil->result();
            }
             
           
            
            //$this->db->select('*');
           // $this->db->from('mes_stock');
           // $this->db->where('stc_itemid', $datab[0]->product);
           // $hasil=$this->db->get();
           // $data= $hasil->result(); 
            
            echo json_encode($datab[0]->avl_qty);
            
        } 
        
        function add_ret_item(){
            
            $itm = $this->input->post('item');
            $qty = $this->input->post('qty');
            $depo = $this->input->post('depo');
            
           
                    $this->db->from('batch');
                    $this->db->where('batch_id',$itm);
                    $hasil=$this->db->get();
                    $resbatch= $hasil->result(); 
            
            if($qty!=0){
               $dat = array(
                'temp_rtn_qty' => $qty,
                'temp_rtn_batch' => $itm,
                'temp_rtn_item'  => $resbatch[0]->product, 
                'temp_rtn_depo'  => $depo, 
                'temp_rtn_user' => $this->session->userdata['login_data']['user_id'],
                'temp_rtn_date'  => date("Y-m-d")
                 );
                $detail = $this->db->insert('temp_rtn_itms',$dat);
                
                $data = "Added";
            } 
            
           // $this->Common_model->cal_stock_no_inventry(0,"RTN",0,$itm,$qty,"min","0");
           $rett = $this->Common_model->cal_stock($depo,"RTN",0,$resbatch[0]->product,$qty,"min","0",$itm);
                
                 echo json_encode($data);
        }
        
        function get_ret_prod(){
                    if(!isset($this->session->userdata['login_data']['user_id'])) { 
                        
                                redirect('Login_controller'); 
                                exit();
                    }
                $this->db->select("*");
                $this->db->from('temp_rtn_itms');
                $this->db->join('batch', 'temp_rtn_itms.temp_rtn_batch = batch.batch_id','left');
                $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('temp_rtn_itms.temp_rtn_user', $this->session->userdata['login_data']['user_id']);
                $hasil=$this->db->get();
                $dataa = $hasil->result();
                
                 for($i=0; $i<count($dataa); $i++){
                     
                            $itm = $dataa[$i]->itm_name;
                            $data[] = array($dataa[$i]->temp_rtn_qty,$dataa[$i]->temp_rtn_auid,$dataa[$i]->batch,$dataa[$i]->itm_name,$dataa[$i]->temp_rtn_auid); 
                        
                }
                if(!empty($data)){$data;}else{$data="";}
               
                 echo json_encode($data);
        }
        
        function ret_prodelete(){
            if(!isset($this->session->userdata['login_data']['user_id'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
        
               $this->db->select('*');
                    $this->db->from('temp_rtn_itms');
                    $this->db->where('temp_rtn_auid',$this->input->post('product_code'));
                    $hasil=$this->db->get();
                    $qto_itm= $hasil->result();
                    
     // $this->Common_model->cal_stock_no_inventry(0,"RTN",0,$qto_itm[0]->temp_rtn_item,$qto_itm[0]->temp_rtn_qty,"plus","0");
     $rett = $this->Common_model->cal_stock($qto_itm[0]->temp_rtn_depo,"RTNrem",0,$qto_itm[0]->temp_rtn_item,$qto_itm[0]->temp_rtn_qty,"plus","0",$qto_itm[0]->temp_rtn_batch);
      
        $product_code=$this->input->post('product_code');
        if($product_code!=""){
            $this->db->where('temp_rtn_auid', $product_code);
            $this->db->where('temp_rtn_user', $this->session->userdata['login_data']['user_id']);
            $data=$this->db->delete('temp_rtn_itms');
        }
        
       ;
                
        $data = "Item Removed";
        echo json_encode($data);
        }
        
}        