Add InvoiceShell to update old invoices for their invoiced_amount
This commit is contained in:
parent
daec410797
commit
5dc842df9c
80
app/vendors/shells/invoice.php
vendored
Normal file
80
app/vendors/shells/invoice.php
vendored
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
2
run_update_invoices.sh
Normal file
2
run_update_invoices.sh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
su www-data -c "/var/www/cmc-sales/cake/console/cake -app /var/www/cmc-sales/app invoice"
|
||||
9
sql/002_add_columns_invoices.sql
Normal file
9
sql/002_add_columns_invoices.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
ALTER TABLE invoices
|
||||
ADD COLUMN amount_invoiced DECIMAL(10,2) NOT NULL,
|
||||
ADD COLUMN amount_received DECIMAL(10,2) NOT NULL,
|
||||
ADD COLUMN comments TEXT;
|
||||
|
||||
ALTER TABLE invoices
|
||||
DROP COLUMN amount_invoiced,
|
||||
DROP COLUMN amount_received,
|
||||
DROP COLUMN comments;
|
||||
Loading…
Reference in a new issue