From a7236b950a176e758054747cfc12ddc81b5f5b77 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Mon, 9 Jan 2023 12:16:02 +1100 Subject: [PATCH 1/5] WIP --- app/controllers/documents_controller.php | 8 +++-- app/controllers/invoices_controller.php | 12 +++++-- app/controllers/line_items_controller.php | 22 +++++++++++-- app/views/invoices/edit.ctp | 2 +- app/views/invoices/index.ctp | 39 +++++++++++++---------- 5 files changed, 58 insertions(+), 25 deletions(-) diff --git a/app/controllers/documents_controller.php b/app/controllers/documents_controller.php index b5ee65a2..5a7c288b 100755 --- a/app/controllers/documents_controller.php +++ b/app/controllers/documents_controller.php @@ -531,8 +531,11 @@ ENDINSTRUCTIONS; //die(print_r($this->data)); } - if ($this->Document->saveAll($this->data)) { + if ($this->Document->saveAll($this->data)) { echo 'SUCCESS'; //this is so stupid + + + } else { echo 'FAILURE'; } @@ -541,8 +544,7 @@ ENDINSTRUCTIONS; echo 'FAILURE'; } } - - + /** * * Convert a Quote to an OrderAcknowledgement. diff --git a/app/controllers/invoices_controller.php b/app/controllers/invoices_controller.php index 1f3e15bf..0468a23c 100755 --- a/app/controllers/invoices_controller.php +++ b/app/controllers/invoices_controller.php @@ -12,8 +12,16 @@ class InvoicesController extends AppController { ); function index() { - $this->Invoice->recursive = 0; + $this->Invoice->recursive = 0; $this->set('invoices', $this->paginate()); + + /*$q = <<Invoice->query($q); + + $this->set('invoices', $results);*/ } @@ -91,4 +99,4 @@ class InvoicesController extends AppController { } -?> \ No newline at end of file +?> diff --git a/app/controllers/line_items_controller.php b/app/controllers/line_items_controller.php index ad8fc147..124a5758 100755 --- a/app/controllers/line_items_controller.php +++ b/app/controllers/line_items_controller.php @@ -14,11 +14,11 @@ class LineItemsController extends AppController { $this->LineItem->create($this->data); if ($this->LineItem->save($this->data)) { - echo "SUCCESS"; + echo "SUCCESS"; // matching on strings rather than HTTP status codes :( + $this->updateInvoice($this->data['LineItem']['document_id']); } else { echo "FAILURE"; - //print_r($this->data); } } @@ -35,6 +35,7 @@ class LineItemsController extends AppController { if ($this->LineItem->save($this->data)) { echo "SUCCESS"; + $this->updateInvoice($this->data['LineItem']['document_id']); } else { echo "FAILURE"; @@ -55,6 +56,7 @@ class LineItemsController extends AppController { else { if ($this->LineItem->del($id)) { echo "SUCCESS"; + $this->updateInvoice($this->data['LineItem']['document_id']); } else { echo "FAILURE"; @@ -107,5 +109,21 @@ class LineItemsController extends AppController { $this->set('principles', $this->LineItem->Product->Principle->find('list')); } + // 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->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); + } + } + } ?> diff --git a/app/views/invoices/edit.ctp b/app/views/invoices/edit.ctp index a1cd37c7..5fee6d8f 100755 --- a/app/views/invoices/edit.ctp +++ b/app/views/invoices/edit.ctp @@ -6,7 +6,7 @@ echo $form->input('id'); echo $form->input('issue_date'); echo $form->input('due_date'); - echo $form->input('title'); + echo $form->input('title', array('class'=>'disabled', 'readonly'=>'readonly')); echo $form->input('paid'); echo $form->input('payment_received_date'); echo $form->input('enquiry_id', array('type'=>'hidden')); diff --git a/app/views/invoices/index.ctp b/app/views/invoices/index.ctp index ac457e7b..7bc98313 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -10,13 +10,15 @@ sort('issue_date');?> - sort('due_date');?> + Date Due + sort('paid');?> + Date Received sort('Invoice Number');?> sort('Job Number');?> sort('enquiry_id');?> sort('Customer'); ?> - sort('paid');?> - sort('payment_received_date');?> + + @@ -35,22 +37,12 @@ toUnix($invoice['Invoice']['issue_date'])); ?> + toUnix($invoice['Invoice']['due_date'])); ?> - - - - - link($invoice['Job']['title'], array('controller'=>'jobs', 'action'=>'view', $invoice['Invoice']['job_id'])); ?> - - - link($invoice['Enquiry']['title'], array('controller' => 'enquiries', 'action' => 'view', $invoice['Enquiry']['id'])); ?> - - - link($invoice['Customer']['name'], array('controller'=>'customers', 'action'=>'view', $invoice['Customer']['id'])); ?> - - + + - + TODO + + + + + link($invoice['Job']['title'], array('controller'=>'jobs', 'action'=>'view', $invoice['Invoice']['job_id'])); ?> + + + link($invoice['Enquiry']['title'], array('controller' => 'enquiries', 'action' => 'view', $invoice['Enquiry']['id'])); ?> + + + link($invoice['Customer']['name'], array('controller'=>'customers', 'action'=>'view', $invoice['Customer']['id'])); ?> + + link('View', '/documents/view/'.$invoice['Invoice']['document_id']);?> -- 2.30.2 From 004b44cd6f4690a05d7d12ae16a14cad8ef4f90e Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Mon, 27 Feb 2023 14:04:22 +1100 Subject: [PATCH 2/5] WIP --- app/views/invoices/index.ctp | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/invoices/index.ctp b/app/views/invoices/index.ctp index 7bc98313..dc84fde0 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -13,6 +13,7 @@ Date Due sort('paid');?> Date Received + TODO sort('Invoice Number');?> sort('Job Number');?> sort('enquiry_id');?> -- 2.30.2 From daec410797297c36f4ccfad4a706ead832c4a5ef Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Tue, 21 Mar 2023 22:13:03 +1100 Subject: [PATCH 3/5] Progress on #54 invoice sheet work --- Dockerfile | 2 +- app/controllers/invoices_controller.php | 14 ++++++-------- app/controllers/line_items_controller.php | 5 ++++- app/views/invoices/index.ctp | 6 ++++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index e66297e3..2bb679ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -54,7 +54,7 @@ RUN chmod -R 755 /var/www/cmc-sales/app/tmp # Copy site into place. ADD . /var/www/cmc-sales RUN chmod +x /var/www/cmc-sales/run_vault.sh - +RUN chmod +x /var/www/cmc-sales/run_update_invoices.sh # By default, simply start apache. diff --git a/app/controllers/invoices_controller.php b/app/controllers/invoices_controller.php index 0468a23c..ccb684fe 100755 --- a/app/controllers/invoices_controller.php +++ b/app/controllers/invoices_controller.php @@ -14,17 +14,15 @@ class InvoicesController extends AppController { function index() { $this->Invoice->recursive = 0; $this->set('invoices', $this->paginate()); - - /*$q = <<Invoice->query($q); - - $this->set('invoices', $results);*/ } + function calculate() { + + + + } + function printView() { $this->layout = 'minimal'; diff --git a/app/controllers/line_items_controller.php b/app/controllers/line_items_controller.php index 124a5758..8933f04b 100755 --- a/app/controllers/line_items_controller.php +++ b/app/controllers/line_items_controller.php @@ -109,9 +109,11 @@ class LineItemsController extends AppController { $this->set('principles', $this->LineItem->Product->Principle->find('list')); } - // Adding or editing an Invoice means we need to update the invoiced_amount column + + // 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->LineItem->Document->find('first', array('conditions' => array('Document.id' => $documentID), 'recursive'=>1)); @@ -125,5 +127,6 @@ class LineItemsController extends AppController { } } + } ?> diff --git a/app/views/invoices/index.ctp b/app/views/invoices/index.ctp index dc84fde0..b723526f 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -13,7 +13,8 @@ Date Due sort('paid');?> Date Received - TODO + Amount Invoiced + Amount Received sort('Invoice Number');?> sort('Job Number');?> sort('enquiry_id');?> @@ -65,7 +66,8 @@ ?> - TODO + + TODO -- 2.30.2 From 5dc842df9c4c08d54ffadc33d2f314c21c2b8673 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Tue, 21 Mar 2023 22:13:52 +1100 Subject: [PATCH 4/5] 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; -- 2.30.2 From b8b894a5f94ddc03ef9095157b56477d8872c8af Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Mon, 27 Mar 2023 16:45:38 +1100 Subject: [PATCH 5/5] Finish invoice changes - Closes #54 --- app/controllers/invoices_controller.php | 9 +-------- app/vendors/shells/invoice.php | 9 ++++++++- app/views/invoices/edit.ctp | 3 +++ app/views/invoices/index.ctp | 7 ++++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/controllers/invoices_controller.php b/app/controllers/invoices_controller.php index ccb684fe..f880ca04 100755 --- a/app/controllers/invoices_controller.php +++ b/app/controllers/invoices_controller.php @@ -2,7 +2,7 @@ class InvoicesController extends AppController { var $name = 'Invoices'; - var $helpers = array('Html', 'Form', 'Time', 'Text','Javascript'); + var $helpers = array('Html', 'Form', 'Time', 'Text','Javascript', 'Number'); var $paginate = array( @@ -17,13 +17,6 @@ class InvoicesController extends AppController { } - function calculate() { - - - - } - - function printView() { $this->layout = 'minimal'; $this->Invoice->recursive = 0; diff --git a/app/vendors/shells/invoice.php b/app/vendors/shells/invoice.php index 27eb892e..28a2673a 100644 --- a/app/vendors/shells/invoice.php +++ b/app/vendors/shells/invoice.php @@ -31,6 +31,13 @@ class InvoiceShell extends Shell { $totals = $this->calculateTotals($document, $enquiry['Enquiry']['gst']); $invoice['Invoice']['amount_invoiced'] = $totals['total']; print_r($totals); + + $parts = explode(" ", $invoice['Invoice']['title'], 2); + if(count($parts) > 1) { + $invoice['Invoice']['title'] = $parts[0]; + $invoice['Invoice']['comments'] = $parts[1]; + } + echo "Saving invoice ".$invoice["Invoice"]["id"]."\n"; $this->LineItem->Document->Invoice->save($invoice); } @@ -64,7 +71,7 @@ class InvoiceShell extends Shell { function main() { - $q = "SELECT id, document_id FROM invoices i"; + $q = "SELECT id, document_id FROM invoices i ORDER BY id DESC"; $results = $this->Invoice->query($q); foreach($results as $row) { if($row["i"]["document_id"]) { diff --git a/app/views/invoices/edit.ctp b/app/views/invoices/edit.ctp index 5fee6d8f..f57d622b 100755 --- a/app/views/invoices/edit.ctp +++ b/app/views/invoices/edit.ctp @@ -9,6 +9,9 @@ echo $form->input('title', array('class'=>'disabled', 'readonly'=>'readonly')); echo $form->input('paid'); echo $form->input('payment_received_date'); + echo $form->input('amount_invoiced', array('class'=>'disabled', 'readonly'=>'readonly')); + echo $form->input('amount_received'); + echo $form->input('comments'); echo $form->input('enquiry_id', array('type'=>'hidden')); echo $form->input('job_id'); ?> diff --git a/app/views/invoices/index.ctp b/app/views/invoices/index.ctp index b723526f..0dd0e18d 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -15,6 +15,7 @@ Date Received Amount Invoiced Amount Received + Comments sort('Invoice Number');?> sort('Job Number');?> sort('enquiry_id');?> @@ -66,8 +67,9 @@ ?> - - TODO + currency($invoice['Invoice']['amount_invoiced'], $invoice['Currency']['iso4217']); ?> + currency($invoice['Invoice']['amount_received'], $invoice['Currency']['iso4217']);?> + @@ -101,4 +103,3 @@ - -- 2.30.2