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

class Manual_Function_Model extends CI_Model {
  
  private $tableName = '';
  
  function __construct() 
  {
    /* Call the Model constructor */
    parent::__construct();
     //$this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
  }

	function save_area($area_data,$area_id)
	{
		if (!$area_id)
		{
			//$this->db->insert($this->tableName,$area_data);
		}else {
			//$this->db->where('area_id', $area_id);
			//return $this->db->update($this->tableName,$area_data);
		}
	}	
	
	public function disable_product($id)
	{
		$data = array(
			'product_status' => 0
		);	
		$this->db->where('product_id', $id);
		$this->db->update('product', $data);
	}
	
	public function enable_product($id)
	{
		$data = array(
			'product_status' => 1
		);	
		$this->db->where('product_id', $id);
		$this->db->update('product', $data);
	}
	
	public function get_unpaid_sales()
	{		
		$this->db->select('s.*');
		$this->db->from('sales s');
		$this->db->where("s.payment_status", 0);
		$query = $this->db->get();
		return $query->result_array(); 	
	}
	
	public function close_sale($id)
	{
		if($id>0){
			$data = array(
			'payment_status' => 1,
			'closed_date' => date("Y-m-d H:i:s")
		);	
		$this->db->where('sale_id', $id);
		$this->db->update('sales', $data);
		}else{
			return false;
		}
		
	}

	public function get_uncomplte_multiple_payment()
	{		
		$this->db->select('s.*');
		$this->db->from('sale_multiple_payment s');
		$this->db->where("s.payment_settlement_status", 0);
		$query = $this->db->get();
		return $query->result_array(); 	
	}

	function get_total_sale_paid_by_smp_id($id)
    {
        $this->db->select_sum('sp.sale_pymnt_amount');
        $this->db->from('sale_payments sp');
        $this->db->where('sp.smp_id', $id);
        $query = $this->db->get();
        $result= $query->row_array();

        if($result['sale_pymnt_amount']){
            return $result['sale_pymnt_amount'];
        }else{
            return 0;
        }
	}
	
	function get_total_cheque_return_paid_by_smp_id($id)
    {
        $this->db->select_sum('sp.paid_amount');
        $this->db->from('cheque_return_payment sp');
        $this->db->where('sp.smp_id', $id);
        $query = $this->db->get();
        $result= $query->row_array();
        if($result['paid_amount']){
            return $result['paid_amount'];
        }else{
            return 0;
        }
	}
	
	public function close_payment($id)
	{
		if($id>0){
			$data = array(
			'payment_settlement_status' => 1
		);	
		$this->db->where('smp_id', $id);
		$this->db->update('sale_multiple_payment', $data);
		}else{
			return false;
		}		
	}


	public function get_pending_sale_list()
	{	
		$this->db->select('s.*');
		$this->db->from('sales s');
		$this->db->join('customer c','c.cus_id=s.customer_id','left');
		$this->db->where("s.payment_status", 0);https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts
		$this->db->where("c.cus_credit_status", 1);
		$this->db->order_by('s.sale_datetime','desc');
		$query = $this->db->get();
		return $query->result_array(); 	
	}
	
		public function get_not_updated_initial_batch()
	{		
		$this->db->select('p.*');
		$this->db->from('product p');
		$this->db->where("p.added_initial_batch", 0);
		$this->db->limit(1000);
		$query = $this->db->get();
		return $query->result_array(); 	
	}
	
		function save_batch($data)
	{
		if ($data)
		{
			$this->db->insert('batch_product',$data);
		}
	}
	
		public function update_product($id)
	{
	    if($id>0){
	        	$data = array(
			'added_initial_batch' => 1
		);	
		$this->db->where('product_id', $id);
		$this->db->update('product', $data);
	        
	    }else{
	        echo "Update Error";
	        return false;
	    }
	}
	
	function insert_initial_grn($data)
	{
		if ($data)
		{
			$this->db->insert('purchase_items',$data);
			$id = $this->db->insert_id();
			return $id;
		}else{
		    return false;
		}
	}
	public function delete_grn_1()
	{
		$this->db->where('purchase_id', 1);
		$this->db->delete('purchase_items');
	}
	function duplicate_stn_siiue_list($id='')
    {
        $this->db->select('count(*) as cnt');
        $this->db->select('psio_id,psio_issue_on,pbsi_id,product_mat_id,product_mat_batch_id,quantity');
        $this->db->where('psnm_id', $id);
        $this->db->group_by(array("product_mat_id","product_mat_batch_id","quantity","pbsi_id"));
        $this->db->having("cnt > 1", null, false);
        $this->db->limit(10000);
        $q= $this->db->get("production_stock_in_out");
        return $q->result();
    }
    public function delete_dublicate_stn($darray)
	{
		$this->db->where_in('psio_id', $darray);
		$this->db->delete('production_stock_in_out');
	}
	
