cmc-sales/controllers/documents_controller.php

167 lines
5 KiB
PHP
Raw Normal View History

<?php
class DocumentsController extends AppController {
var $name = 'Documents';
var $helpers = array('Html', 'Form');
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);
$this->set('document', $document);
$docType = "";
if(!empty($document['Invoice']['id'])) {
$docType = "invoice";
}
elseif(!empty($document['Quote']['id'])) {
$docType = "quote";
2011-05-16 20:19:32 -07:00
$enquiry = $this->Document->Quote->Enquiry->read(null, $document['Quote']['enquiry_id']);
$this->set('enquiry',$enquiry);
}
elseif(!empty($document['PurchaseOrder']['id'])) {
$docType = "purchaseOrder";
}
2011-05-16 20:19:32 -07:00
$this->data = $document;
$this->set('docType',$docType);
2011-05-18 16:46:22 -07:00
$this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
}
function getProducts($principleID = null) {
$this->layout = 'ajax';
if($principleID == null) {
echo "INVALID PRINCIPLE ID";
}
}
/**
* 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();
2011-05-16 20:19:32 -07:00
$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'];
2011-05-16 20:19:32 -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) {
$enquiryid = $id;
$this->Document->create();
$enquiry = $this->Document->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']['customer_id'] = $enquiry['Enquiry']['customer_id'];
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['contact_id']= $enquiry['Enquiry']['contact_id'];
$this->data['Page'][0]['page_number'] = 1;
$this->data['Page'][0]['contentPage'] = 0;
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));
}
}
2011-05-18 16:46:22 -07:00
function edit($id = null) {
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));
$this->Document->DocPage->updateCounterCache(array('document_id'=>$docID));
2011-05-16 20:19:32 -07:00
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-16 20:19:32 -07:00
}
/*function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for Document', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Document->del($id)) {
$this->Session->setFlash(__('Document deleted', true));
$this->redirect(array('action'=>'index'));
}
}*/
}
?>