cmc-sales/controllers/invoices_controller.php

103 lines
3.4 KiB
PHP
Raw Normal View History

<?php
class InvoicesController extends AppController {
var $name = 'Invoices';
var $helpers = array('Html', 'Form', 'Time');
var $paginate = array(
'limit' => 100,
'contain' => array('Customer')
);
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'];
2010-05-17 23:45:38 -07:00
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);
2010-07-15 19:43:23 -07:00
$jobs = $this->Invoice->Job->find('list', array('conditions'=>array('Job.enquiry_id'=>$enquiryid)));
2010-05-17 23:45:38 -07:00
$user = $this->Auth->user();
2010-07-15 19:43:23 -07:00
$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)) {
$this->data = $this->Invoice->read(null, $id);
}
$enquiries = $this->Invoice->Enquiry->find('list');
$users = $this->Invoice->User->find('list');
$this->set(compact('enquiries','users'));
}
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'));
}
}
}
?>