2010-07-07 23:18:04 -07:00
|
|
|
<?php
|
|
|
|
|
class DocumentsController extends AppController {
|
|
|
|
|
|
|
|
|
|
var $name = 'Documents';
|
2011-06-19 18:20:44 -07:00
|
|
|
var $helpers = array('Html', 'Form', 'Number', 'Decimal');
|
2010-07-07 23:18:04 -07:00
|
|
|
|
|
|
|
|
function index() {
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->Document->recursive = 0;
|
|
|
|
|
$this->set('documents', $this->paginate());
|
2010-07-07 23:18:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function view($id = null) {
|
2011-05-19 00:05:01 -07:00
|
|
|
if (!$id) {
|
|
|
|
|
$this->Session->setFlash(__('Invalid Document.', true));
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
}
|
2011-05-24 02:11:07 -07:00
|
|
|
$document = $this->Document->read(null,$id);
|
|
|
|
|
//$document = $this->Document->find('first',array('conditions'=>array('Document.id'=>$id), 'recursive'=>2));
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->set('document', $document);
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data = $document;
|
2011-05-24 02:11:07 -07:00
|
|
|
$this->set('docType',$this->Document->getDocType($document));
|
|
|
|
|
$this->set('currency', $this->Document->getCurrency($document));
|
|
|
|
|
$this->set('enquiry', $this->Document->getEnquiry($document));
|
2011-05-19 00:05:01 -07:00
|
|
|
// $this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
|
2011-05-18 16:46:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getProducts($principleID = null) {
|
|
|
|
|
$this->layout = 'ajax';
|
|
|
|
|
|
|
|
|
|
if($principleID == null) {
|
|
|
|
|
echo "INVALID PRINCIPLE ID";
|
|
|
|
|
}
|
2011-05-19 00:05:01 -07:00
|
|
|
else {
|
|
|
|
|
$products = $this->Document->LineItem->Product->find('list', array('conditions'=>array('Product.principle_id'=>$principleID)));
|
|
|
|
|
$this->set('products',$products);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a JSON object of the Product.
|
|
|
|
|
*/
|
|
|
|
|
function getProductDetails($productID = null) {
|
|
|
|
|
$this->layout = 'ajax';
|
|
|
|
|
|
|
|
|
|
if($productID == null) {
|
|
|
|
|
echo "INVALID PRODUCT ID";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$product = $this->Document->LineItem->Product->find('first',array('conditions'=>array('Product.id'=>$productID)));
|
|
|
|
|
$this->set('product',$product);
|
|
|
|
|
}
|
2010-07-07 23:18:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new Quote, along with its associated Document and the first two pages.
|
|
|
|
|
* @param int $id - the Enquiry ID that this quote belongs to.
|
|
|
|
|
*/
|
|
|
|
|
function newQuote($id = null) {
|
2011-05-19 00:05:01 -07:00
|
|
|
$enquiryid = $id;
|
|
|
|
|
$this->Document->create();
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$enquiry = $this->Document->Quote->Enquiry->findById($id);
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Document']['enquiry_id'] = $enquiryid;
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Quote']['enquiry_id'] = $enquiryid;
|
|
|
|
|
$number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid);
|
|
|
|
|
$this->data['Quote']['revision'] = $number_of_revisions;
|
2011-08-08 21:41:46 -07:00
|
|
|
$this->data['Quote']['date_issued'] = date('Y-m-d');
|
|
|
|
|
$this->data['Quote']['currency_id'] = 2; //All new Quotes default to AUD.
|
|
|
|
|
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
|
2011-05-24 02:11:07 -07:00
|
|
|
$this->data['Document']['enquiry_id'] = $enquiryid;
|
|
|
|
|
$this->data['Document']['type'] = 'quote';
|
2011-06-27 19:32:45 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
if ($this->Document->saveAll($this->data)) {
|
|
|
|
|
$newid = $this->Document->id;
|
|
|
|
|
|
|
|
|
|
$this->Session->setFlash(__('The Document has been saved', true));
|
|
|
|
|
$this->redirect(array('action'=>'view',$newid));
|
|
|
|
|
} else {
|
|
|
|
|
$this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
|
|
|
|
|
}
|
2010-07-07 23:18:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new Quote, along with its associated Document and the first two pages.
|
|
|
|
|
* @param int $id - the Enquiry ID that this quote belongs to.
|
|
|
|
|
*/
|
|
|
|
|
function newInvoice($id = null) {
|
2011-05-19 00:05:01 -07:00
|
|
|
$enquiryid = $id;
|
|
|
|
|
$this->Document->create();
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-24 02:11:07 -07:00
|
|
|
$enquiry = $this->Document->Quote->Enquiry->findById($id);
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Document']['enquiry_id'] = $enquiryid;
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Invoice']['enquiry_id'] = $enquiryid;
|
|
|
|
|
$invoice_number_offset = 4436; //What Invoice number we were up to before the new system.
|
|
|
|
|
$number_of_invoices = $this->Document->Invoice->findCount();
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices;
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;
|
|
|
|
|
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
|
2011-05-24 02:11:07 -07:00
|
|
|
$this->data['Document']['type'] = 'invoice';
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
if ($this->Document->saveAll($this->data)) {
|
|
|
|
|
$newid = $this->Document->id;
|
2010-07-07 23:18:04 -07:00
|
|
|
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->Session->setFlash(__('The Document has been saved', true));
|
|
|
|
|
$this->redirect(array('action'=>'view',$newid));
|
|
|
|
|
} else {
|
|
|
|
|
$this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
|
|
|
|
|
}
|
2010-07-07 23:18:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 16:46:22 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-07 23:18:04 -07:00
|
|
|
|
|
|
|
|
function edit($id = null) {
|
2011-05-19 00:05:01 -07:00
|
|
|
if (!$id && empty($this->data)) {
|
|
|
|
|
$this->Session->setFlash(__('Invalid Document', true));
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
}
|
|
|
|
|
if (!empty($this->data)) {
|
2011-05-18 16:46:22 -07:00
|
|
|
$docID = $this->data['Document']['id'];
|
|
|
|
|
|
|
|
|
|
$this->Document->DocPage->deleteAll(array('document_id'=>$docID));
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->Document->DocPage->updateCounterCache(array('document_id'=>$docID));
|
|
|
|
|
if ($this->Document->saveAll($this->data)) {
|
|
|
|
|
$this->Session->setFlash(__('The Document has been saved', true));
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
} else {
|
|
|
|
|
$this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (empty($this->data)) {
|
|
|
|
|
$this->data = $this->Document->read(null, $id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 16:46:22 -07:00
|
|
|
|
2011-05-24 02:11:07 -07:00
|
|
|
function ajax_edit($id = null) {
|
2011-05-19 00:05:01 -07:00
|
|
|
$this->layout = 'ajax';
|
|
|
|
|
|
|
|
|
|
if (!$id && empty($this->data)) {
|
|
|
|
|
echo 'Invalid Document ID';
|
|
|
|
|
}
|
|
|
|
|
if (!empty($this->data)) {
|
|
|
|
|
$docID = $this->data['Document']['id'];
|
|
|
|
|
$this->Document->DocPage->deleteAll(array('document_id'=>$docID));
|
2011-05-18 16:46:22 -07:00
|
|
|
$this->Document->DocPage->updateCounterCache(array('document_id'=>$docID));
|
2011-05-19 00:05:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->Document->saveAll($this->data)) {
|
|
|
|
|
echo 'SUCCESS';
|
|
|
|
|
} else {
|
|
|
|
|
echo 'FAILURE';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
echo 'FAILURE';
|
|
|
|
|
}
|
2010-07-07 23:18:04 -07:00
|
|
|
}
|
|
|
|
|
|
2011-05-24 02:11:07 -07:00
|
|
|
|
|
|
|
|
function pdf($id = null) {
|
|
|
|
|
//Configure::write('debug',0);
|
|
|
|
|
if(!$id) {
|
|
|
|
|
$this->Session->setFlash(__('Invalid Document', true));
|
|
|
|
|
$this->redirect(array('controller'=>'documents', 'action'=>'index'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$document = $this->Document->read(null,$id);
|
|
|
|
|
|
|
|
|
|
$this->set('document', $document);
|
|
|
|
|
|
|
|
|
|
$this->data = $document;
|
|
|
|
|
|
|
|
|
|
$docType = $this->Document->getDocType($document);
|
|
|
|
|
$docTypeFullName = $this->Document->getDocFullName($docType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$currency = $this->Document->getCurrency($document);
|
|
|
|
|
$enquiry = $this->Document->getEnquiry($document);
|
|
|
|
|
|
|
|
|
|
$this->set('docType', $docType);
|
|
|
|
|
$this->set('docTypeFullName',strtoupper($docTypeFullName));
|
|
|
|
|
$this->set('currency',$currency);
|
|
|
|
|
$this->set('currencyCode', $currency['Currency']['iso4217']);
|
2011-06-23 00:03:24 -07:00
|
|
|
$this->set('currencySymbol', $currency['Currency']['symbol']);
|
2011-05-24 02:11:07 -07:00
|
|
|
|
|
|
|
|
$gst = $enquiry['Enquiry']['gst'];
|
|
|
|
|
|
|
|
|
|
$this->set('gst', $gst);
|
|
|
|
|
|
|
|
|
|
if($gst == 1) {
|
|
|
|
|
$totalsDescText = array(
|
|
|
|
|
'subtotal' => 'SUB-TOTAL (EXCLUDING GST)',
|
|
|
|
|
'gst' => 'GST (10%)',
|
|
|
|
|
'total' => 'TOTAL PAYABLE (INCLUDING GST)'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$totalsDescText = array(
|
|
|
|
|
'subtotal' => 'SUB-TOTAL',
|
|
|
|
|
'gst' => 'GST (10%)',
|
|
|
|
|
'total' => 'TOTAL PAYABLE'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$totals = $this->calculateTotals($document, $gst);
|
|
|
|
|
$this->set('totals',$totals);
|
|
|
|
|
$this->set('totalsDescText',$totalsDescText);
|
|
|
|
|
/**
|
|
|
|
|
* Call the commercial Comments. Need to have already figured out what page to do this on..
|
|
|
|
|
*
|
|
|
|
|
* * Passing a data structure in the following format.
|
|
|
|
|
*
|
|
|
|
|
* $details = array(
|
|
|
|
|
* 'deliveryTime' => (String) eg. 2-3
|
|
|
|
|
* 'paymentTerms => (String) eg. 100% PAYMENT WITH ORDER || NET 30 DAYS FOR APPROVED ACCOUNTS
|
|
|
|
|
* 'dateIssued' => (String) eg. 2009-05-20
|
|
|
|
|
* 'daysValid' => (Int) eg. 30
|
|
|
|
|
* 'deliveryPoint' => (String) eg. EX-CMC Technologies, NSW. || EX-SUPPLIER NAME
|
|
|
|
|
* 'exchangeRate' => (String) eg. FIXED
|
|
|
|
|
* 'customsDuty' => (String) eg. NIL || 5%
|
|
|
|
|
* 'gst' => (String) eg. 10% EXTRA || Not Applicable for Export
|
|
|
|
|
* 'salesEngineer' => (String) eg. Karl Cordes || Con Carpis || Kenan Fernandes etc
|
|
|
|
|
* );
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if($enquiry['Enquiry']['gst'] == 1) {
|
|
|
|
|
$gstPhrase = "10% EXTRA";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$gstPhrase = "Not Applicable for Export";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$commercialDetails = array (
|
|
|
|
|
'deliveryTime' => strtoupper($document['Quote']['delivery_time']),
|
|
|
|
|
'paymentTerms' => strtoupper($document['Quote']['payment_terms']),
|
|
|
|
|
'dateIssued' => $document['Quote']['date_issued'],
|
|
|
|
|
'validFor' => $document['Quote']['days_valid'],
|
|
|
|
|
'deliveryPoint' => strtoupper($document['Quote']['delivery_point']),
|
|
|
|
|
'exchangeRate' => strtoupper($document['Quote']['exchange_rate']),
|
|
|
|
|
'customsDuty' => strtoupper($document['Quote']['customs_duty']),
|
|
|
|
|
'gst' => strtoupper($gstPhrase),
|
|
|
|
|
'salesEngineer' => strtoupper($document['User']['first_name'].' '.$document['User']['last_name'])
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->set('commercialDetails', $commercialDetails);
|
|
|
|
|
|
|
|
|
|
//debug($quote);
|
|
|
|
|
//debug($commercialDetails);
|
|
|
|
|
|
|
|
|
|
$this->set('currency', $currency);
|
|
|
|
|
$this->set('enquiry', $enquiry);
|
|
|
|
|
|
|
|
|
|
$this->set('principlesList', $this->Document->LineItem->Product->Principle->find('list'));
|
|
|
|
|
|
|
|
|
|
$this->set('products', $document['LineItem']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$colWidths = array(
|
|
|
|
|
'item' => '8%',
|
|
|
|
|
'qty' => '8%',
|
|
|
|
|
'desc' => '60%',
|
|
|
|
|
'unit' => '12%',
|
|
|
|
|
'total' => '12%'
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->set('colWidths',$colWidths);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-15 01:26:22 -07:00
|
|
|
function generateFirstPage($id = null) {
|
|
|
|
|
$this->layout = 'ajax';
|
|
|
|
|
|
|
|
|
|
if(!$id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$document = $this->Document->read(null, $id);
|
|
|
|
|
$this->set('document',$document);
|
|
|
|
|
|
|
|
|
|
$enquiry = $this->Document->Quote->Enquiry->read(null, $document['Quote']['enquiry_id']);
|
|
|
|
|
$this->set('enquiry', $enquiry);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2010-07-07 23:18:04 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
?>
|