cmc-sales/app/controllers/line_items_controller.php

133 lines
3.4 KiB
PHP
Raw Normal View History

<?php
class LineItemsController extends AppController {
2014-04-20 22:35:39 -07:00
var $name = 'LineItems';
var $helpers = array('Html', 'Form', 'Javascript', 'Number', 'Decimal');
2014-04-20 22:35:39 -07:00
var $components = array('RequestHandler');
2014-04-20 22:35:39 -07:00
function ajax_add() {
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->LineItem->create($this->data);
2014-04-20 22:35:39 -07:00
if ($this->LineItem->save($this->data)) {
2023-01-08 17:16:02 -08:00
echo "SUCCESS"; // matching on strings rather than HTTP status codes :(
$this->updateInvoice($this->data['LineItem']['document_id']);
2014-04-20 22:35:39 -07:00
}
else {
echo "FAILURE";
//print_r($this->data);
}
}
else {
echo "NO-DATA";
}
2011-05-19 00:05:01 -07:00
}
2014-04-20 22:35:39 -07:00
function ajax_edit() {
$this->layout = 'ajax';
if(!empty($this->data)) {
2014-04-20 22:35:39 -07:00
if ($this->LineItem->save($this->data)) {
echo "SUCCESS";
2023-01-08 17:16:02 -08:00
$this->updateInvoice($this->data['LineItem']['document_id']);
2014-04-20 22:35:39 -07:00
}
else {
echo "FAILURE";
}
}
else {
echo "NO-DATA";
}
}
2014-04-20 22:35:39 -07:00
function ajax_delete($id = null) {
$this->layout = 'ajax';
if($id == null) {
echo "FAILURE";
}
else {
if ($this->LineItem->del($id)) {
echo "SUCCESS";
2023-01-08 17:16:02 -08:00
$this->updateInvoice($this->data['LineItem']['document_id']);
2014-04-20 22:35:39 -07:00
}
else {
echo "FAILURE";
}
}
2011-05-19 00:05:01 -07:00
}
2014-04-20 22:35:39 -07:00
/**
* Get the products table with prices (for quotes, invoices etc)
* @param <type> $documentID
*/
function getTable($documentID = null) {
$this->layout = 'ajax';
if($documentID == null) {
echo "INVALID DOCUMENT ID";
}
else {
$document = $this->LineItem->Document->find('first',array('conditions'=>array('Document.id'=>$documentID)));
$this->set('document',$document);
$enquiry = $this->LineItem->Document->getEnquiry($document);
$this->set('enquiry', $enquiry);
$gst = $this->LineItem->Document->gstApplies($enquiry);
$this->set('gst',$gst);
$this->set('totals', $this->calculateTotals($document, $gst));
$currency = $this->LineItem->Document->getCurrency($document);
$this->set('currencyCode', $currency['Currency']['iso4217']);
$this->set('currencySymbol', $currency['Currency']['symbol']);
}
2011-05-19 00:05:01 -07:00
}
2014-04-20 22:35:39 -07:00
function edit($id = null) {
$this->layout = 'ajax';
2014-04-20 22:35:39 -07:00
$this->data = $this->LineItem->read(null,$id);
$this->set('yesNo', array(0=>'No', 1=>'Yes'));
$this->set('principles', $this->LineItem->Product->Principle->find('list'));
2014-04-20 22:35:39 -07:00
}
function add($documentID = null) {
$this->layout = 'ajax';
$document = $this->LineItem->Document->read(null, $documentID);
$this->set('document', $document);
$this->set('yesNo', array(0=>'No', 1=>'Yes'));
$this->set('principles', $this->LineItem->Product->Principle->find('list'));
}
2023-03-21 04:13:03 -07:00
// Adding or editing an Invoice means we need to update the invoiced_amount column
2023-01-08 17:16:02 -08:00
// issue #54
function updateInvoice($documentID) {
2023-03-21 04:13:03 -07:00
2023-01-08 17:16:02 -08:00
// If we've changed a line_item for an invoice, update the invoiced amount
// on the invoices table.
$document = $this->LineItem->Document->find('first', array('conditions' => array('Document.id' => $documentID), 'recursive'=>1));
// For invoices, we need to save the calculated invoice total to implement issue #54.
if($document['Document']['type'] == 'invoice') {
$enquiry = $this->LineItem->Document->getEnquiry($document);
$invoice = $this->LineItem->Document->Invoice->find('first', array('conditions'=>array('Invoice.id'=>$document['Invoice']['id'])));
$totals = $this->calculateTotals($document, $enquiry['Enquiry']['gst']);
$invoice['Invoice']['amount_invoiced'] = $totals['total'];
$this->LineItem->Document->Invoice->save($invoice);
}
}
2023-03-21 04:13:03 -07:00
}
?>