cmc-sales/controllers/invoices_controller.php
2010-07-16 13:56:25 +10:00

107 lines
3.5 KiB
PHP

<?php
class InvoicesController extends AppController {
var $name = 'Invoices';
var $helpers = array('Html', 'Form', 'Time');
var $paginate = array(
'limit' => 300,
'contain' => array('Customer'),
'order'=>array('Invoice.id' => 'desc')
);
function index() {
$this->Invoice->recursive = 0;
$this->set('invoices', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Invoice.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('invoice', $this->Invoice->read(null, $id));
}
function add() { //No longer creating invoices directly. Done through the Documents Controller
if (!empty($this->data)) {
$this->Invoice->create();
$invoice_number_offset = 4436; //What Invoice number we are up to. Starting at 4500 due to the data loss.
$number_of_invoices = $this->Invoice->findCount();
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices;
$this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;
$enqid = $this->data['Invoice']['enquiry_id'];
if ($this->Invoice->save($this->data)) {
$this->Session->setFlash(__('The Invoice has been saved', true));
$this->redirect(array('controller'=>'enquiries', 'action'=>'view/'.$enqid));
} else {
$this->Session->setFlash(__('The Invoice could not be saved. Please, try again.', true));
}
}
else {
if(isset($this->params['named']['enquiryid'])) {
$enquiryid = $this->params['named']['enquiryid'];
$enquiry = $this->Invoice->Enquiry->findById($enquiryid);
$jobs = $this->Invoice->Job->find('list', array('conditions'=>array('Job.enquiry_id'=>$enquiryid)));
$user = $this->Auth->user();
$this->set(compact('enquiry', 'user','jobs'));
}
else {
$this->Session->setFlash(__('Invalid Enquiry ID', true));
$this->redirect(array('action'=>'index'));
}
}
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Invoice', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
if ($this->Invoice->save($this->data)) {
$this->Session->setFlash(__('The Invoice has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Invoice could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$invoice = $this->Invoice->read(null, $id);
$this->data = $invoice;
}
$users = $this->Invoice->User->find('list');
$jobs = $this->Invoice->Job->find('list', array('conditions'=>array('Job.enquiry_id'=>$invoice['Invoice']['enquiry_id'])));
$this->set(compact('users', 'jobs'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for Invoice', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Invoice->del($id)) {
$this->Session->setFlash(__('Invoice deleted', true));
$this->redirect(array('action'=>'index'));
}
}
}
?>