false, 'limit' => 200, 'order'=>array('Document.id' => 'desc') ); function index() { $this->Document->recursive = 0; $this->set('documents', $this->paginate()); $this->set('users', $this->Document->User->getUsersList()); } 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; $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)); switch($docType) { case 'quote': $docTypeElement = 'document_quote_view'; break; case 'invoice': $docTypeElement = 'document_invoice_view'; break; case 'purchaseOrder': $docTypeElement = 'document_purchase_order_view'; break; } $this->set('docTypeElement', $docTypeElement); // $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 Document * * * @param $type * @param $id */ function newDocument($type = null, $enquiryid = null, $jobid = 0) { if(!$type) { $this->Session->setFlash('Invalid Doc Type or ID'); } $this->Document->create(); switch($type) { case "quote": $enquiry = $this->Document->Quote->Enquiry->findById($enquiryid); if(!$enquiry) { $this->Session->setFlash('Invalid Enquiry ID'); $this->redirect(array('action'=>'index')); return; } $this->data['Quote']['enquiry_id'] = $enquiryid; $number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid); $this->data['Document']['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. $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id']; $this->data['Document']['type'] = 'quote'; break; case "invoice": $invoice_number_offset = 4436; //What Invoice number we are up to. Starting at 4500 due to the data loss. $number_of_invoices = $this->Document->Invoice->findCount(); $enquiry = $this->Document->Invoice->Enquiry->read(null, $enquiryid); if(!$enquiry) { $this->Session->setFlash('Invalid Enquiry ID'); $this->redirect(array('action'=>'index')); return; } $newInvoiceNumber = $invoice_number_offset + $number_of_invoices; $this->data['Invoice']['enquiry_id'] = $enquiryid; $this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber; $this->data['Invoice']['job_id'] = $jobid; $this->data['Invoice']['customer_id'] = $enquiry['Enquiry']['customer_id']; $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id']; //$number_of_revisions = $this->Document->Invoice->findCount('Invoice.enquiry_id ='. $enquiryid); $this->data['Document']['revision'] = 0; //No revisions for Invoices. I have decreed it! $this->data['Document']['type'] = 'invoice'; $this->data['Invoice']['issue_date'] = date('Y-m-d'); //Argh. Quote model uses 'date_issed'. $this->data['Document']['currency_id'] = 2; //All new Invoices default to AUD. break; case "purchaseOrder": break; case "orderAck": break; } 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)); } } /** * Revise a Document. * * @param int $id - the document ID we want to make a new revision of. * @return */ function revise($id = null) { if(!$id) { $this->Session->setFlash('Invalid Document ID'); return; } $document = $this->Document->find('first', array('conditions' => array('Document.id' => $id))); $this->set('document', $document); if(!empty($document['Invoice']['id'])) { echo "WE HAVE AN INVOICE"; } else if (!empty($document['Quote']['id'])) { //First setup the Date and default valid until date. (30 days from today). $today_date = date("Y-m-d"); $valid_until = date("Y-m-d", strtotime("+30 days")); $newDoc = array(); $newDoc['Quote'] = $this->unset_keys($document['Quote'], array('id','created')); $newDoc['Quote']['date_issued'] = $today_date; $newDoc['Quote']['days_valid'] = 30; $newDoc['Quote']['valid_until'] = $valid_until; $enquiryid = $document['Quote']['enquiry_id']; $number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid); $newDoc['Document']['revision'] = $number_of_revisions; $newDoc['Document']['type'] = 'quote'; $newDoc['DocPage'] = $document['DocPage']; foreach($newDoc['DocPage'] as $index => $li) { $newDoc['DocPage'][$index]['id'] = null; $newDoc['DocPage'][$index]['document_id'] = null; } } else if (!empty($document['PurchaseOrder']['id'])) { echo "WE ARE REVISING A PO"; } else if (!empty($document['OrderAcknowledgement']['id'])) { echo "WE ARE REVISING An ORDER ACK"; } /** Copy the LineItems from this Document. Applies to all of the Document Types */ if(!empty($document['LineItem'])) { $newDoc['LineItem'] = $document['LineItem']; foreach($newDoc['LineItem'] as $index => $li) { $newDoc['LineItem'][$index]['id'] = null; $newDoc['LineItem'][$index]['document_id'] = null; $newDoc['LineItem'][$index]['costing_id'] = null; } } $this->set('newDoc', $newDoc); if ($this->Document->saveAll($newDoc)) { $newid = $this->Document->id; $this->Session->setFlash(__("Revision {$number_of_revisions} created", 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 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']); //If this is a quote, set the filename //@TODO FIX THIS FOR OTHER DOCTYPES if($document['Document']['revision'] > 0) { $filename = $enquiry['Enquiry']['title'].'_'.$document['Document']['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); } //Called via AJAX to generate Quotation First Pages. 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); } } ?>