<?php
require_once (PATH_TABLES . "corporate_gift/CorporateGiftHistoryTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class CorporateGiftHistoryClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= CorporateGiftHistoryTable::C_TABLE;
	}
	function __destruct() {
	}

	function getHistoryReport($customer_id, $contact_id, $delivery_from, $delivery_to, $gift){
		$sql = "SELECT * FROM " . CorporateGiftHistoryTable::C_TABLE . " his " .
				" LEFT JOIN " . CustomerTable::C_TABLE . " cus ON his." . CorporateGiftHistoryTable::C_CUSTOMER_ID . " = cus." . CustomerTable::C_ID .
				" LEFT JOIN " . CustomerContactTable::C_TABLE . " con ON his." . CorporateGiftHistoryTable::C_CONTACT_ID . " = con." . CustomerContactTable::C_CONTACT_ID .
				" LEFT JOIN " . CorporateGiftTable::C_TABLE . " gif ON his." . CorporateGiftHistoryTable::C_GIFT_MASTER_ID . " = gif." . CorporateGiftTable::C_ID . 
				" LEFT JOIN " . CorporateGiftDetailTable::C_TABLE . " det ON his." . CorporateGiftHistoryTable::C_CHOSEN_ITEM_ID . " = det." . CorporateGiftDetailTable::C_DETAIL_ID . 
					" WHERE his." . CorporateGiftHistoryTable::C_ID . " = " . $gift;
		$filter = "";
		if($customer_id != 0 && $customer_id != ""){
			$filter .= " AND " . CorporateGiftHistoryTable::C_CUSTOMER_ID . " = " . $customer_id;
		}
		if($contact_id != 0 && $contact_id != ""){
			$filter .= " AND " . CorporateGiftHistoryTable::C_CONTACT_ID . " = " . $contact_id;
		}
		if($delivery_from != 0 && $delivery_from != ""){
			$filter .= " AND " . CorporateGiftHistoryTable::C_DELIVERY_DATE . " >= '" . $delivery_from . "'";
		}
		if($delivery_to != 0 && $delivery_to != ""){
			$filter .= " AND " . CorporateGiftHistoryTable::C_DELIVERY_DATE . " <= '" . $delivery_to . "'";
		}
		return $this->db->getTable($sql . $filter);
	}
}
