<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Bulk_Break_Model extends CI_Model
{
	private $tableName = 'bulk_break';
    public function __construct()
    {
        parent::__construct();
    }
    function sub_products_of($term,$product_id){
	// echo "subcategory:$subcategory";
	    $this->db->select('p.*');
		$this->db->from('product p');
		$this->db->order_by("p.product_name", "asc");
		$this->db->group_by('p.product_id');
		$sql="(p.product_name LIKE '%$term%' OR p.product_code LIKE '%$term%')";
		$this->db->where($sql);
		$this->db->where("p.sub_product_of",$product_id);
		$this->db->limit(20, 0);
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();
	}
	function save_bulk_break($break_data,$sale_id=false)	{

		if (!$sale_id)
		{
			/*
			$this->db->trans_start(); // Query will be rolled back
			$this->db->insert($this->tableName,$break_data);
			$this->db->trans_complete();
			if($this->db->trans_status() === FALSE)
			{
				return FALSE;  
			}
			*/
			if($this->db->insert($this->tableName,$break_data))
			return 1;
			else
			return $this->db->_error_message();
		}else {
			$this->db->where('sale_id', $sale_id);
			return $this->db->update($this->tableName,$supplier_data);
		}
	}
	function getMpBreakQty($warehouse_id,$product_id)
	{
		$this->db->select_sum('mp_qty');
		$this->db->from($this->tableName);
		if($warehouse_id)
			$this->db->where('warehouse_id',$warehouse_id);
		$this->db->where('mp_id',$product_id);
		$query=$this->db->get();
		return $query->row()->mp_qty;
	}
	function getSpBreakQty($warehouse_id,$product_id)
	{
		$this->db->select_sum('sp_qty');
		$this->db->from($this->tableName);
		if($warehouse_id)
			$this->db->where('warehouse_id',$warehouse_id);
		$this->db->where('sp_id',$product_id);
		$query=$this->db->get();
		return $query->row()->sp_qty;
	}
	function update_product($product_id, $sub_product_cost, $retail_price,$credit_price, $wholesale_price)
    {
		$data = array(
            'product_cost' => $sub_product_cost,
            'retail_cash' => $retail_price,
            'retail_credit' => $credit_price,
            'retail_wholesale' => $wholesale_price
        );
        $this->db->where('product_id', $product_id);
        if ($this->db->update('product', $data)) {
            return true;
        } else {
            return false;
        }
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}