<?php
 
class Final_Batch_Model extends CI_Model {
  
  
  function __construct() 
  {
    parent::__construct();
    //$this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
  }
    
	function insert_batch($data_item)
	{
		$result=$this->db->insert('batch_final_product',$data_item);
		return $this->db->insert_id();
	}
	function update_batch($id,$data)
	{
	    if($id>0){
		$this->db->where('bfp_id', $id);
		return $this->db->update('batch_final_product', $data);
	    }else{
	        return false;
	    }
	}
	 function checkExistingBatch($batch_code){
     $this->db->select("si.*");
     $this->db->from("batch_final_product si");
     $this->db->where("si.bfp_batch_code",$batch_code);
     $query = $this->db->get();  
     return $query->row_array();
    }
	
	public function createNewBatch($product_id,$ref_id,$batch_code,$type){
	  $recode=$this->checkExistingBatch($batch_code);
	  //print_r($recode);
	  if(isset($recode['bfp_id'])){
	      if($recode['bfp_id']>0){
	          //-----------------------------------------------------
	          return $recode['bfp_id'];
	          //-----------------------------------------------------
	      }
	  }
	  else{
	          //-----------------------------------------------------
	          
	           $data=array(
                'bfp_product_id'=>$product_id,
		    	'bfp_batch_code'=>$batch_code,
		    	'bfp_refer_id'=>$ref_id,
		    	'bfp_type'=>$type,
		    	'bfp_user_id'=>$this->session->userdata('ss_user_id'),
		    	'bfp_date_time'=>date('Y-m-d H:i:s'),
	        	);
	        	return $this->insert_batch($data);
	          //-----------------------------------------------------
	      }
	}
	
	


 


}