From 5dc842df9c4c08d54ffadc33d2f314c21c2b8673 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Tue, 21 Mar 2023 22:13:52 +1100 Subject: [PATCH] Add InvoiceShell to update old invoices for their invoiced_amount --- app/vendors/shells/invoice.php | 80 ++++++++++++++++++++++++++++++++ run_update_invoices.sh | 2 + sql/002_add_columns_invoices.sql | 9 ++++ 3 files changed, 91 insertions(+) create mode 100644 app/vendors/shells/invoice.php create mode 100644 run_update_invoices.sh create mode 100644 sql/002_add_columns_invoices.sql diff --git a/app/vendors/shells/invoice.php b/app/vendors/shells/invoice.php new file mode 100644 index 00000000..27eb892e --- /dev/null +++ b/app/vendors/shells/invoice.php @@ -0,0 +1,80 @@ +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"]); + } + } + } + + } + + diff --git a/run_update_invoices.sh b/run_update_invoices.sh new file mode 100644 index 00000000..46970422 --- /dev/null +++ b/run_update_invoices.sh @@ -0,0 +1,2 @@ +#!/bin/bash +su www-data -c "/var/www/cmc-sales/cake/console/cake -app /var/www/cmc-sales/app invoice" diff --git a/sql/002_add_columns_invoices.sql b/sql/002_add_columns_invoices.sql new file mode 100644 index 00000000..f7b77a1d --- /dev/null +++ b/sql/002_add_columns_invoices.sql @@ -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;