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

class Stock_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 index(){
            
              $data['content'] = "pages/annualstock";        
            $this->load->view('template',$data);
        }
        
        function all_cat(){
            
               $this->db->from('mes_brand');
            $this->db->select("*");
            //$this->db->order_by("cat_status desc");
           //$this->db->where('cat_live',0);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
             
              echo json_encode($data);
            
        }
        
        function all_depo(){
            $this->db->select('*');
            $this->db->from('mes_depot');
            $this->db->where('dep_stetus',1);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            echo json_encode($data);
            
        }
        
        function showsalltock(){
          
            $this->db->select('*');
            $this->db->from('mes_stock');
            $this->db->join( "mes_item", "mes_item.item_id = mes_stock.stc_itemid", 'left');
            $this->db->join( "mes_unit", "mes_item.item_unit  = mes_unit.unit_id", 'left');
            
            
            
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            $link = site_url('view_stockrep');
            
            $det[0]="<a href='$link' target='_blank'><button type='button' class='btn btn-outline-danger' >Stock Report</button></a></br>";
            
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Product ID</th>
													
															<th>Brand</th>
															
															<th>Item description</th>
															<th>Model / Type</th>
															<th>Part Number</th>
															<th>Specification</th>
															
															<th>Unit</th>
														<th>Stock Qty</th>
														<th>New Qty</th>
														<th>Reason</th>
													   
                                                                                                             
																											
													</tr>
													
													";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                    $pro = $this->Product_model->single_product($data[$i]->stc_itemid);
                    
                    if($pro[0]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($pro[0]->spec_spec!=""){$spece1 = "  ";}else{$spece1 = "";}
                   //if($pro[0]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $pro[0]->brand_name.$spece.$pro[0]->itm_name."\n".$pro[0]->model_name.$spece1.$pro[0]->spec_spec;
              
                    $dt = $this->Product_model->part_num($data[$i]->stc_itemid);
                    $prt="";$ii=1;
                    foreach($dt as $part){
                        $prt = "($ii) ".$part->part_no."</br>".$prt;
                        $ii++;
                    }
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td>".$pro[0]->cat_shortcode."-".$pro[0]->item_catcount."</td>
                                 <td>".$pro[0]->brand_name."</td>
                                 <td>".$pro[0]->itm_name."</td>
                                 <td>".$pro[0]->model_name."</td>
                                 <td>".$prt."</td>
                                 <td>".$pro[0]->spec_spec."</td>
                                 <td style='text-align: center';>".$pro[0]->unit_symble."</td>
                                 
                                 
                                 <td style='text-align: right;width: 100px;'>".$data[$i]->stc_qty."</td>
                                 <td style='text-align: right;width: 100px; '><input type='text' name='nstock[]' id='nstock' class='input-sm form-control-sm form-control' placeholder='' style='text-align: right;' ></td>
                                 <td><input type='text' name='stockreson[]' id='stockreson' class='input-sm form-control-sm form-control' placeholder=''></td>
                                 <input type='hidden'  name='item_id[]' value='".$data[$i]->stc_id."'>
                              
                               
                              
                              </tr>
                             
                              ";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        

        
        function showsalltockrep(){
        $this->db->select("*");    
        $this->db->from('mes_item');
        $this->db->where('item_status', 1);
       // $this->db->join('mes_item', 'inv_items.co_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'mes_item.item_id  = mes_partnumber.part_itmno');
        $this->db->join('mes_unit', 'mes_item.item_unit   = mes_unit.unit_id');
        //$this->db->join('mes_currancy', 'inv_items.co_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_stock", "mes_stock.stc_itemid = mes_item.item_id", 'inner');
        
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        //$this->db->join('mes_service_type', 'inv_items.service_des   = mes_service_type.serv_id');
        $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]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$dataa[$i]->brand_name,$dataa[$i]->itm_name,$dataa[$i]->model_name,$dataa[$i]->part_no,$dataa[$i]->spec_spec,$dataa[$i]->unit_symble,$dataa[$i]->stc_qty,$dataa[$i]->item_catcount,"");
                    
         }
        
        $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/allstockpdf',$data);
        }
        
       function save_invtry(){
                
               //var_dump($_POST); 
               
                    $item = $this->input->post('item_id');
                    $val = $this->input->post('nstock');
                    $term = $this->input->post('stockreson');
                    
                      for($i=0; $i<count($item); $i++){
                         $this->db->select("*");    
                                $this->db->from('mes_inventry_stock');
                                $this->db->where('invstk_stckid',$item[$i]);
                                $this->db->where('invstk_auth',0);
                                $dquery = $this->db->get();
                                $dta = $dquery->result();
                          
                          if($dquery->num_rows()<1){ 
                          
                                    if($val[$i]!="" && $term[$i]!=""){
                                        $this->db->select("*");    
                                        $this->db->from('mes_stock');
                                        $this->db->where('stc_id',$item[$i]);
                                        $query = $this->db->get();
                                        $dup = $query->result();
                                        
                                        
                                        if($dup[0]->stc_qty > $val[$i]){ $type = "remove";}else{$type = "add";}
                                        
                                       $diff = $val[$i] - $dup[0]->stc_qty;
                                       
                                       
                                        
                                        $data = array(
                        'invstk_stckid' => $item[$i], 
                        'invstk_reson'  => $term[$i],
                        'invstk_qty'  => $val[$i],
                        'invstk_type'  => $type,
                        'invstk_dif'  => $diff,
                        'invstk_user'  => "admin",
                        'invstk_date'  => (date('Y-m-d'))
                        
                    );
                    $result = $this->db->insert('mes_inventry_stock',$data);
                                        
                                    }else{
                                        
                                      $dt = "Please Type Reason";  
                                    }
                          }else{
                               $dt = "Please authenticate befor updating";  
                          }            
            }
            
             
            
            echo json_encode($dt);
            
            
        }
        
        function showedit_invtry(){
            
            $this->db->select('*');
            $this->db->from('mes_inventry_stock');
            $this->db->join( "mes_item", "mes_item.item_id = mes_inventry_stock.invstk_stckid", 'inner');
            $this->db->join( "mes_unit", "mes_item.item_unit  = mes_unit.unit_id", 'inner');
            $this->db->join( "mes_stock", "mes_inventry_stock.invstk_stckid  = mes_stock.stc_itemid", 'inner');
            $this->db->where('invstk_auth', 0);
            
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
           // $link = site_url('view_stockrep');
            
           // $det[0]="<a href='$link' target='_blank'><button type='button' class='btn btn-outline-danger' >Stock Report</button></a></br>";
            
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>Product ID</th>
													
															<th>Brand</th>
															
															<th>Item description</th>
															<th>Model / Type</th>
															<th>Part Number</th>
															<th>Specification</th>
															
															<th>Unit</th>
														<th>Stock Qty</th>
														<th>New Qty</th>
														<th>Reason</th>
													   
                                                                                                             
																											
													</tr>
													
													";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                    $pro = $this->Product_model->single_product($data[$i]->invstk_stckid);
                    
                    if($pro[0]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($pro[0]->spec_spec!=""){$spece1 = "  ";}else{$spece1 = "";}
                   //if($pro[0]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $pro[0]->brand_name.$spece.$pro[0]->itm_name."\n".$pro[0]->model_name.$spece1.$pro[0]->spec_spec;
              
                    $dt = $this->Product_model->part_num($data[$i]->invstk_stckid);
                    $prt="";$ii=1;
                    foreach($dt as $part){
                        $prt = "($ii) ".$part->part_no."</br>".$prt;
                        $ii++;
                    }
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td>".$pro[0]->cat_shortcode."-".$pro[0]->item_catcount."</td>
                                 <td>".$pro[0]->brand_name."</td>
                                 <td>".$pro[0]->itm_name."</td>
                                 <td>".$pro[0]->model_name."</td>
                                 <td>".$prt."</td>
                                 <td>".$pro[0]->spec_spec."</td>
                                 <td style='text-align: center';>".$pro[0]->unit_symble."</td>
                                 
                                 
                                 <td style='text-align: right;width: 100px;'>".$data[$i]->stc_qty."</td>
                                 <td style='text-align: right;width: 100px; '><input type='text' name='nstock[]' id='nstock' class='input-sm form-control-sm form-control' placeholder='' style='text-align: right;' value = ".$data[$i]->invstk_qty." ></td>
                                 <td><input type='text' name='stockreson[]' id='stockreson' class='input-sm form-control-sm form-control' placeholder='' value = '".$data[$i]->invstk_reson."'></td>
                                 <input type='hidden'  name='item_id[]' value='".$data[$i]->stc_id."'>
                                    <input type='hidden'  name='iteminv_id[]' value='".$data[$i]->invstk_stckid."'>
                               
                              
                              </tr>
                             
                              ";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
        function showauth_invtry(){
            
            $data['content'] = "pages/annualstockauth";        
            $this->load->view('template',$data);
        }
        
        function saveauth_invtry(){
                    $item = $this->input->post('item_id');
                    $val = $this->input->post('nstock');
                    $term = $this->input->post('stockreson');
                    $invitem = $this->input->post('iteminv_id');
                    
                      for($i=0; $i<count($item); $i++){
                         
                               if($val[$i]!="" && $term[$i]!=""){
                                        $this->db->select("*");    
                                        $this->db->from('mes_stock');
                                        $this->db->where('stc_id',$item[$i]);
                                        $query = $this->db->get();
                                        $dup = $query->result();
                                        
                                        if($dup[0]->stc_qty > $val[$i]){ $type = "remove";}else{$type = "add";}
                                        
                                       $diff = $val[$i] - $dup[0]->stc_qty;
                                       $newact = $dup[0]->stc_actqty + $diff;
                                       $newstk = $dup[0]->stc_qty + $diff;
                                       
                                        $this->db->set('stc_actqty', $newact);
                                        $this->db->set('stc_qty', $newstk);
                                        $this->db->where('stc_id',$item[$i]);
                                        $result=$this->db->update('mes_stock');
                                        
                                        
                                        $this->db->set('invstk_auth', 1);
                                        $this->db->where('invstk_id',$invitem[$i]);
                                        $result=$this->db->update('mes_inventry_stock');
                                        
                                    }else{
                                        
                                      $dt = "Please Type Reason";  
                                    }
                         
                          
                                   
            }
            $dt = "Done";  
             
            
            echo json_encode($dt);
        }
        
}