cmc-sales/app/vendors/shells/invoice.php

81 lines
2.1 KiB
PHP

<?php
/**
* file: invoice.php
*/
class InvoiceShell extends Shell {
var $uses = array('Invoice', 'Document', 'LineItem');
// Adding or editing an Invoice means we need to update the invoiced_amount column
// issue #54
function updateInvoice($documentID) {
// If we've changed a line_item for an invoice, update the invoiced amount
// on the invoices table.
$document = $this->Document->find('first', array('conditions' => array('Document.id' => $documentID), 'recursive'=>1));
if(count($document["LineItem"]) == 0) {
echo "No line items for DocumentID: ".$documentID."\n";
return;
}
//print_r($document);
// 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'];
print_r($totals);
echo "Saving invoice ".$invoice["Invoice"]["id"]."\n";
$this->LineItem->Document->Invoice->save($invoice);
}
}
function calculateTotals($document, $gst) {
$totals = array('subtotal'=>0, 'gst'=>0, 'total'=>0);
foreach($document['LineItem'] as $lineitem) {
if($lineitem['option'] == 1) {
$totals['subtotal'] = 'TBA';
$totals['total'] = 'TBA';
$totals['gst'] = 'TBA';
return $totals;
}
else {
$totals['subtotal'] += $lineitem['net_price'];
}
}
if($gst == 1) {
$totals['gst'] = 0.1*$totals['subtotal'];
}
$totals['total'] = $totals['gst'] + $totals['subtotal'];
return $totals;
}
function main() {
$q = "SELECT id, document_id FROM invoices i";
$results = $this->Invoice->query($q);
foreach($results as $row) {
if($row["i"]["document_id"]) {
$id = $row["i"]["document_id"];
//echo $id."\n";
$this->updateInvoice($row["i"]["document_id"]);
}
}
}
}