<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
    include('core_stock_in_out_model.php');
class Stock_In_Out_Model extends Core_Stock_In_Out_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    /* standard function */
    public function runSaveMaster($data){
        return $this->saveMaster($data);
    }
    public function runSaveItems($data){
        return $this->saveItems($data);
    }
    public function runSaveSubItems($data){
        return $this->saveSubItems($data);
    }
    public function runUpdateMasterUsingPrimaryId($id,$data){
        return $this->updateMasterUsingPrimaryId($id,$data);
    }
    public function runUpdateItemsUsingPrimaryId($id,$data){
        return $this->updateItemsUsingPrimaryId($id,$data);
    }
    public function runUpdateSubItemsUsingPrimaryId($id,$data){
        return $this->updateSubItemsUsingPrimaryId($id,$data);
    }
    public function runDeleteMasterUsingPrimaryId($id){
        return $this->deleteMasterUsingPrimaryId($id);
    }
    public function runDeleteItemsUsingPrimaryId($id){
        return $this->deleteItemsUsingPrimaryId($id);
    }
    public function runDeleteSubItemsUsingPrimaryId($id){
        return $this->deleteSubItemsUsingPrimaryId($id);
    }
    public function runSelectMasterUsingPrimaryId($id){
        return $this->selectMasterUsingPrimaryId($id);
    }
    public function runSelectItemsUsingPrimaryId($id){
        return $this->selectItemsUsingPrimaryId($id);
    }
    public function runSelectSubItemsUsingPrimaryId($id){
        return $this->selectSubItemsUsingPrimaryId($id);
    }
     public function runUpdateMasterUsingSecondaryId($id,$data){
        return $this->updateMasterUsingSecondaryId($id,$data);
    }
    public function runUpdateItemsUsingSecondaryId($id,$data){
        return $this->updateItemsUsingSecondaryId($id,$data);
    }
    public function runUpdateSubItemsUsingSecondaryId($id,$data){
        return $this->updateSubItemsUsingSecondaryId($id,$data);
    }
    public function runSelectMasterUsingSecondaryId($id){
        return $this->selectMasterUsingSecondaryId($id);
    }
    public function runSelectItemsUsingSecondaryId($id){
        return $this->selectItemsUsingSecondaryId($id);
    }
    public function runSelectSubItemsUsingSecondaryId($id){
        return $this->selectSubItemsUsingSecondaryId($id);
    }
    
    
    
     public function runGetproductionBatchMaterial($id,$data){
       return  $this->getProductionBatchMaterial($id,$data);
    }
    private function getProductionBatchMaterial($id,$data)
    {
        $this->db->select('pbsi.*,psnsi.psni_id,psnsi.psnm_id,psnm.psnm_warehouse_id');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psnsi.psnm_id', 'inner');
        //$this->db->join('production_stock_out_item psoi', 'psoi.psnm_id = psnm.psnm_id', 'inner');
        //$this->db->where("psoi.psoi_status = 0");
        //$this->db->where("psoi.is_deleted = 0");
        //$this->db->where("psoi.psoi_id",$id);
        $this->db->where("pbsi.pbi_id",$id);
        $this->db->group_by("pbsi.pbsi_id");
        $query = $this->db->get();
        return $query->result_array();
    }
    /*private function getProductionBatchMaterial($id,$data)
    {
        $this->db->select('pbsi.*,psnsi.psni_id,psnsi.psnm_id,psnm.psnm_warehouse_id');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psnsi.psnm_id', 'inner');
        //$this->db->join('production_stock_out_item psoi', 'psoi.psnm_id = psnm.psnm_id', 'inner');
        //$this->db->where("psoi.psoi_status = 0");
        //$this->db->where("psoi.is_deleted = 0");
        //$this->db->where("psoi.psoi_id",$id);
        $this->db->where("psnsi.psnm_id",$id);
        $this->db->group_by("pbsi.pbsi_id");
        $query = $this->db->get();
        return $query->result_array();
    }*/
    
    public function runGetMaterialBatch($id){
       return  $this->getMaterialBatch($id);
    }
    private function getMaterialBatch($id)
    {
        $this->db->select('bp.batch_id,bp.product_cost');
        $this->db->from('batch_product bp');
        $this->db->where("bp.batch_status",1);
        $this->db->where("bp.is_deleted",0);
        $this->db->where("bp.product_id",$id);
        $query = $this->db->get();
        return $query->result_array();
    }
    
     public function runGetproductionRequiredMaterial($id,$data){
       return  $this->getproductionRequiredMaterial($id,$data);
    }
    private function getproductionRequiredMaterial($id,$data)
    {
        $this->db->select('pbsi.*,psnsi.psni_id,psnsi.psnm_id,psnm.psnm_warehouse_id');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
       //$this->db->join('production_store_note_item psni', 'psni.psni_id = psnsi.psni_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psnsi.psnm_id', 'inner');
        $this->db->where("pbsi.pbi_id",$id);
        $this->db->group_by('pbsi.pbsi_product_mat_id');
        $query = $this->db->get();
        return $query->result_array();
    }
    /*
    private function getproductionRequiredMaterial($id,$data)
    {
        $this->db->select('pbsi.*,psnsi.psni_id,psnsi.psnm_id,psnm.psnm_warehouse_id');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
       //$this->db->join('production_store_note_item psni', 'psni.psni_id = psnsi.psni_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psnsi.psnm_id', 'inner');
        $this->db->where("psnsi.psnm_id",$id);
        $this->db->group_by('pbsi.pbsi_product_mat_id');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    
    private function getproductionRequiredMaterial($id,$data)
    {
        $this->db->select('pbsi.*,psnsi.psni_id,psnsi.psnm_id,psnm.psnm_warehouse_id');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psnsi.psnm_id', 'inner');
        $this->db->join('production_stock_out_item psoi', 'psoi.psnm_id = psnm.psnm_id', 'inner');
        $this->db->where("psoi.psoi_status = 0");
        $this->db->where("psoi.is_deleted = 0");
        $this->db->where("psoi.psoi_id",$id);
        $this->db->group_by('pbsi.pbsi_product_mat_id');
        $query = $this->db->get();
        return $query->result_array();
    }*/
    public function runGetSumAllBatchMaterial($id,$mstr_id){
       return  $this->getSumAllBatchMaterial($id,$mstr_id);
    }
    private function getSumAllBatchMaterial($id,$mstr_id)
    {
        $this->db->select_sum('pbsi.pbis_qty');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_store_note_sub_items psnsi', 'psnsi.pbsi_id = pbsi.pbsi_id', 'inner');
        $this->db->where("pbsi.pbsi_product_mat_id",$id);
        $this->db->where("psnsi.psnm_id",$mstr_id);
        $query = $this->db->get();
        $result= $query->row_array();
        if(isset($result['pbis_qty'])){
            return $result['pbis_qty'];
        }else{
            return 0;
        }
    }
    public function runUpdateBatchSubItemsUsingPrimaryId($id,$data){
       return  $this->updateBatchSubItemsUsingPrimaryId($id,$data);
    }
     private function updateBatchSubItemsUsingPrimaryId($id,$data){
        if($id>0){
            $this->db->where('pbsi_id', $id);
            return $this->db->update('production_batch_sub_items',$data);  
        }else{
            return false;
        }
    }
    
    public function runSaveBatchSTN($data){
       return  $this->saveBatchSTN($data);
    }
     private function saveBatchSTN($data){
        if ($this->db->insert('production_batch_stn', $data)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    
     public function updateProductionBatch($id,$data){
        if($id>0){
            $this->db->where('pbi_id', $id);
            return $this->db->update('production_batch_items',$data);  
        }else{
            return false;
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   // old function----------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    /*customizable function*/
     public function runGetPendingSTN($id,$data){
       return  $this->getPendingSTN($id,$data);
    }
    private function getPendingSTN($id,$data)
    {
       $this->db->select('p.*');
        $this->db->select('w.name AS warehouse_name');
        $this->db->select('um.user_first_name');
        $this->db->from('production_store_note_master p');
        $this->db->join('warehouses w', 'w.id = p.psnm_warehouse_id', 'inner');
        $this->db->join('user um', 'um.user_id = p.psnm_user_id', 'inner');
        $this->db->where("p.psnm_warehouse_id",$id);
        $this->db->where("p.psnm_pickup_status",0);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function runGetSelectedSTN($id,$data){
       return  $this->getSelectedSTN($id,$data);
    }
    private function getSelectedSTN($id,$data)
    {
        $this->db->select('p.*,psoi.psoi_id,psoi.psoi_status');
        $this->db->select('w.name AS warehouse_name');
        $this->db->select('um.user_first_name');
        $this->db->from('production_stock_out_item psoi');
        $this->db->join('production_store_note_master p', 'p.psnm_id = psoi.psnm_id', 'inner');
        $this->db->join('warehouses w', 'w.id = p.psnm_warehouse_id', 'inner');
        $this->db->join('user um', 'um.user_id = p.psnm_user_id', 'inner');
        $this->db->where("psoi.psom_id",$id);
        $this->db->where("p.psnm_pickup_status",1);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    
     public function runGetSelectedSTNMaterial($id,$data){
       return  $this->getSelectedSTNMaterial($id,$data);
    }
    private function getSelectedSTNMaterial($id,$data)
    {
        $this->db->select('pm.*,u.unit_name,mpt.mpt_name,');
        $this->db->from('product_material pm');
        $this->db->join('production_batch_sub_items pbsi', 'pbsi.pbsi_product_mat_id = pm.product_mat_id', 'inner');
        $this->db->join('production_batch_items pbi', 'pbi.pbi_id = pbsi.pbi_id', 'inner');
        $this->db->join('production_store_note_item psni', 'psni.pbi_id = pbi.pbi_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psni.psnm_id', 'inner');
        $this->db->join('production_stock_out_item psoi', 'psoi.psnm_id = psnm.psnm_id', 'inner');
        $this->db->join('mstr_unit u', 'u.unit_id = pm.product_mat_unit', 'inner');
        $this->db->join('master_product_type mpt', 'mpt.mpt_id = pm.product_mat_type_id', 'inner');
        $this->db->where("psoi.psoi_status = 0");
        $this->db->where("psoi.is_deleted = 0");
        $this->db->where("psoi.psom_id",$id);
        $this->db->group_by('pm.product_mat_id');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    
    public function runGetSumAllSTNMaterial($id,$data){
       return  $this->getSumAllSTNMaterial($id,$data);
    }
    private function getSumAllSTNMaterial($id,$mstr_id)
    {
        $this->db->select_sum('pbsi.pbis_qty');
        $this->db->from('production_batch_sub_items pbsi');
        $this->db->join('production_batch_items pbi', 'pbi.pbi_id = pbsi.pbi_id', 'inner');
        $this->db->join('production_store_note_item psni', 'psni.pbi_id = pbi.pbi_id', 'inner');
        $this->db->join('production_store_note_master psnm', 'psnm.psnm_id = psni.psnm_id', 'inner');
        $this->db->join('production_stock_out_item psoi', 'psoi.psnm_id = psnm.psnm_id', 'inner');
        $this->db->where("psoi.psoi_status = 0");
        $this->db->where("psoi.is_deleted = 0");
        $this->db->where("pbsi.pbsi_product_mat_id",$id);
        $this->db->where("psoi.psom_id",$mstr_id);
        $query = $this->db->get();
        $result= $query->row_array();
        if(isset($result['pbis_qty'])){
            return $result['pbis_qty'];
        }else{
            return 0;
        }
    }
    
    
    
    
    
    
    
    
    /*Old function*/
    
  
   
    public function runGetNotCompleteSTNListServerSide($start, $length, $search_key_val,$customer_condition){
       return  $this->getNotCompleteSTNListServerSide($start, $length, $search_key_val,$customer_condition);
    }
    private function getNotCompleteSTNListServerSide($start, $length, $search_key_val,$customer_condition)
    {
        $this->db->select('p.*');
        $this->db->select('w.name AS warehouse_name');
        $this->db->select('um.user_first_name');
        $this->db->from('production_store_note_master p');
        $this->db->join('warehouses w', 'w.id = p.psnm_warehouse_id', 'inner');
        $this->db->join('user um', 'um.user_id = p.psnm_user_id', 'inner');
        $filters='';
        if(isset($customer_condition['id'])){
            $filters.=' AND p.psnm_warehouse_id = '.$customer_condition["id"];
        }
        if ($search_key_val) {
            $this->db->where("p.is_deleted = 0 AND p.psnm_status= 0 AND p.psnm_pickup_status = 0 AND p.psnm_ref_no LIKE '$search_key_val%'".$filters);
		    $this->db->or_where("p.is_deleted = 0 AND p.psnm_status = 0 AND p.psnm_pickup_status = 0 AND p.psnm_manual_ref_no LIKE '%$search_key_val%'".$filters);
        }else{
            $this->db->where("p.is_deleted = 0 AND p.psnm_status = 0 AND p.psnm_pickup_status = 0 ".$filters);
        }
		$this->db->order_by("p.psnm_id", "asc");
        if ($start != '' && $length != '') {
            $this->db->limit($length, $start);
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return $query->result();
            } else {
                return false;
            }
        } else {
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return $query->num_rows();
            } else {
                return false;
            }
        }
    }
    public function runGetSelectedBatchMaterial($id,$data){
       return  $this->getSelectedBatchMaterial($id,$data);
    }
    private function getSelectedBatchMaterial($id,$data)
    {
        $this->db->select('pm.*,u.unit_name,mpt.mpt_name,');
        $this->db->from('product_material pm');
        $this->db->join('production_batch_sub_items pbsi', 'pbsi.pbsi_product_mat_id = pm.product_mat_id', 'inner');
        $this->db->join('production_batch_items pbi', 'pbi.pbi_id = pbsi.pbi_id', 'inner');
        $this->db->join('production_store_note_item psni', 'psni.pbi_id = pbi.pbi_id', 'inner');
        $this->db->join('mstr_unit u', 'u.unit_id = pm.product_mat_unit', 'inner');
        $this->db->join('master_product_type mpt', 'mpt.mpt_id = pm.product_mat_type_id', 'inner');
        $this->db->where("psni.issue_status = 0");
        $this->db->where("psni.is_deleted = 0");
        $this->db->where("psni.psnm_id",$id);
        $this->db->group_by('pm.product_mat_id');
        $query = $this->db->get();
        return $query->result_array();
    }
    
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  
}