	function duplicate_stn_item($id='')
    {
        $this->db->select('count(*) as cnt');
        $this->db->select('psni_id');
        $this->db->where('psnm_id', $id);
        $this->db->group_by(array("pbi_id"));
        $this->db->having("cnt > 1", null, false);
        $this->db->limit(10000);
        $q= $this->db->get("production_store_note_item");
        return $q->result();
    }
    
    	function duplicate_stn_sub_item($id='')
    {
        $this->db->select('count(*) as cnt');
        $this->db->select('psnsi_id');
        $this->db->where('psnm_id', $id);
        $this->db->group_by(array("pbsi_id"));
        $this->db->having("cnt > 1", null, false);
        $this->db->limit(10000);
        $q= $this->db->get("production_store_note_sub_items");
        return $q->result();
    }
    
    public function delete_dublicate_stn_item($darray)
	{
		$this->db->where_in('psni_id', $darray);
		$this->db->delete('production_store_note_item');
	}
    
    public function delete_dublicate_stn_sub_item($darray)
	{
		$this->db->where_in('psnsi_id', $darray);
		$this->db->delete('production_store_note_sub_items');
	}
	
	public function auto_enable_disable_zero_batch_qty(){
	   $grn_product_list=$this->get_grn_all_product_list();
       foreach($grn_product_list as $row){
       $product_id=$row['product_mat_id']; 
       $batch_product_list=$this->get_all_batch_list($product_id);
       foreach($batch_product_list as $row2){
           $reamining_qty = $this->Stock_Counter_Model->get_stock_balance_batch('',$product_id,$row2['batch_id']);
           if($reamining_qty<=0){
                $data=array(
		            'batch_status'=>0,
		            );
		        $insert_result=$this->update_batch_product_table($data,$row2['batch_id']);
           }
       }
       }
	}
	private function get_grn_all_product_list(){
     $this->db->select("i.product_mat_id");
     $this->db->from("product_material i");
     $query = $this->db->get();  
     return $query->result_array();
    }
    private function update_batch_product_table($data,$id)
	{
	 if($id>0){
		$this->db->where('batch_id', $id);
		return $this->db->update('batch_product', $data);
	    }else{
	        return false;
	    }
	}
	 private function get_all_batch_list($product_id)
	{
	    $this->db->select("b.*");
        $this->db->from("batch_product b");
        $this->db->where("b.product_id",$product_id);
        $this->db->where("b.batch_status",1);
        $query = $this->db->get();  
        return $query->result_array();
       
	}
	
	 function get_null_batch_list()
	{
	    $this->db->select("o.*,p.product_type_id,p.product_name,p.product_code,pbi.pbi_ref_no,s.stm_date_time,pg.product_mat_id,pg.batch_id AS pgbatch_id");
        $this->db->from('stock_transfer_item o');
        $this->db->join("product p", "p.product_id = o.product_id", "inner");
        $this->db->join("stock_transfer_master s", "s.stm_id = o.stm_id", "inner");
        $this->db->join("production_batch_items pbi", "pbi.pbi_id = o.batch_id", "inner");
        $this->db->join("production_grn_intermediat_product pg", "pg.pbi_id = o.batch_id", "inner");
        
        $this->db->where("s.stm_to_id","PRODUCTION01");
        $this->db->where("o.batch_code",null);
        $this->db->group_by('o.sti_id');
        $query = $this->db->get();  
        return $query->result_array();
	}
	
	function check_finish_batch_table($code){
	    $this->db->select('b.bfp_id');
        $this->db->from('batch_final_product b');
        $this->db->where('b.bfp_batch_code', $code);
        $query = $this->db->get();
        $result= $query->row_array();
        if(isset($result['bfp_id'])){
            return $result['bfp_id'];
        }else{
            return 0;
        }
	    
	}
	function save_fbatch($data)
	{
	    $this->db->insert('batch_final_product',$data);
	
	}	
		function save_convert($data)
	{
	    $this->db->insert('production_convert',$data);
	
	}	
	
     function update_sti_table($data,$id)
	{
	 if($id>0){
		$this->db->where('sti_id', $id);
		return $this->db->update('stock_transfer_item', $data);
	    }else{
	        return false;
	    }
	}
	
	
	 function get_finished_batch_list()
	{
	    $this->db->select("pbi.*");
        $this->db->from('production_batch_items pbi');
        $query = $this->db->get();  
        return $query->result_array();
	}
	
	function update_pbi_table($data,$id)
	{
	 if($id>0){
		$this->db->where('pbi_id', $id);
		return $this->db->update('production_batch_items', $data);
	    }else{
	        return false;
	    }
	}
	
}