Fix empty invoice amounts when creating a new invoice
This commit is contained in:
parent
b8b894a5f9
commit
3ab425774e
|
|
@ -45,7 +45,7 @@ class DocumentsController extends AppController {
|
|||
$enquiry = $this->Document->getEnquiry($document);
|
||||
$this->set('enquiry', $enquiry);
|
||||
if($enquiry) {
|
||||
$this->set('jobs', $this->Document->Invoice->Job->find('list',
|
||||
$this->set('jobs', $this->Document->Invoice->Job->find('list',
|
||||
array('conditions'=> array('Job.customer_id' => $enquiry['Enquiry']['customer_id']), 'recursive' =>0, 'order'=>'id DESC')));
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +82,7 @@ class DocumentsController extends AppController {
|
|||
case 'purchaseOrder':
|
||||
$docTypeElement = 'document_purchase_order_view';
|
||||
$this->set('currencies', $this->Document->PurchaseOrder->Currency->find('list'));
|
||||
$this->set('purchaseOrder', $this->Document->PurchaseOrder->find('first',
|
||||
$this->set('purchaseOrder', $this->Document->PurchaseOrder->find('first',
|
||||
array('conditions'=>
|
||||
array('PurchaseOrder.id' => $document['PurchaseOrder']['id'])
|
||||
)
|
||||
|
|
@ -169,7 +169,7 @@ class DocumentsController extends AppController {
|
|||
$attachment[$i]['DocumentAttachment']['document_id'] = $this->data['DocumentAttachment']['document_id'];
|
||||
$attachment[$i]['DocumentAttachment']['attachment_id'] = $attachment_id;
|
||||
}
|
||||
if($this->Document->DocumentAttachment->saveAll($attachment)) {
|
||||
if($this->Document->DocumentAttachment->saveAll($attachment)) {
|
||||
echo "SUCCESS";
|
||||
}
|
||||
else {
|
||||
|
|
@ -260,6 +260,9 @@ class DocumentsController extends AppController {
|
|||
if(isset($source_document_id)) {
|
||||
//This is not ideal. But nothing else is either.
|
||||
$sourceDoc = $this->Document->find('first', array('conditions' => array('Document.id' => $source_document_id)));
|
||||
|
||||
$totals = $this->calculateTotals($sourceDoc, $enquiry['Enquiry']['gst']);
|
||||
$this->data['Invoice']['amount_invoiced'] = $totals['total'];
|
||||
if($sourceDoc['Document']['type'] == 'orderAck') {
|
||||
$oa_attributes = array(
|
||||
'ship_via',
|
||||
|
|
@ -312,8 +315,8 @@ ENDINSTRUCTIONS;
|
|||
$this->data['Document']['doc_page_count'] = 0;
|
||||
$this->data['Document']['type'] = 'purchaseOrder';
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
case "orderAck":
|
||||
$enquiry = $this->Document->OrderAcknowledgement->Enquiry->read(null, $enquiryid);
|
||||
$this->data['OrderAcknowledgement']['job_id'] = $jobid;
|
||||
|
|
@ -359,8 +362,8 @@ ENDINSTRUCTIONS;
|
|||
if(isset($source_document_id)) {
|
||||
//This is not ideal. But nothing else is either.
|
||||
$sourceDoc = $this->Document->find('first', array('conditions' => array('Document.id' => $source_document_id)));
|
||||
|
||||
|
||||
|
||||
|
||||
if($sourceDoc['Document']['type'] == 'orderAck') {
|
||||
foreach($source_attributes as $attr) {
|
||||
$this->data['PackingList'][$attr] = $sourceDoc['OrderAcknowledgement'][$attr];
|
||||
|
|
@ -373,7 +376,7 @@ ENDINSTRUCTIONS;
|
|||
'fob'
|
||||
);
|
||||
|
||||
|
||||
|
||||
foreach($source_attributes as $attr) {
|
||||
$this->data['PackingList'][$attr] = $sourceDoc['Invoice'][$attr];
|
||||
}
|
||||
|
|
@ -383,8 +386,8 @@ ENDINSTRUCTIONS;
|
|||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
if(isset($source_document_id)) {
|
||||
|
|
@ -396,7 +399,7 @@ ENDINSTRUCTIONS;
|
|||
$this->data['Document'][$attr] = $sourceDoc['Document'][$attr];
|
||||
}
|
||||
|
||||
|
||||
|
||||
$this->data['LineItem'] = $this->copy_related($sourceDoc, 'LineItem', array('id', 'document_id', 'costing_id'));
|
||||
|
||||
}
|
||||
|
|
@ -475,14 +478,14 @@ ENDINSTRUCTIONS;
|
|||
|
||||
|
||||
/** Copy the LineItems from this Document. Applies to all of the Document Types */
|
||||
|
||||
|
||||
$newDoc['LineItem'] = $this->copy_related($document, 'LineItem', array('id', 'document_id', 'costing_id'));
|
||||
|
||||
$newDoc['DocumentAttachment'] = $this->copy_related($document, 'DocumentAttachment', 'id', 'document_id');
|
||||
|
||||
|
||||
$this->set('newDoc', $newDoc);
|
||||
|
||||
|
||||
if ($this->Document->saveAll($newDoc)) {
|
||||
$newid = $this->Document->id;
|
||||
$this->Session->setFlash(__("Revision {$number_of_revisions} created", true));
|
||||
|
|
@ -515,8 +518,8 @@ ENDINSTRUCTIONS;
|
|||
}
|
||||
|
||||
if(isset($this->data['PurchaseOrder']['Job'])) {
|
||||
//Loop over the submitted Jobs and add them. Fuck CakePHP is a piece of shit.
|
||||
//This is easier than using the fucking built in crap that doesnt work.
|
||||
//Loop over the submitted Jobs and add them. Fuck CakePHP is a piece of shit.
|
||||
//This is easier than using the fucking built in crap that doesnt work.
|
||||
//Fuck everything about this.
|
||||
$po_id = $this->data['PurchaseOrder']['id'];
|
||||
foreach($this->data['PurchaseOrder']['Job'] as $job_id) {
|
||||
|
|
@ -525,17 +528,17 @@ ENDINSTRUCTIONS;
|
|||
}
|
||||
$result = $this->Document->PurchaseOrder->saveAll($this->data['PurchaseOrder']);
|
||||
|
||||
|
||||
|
||||
//die(print_r($this->data['PurchaseOrder']));
|
||||
//die(print_r($result));
|
||||
//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';
|
||||
}
|
||||
|
|
@ -544,7 +547,7 @@ ENDINSTRUCTIONS;
|
|||
echo 'FAILURE';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Convert a Quote to an OrderAcknowledgement.
|
||||
|
|
@ -578,7 +581,7 @@ ENDINSTRUCTIONS;
|
|||
|
||||
$newDoc = array();
|
||||
if($this->data['Job']['create_new_job']) {
|
||||
$job = $this->Document->OrderAcknowledgement->Job->newJob($enquiry, $this->data['Job']['customer_order_number']); //Only way to know Enquiry ID is if it's a Quote.
|
||||
$job = $this->Document->OrderAcknowledgement->Job->newJob($enquiry, $this->data['Job']['customer_order_number']); //Only way to know Enquiry ID is if it's a Quote.
|
||||
$newDoc['OrderAcknowledgement']['job_id'] = $job['Job']['id'];
|
||||
|
||||
}
|
||||
|
|
@ -707,18 +710,18 @@ ENDINSTRUCTIONS;
|
|||
|
||||
case "purchaseOrder":
|
||||
$principle = $this->Document->LineItem->Product->Principle->find('first', array('conditions'=>array('Principle.id' => $document['PurchaseOrder']['principle_id'])));
|
||||
|
||||
|
||||
$this->set('principle', $principle);
|
||||
$purchase_order = $this->Document->PurchaseOrder->find('first',
|
||||
$purchase_order = $this->Document->PurchaseOrder->find('first',
|
||||
array('conditions'=>array('PurchaseOrder.id' => $document['PurchaseOrder']['id'])));
|
||||
$this->set('purchase_order', $purchase_order);
|
||||
$this->set('purchase_order', $purchase_order);
|
||||
|
||||
$totalsDescText = array(
|
||||
'subtotal' => 'SUBTOTAL',
|
||||
'gst' => 'GST',
|
||||
'total' => 'ORDER TOTAL'
|
||||
);
|
||||
//No GST for POs for now.
|
||||
//No GST for POs for now.
|
||||
if($principle['Country']['id'] == 1) {
|
||||
$totals = $this->calculateTotals($document, 1);
|
||||
}
|
||||
|
|
@ -729,9 +732,9 @@ ENDINSTRUCTIONS;
|
|||
|
||||
$this->set('totals',$totals);
|
||||
$this->set('totalsDescText',$totalsDescText);
|
||||
|
||||
|
||||
$filename = $document['PurchaseOrder']['title'];
|
||||
|
||||
|
||||
$issue_date = date('l, j F Y', strtotime($document['PurchaseOrder']['issue_date']));
|
||||
$this->set('issue_date', $issue_date);
|
||||
$template_name = 'pdf_po';
|
||||
|
|
@ -824,41 +827,41 @@ ENDINSTRUCTIONS;
|
|||
|
||||
// Do a bunch of queries to build a subject line for PO emails.
|
||||
function po_email_subject($document) {
|
||||
$id = $document['id'];
|
||||
$subject = 'CMC Technologies Purchase Order: '.$document['title'];
|
||||
$q = <<<EOT
|
||||
$id = $document['id'];
|
||||
$subject = 'CMC Technologies Purchase Order: '.$document['title'];
|
||||
$q = <<<EOT
|
||||
SELECT jobs.id, title FROM jobs
|
||||
JOIN jobs_purchase_orders as jpo ON (jpo.purchase_order_id = $id AND jpo.job_id = jobs.id)
|
||||
|
||||
EOT;
|
||||
|
||||
$jobs = $this->Document->query($q);
|
||||
if(count($jobs) <= 0) {
|
||||
return $subject;
|
||||
}
|
||||
$job_numbers = array();
|
||||
$job_ids = array();
|
||||
foreach($jobs as $job) {
|
||||
$job_numbers[] = $job["jobs"]["title"];
|
||||
$job_ids[] = $job["jobs"]["id"];
|
||||
}
|
||||
$job_ids_in = implode(",", $job_ids);
|
||||
$jobs = $this->Document->query($q);
|
||||
if(count($jobs) <= 0) {
|
||||
return $subject;
|
||||
}
|
||||
$job_numbers = array();
|
||||
$job_ids = array();
|
||||
foreach($jobs as $job) {
|
||||
$job_numbers[] = $job["jobs"]["title"];
|
||||
$job_ids[] = $job["jobs"]["id"];
|
||||
}
|
||||
$job_ids_in = implode(",", $job_ids);
|
||||
|
||||
$q = <<<EOT
|
||||
$q = <<<EOT
|
||||
SELECT enquiries.title from enquiries join jobs on enquiries.id = jobs.enquiry_id AND jobs.id IN($job_ids_in);
|
||||
EOT;
|
||||
|
||||
$enquiries = $this->Document->query($q);
|
||||
$enquiries = $this->Document->query($q);
|
||||
|
||||
$enquiry_numbers = array();
|
||||
foreach($enquiries as $enquiry) {
|
||||
$enquiry_numbers[] = $enquiry["enquiries"]["title"];
|
||||
}
|
||||
$enquiry_numbers = array();
|
||||
foreach($enquiries as $enquiry) {
|
||||
$enquiry_numbers[] = $enquiry["enquiries"]["title"];
|
||||
}
|
||||
|
||||
$subject = $subject . ' ( ' .implode(" ", $job_numbers). ' )';
|
||||
$subject = $subject . ' ( ' .implode(" ", $enquiry_numbers) . ' )';
|
||||
$subject = $subject . ' ( ' .implode(" ", $job_numbers). ' )';
|
||||
$subject = $subject . ' ( ' .implode(" ", $enquiry_numbers) . ' )';
|
||||
|
||||
return $subject;
|
||||
return $subject;
|
||||
}
|
||||
|
||||
function orderack_email_subject($document) {
|
||||
|
|
@ -878,7 +881,7 @@ EOT;
|
|||
$subject = 'CMC Technologies Order Acknowledgement';
|
||||
return $subject;
|
||||
}
|
||||
|
||||
|
||||
$subject = 'CMC Technologies Order Acknowledgement: ('.$titles[0]['e']['enquiry_title'].') ('.$titles[0]['j']['job_title'].')';
|
||||
return $subject;
|
||||
}
|
||||
|
|
@ -900,7 +903,7 @@ EOT;
|
|||
$subject = 'Invoice';
|
||||
return $subject;
|
||||
}
|
||||
|
||||
|
||||
$subject = 'CMC Technologies Tax Invoice: '.$titles[0]['i']['invoice_title'].' ('.$titles[0]['e']['enquiry_title'].') ('.$titles[0]['j']['job_title'].')';
|
||||
return $subject;
|
||||
}
|
||||
|
|
@ -922,11 +925,11 @@ EOT;
|
|||
$subject = 'Packing list';
|
||||
return $subject;
|
||||
}
|
||||
|
||||
|
||||
$subject = 'CMC Technologies Packing List: '.$titles[0]['p']['packing_list_title'].' ('.$titles[0]['e']['enquiry_title'].') ('.$titles[0]['j']['job_title'].')';
|
||||
return $subject;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -952,8 +955,8 @@ EOT;
|
|||
|
||||
$attachment_files = array($pdf_dir.$document['Document']['pdf_filename']);
|
||||
foreach($document['DocumentAttachment'] as $document_attachment) {
|
||||
$attachment = $this->Document->DocumentAttachment->Attachment->read(null, $document_attachment['attachment_id']);
|
||||
$attachment_files[] = $attachment['Attachment']['file'];
|
||||
$attachment = $this->Document->DocumentAttachment->Attachment->read(null, $document_attachment['attachment_id']);
|
||||
$attachment_files[] = $attachment['Attachment']['file'];
|
||||
}
|
||||
$this->Email->attachments = $attachment_files;
|
||||
|
||||
|
|
@ -978,35 +981,35 @@ EOT;
|
|||
|
||||
switch($docType) {
|
||||
case 'quote':
|
||||
$subject = "Quotation: ".$enquiry['Enquiry']['title'];
|
||||
break;
|
||||
$subject = "Quotation: ".$enquiry['Enquiry']['title'];
|
||||
break;
|
||||
case 'invoice':
|
||||
$subject = $this->invoice_email_subject($document);
|
||||
$this->set('invoice', $this->Document->Invoice->find('first', array('conditions'=>array('Invoice.id'=>$document['Invoice']['id']))));
|
||||
$this->set('job', $this->Document->Invoice->Job->find('first', array('conditions'=>array('Job.id'=>$document['Invoice']['job_id']))));
|
||||
break;
|
||||
$this->set('invoice', $this->Document->Invoice->find('first', array('conditions'=>array('Invoice.id'=>$document['Invoice']['id']))));
|
||||
$this->set('job', $this->Document->Invoice->Job->find('first', array('conditions'=>array('Job.id'=>$document['Invoice']['job_id']))));
|
||||
break;
|
||||
case 'purchaseOrder':
|
||||
$subject .= "Purchase Order";
|
||||
$primary_contact = $this->Document->User->find('first', array('conditions'=>array('User.principle_id' => $document['PurchaseOrder']['principle_id'],'User.primary_contact' => 1)));
|
||||
if(empty($primary_contact)) {
|
||||
|
||||
$this->Session->setFlash(__('Unable to send. No primary Principle Contact', true));
|
||||
$this->redirect(array('action'=>'view/'.$id), null, false);
|
||||
return;
|
||||
}
|
||||
|
||||
$subject = $this->po_email_subject($document['PurchaseOrder']);
|
||||
$this->Email->to = $this->email_format($primary_contact['User']['email']);
|
||||
$this->Email->replyTo = null;
|
||||
$this->Email->cc = null;
|
||||
$this->set('job', $this->Document->PurchaseOrder->Job->find('first', array('conditions'=>array('Job.id'=>$document['OrderAcknowledgement']['job_id']))));
|
||||
break;
|
||||
$subject .= "Purchase Order";
|
||||
$primary_contact = $this->Document->User->find('first', array('conditions'=>array('User.principle_id' => $document['PurchaseOrder']['principle_id'],'User.primary_contact' => 1)));
|
||||
if(empty($primary_contact)) {
|
||||
|
||||
$this->Session->setFlash(__('Unable to send. No primary Principle Contact', true));
|
||||
$this->redirect(array('action'=>'view/'.$id), null, false);
|
||||
return;
|
||||
}
|
||||
|
||||
$subject = $this->po_email_subject($document['PurchaseOrder']);
|
||||
$this->Email->to = $this->email_format($primary_contact['User']['email']);
|
||||
$this->Email->replyTo = null;
|
||||
$this->Email->cc = null;
|
||||
$this->set('job', $this->Document->PurchaseOrder->Job->find('first', array('conditions'=>array('Job.id'=>$document['OrderAcknowledgement']['job_id']))));
|
||||
break;
|
||||
case 'orderAck':
|
||||
$subject = $this->orderack_email_subject($document);
|
||||
$this->set('job', $this->Document->OrderAcknowledgement->Job->find('first', array('conditions'=>array('Job.id'=>$document['OrderAcknowledgement']['job_id']))));
|
||||
if ($document['OrderAcknowledgement']['signature_required']) {
|
||||
$template = 'orderAck_email_signature_required';
|
||||
}
|
||||
$this->set('job', $this->Document->OrderAcknowledgement->Job->find('first', array('conditions'=>array('Job.id'=>$document['OrderAcknowledgement']['job_id']))));
|
||||
if ($document['OrderAcknowledgement']['signature_required']) {
|
||||
$template = 'orderAck_email_signature_required';
|
||||
}
|
||||
break;
|
||||
case 'packingList':
|
||||
$subject = $this->packing_list_email_subject($document);
|
||||
|
|
@ -1021,7 +1024,7 @@ EOT;
|
|||
$this->Email->sendAs = 'both';
|
||||
$this->Email->charset = 'iso-8859-1';
|
||||
$this->set('enquiry', $enquiry);
|
||||
$this->set('document', $document);
|
||||
$this->set('document', $document);
|
||||
$this->set('DocFullName', $this->Document->getDocFullName($document['Document']['type']));
|
||||
if($this->Email->send()) {
|
||||
$this->Session->setFlash(__('The Email has been sent', true));
|
||||
|
|
@ -1043,14 +1046,14 @@ EOT;
|
|||
|
||||
$orderack = $this->Document->OrderAcknowledgement->find('first', array('conditions'=>array('OrderAcknowledgement.enquiry_id' => $job['Job']['enquiry_id']), 'recursive' => 1));
|
||||
|
||||
$oa_document = $this->Document->find('first',
|
||||
array('conditions'=>
|
||||
$oa_document = $this->Document->find('first',
|
||||
array('conditions'=>
|
||||
array('Document.id' => $orderack['Document']['id']
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
|
||||
$destination_document = $this->Document->find('first', array('conditions'=>array('Document.id' => $document_id)));
|
||||
|
||||
$new_items = $this->copy_related($oa_document, 'LineItem', array('id', 'document_id', 'costing_id'));
|
||||
|
|
@ -1070,21 +1073,21 @@ EOT;
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if ($this->Document->LineItem->saveAll($destination_document['LineItem'])) {
|
||||
echo 'SUCCESS';
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo 'FAILURE';
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** Helper function to make copying related models more DRY
|
||||
/** Helper function to make copying related models more DRY
|
||||
*
|
||||
* @param array $document
|
||||
* @param string $model - eg 'LineItem'
|
||||
|
|
@ -1096,13 +1099,13 @@ EOT;
|
|||
return null;
|
||||
}
|
||||
$newDoc[$model] = $document[$model];
|
||||
|
||||
|
||||
foreach($newDoc[$model] as $index => $ele) {
|
||||
foreach($keys_to_null as $key) {
|
||||
$newDoc[$model][$index][$key] = null;
|
||||
}
|
||||
}
|
||||
return $newDoc[$model];
|
||||
return $newDoc[$model];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue