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

class Product_Damage_Customer_Model extends CI_Model {
  
  private $tableName = 'sales';
  private $tableName2 = 'cus_damage';
  function __construct() 
  {
    /* Call the Model constructor */
    parent::__construct();
  }
  
  	
	
	
  
  
  function getwarehousename($wid){
	$this->db->select('warehouses.name');
	$this->db->from('warehouses');
	$this->db->where("id", $wid);
	$this->db->order_by("id", "desc");
	$query = $this->db->get();
	//echo $this->db->last_query();
	return $query->row_array(); 
  }
  
  //Product_Damage genarate referance number
  function get_next_ref_no(){
	  $this->db->select_max('cus_damage_id');
	  return $this->db->get('cus_damage');
  }
 
 
  

  
   
	 
	



	
	

	
	//Get product sujetions
	function get_products_suggestions($inc_id,$term) {
		$this->db->select('product.*');
		$this->db->join('sale_items','product.product_id = sale_items.product_id ','left');
		$this->db->order_by("product_name", "asc");
		$this->db->where("sale_id",$inc_id);
		//$this->db->limit(10,0);
		$query = $this->db->get('product');
		//echo $this->db->last_query();
		return $query->result_array();
		
		
	}

	//Get all products
	function get_all_products() {
		$this->db->select('product'.'.*');
		$this->db->order_by("product_name", "asc");
		$this->db->where("product_id IS NOT NULL");//("id !=",$id);
		$query = $this->db->get('product');
		return $query->result_array();
	}
	
	
		public function get_all_invoice_by_warehouse_id($warehouse_id)
	{    
	   
	    $this->db->select('sales.*');
		$this->db->join('sales_return','sales.sale_id = sales_return.sale_id ','LEFT');
		//$this->db->order_by("sale_id", "asc");
		$this->db->where("(sales.sale_id  NOT IN(select sale_id from sales_return ))");
		$this->db->where('sales.warehouse_id',$warehouse_id);
		//$this->db->limit(10, 0);
		$query = $this->db->get('sales');
		//echo $this->db->last_query();
	   $data=array();
	   foreach ($query->result() as $row)
{
$data[$row->sale_id]= $row->sale_reference_no;
}
return ($data);
}
function get_sale_details($invoice_id){
	    $this->db->select('*');
		$this->db->from('sales');
		$this->db->where("sale_id",$invoice_id);
		$query = $this->db->get();
		$data=array();
 
//$data[0] = 'SELECT'; 
foreach ($query->result() as $row)
{
$data[$row->customer_id] = $row->customer_id;
}
return ($data);
}
function get_cus_id($invoice_id){
	    $this->db->select('*');
		$this->db->from('sales');
		$this->db->where("sale_id",$invoice_id);
		$query = $this->db->get();
	    return $query->result_array();
	}


 public function get_avalable_product_qt($product_id,$inc_id){
	    $this->db->select('quantity');
		$this->db->from('sale_items');
		$this->db->where("sale_id",$inc_id );
		$this->db->where("product_id",$product_id);
        $query = $this->db->get();
		return $query->result_array();
}
function check_avalable_product_qty($product_id,$inc_id){
		 $this->db->select_sum('quantity');
		 $query = $this->db->get('sale_items');
		 return $query->row()->quantity;
  }

function get_sales_id($inc_id){
	    $this->db->select('sale_reference_no');
		$this->db->from('sales');
		$this->db->where("sale_id",$inc_id );
        $query = $this->db->get();
		return $query->result_array();
	}	
function save_product_damage_customer(&$supplier_data,$cus_damage_id=false)
{               
	if (!$cus_damage_id)
		{
			$this->db->insert($this->tableName2,$supplier_data);
		}else {
			$this->db->where('cus_damage_id',$cus_damage_id);
			return $this->db->update($this->tableName2,$supplier_data);
		}
}
function save_product_damage_customer_item(&$data)
	{      
			$this->db->insert('cus_damage_item',$data);
	}
function get_cus_damage_details($id){
	    $this->db->select('*');
		$this->db->from('cus_damage');
		$this->db->where("cus_damage_id",$id );
        $query = $this->db->get();
		return $query->row_array();
	}
function get_sales_details($id){
	    $this->db->select('*');
		$this->db->from('sales');
		$this->db->where("sale_id",$id );
        $query = $this->db->get();
		return $query->row_array();
	}
function get_cus_damage_item_details($id){
	    $this->db->select('*');
		$this->db->from('cus_damage_item');
		$this->db->where("cus_damage_id",$id );
        $query = $this->db->get();
		return $query->result_array();
	}
function get_product_info($id){
	    $this->db->select('*');
		$this->db->from('product');
		$this->db->where("product_id",$id );
        $query = $this->db->get();
		return $query->row_array();
	}	
function get_customer_info($id){
	   $this->db->select('*');
	   $this->db->from('user');
	   $this->db->where("user_id",$id);
	   $query = $this->db->get();
	   return $query->row_array();
	}	
function get_customer_id($id){
	   $this->db->select('*');
	   $this->db->from('cus_damage');
	   $this->db->where("cus_damage_id",$id);
	   $query = $this->db->get();
	   return $query->row_array();
	}
	function get_cus_damage_item_qty($product_id,$inc_id){
    $this->db->select_sum('quantity');
	$this->db->join('cus_damage','cus_damage.cus_damage_id =cus_damage_item.cus_damage_id','left');
    $this->db->where('cus_damage.sale_id',$inc_id);
	$this->db->where('cus_damage_item.product_id',$product_id);
    $query = $this->db->get('cus_damage_item');
    return $query->result_array();
}	
function get_all_username() {
		
		$this->db->select('*');
		$this->db->from("user");
		
		$query = $this->db->get();
		
		return $query->result_array();

	}
function get_sale_id ($id){
	   $this->db->select('*');
	   $this->db->from('sales');
	   $this->db->where("sale_reference_no",$id);
	   $query = $this->db->get();
	   return $query->result_array();
	}	
	
}