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/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..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( @@ -12,12 +12,11 @@ class InvoicesController extends AppController { ); function index() { - $this->Invoice->recursive = 0; + $this->Invoice->recursive = 0; $this->set('invoices', $this->paginate()); } - function printView() { $this->layout = 'minimal'; $this->Invoice->recursive = 0; @@ -91,4 +90,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..8933f04b 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,24 @@ 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/vendors/shells/invoice.php b/app/vendors/shells/invoice.php new file mode 100644 index 00000000..28a2673a --- /dev/null +++ b/app/vendors/shells/invoice.php @@ -0,0 +1,87 @@ +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); + + $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); + } + } + + + 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 ORDER BY id DESC"; + $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/app/views/invoices/edit.ctp b/app/views/invoices/edit.ctp index a1cd37c7..f57d622b 100755 --- a/app/views/invoices/edit.ctp +++ b/app/views/invoices/edit.ctp @@ -6,9 +6,12 @@ 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('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 ac457e7b..0dd0e18d 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -10,13 +10,18 @@ sort('issue_date');?> - sort('due_date');?> + Date Due + sort('paid');?> + Date Received + Amount Invoiced + Amount Received + Comments sort('Invoice Number');?> sort('Job Number');?> sort('enquiry_id');?> sort('Customer'); ?> - sort('paid');?> - sort('payment_received_date');?> + + @@ -35,22 +40,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'])); ?> - - + + - + currency($invoice['Invoice']['amount_invoiced'], $invoice['Currency']['iso4217']); ?> + currency($invoice['Invoice']['amount_received'], $invoice['Currency']['iso4217']);?> + + + + + + 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']);?> @@ -93,4 +103,3 @@ - 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;