From 56b487c30e3867aed15ce8e68b392724010d8551 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Mon, 22 Apr 2013 22:57:57 +1000 Subject: [PATCH] Refactored saving documents into a method --- app/controllers/documents_controller.php | 53 ++++---- .../purchase_orders_controller.php | 8 +- app/models/document.php | 7 +- app/models/purchase_order.php | 10 +- app/views/elements/document_orderack_view.ctp | 8 +- app/views/elements/document_quote_view.ctp | 12 +- app/views/invoices/index.ctp | 1 + app/webroot/css/quotenik.css | 7 + app/webroot/js/document_add_edit.js | 120 ++++++++---------- 9 files changed, 114 insertions(+), 112 deletions(-) diff --git a/app/controllers/documents_controller.php b/app/controllers/documents_controller.php index 7a4de97b..741bcaae 100755 --- a/app/controllers/documents_controller.php +++ b/app/controllers/documents_controller.php @@ -39,10 +39,10 @@ class DocumentsController extends AppController { $this->set('currency', $this->Document->getCurrency($document)); $enquiry = $this->Document->getEnquiry($document); $this->set('enquiry', $enquiry); - $this->set('jobs', $this->Document->OrderAcknowledgement->Job->find('list', - array('conditions'=> array('Job.customer_id' - => $enquiry['Enquiry']['customer_id']), 'recursive' =>0, 'order'=>'id DESC'))); - + if($enquiry) { + $this->set('jobs', $this->Document->Invoice->Job->find('list', + array('conditions'=> array('Job.customer_id' => $enquiry['Enquiry']['customer_id']), 'recursive' =>0, 'order'=>'id DESC'))); + } $customerAddresses = $this->Document->User->Enquiry->BillingAddress->find('all', array('conditions'=>array('BillingAddress.customer_id' => $enquiry['Enquiry']['customer_id']))); @@ -76,6 +76,7 @@ class DocumentsController extends AppController { break; case 'purchaseOrder': $docTypeElement = 'document_purchase_order_view'; + $this->set('currencies', $this->Document->PurchaseOrder->Currency->find('list')); break; case 'orderAck': $this->set('currencies', $this->Document->OrderAcknowledgement->Currency->find('list')); @@ -84,7 +85,6 @@ class DocumentsController extends AppController { } $this->set('docTypeElement', $docTypeElement); - // $this->set('principles', $this->Document->LineItem->Product->Principle->find('list')); } @@ -174,10 +174,12 @@ class DocumentsController extends AppController { * Create a New Document * * - * @param $type - * @param $id + * @param string $type + * @param int $id + * @param int $jobid + * @param int $source_document_id */ - function newDocument($type = null, $enquiryid = null, $jobid = 0) { + function newDocument($type = null, $enquiryid = null, $jobid = 0, $source_document_id = null) { if(!$type) { $this->Session->setFlash('Invalid Doc Type or ID'); } @@ -238,7 +240,7 @@ class DocumentsController extends AppController { break; case "purchaseOrder": - + $enquiry = $this->Document->OrderAcknowledgement->Enquiry->read(null, $enquiryid); $count = $this->Document->PurchaseOrder->findCount(); $count++; @@ -247,7 +249,6 @@ class DocumentsController extends AppController { $newPOnumber = "CMCPO".$count; $this->data['PurchaseOrder']['title'] = $newPOnumber; $this->data['PurchaseOrder']['issue_date'] = date('Y-m-d'); - $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id']; $this->data['Document']['revision'] = 0; //No revisions for POs. I have decreed it! $this->data['Document']['type'] = 'purchaseOrder'; @@ -264,9 +265,19 @@ class DocumentsController extends AppController { } + if(isset($source_document_id)) { + $sourceDoc = $this->Document->find('first', array('conditions' => array('Document.id' => $source_document_id))); + if(!empty($sourceDoc['LineItem'])) { + $this->data['LineItem'] = $sourceDoc['LineItem']; + foreach($this->data['LineItem'] as $index => $li) { + $this->data['LineItem'][$index]['id'] = null; + $this->data['LineItem'][$index]['document_id'] = null; + $this->data['LineItem'][$index]['costing_id'] = null; + } + } + } if ($this->Document->saveAll($this->data)) { $newid = $this->Document->id; - $this->Session->setFlash(__('The Document has been saved', true)); $this->redirect(array('action'=>'view',$newid)); } else { @@ -475,26 +486,6 @@ class DocumentsController extends AppController { } - /** - * Convert an Order Acknowledgement to an Invoice. - * - * @TODO Finish this. - * - * @param $id - */ - function convert_to_invoice($id = null) { - if (!$id) { - echo 'Invalid Document ID'; - } - else { - - - } - - - } - - function pdf($id = null) { //Configure::write('debug',0); if(!$id) { diff --git a/app/controllers/purchase_orders_controller.php b/app/controllers/purchase_orders_controller.php index ad0717f4..2a9f5270 100755 --- a/app/controllers/purchase_orders_controller.php +++ b/app/controllers/purchase_orders_controller.php @@ -44,10 +44,10 @@ class PurchaseOrdersController extends AppController { if ($this->PurchaseOrder->save($this->data)) { - $this->Session->setFlash(__('The PurchaseOrder has been saved', true)); - $this->redirect(array('action'=>'index')); + $this->Session->setFlash(__('The PurchaseOrder has been saved', true)); + $this->redirect(array('action'=>'index')); } else { - $this->Session->setFlash(__('The PurchaseOrder could not be saved. Please, try again.', true)); + $this->Session->setFlash(__('The PurchaseOrder could not be saved. Please, try again.', true)); } } $jobs = $this->PurchaseOrder->Job->find('list'); @@ -126,4 +126,4 @@ class PurchaseOrdersController extends AppController { } -?> \ No newline at end of file +?> diff --git a/app/models/document.php b/app/models/document.php index aa1d7eb9..29fa542e 100755 --- a/app/models/document.php +++ b/app/models/document.php @@ -61,8 +61,8 @@ class Document extends AppModel { ); //So much refactoring possible on this. + //Leaving it for now. function getCurrency($document) { - if(!empty($document['Invoice']['id'])) { $currencyID = $document['Invoice']['currency_id']; $conditions = $this->__getCurrencyConditions($currencyID); @@ -85,7 +85,7 @@ class Document extends AppModel { $currencyID = $document['OrderAcknowledgement']['currency_id']; $conditions = $this->__getCurrencyConditions($currencyID); return $this->OrderAcknowledgement->Currency->find('first',$conditions); - } + } } @@ -147,7 +147,8 @@ class Document extends AppModel { return $this->Quote->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Quote']['enquiry_id']))); } elseif(!empty($document['PurchaseOrder']['id'])) { - return $this->PurchaseOrder->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Invoice']['enquiry_id']))); + //No enquiries for POs. We want the Jobs. + return; } elseif(!empty($document['OrderAcknowledgement']['id'])) { return $this->OrderAcknowledgement->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['OrderAcknowledgement']['enquiry_id']))); diff --git a/app/models/purchase_order.php b/app/models/purchase_order.php index 29474236..f47c155a 100755 --- a/app/models/purchase_order.php +++ b/app/models/purchase_order.php @@ -18,8 +18,12 @@ class PurchaseOrder extends AppModel { 'conditions' => '', 'fields' => '', 'order' => '' - ) - ); + ), + 'Currency' => array( + 'className' => 'Currency', + 'foreignKey' => 'currency_id' + ), + ); var $hasAndBelongsToMany = array( 'Job' => array( @@ -51,4 +55,4 @@ class PurchaseOrder extends AppModel { } -?> \ No newline at end of file +?> diff --git a/app/views/elements/document_orderack_view.ctp b/app/views/elements/document_orderack_view.ctp index 14c057c2..fc6cae67 100755 --- a/app/views/elements/document_orderack_view.ctp +++ b/app/views/elements/document_orderack_view.ctp @@ -9,8 +9,12 @@
-

Create an Invoice from this OA

- link('Create an Invoice based on this OA', '/documents/convert_to_invoice/'.$document['Document']['id'], array('class'=>'actionLink'));?> +

Create new Documents based on this

+
    +
  • + +
  • +
diff --git a/app/views/elements/document_quote_view.ctp b/app/views/elements/document_quote_view.ctp index 82d14999..04240701 100755 --- a/app/views/elements/document_quote_view.ctp +++ b/app/views/elements/document_quote_view.ctp @@ -28,8 +28,16 @@ echo $form->input('Document.id');
-

Has link($enquiry['Customer']['name'], array('controller'=>'customers','action'=>'view',$enquiry['Customer']['id']));?> placed an order for this Quote?

- +

Create new Documents based on this

+
    +
  • + +
  • + +
  • + +
  • +
diff --git a/app/views/invoices/index.ctp b/app/views/invoices/index.ctp index 907c6bfb..ac457e7b 100755 --- a/app/views/invoices/index.ctp +++ b/app/views/invoices/index.ctp @@ -75,6 +75,7 @@ + link('View', '/documents/view/'.$invoice['Invoice']['document_id']);?> link('Edit', '/invoices/edit/'.$invoice['Invoice']['id']);?> diff --git a/app/webroot/css/quotenik.css b/app/webroot/css/quotenik.css index f0a45d35..f27ea5e0 100755 --- a/app/webroot/css/quotenik.css +++ b/app/webroot/css/quotenik.css @@ -1670,3 +1670,10 @@ h2.document-attachments { tr.archived { text-decoration: line-through; } + +ul.document-buttons { + list-style: none; +} +ul.document-buttons li { + padding: 0.5em; +} diff --git a/app/webroot/js/document_add_edit.js b/app/webroot/js/document_add_edit.js index 9e89b36d..377c25cd 100755 --- a/app/webroot/js/document_add_edit.js +++ b/app/webroot/js/document_add_edit.js @@ -353,19 +353,19 @@ $(function() { var confirmed = confirm("This will overwrite any changes you have made to the first page"); } - if(confirmed) { - $.get('/documents/generateFirstPage/'+docID, function(data) { - $(".firstPage").val(data); - savePages(); - }); - } + if(confirmed) { + $.get('/documents/generateFirstPage/'+docID, function(data) { + $(".firstPage").val(data); + savePages(); + }); + } }); $("#pdfDocButton").click(function() { window.location = $(this).data('url'); }); - + $("#emailDocButton").click(function() { var confirmed = confirm("This will email this Quote and all Attachments to the Customer. Are you sure you want to do this?"); if(confirmed) { @@ -381,81 +381,33 @@ $(function() { $("#DocumentShipTo").ckeditor(config); $("#saveInvoiceButton").click(function() { + saveDocument('Invoice'); - - updateTextFields(); //Update the CKEditor instances. - - var invoiceDetails = $('#DocumentEditForm').find('input,select,textarea'); - - - - $.post('/documents/ajax_edit', invoiceDetails, function(data) { - if(data =='SUCCESS') { - $("#flashMessage").html("Invoice Saved Successfully"); - } - else { - $("#flashMessage").html("Unable to Save Invoice"); - } - - $("#flashMessage").show(); - - - loadLineItems(); - }); - }); //OA View. $("#saveOAButton").click(function() { - - - updateTextFields(); //Update the CKEditor instances. - - var oaDetails = $('#DocumentEditForm').find('input,select,textarea'); - - - - $.post('/documents/ajax_edit', oaDetails, function(data) { - if(data =='SUCCESS') { - $("#flashMessage").html("Order Acknowledgement Saved Successfully"); - } - else { - $("#flashMessage").html("Unable to Save Order Acknowledgement"); - } - + saveDocument('Order Acknowledgement'); $('.job-title').html($("#OrderAcknowledgementJobId :selected").text()); - $("#flashMessage").show(); - - - loadLineItems(); - }); - }); - $("#createOA").click(function() { $("#addJobConfirmation").dialog('open'); }); - $(".billing_address").click(function() { - if($(this).val() > 0 ) { - var address = $(this).next().html(); - var c = confirm("Set the 'Bill To' field to this address?"); - if(c) { - $("#DocumentBillTo").val(address); - } - } + $("#createInvoice").click(function() { + location.href = $(this).data('href'); + }); + + //Choosing an address + $(".billing_address").click(function() { + var address = $(this).next().html(); + setAddress(address, '#DocumentBillTo', 'Bill To'); }); - $(".shipping_address").click(function() { - if($(this).val() > 0 ) { - var address = $(this).next().html(); - var c = confirm("Set the 'Ship To' field to this address?"); - if(c) { - $("#DocumentShipTo").val(address); - } - } + var address = $(this).next().html(); + setAddress(address, '#DocumentShipTo', 'Ship To'); }); }); @@ -496,6 +448,40 @@ $('.generateCommercialComments').live('click', function(event) { }); + +//Save the current document +//@param string documentName for message +function saveDocument(documentName) { + updateTextFields(); //Update the CKEditor instances. + var fields = $('#DocumentEditForm').find('input,select,textarea'); + + $.post('/documents/ajax_edit', fields, function(data) { + if(data =='SUCCESS') { + $("#flashMessage").html(documentName+" saved Successfully"); + } + else { + $("#flashMessage").html("Unable to save "+documentName); + } + $("#flashMessage").show(); + loadLineItems(); + }); +} + + +//Set the address given the addressFieldID and a label FieldName +function setAddress(address, addressFieldID, fieldName) { + console.log(address); + if($(addressFieldID).val() == '') { + $(addressFieldID).val(address); + } + else { + var c = confirm("Set " + fieldName+ " to this address?"); + if(c) { + $(addressFieldID).val(address); + } + } +} + function showHideTextPrices() { if( $('#LineItemHasTextPrices').val() == 1) { $("#noCosting").hide();