<?php
class Document extends AppModel {

	var $name = 'Document';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(

		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)

	);

	var $hasMany = array(

		'LineItem' => array(
			'className' => 'LineItem',
			'foreignKey' => 'document_id',
			'order' => 'LineItem.item_number ASC'
		),

		'DocPage' => array(
			'className'=> 'DocPage',
			'foreignKey' => 'document_id',
			'order' => 'DocPage.page_number ASC'
		),

		'DocumentAttachment' => array(
			'className'=> 'DocumentAttachment',
			'foreignKey' => 'document_id',
			'order' => 'DocumentAttachment.id ASC'
		),

	);


	var $hasOne = array(
		'Invoice'   => array(
			'className' => 'Invoice',
			'foreignKey'=> 'document_id'
		),
		'Quote' => array(
			'className'=>'Quote',
			'foriegnKey'=>'document_id'
		),

		'PurchaseOrder' => array(
			'className'=>'PurchaseOrder',
			'foreignKey'=>'document_id'
		),

		'OrderAcknowledgement' => array(
			'className' => 'OrderAcknowledgement',
			'foreignKey' => 'document_id'
		),
		'PackingList' => array(
			'className' => 'PackingList',
			'foreignKey' => 'document_id'
		)
	);

	//So much refactoring possible on this.
	//Leaving it for now.
	function getCurrency($document) {
		if(!empty($document['Invoice']['id'])) {
			$currencyID = $document['Invoice']['currency_id'];
			$conditions = $this->__getCurrencyConditions($currencyID);

			return $this->Invoice->Currency->find('first',$conditions);

		}
		elseif(!empty($document['Quote']['id'])) {
			$currencyID = $document['Quote']['currency_id'];
			$conditions = $this->__getCurrencyConditions($currencyID);
			return $this->Quote->Currency->find('first',$conditions);

		}
		elseif(!empty($document['PurchaseOrder']['id'])) {
			$currencyID = $document['PurchaseOrder']['currency_id'];
			$conditions = $this->__getCurrencyConditions($currencyID);
			return $this->PurchaseOrder->Currency->find('first',$conditions);
		}
		elseif(!empty($document['OrderAcknowledgement']['id'])) {
			$currencyID = $document['OrderAcknowledgement']['currency_id'];
			$conditions = $this->__getCurrencyConditions($currencyID);
			return $this->OrderAcknowledgement->Currency->find('first',$conditions);
		}
		elseif(!empty($document['PackingList']['id'])) {
			$currencyID = $document['PackingList']['currency_id'];
			$conditions = $this->__getCurrencyConditions($currencyID);
			return $this->OrderAcknowledgement->Currency->find('first',$conditions);
		}	
	}


	function __getCurrencyConditions($currencyID) {
		return array('recursive'=>0, 'conditions'=>array('Currency.id'=>$currencyID));
	}

	function getDocType($document) {
		return $document['Document']['type'];
	}


	function getDocFullName($type) {

		switch($type) {
		case 'quote':
			$fullName = 'Quotation';
			break;
		case 'invoice':
			$fullName = 'Invoice';
			break;
		case 'purchaseOrder':
			$fullName = 'Purchase Order';
			break;
		case 'orderAck':
			$fullName = 'Order Acknowledgement';
			break;
		case 'packingList':
			$fullName = 'Packing List';
			break;
		}
		return $fullName;
	}

	/**
	 * Depending on the type of document. Return the CMC reference
	 * ie. Enquiry Number (Quote)
	 *     Invoice Number (Invoice)
	 * @param <type> $document
	 * @param <type> $type
	 */
	function getCMCReferenceNumber($document, $type) {
		switch($type) {
		case 'quote':
			break;
		case 'invoice':
			break;
		case 'purchaseOrder':
			break;
		case 'orderAck':
			break;
		}
		return $fullName;
	}


	function getEnquiry($document) {
		if(!empty($document['Invoice']['id'])) {
			return $this->Invoice->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Invoice']['enquiry_id'])));
		}
		elseif(!empty($document['Quote']['id'])) {
			return $this->Quote->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Quote']['enquiry_id'])));
		}
		elseif(!empty($document['PackingList']['id'])) {
			return $this->PackingList->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['PackingList']['enquiry_id'])));
		}
		elseif(!empty($document['PurchaseOrder']['id'])) {
			//No enquiries for POs. We want the Jobs.
			return; 
		}
		elseif(!empty($document['OrderAcknowledgement']['id'])) {
			return $this->OrderAcknowledgement->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['OrderAcknowledgement']['enquiry_id'])));

		}
	}
	/**
	 * Does GST apply on this document. Based on the Enquiry GST TinyInt.
	 * @param <type> $enquiry
	 * @return Bool
	 */
	function gstApplies($enquiry) {
		if ($enquiry['Enquiry']['gst'] == 0) {
			return false;
		}
		else {
			return true;
		}
	}

	function isLatestRevision($enquiry) {
		$number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiry);


	}


}
?>
