cmc-sales/controllers/documents_controller.php

324 lines
9 KiB
PHP
Raw Normal View History

<?php
class DocumentsController extends AppController {
var $name = 'Documents';
2011-06-19 18:20:44 -07:00
var $helpers = array('Html', 'Form', 'Number', 'Decimal');
var $paginate = array(
'contain' => false,
'limit' => 200,
'order'=>array('Document.id' => 'desc')
);
function index() {
2011-05-19 00:05:01 -07:00
$this->Document->recursive = 0;
$this->set('documents', $this->paginate());
$this->set('users', $this->Document->User->getUsersList());
}
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'));
}
$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);
2011-05-19 00:05:01 -07:00
$this->data = $document;
$docType = $this->Document->getDocType($document);
$this->set('docType', $docType);
$this->set('docTypeFullName', $this->Document->getDocFullName($docType));
$this->set('users', $this->Document->User->getUsersList());
$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);
}
}
/**
* 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();
2011-05-19 00:05:01 -07:00
$enquiry = $this->Document->Quote->Enquiry->findById($id);
2011-05-19 00:05:01 -07:00
$this->data['Document']['enquiry_id'] = $enquiryid;
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;
$this->data['Quote']['date_issued'] = date('Y-m-d');
$this->data['Quote']['currency_id'] = 2; //All new Quotes default to AUD.
2011-05-19 00:05:01 -07:00
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['enquiry_id'] = $enquiryid;
$this->data['Document']['type'] = 'quote';
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));
}
}
/**
* 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();
$enquiry = $this->Document->Quote->Enquiry->findById($id);
2011-05-19 00:05:01 -07:00
$this->data['Document']['enquiry_id'] = $enquiryid;
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();
2011-05-19 00:05:01 -07:00
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices;
2011-05-19 00:05:01 -07:00
$this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['type'] = 'invoice';
2011-05-19 00:05:01 -07:00
if ($this->Document->saveAll($this->data)) {
$newid = $this->Document->id;
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));
}
}
2011-05-18 16:46:22 -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';
}
}
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']);
$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']);
//If this is a quote, set the filename
//@TODO FIX THIS FOR OTHER DOCTYPES
if($document['Quote']['revision'] > 0) {
$filename = $enquiry['Enquiry']['title'].'rev'.$document['Quote']['revision'].'.pdf';
}
else {
$filename = $enquiry['Enquiry']['title'].'.pdf';
}
$this->set('filename', $filename);
$document['Document']['pdf_filename'] = $filename;
$document['Document']['pdf_created_at'] = date('Y-m-d H:i:s');
$document['Document']['pdf_created_by_user_id'] = $this->getCurrentUserID();
if($this->Document->save($document)) {
//echo "Set pdf_filename attritbute to: ".$filename;
}
else {
//echo 'Failed to set pdf_filename to: '.$filename;
}
$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);
}
}
?>