cmc-sales/controllers/documents_controller.php

315 lines
8.7 KiB
PHP
Executable file

<?php
class DocumentsController extends AppController {
var $name = 'Documents';
var $helpers = array('Html', 'Form', 'Number', 'Decimal');
function index() {
$this->Document->recursive = 0;
$this->set('documents', $this->paginate());
}
function view($id = null) {
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));
$this->set('document', $document);
$this->data = $document;
$this->set('docType',$this->Document->getDocType($document));
$this->set('currency', $this->Document->getCurrency($document));
$this->set('enquiry', $this->Document->getEnquiry($document));
// $this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
}
function getProducts($principleID = null) {
$this->layout = 'ajax';
if($principleID == null) {
echo "INVALID PRINCIPLE ID";
}
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) {
$enquiryid = $id;
$this->Document->create();
$enquiry = $this->Document->Quote->Enquiry->findById($id);
$this->data['Document']['enquiry_id'] = $enquiryid;
$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['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['enquiry_id'] = $enquiryid;
$this->data['Document']['type'] = 'quote';
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) {
$enquiryid = $id;
$this->Document->create();
$enquiry = $this->Document->Quote->Enquiry->findById($id);
$this->data['Document']['enquiry_id'] = $enquiryid;
$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();
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices;
$this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['type'] = 'invoice';
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));
}
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Document', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
$docID = $this->data['Document']['id'];
$this->Document->DocPage->deleteAll(array('document_id'=>$docID));
$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);
}
}
function ajax_edit($id = null) {
$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));
$this->Document->DocPage->updateCounterCache(array('document_id'=>$docID));
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']);
$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']);
$colWidths = array(
'item' => '8%',
'qty' => '8%',
'desc' => '60%',
'unit' => '12%',
'total' => '12%'
);
$this->set('colWidths',$colWidths);
}
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);
}
}
?>