<?php
require_once (PATH_TABLES . "campaign/CampaignHistoryTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

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

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