Merge branch 'kcordes/54-invoice-sheet-work' into 'master'

Kcordes/54 invoice sheet work

Closes #54

See merge request minimalist.software/cmc-sales!1
This commit is contained in:
Karl Cordes 2023-03-27 06:44:49 +00:00
commit a4995ab0ac
9 changed files with 161 additions and 29 deletions

View file

@ -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.

View file

@ -533,6 +533,9 @@ ENDINSTRUCTIONS;
if ($this->Document->saveAll($this->data)) {
echo 'SUCCESS'; //this is so stupid
} else {
echo 'FAILURE';
}
@ -542,7 +545,6 @@ ENDINSTRUCTIONS;
}
}
/**
*
* Convert a Quote to an OrderAcknowledgement.

View file

@ -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,7 +17,6 @@ class InvoicesController extends AppController {
}
function printView() {
$this->layout = 'minimal';
$this->Invoice->recursive = 0;

View file

@ -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);
}
}
}
?>

87
app/vendors/shells/invoice.php vendored Normal file
View file

@ -0,0 +1,87 @@
<?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);
$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"]);
}
}
}
}

View file

@ -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');
?>

View file

@ -10,13 +10,18 @@
<tr>
<th><?php echo $paginator->sort('issue_date');?></th>
<th><?php echo $paginator->sort('due_date');?></th>
<th>Date Due</th>
<th><?php echo $paginator->sort('paid');?></th>
<th>Date Received</th>
<th>Amount Invoiced</th>
<th>Amount Received</th>
<th>Comments</th>
<th><?php echo $paginator->sort('Invoice Number');?></th>
<th><?php echo $paginator->sort('Job Number');?></th>
<th><?php echo $paginator->sort('enquiry_id');?></th>
<th><?php echo $paginator->sort('Customer'); ?></th>
<th><?php echo $paginator->sort('paid');?></th>
<th><?php echo $paginator->sort('payment_received_date');?></th>
<th class="actions"><?php __('Actions');?></th>
@ -35,21 +40,11 @@
<td>
<?php echo date('j M Y',$time->toUnix($invoice['Invoice']['issue_date'])); ?>
</td>
<td>
<?php echo date('j M Y',$time->toUnix($invoice['Invoice']['due_date'])); ?>
</td>
<td>
<?php echo $invoice['Invoice']['title']; ?>
</td>
<td>
<?php echo $html->link($invoice['Job']['title'], array('controller'=>'jobs', 'action'=>'view', $invoice['Invoice']['job_id'])); ?>
</td>
<td>
<?php echo $html->link($invoice['Enquiry']['title'], array('controller' => 'enquiries', 'action' => 'view', $invoice['Enquiry']['id'])); ?>
</td>
<td>
<?php echo $html->link($invoice['Customer']['name'], array('controller'=>'customers', 'action'=>'view', $invoice['Customer']['id'])); ?>
</td>
<td>
<?php
if($invoice['Invoice']['paid'] == 0) {
@ -72,6 +67,21 @@
?>
</td>
<td><?php echo $number->currency($invoice['Invoice']['amount_invoiced'], $invoice['Currency']['iso4217']); ?></td>
<td><?php echo $number->currency($invoice['Invoice']['amount_received'], $invoice['Currency']['iso4217']);?></td>
<td><?php echo $invoice['Invoice']['comments'];?></td>
<td>
<?php echo $invoice['Invoice']['title']; ?>
</td>
<td>
<?php echo $html->link($invoice['Job']['title'], array('controller'=>'jobs', 'action'=>'view', $invoice['Invoice']['job_id'])); ?>
</td>
<td>
<?php echo $html->link($invoice['Enquiry']['title'], array('controller' => 'enquiries', 'action' => 'view', $invoice['Enquiry']['id'])); ?>
</td>
<td>
<?php echo $html->link($invoice['Customer']['name'], array('controller'=>'customers', 'action'=>'view', $invoice['Customer']['id'])); ?>
</td>
<td class="actions">
@ -93,4 +103,3 @@
</ul>
</div>
<?php debug($invoices); ?>

2
run_update_invoices.sh Normal file
View 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"

View 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;