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

class Trancfer_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_controller'); 
                        exit();
            }
        }
        
        function test(){
            $usr = $this->session->userdata['login_data']['user_id'];
                        $data = array(
                            'depo' => 0,
                            'type' => 'tmpgrn',
                            'inv_id' => 523,
                            'item_id' => 3,
                            'qty' => 20,
                            'add' => "plus",
                            'batch' => 3,
                            'usr' => $usr 
                       );    
                         
                        $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                        $post_endpoint = '/stock_cal';
                        $request_data = $data;
                        $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);    
                        // $det = json_decode($response); 
                echo $response;
        }
        
         function index(){
             
            
             $data['itemdetail'] =  $this->Product_model->product_list();
             
             //if(isset($this->session->userdata['login_data']['depot']) && ($this->session->userdata['login_data']['userlevel']>2)){
                 
                if($this->session->userdata['login_data']['depot']!=0){
                    $this->db->select("*");
                            $this->db->from('mes_depot');
                            $this->db->where('dep_id',$this->session->userdata['login_data']['depot']);
                            $all = $this->db->get();
                            $data['frmdepo'] = $all->result();
                    
                            $this->db->select("*");
                            $this->db->from('mes_depot');
                            $this->db->where('dep_id','0');
                            $all = $this->db->get();
                            $data['dpodetail'] = $all->result();
                }else{
                    if($this->session->userdata['login_data']['userlevel']>2){
                            $this->db->from('mes_depot');
                            $this->db->where('dep_id',$this->session->userdata['login_data']['depot']);
                            $all = $this->db->get();
                            $data['frmdepo'] = $all->result();
                            
                            $this->db->select("*");
                            $this->db->from('mes_depot');
                            $this->db->where('dep_id','0');
                            $all = $this->db->get();
                    }else{
                            $this->db->select("*");
                            $this->db->from('mes_depot');
                            //$this->db->where('dep_id!=',$this->session->userdata['login_data']['depot']);
                            $all = $this->db->get();
                            $data['frmdepo'] = $all->result();
                            $data['dpodetail'] = $all->result();
                    }
                    
                    
                }
                            
                if($this->session->userdata['login_data']['userlevel']<3){
                            $this->db->select("*");
                            $this->db->from('mes_depot');
                            $all = $this->db->get();
                            $data['frmdepo'] = $all->result();
                            $data['dpodetail'] = $all->result();
                    }            
                            
                 
               $data['content'] = ('pages/newtransfer'); 
		       $this->load->view('template',$data);
         }
         
         function addtrfitm(){
             
              if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            
            $depo_id = $this->input->post('depo_id');
            $tritm = $this->input->post('tritm');
            $qty = $this->input->post('qty');
            
            $stk = $this->Common_model->check_stock($tritm,$qty,$this->input->post('frmdepo_id'),$this->input->post('batch')); 
            if(!$stk){
                $data = "No Stock Available";
            }else{
                
                $dat = array(
                'temp_trf_depo'  => $this->input->post('depo_id'),
                'temp_trf_fromd'  => $this->input->post('frmdepo_id'),
                'temp_trf_qty' => $this->input->post('qty'),
                'temp_trf_id'  => $this->input->post('tritm'),
                'temp_trf_batch'  => $this->input->post('batch'),
                'temp_trf_user' => $this->session->userdata['login_data']['username'],
                'temp_trf_date'  => date("Y-m-d")
                 );
                $detail = $this->db->insert('temp_trf_itms',$dat);
                
                $stock = $this->Common_model->cal_stock($this->input->post('frmdepo_id'),"tmptran",0,$this->input->post('tritm'),$this->input->post('qty'),"min",0,$this->input->post('batch'));
                
                
                $data = "Added";
            }
            
                
                 echo json_encode($data);
            
             
         }
         
        function gettrfitm() {     
     if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
        $this->db->select("*");
        $this->db->from('temp_trf_itms');
        $this->db->join('mes_item', 'temp_trf_itms.temp_trf_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->join( "batch", "temp_trf_itms.temp_trf_batch = batch.batch_id",'left');
        $this->db->where('temp_trf_itms.temp_trf_user', $this->session->userdata['login_data']['username']);
        $this->db->order_by("temp_id","asc");
        $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]->temp_trf_itmprice,$dataa[$i]->temp_trf_qty,$dataa[$i]->temp_trf_id,$dataa[$i]->brand_shordcode,$dataa[$i]->temp_id,$dataa[$i]->batch); 
                
        }
        if(!empty($data)){$data;}else{$data="";}
       
         echo json_encode($data);
    }
         
        function trnser_all(){
            
             $data['content'] = "pages/alltran";        
            $this->load->view('template',$data); 
        }
        
        function showalltrnf(){
             
             
            $this->db->select('*');
            $this->db->from('mes_stock_trancfer');
            $this->db->order_by("trncfer_cdate","desc");
           $this->db->order_by("trncfer_prosses","desc");
           $this->db->order_by("trncfer_stetus","desc");
            //$this->db->order_by("trncfer_id","desc");
            $this->db->limit(100);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
            
            
         
            $link = site_url('newtranser');
           
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Transfer</button></a></br>";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														
														<th>Transfer Number</th>
														<th>Depot</th>
														<th>Transfer Oder</th>
														<th>Refrence</th>
														<th>Total QTY</th>
														<th>Total ITM</th>
														<th>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_depot');
            $this->db->where('dep_id', $data[$i]->trncfer_dpoid);
            $this->db->join( "mes_stock_trancfer", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left');
            $hasil1=$this->db->get();
            $datanew= $hasil1->result();
            
            if($this->session->userdata['login_data']['userlevel']<6){
                
                $vw_btn1 = "<a href ='".base_url()."pdf_trnf/".$data[$i]->trncfer_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."' name='edtbutton' onclick='cancel_view(this.id)' data-target='#Modal_edit'>Cancel</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."'  onclick='po_proceed(this.id)' >Process</button>
                                ";
            }else{$vw_btn1 ="";}
       
                    
                   $vw_btn1 = "<a href ='".base_url()."pdf_trnf/".$data[$i]->trncfer_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."' name='edtbutton' onclick='cancel_view(this.id)' data-target='#Modal_edit'>Cancel</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."'  onclick='po_proceed(this.id)' >Process</button>
                                ";
                    $vw_btn2 = "<a href ='".base_url()."pdf_trnf/".$data[$i]->trncfer_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                
                                ";
                        
                    if($data[$i]->trncfer_prosses==1){
                        if($data[$i]->trncfer_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]->trncfer_stetus==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                }
                 
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->trncfer_code.$data[$i]->trncfer_id."</td>
                                 <td></td>
                                 <td>".$data[$i]->trncfer_trfodr."</td>
                                  <td>".$data[$i]->trncfer_ref."</td>
                                 
                                 <td style='text-align: right;'>".$data[$i]->trncfer_tqty."</td>
                                 <td style='text-align: right;'>".$data[$i]->trncfer_titm."</td>
                                <td style='text-align: right;'>".$data[$i]->trncfer_cdate."</td>
                                $sts
                                <td>".$vw_btn."  </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
        function searchtrf(){
            
                         
            $this->db->select('*');
            $this->db->from('mes_stock_trancfer');
            $this->db->order_by("trncfer_cdate","desc");
            $this->db->order_by("trncfer_prosses","desc");
            $this->db->order_by("trncfer_stetus","desc");
            
            if($this->input->post('grn')!=""){
                
                $this->db->where('trncfer_id', $this->input->post('grn'));
            }
            
             if($this->input->post('date')!=""){
                
                $this->db->where('trncfer_cdate', $this->input->post('date'));
            }
            
           
            //$this->db->order_by("trncfer_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
            
            
         
            $link = site_url('newtranser');
           
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Transfer</button></a></br>";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														
														<th>Transfer Number</th>
														<th>Depot</th>
														<th>Transfer Oder</th>
														<th>Total QTY</th>
														<th>Total ITM</th>
														<th>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_depot');
            $this->db->where('dep_id', $data[$i]->trncfer_dpoid);
            $this->db->join( "mes_stock_trancfer", "mes_depot.dep_id = mes_stock_trancfer.trncfer_dpoid", 'left');
            $hasil1=$this->db->get();
            $datanew= $hasil1->result();
            
       
                    
                   $vw_btn1 = "<a href ='".base_url()."pdf_trnf/".$data[$i]->trncfer_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."' name='edtbutton' onclick='cancel_view(this.id)' data-target='#Modal_edit'>Cancel</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->trncfer_id."'  onclick='po_proceed(this.id)' >Process</button>
                                ";
                    $vw_btn2 = "<a href ='".base_url()."pdf_trnf/".$data[$i]->trncfer_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                
                                ";
                        
                    if($data[$i]->trncfer_prosses==1){
                        if($data[$i]->trncfer_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]->trncfer_stetus==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                }
                 
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->trncfer_code.$data[$i]->trncfer_id."</td>
                                 <td>".$datanew[0]->dep_name."</td>
                                 <td>".$data[$i]->trncfer_trfodr."</td>
                                 
                                 <td style='text-align: right;'>".$data[$i]->trncfer_tqty."</td>
                                 <td style='text-align: right;'>".$data[$i]->trncfer_titm."</td>
                                <td style='text-align: right;'>".$data[$i]->trncfer_cdate."</td>
                                $sts
                                <td>".$vw_btn."  </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
            
        }
        
        function transer_save_old(){
            
           $id = $this->input->post('ids');
            $qt = $this->input->post('values');
           
            $idt = 0;
            $qtt = 0;
            for($i=0; $i<count($id); $i++){
              $idt = $idt + $id[$i];
              $qtt = $qtt + $qt[$i];
              
            }
          
           
           $tot = 0;
            $dat = array(
                        'trncfer_dpoid'  => $this->input->post('dpo'),
                        'trncfer__from_dpoid'  => $this->session->userdata['login_data']['depot'],
                        'trncfer_trfodr' => $this->input->post('podo'),
                        'trncfer_tqty'  => $qtt,
                        'trncfer_titm'  => count($id),
                        'trncfer_cuser'  => "admin",
                        'trncfer_cdate'  => date('Y-m-d')
                        
                    );
            $data = $this->db->insert('mes_stock_trancfer',$dat);
           $insert_id = $this->db->insert_id();
           
            for($i=0; $i<count($id); $i++){
                 if($qt[$i]!=""){
                    $dat = array(
                        'trf_trfid'  => $insert_id,
                        'trf_itmid'  => $id[$i],
                        'trf_qty'  => $qt[$i]
                    );
                    $data = $this->db->insert('mes_trnfer_item',$dat);
                    $tot = $tot+$qt[$i];
               
            
            
                }
                
            }
           
            //$this->db->set('trncfer_stetus', 1);
           // $this->db->where('trncfer_id', $id);
            //$result=$this->db->update('mes_stock_trancfer');
           
      
            $send = "Transfer Note Created";
            echo json_encode($send);
        }
        
        function transer_save(){
            
            $this->db->select('*');
            $this->db->from('temp_trf_itms');
            $this->db->where('temp_trf_user',$this->session->userdata['login_data']['username']);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
          
            
            if(count($data)>0){
                $dat = array(
                        'trncfer_dpoid'  => $this->input->post('dpo'),
                        'trncfer__from_dpoid'  => $this->input->post('fdpo'),
                        'trncfer_tqty'  => 0,
                        'trncfer_titm'  => count($data),
                        'trncfer_ref'  => $this->input->post('refid'),
                        'trncfer_cuser'  => $this->session->userdata['login_data']['username'],
                        'trncfer_cdate'  => date('Y-m-d')
                        
                    );
                        $dataa = $this->db->insert('mes_stock_trancfer',$dat);
                        $insert_id = $this->db->insert_id();
            
          $tot = 0;
           for($i=0; $i<count($data); $i++){
               $dat = array(
                        'trf_trfid'  => $insert_id,
                        'trf_itmid'  => $data[$i]->temp_trf_id,
                        'trf_batch'  => $data[$i]->temp_trf_batch,
                        'trf_qty'  => $data[$i]->temp_trf_qty
                    );
                   $ins = $this->db->insert('mes_trnfer_item',$dat);
                    $tot = $tot + $data[$i]->temp_trf_qty;
               
                $this->db->where('temp_trf_id', $data[$i]->temp_trf_id);
                $this->db->where('temp_trf_user', $this->session->userdata['login_data']['username']);
                $del=$this->db->delete('temp_trf_itms');
               
           }
        
           
            $this->db->set('trncfer_tqty', $tot);
            $this->db->where('trncfer_id', $insert_id);
            $result=$this->db->update('mes_stock_trancfer');
           
      
            $send = "Transfer Note Created";
            }else{
                $send = "No Transfer Items Added";
            }
            echo json_encode($send);
        } 
        
        function view_pdf($insert_id){
            
        $this->db->select("*");    
        $this->db->from('mes_stock_trancfer');
        $this->db->where('trncfer_id', $insert_id );
        $this->db->join( "mes_depot", "mes_stock_trancfer.trncfer_dpoid = mes_depot.dep_id", 'left');
        $query = $this->db->get();
        $data['inv']=$query->result();
        
        $this->db->select("*");    
        $this->db->from('mes_stock_trancfer');
        $this->db->where('trncfer_id', $insert_id );
        $this->db->join( "mes_depot", "mes_stock_trancfer.trncfer__from_dpoid = mes_depot.dep_id", 'left');
        $query1 = $this->db->get();
        $frdepo = $query1->result();
        array_push($data['inv'],$frdepo[0]->dep_name);
        
        $this->db->select("*");    
        $this->db->from('mes_trnfer_item');
        $this->db->where('trf_trfid',$insert_id);
        //$this->db->where('invt_type_id',$insert_id);
        $this->db->join( "mes_item", "mes_item.item_id = mes_trnfer_item.trf_itmid", '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( "batch", "batch.batch_id  = mes_trnfer_item.trf_batch", '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');
        $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]->itm_name;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->trf_qty,$dataa[$i]->cat_shortcode,$dataa[$i]->brand_shordcode,$dataa[$i]->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/trnfnotepdf',$data);
            
        }
        
        function transer_getbatch(){
             
            
            if(isset($this->session->userdata['login_data']['depot']) && ($this->input->post('depo')!=0)){
                
                $this->db->select('*');
                $this->db->from('batch_depo'); 
                $this->db->where('depo',$this->input->post('depo'));
                $this->db->where('batch_depo.product',$this->input->post('code')); 
                        $this->db->where('batch_depo.avl_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
                 
             }else{
                 $this->db->select('*');
                    $this->db->from('batch'); 
                    $this->db->where('batch.product',$this->input->post('code'));
                        $this->db->where('batch.avl_qty>','0');
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
             }
        
                echo json_encode($resultitem);
        
        }
        
        function all_getbatch(){
             
            
            if(isset($this->session->userdata['login_data']['depot']) && ($this->input->post('depo')!=0)){
                
                $this->db->select('*');
                $this->db->from('batch_depo'); 
                $this->db->where('depo',$this->input->post('depo'));
                $this->db->where('batch_depo.product',$this->input->post('code')); 
                       // $this->db->where('batch_depo.avl_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
                 
             }else{
                 $this->db->select('*');
                    $this->db->from('batch'); 
                    $this->db->where('batch.product',$this->input->post('code'));
                       // $this->db->where('batch.avl_qty>','0');
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
             }
        
                echo json_encode($resultitem);
        
        }
        
        function transer_getstk(){
             
            
            if(isset($this->session->userdata['login_data']['depot']) && ($this->input->post('depo')!=0)){
                 
                 $this->db->select('avl_qty');
                $this->db->from('batch_depo'); 
                $this->db->where('depo',$this->input->post('depo'));
                $this->db->where('batch_depo.batch_id',$this->input->post('batch')); 
                        $this->db->where('batch_depo.avl_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
                 
             }else{
                    $this->db->select('avl_qty');
                    $this->db->from('batch'); 
                    $this->db->where('batch.batch_id',$this->input->post('batch'));
                        $this->db->where('batch.avl_qty>','0');
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
             }
        
                echo json_encode($resultitem);
        
        }
         
        function gtdepo_product(){
            
           $data['item'] =  $this->Product_model->get_stockwise_prod($this->input->post('depo')); 
           echo json_encode($data);
           
        }
        
        function auth_trans(){
            
            if(isset($this->session->userdata['login_data']['username'])) { 
                $id = $this->input->post('id');
                
                $this->db->select('*');
                    $this->db->from('mes_stock_trancfer');
                    $this->db->where('trncfer_id', $id);
                    $hasil1=$this->db->get();
                    $depo= $hasil1->result(); 
                
                    $this->db->select('*');
                    $this->db->from('mes_trnfer_item');
                    $this->db->where('trf_trfid', $id);
                    $this->db->where('trf_item_auth',0);
                    $hasil1=$this->db->get();
                    $det= $hasil1->result();
                
                for($i=0; $i<count($det); $i++){
                    
                            $this->db->select("*");    
                            $this->db->from('mes_depot_stock');
                            $this->db->where('dpstock_itemid', $det[$i]->trf_itmid);
                            $this->db->where('dpstock_dpoid', $depo[0]->trncfer_dpoid);
                            $query = $this->db->get();
                            $sdata = $query->result();
                    if($query->num_rows()<1){
                          $dat = array(
                                        'dpstock_itemid'  => $det[$i]->trf_itmid,
                                        'dpstock_dpoid'  => $depo[0]->trncfer_dpoid,
                                        'dpstock_qty'  => 0
                                    );
                                    $data = $this->db->insert('mes_depot_stock',$dat);
                    }
                    
                    //add to batch_depo
                                                $this->db->select("*");    
                                                $this->db->from('batch_depo');
                                                $this->db->where('batch_id', $det[$i]->trf_batch);
                                                $this->db->where('depo', $depo[0]->trncfer_dpoid);
                                                $query = $this->db->get();
                                                $sdata = $query->result();
                                                             if($query->num_rows()<1){
                                                                $this->db->select("*");    
                                                                $this->db->from('batch');
                                                                $this->db->where('batch_id', $det[$i]->trf_batch);
                                                                $query = $this->db->get();
                                                                $data = $query->result(); 
                                                                    $dat = array(
                                                                        'batch_id'  => $det[$i]->trf_batch,
                                                                        'batch'  => $data[0]->batch,
                                                                        'depo'  => $depo[0]->trncfer_dpoid,
                                                                        'product'  => $data[0]->product,
                                                                        'tot_qty'  => 0,
                                                                        'avl_qty'  => 0,
                                                                        'buying_price'  => $data[0]->buying_price,
                                                                        'selling_price'  => $data[0]->selling_price,
                                                                        'retail_price'  => $data[0]->retail_price,
                                                                        'man_date'  => $data[0]->man_date,
                                                                        'exp_date'  => $data[0]->exp_date
                                                                    );
                                                                    
                                                                    $this->db->insert('batch_depo',$dat); 
                                                                    $insertId = $this->db->insert_id();
                                                            }
                    
                    $this->Common_model->cal_stock($depo[0]->trncfer__from_dpoid,"tmptran_rem",0,$det[$i]->trf_itmid,$det[$i]->trf_qty,"plus",0,$det[$i]->trf_batch);
               
                       if($depo[0]->trncfer__from_dpoid!=0){
                           
                            $this->Common_model->cal_stock($depo[0]->trncfer__from_dpoid,"TRS",$id,$det[$i]->trf_itmid,$det[$i]->trf_qty,"min","0",$det[$i]->trf_batch);
                           $this->Common_model->cal_stock($depo[0]->trncfer_dpoid,"TRS",$id,$det[$i]->trf_itmid,$det[$i]->trf_qty,"plus","0",$det[$i]->trf_batch);
                           
                           
                            $this->db->select('*');
                            $this->db->from('mes_depot_stock');
                            $this->db->where('dpstock_itemid', $det[$i]->trf_itmid);
                            $this->db->where('dpstock_dpoid', $depo[0]->trncfer_dpoid);
                            $hasil1=$this->db->get();
                            $datanew= $hasil1->result();
                               
                             /*  if($hasil1->num_rows()>0){
                            
                                    //echo $datanew[0]->dpstock_qty;
                                    
                                    $newqty = $datanew[0]->dpstock_qty + $det[$i]->trf_qty;
                                    
                                    $this->db->set('dpstock_qty', $newqty);
                                    $this->db->where('dpstock_itemid', $det[$i]->trf_itmid);
                                    $this->db->where('dpstock_dpoid', $depo[0]->trncfer_dpoid);
                                    $result=$this->db->update('mes_depot_stock');
                                    
                                            $this->db->select('*');
                                            $this->db->from('mes_stock');
                                            $this->db->where('stc_itemid', $det[$i]->trf_itmid);
                                            $hasilstk=$this->db->get();
                                            $datastk= $hasilstk->result();
                                    
                                        
                                    }else{      
                                            
                               }*/
                       }else{
                           
                           $this->Common_model->cal_stock($depo[0]->trncfer__from_dpoid,"TRS",$id,$det[$i]->trf_itmid,$det[$i]->trf_qty,"min","0",$det[$i]->trf_batch);
                           $this->Common_model->cal_stock($depo[0]->trncfer_dpoid,"TRS",$id,$det[$i]->trf_itmid,$det[$i]->trf_qty,"plus","0",$det[$i]->trf_batch);
                       }
                       
                       $this->db->set('trf_item_auth', 1);
                        $this->db->where('trf_id',$det[$i]->trf_id);
                        $this->db->update('mes_trnfer_item');
                }
                
                 $this->db->select('*');
                    $this->db->from('mes_trnfer_item');
                    $this->db->where('trf_trfid', $id);
                    $this->db->where('trf_item_auth',0);
                    $hasill=$this->db->get();
                    $dett= $hasill->result();
                    
               if(count($dett)<1){
                     $this->db->set('trncfer_prosses', 1);
                $this->db->where('trncfer_id', $id);
                $result=$this->db->update('mes_stock_trancfer');
                    
                    $ret = "Transfer Completed";
               }else{  $ret = "Not Completed"; }
            
                
            }else{
                $ret = "Session Expired. Please Login";
            }    
                
            
            echo json_encode($ret);
        }
        
        function cancel_trans(){
            $id = $this->input->post('id');
             
              $this->db->select('*');
                    $this->db->from('mes_stock_trancfer');
                    $this->db->where('trncfer_id', $id);
                    $hasil1=$this->db->get();
                    $main= $hasil1->result();
             
              $this->db->select('*');
                    $this->db->from('mes_trnfer_item');
                    $this->db->where('trf_trfid', $id);
                    $hasil1=$this->db->get();
                    $det= $hasil1->result();
                
                for($i=0; $i<count($det); $i++){
                   $this->Common_model->cal_stock($main[0]->trncfer__from_dpoid,"TEMPTRREM",$id,$det[$i]->trf_itmid,$det[$i]->trf_qty,"plus","0",$det[$i]->trf_batch);
                }
             
             $this->db->set('trncfer_stetus', 0);
           $this->db->where('trncfer_id', $id);
            $result=$this->db->update('mes_stock_trancfer');
            
            
            $ret = "Transfer Canceled";
            
            echo json_encode($ret);
        }
        
        function prodelete(){
       if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('Login_controller'); 
                        exit();
            }
        
               $this->db->select('*');
                    $this->db->from('temp_trf_itms');
                    $this->db->where('temp_id',$this->input->post('product_code'));
                    $hasil=$this->db->get();
                    $qto_itm = $hasil->result();
                
        
                  $this->db->select('user_depoid');
                    $this->db->from('mes_user');
                    $this->db->where('user_id',$this->input->post('sales'));
                    $hasil=$this->db->get();
                    $sls= $hasil->result();
        
            //$this->Common_model->cal_stock($this->session->userdata['login_data']['depot'],"tmptran_del",0,$qto_itm[0]->temp_trf_id,$qto_itm[0]->temp_trf_qty,"plus",0);
            $stock = $this->Common_model->cal_stock($qto_itm[0]->temp_trf_fromd,"tmptran_del",0,$qto_itm[0]->temp_trf_id,$qto_itm[0]->temp_trf_qty,"plus",0,$qto_itm[0]->temp_trf_batch);
                   
                    
        $product_code=$this->input->post('product_code'); 
        if($product_code!=""){
            $this->db->where('temp_id', $product_code);
            $this->db->where('temp_trf_user', $this->session->userdata['login_data']['username']);
            $data=$this->db->delete('temp_trf_itms');
        }
        
       
        echo json_encode("Removed");
    
        }
        
        function all_transfer(){
            if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('Login_controller'); 
                        exit();
            }
            
            $depo = $this->input->post('dpo');
            $tdepo = $this->input->post('todpo');
            
                $this->db->select('*');
                //$this->db->from('batch_depo');
                $this->db->from('batch');
               // $this->db->where('depo', $depo);
                $this->db->where('avl_qty>', 0);
                $hasil=$this->db->get();
                $stockk= $hasil->result();
                
                for($i=0; $i<count($stockk); $i++){
                        $depo_id = $depo;
                        $tritm = $stockk[$i]->product;
                        $qty = $stockk[$i]->avl_qty;
                        $batch_id = $stockk[$i]->batch_id;
                        
                       //$stk = $this->Common_model->check_stock($tritm,$qty,$depo,$batch_id); 
                            
                            $dat = array(
                                'temp_trf_depo'  => $tdepo,
                                'temp_trf_fromd'  => $depo,
                                'temp_trf_qty' => $qty,
                                'temp_trf_id'  => $tritm,
                                'temp_trf_batch'  => $batch_id,
                                'temp_trf_user' => $this->session->userdata['login_data']['username'],
                                'temp_trf_date'  => date("Y-m-d")
                                 );
                                $detail = $this->db->insert('temp_trf_itms',$dat);
                                
                                $stock = $this->Common_model->cal_stock($depo,"tmptran",0,$tritm,$qty,"min",0,$batch_id);
                                
                                $data = "Added";
                        
                }
                
            $ret = "Please ReCheck";
            echo json_encode($ret);
        }
        
        
}