cmc-sales/php/app/models/document.php

196 lines
4.8 KiB
PHP
Raw Permalink Normal View History

<?php
class Document extends AppModel {
2013-04-13 03:01:47 -07:00
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'
2013-06-10 02:27:23 -07:00
),
'PackingList' => array(
'className' => 'PackingList',
'foreignKey' => 'document_id'
2013-04-13 03:01:47 -07:00
)
);
//So much refactoring possible on this.
//Leaving it for now.
2013-04-13 03:01:47 -07:00
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);
}
}
2013-04-13 03:01:47 -07:00
function __getCurrencyConditions($currencyID) {
return array('recursive'=>0, 'conditions'=>array('Currency.id'=>$currencyID));
}
2013-04-13 03:01:47 -07:00
function getDocType($document) {
return $document['Document']['type'];
}
2013-04-13 03:01:47 -07:00
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;
2013-06-10 02:27:23 -07:00
case 'packingList':
$fullName = 'Packing List';
break;
2013-04-13 03:01:47 -07:00
}
return $fullName;
}
2013-04-13 03:01:47 -07:00
/**
* 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;
}
2013-04-13 03:01:47 -07:00
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'])));
}
2013-06-10 02:27:23 -07:00
elseif(!empty($document['PackingList']['id'])) {
return $this->PackingList->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['PackingList']['enquiry_id'])));
}
2013-04-13 03:01:47 -07:00
elseif(!empty($document['PurchaseOrder']['id'])) {
//No enquiries for POs. We want the Jobs.
return;
2013-04-13 03:01:47 -07:00
}
elseif(!empty($document['OrderAcknowledgement']['id'])) {
return $this->OrderAcknowledgement->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['OrderAcknowledgement']['enquiry_id'])));
}
}
2013-04-13 03:01:47 -07:00
/**
* 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;
}
}
2013-04-13 03:01:47 -07:00
function isLatestRevision($enquiry) {
$number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiry);
2013-04-13 03:01:47 -07:00
}
}
2012-11-18 12:19:55 -08:00
?>