cmc-sales/models/document.php
2011-08-08 17:25:55 +10:00

160 lines
3.8 KiB
PHP
Executable file

<?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'
)
);
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'
)
);
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);
}
}
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;
}
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['PurchaseOrder']['id'])) {
return $this->PurchaseOrder->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Invoice']['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;
}
}
}
?>