Refactored saving documents into a method

This commit is contained in:
Karl Cordes 2013-04-22 22:57:57 +10:00
parent f9a8628135
commit 56b487c30e
9 changed files with 114 additions and 112 deletions

View file

@ -39,10 +39,10 @@ class DocumentsController extends AppController {
$this->set('currency', $this->Document->getCurrency($document)); $this->set('currency', $this->Document->getCurrency($document));
$enquiry = $this->Document->getEnquiry($document); $enquiry = $this->Document->getEnquiry($document);
$this->set('enquiry', $enquiry); $this->set('enquiry', $enquiry);
$this->set('jobs', $this->Document->OrderAcknowledgement->Job->find('list', if($enquiry) {
array('conditions'=> array('Job.customer_id' $this->set('jobs', $this->Document->Invoice->Job->find('list',
=> $enquiry['Enquiry']['customer_id']), 'recursive' =>0, 'order'=>'id DESC'))); 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']))); $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; break;
case 'purchaseOrder': case 'purchaseOrder':
$docTypeElement = 'document_purchase_order_view'; $docTypeElement = 'document_purchase_order_view';
$this->set('currencies', $this->Document->PurchaseOrder->Currency->find('list'));
break; break;
case 'orderAck': case 'orderAck':
$this->set('currencies', $this->Document->OrderAcknowledgement->Currency->find('list')); $this->set('currencies', $this->Document->OrderAcknowledgement->Currency->find('list'));
@ -84,7 +85,6 @@ class DocumentsController extends AppController {
} }
$this->set('docTypeElement', $docTypeElement); $this->set('docTypeElement', $docTypeElement);
// $this->set('principles', $this->Document->LineItem->Product->Principle->find('list')); // $this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
} }
@ -174,10 +174,12 @@ class DocumentsController extends AppController {
* Create a New Document * Create a New Document
* *
* *
* @param <type> $type * @param string $type
* @param <type> $id * @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) { if(!$type) {
$this->Session->setFlash('Invalid Doc Type or ID'); $this->Session->setFlash('Invalid Doc Type or ID');
} }
@ -238,7 +240,7 @@ class DocumentsController extends AppController {
break; break;
case "purchaseOrder": case "purchaseOrder":
$enquiry = $this->Document->OrderAcknowledgement->Enquiry->read(null, $enquiryid);
$count = $this->Document->PurchaseOrder->findCount(); $count = $this->Document->PurchaseOrder->findCount();
$count++; $count++;
@ -247,7 +249,6 @@ class DocumentsController extends AppController {
$newPOnumber = "CMCPO".$count; $newPOnumber = "CMCPO".$count;
$this->data['PurchaseOrder']['title'] = $newPOnumber; $this->data['PurchaseOrder']['title'] = $newPOnumber;
$this->data['PurchaseOrder']['issue_date'] = date('Y-m-d'); $this->data['PurchaseOrder']['issue_date'] = date('Y-m-d');
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id']; $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']['revision'] = 0; //No revisions for POs. I have decreed it!
$this->data['Document']['type'] = 'purchaseOrder'; $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)) { if ($this->Document->saveAll($this->data)) {
$newid = $this->Document->id; $newid = $this->Document->id;
$this->Session->setFlash(__('The Document has been saved', true)); $this->Session->setFlash(__('The Document has been saved', true));
$this->redirect(array('action'=>'view',$newid)); $this->redirect(array('action'=>'view',$newid));
} else { } else {
@ -475,26 +486,6 @@ class DocumentsController extends AppController {
} }
/**
* Convert an Order Acknowledgement to an Invoice.
*
* @TODO Finish this.
*
* @param <int> $id
*/
function convert_to_invoice($id = null) {
if (!$id) {
echo 'Invalid Document ID';
}
else {
}
}
function pdf($id = null) { function pdf($id = null) {
//Configure::write('debug',0); //Configure::write('debug',0);
if(!$id) { if(!$id) {

View file

@ -44,10 +44,10 @@ class PurchaseOrdersController extends AppController {
if ($this->PurchaseOrder->save($this->data)) { if ($this->PurchaseOrder->save($this->data)) {
$this->Session->setFlash(__('The PurchaseOrder has been saved', true)); $this->Session->setFlash(__('The PurchaseOrder has been saved', true));
$this->redirect(array('action'=>'index')); $this->redirect(array('action'=>'index'));
} else { } 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'); $jobs = $this->PurchaseOrder->Job->find('list');
@ -126,4 +126,4 @@ class PurchaseOrdersController extends AppController {
} }
?> ?>

View file

@ -61,8 +61,8 @@ class Document extends AppModel {
); );
//So much refactoring possible on this. //So much refactoring possible on this.
//Leaving it for now.
function getCurrency($document) { function getCurrency($document) {
if(!empty($document['Invoice']['id'])) { if(!empty($document['Invoice']['id'])) {
$currencyID = $document['Invoice']['currency_id']; $currencyID = $document['Invoice']['currency_id'];
$conditions = $this->__getCurrencyConditions($currencyID); $conditions = $this->__getCurrencyConditions($currencyID);
@ -85,7 +85,7 @@ class Document extends AppModel {
$currencyID = $document['OrderAcknowledgement']['currency_id']; $currencyID = $document['OrderAcknowledgement']['currency_id'];
$conditions = $this->__getCurrencyConditions($currencyID); $conditions = $this->__getCurrencyConditions($currencyID);
return $this->OrderAcknowledgement->Currency->find('first',$conditions); 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']))); return $this->Quote->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['Quote']['enquiry_id'])));
} }
elseif(!empty($document['PurchaseOrder']['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'])) { elseif(!empty($document['OrderAcknowledgement']['id'])) {
return $this->OrderAcknowledgement->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['OrderAcknowledgement']['enquiry_id']))); return $this->OrderAcknowledgement->Enquiry->find('first',array('conditions'=>array('Enquiry.id'=>$document['OrderAcknowledgement']['enquiry_id'])));

View file

@ -18,8 +18,12 @@ class PurchaseOrder extends AppModel {
'conditions' => '', 'conditions' => '',
'fields' => '', 'fields' => '',
'order' => '' 'order' => ''
) ),
); 'Currency' => array(
'className' => 'Currency',
'foreignKey' => 'currency_id'
),
);
var $hasAndBelongsToMany = array( var $hasAndBelongsToMany = array(
'Job' => array( 'Job' => array(
@ -51,4 +55,4 @@ class PurchaseOrder extends AppModel {
} }
?> ?>

View file

@ -9,8 +9,12 @@
</div> </div>
<div class="docOperations"> <div class="docOperations">
<h3>Create an Invoice from this OA</h3> <h3>Create new Documents based on this</h3>
<?=$html->link('Create an Invoice based on this OA', '/documents/convert_to_invoice/'.$document['Document']['id'], array('class'=>'actionLink'));?> <ul class="document-buttons">
<li>
<button id="createInvoice" data-href="/documents/newDocument/invoice/<?=$enquiry['Enquiry']['id']?>/<?=$job['Job']['id']?>/<?=$document['Document']['id']?>">Create Invoice</button>
</li>
</ul>
</div> </div>

View file

@ -28,8 +28,16 @@ echo $form->input('Document.id');
<div class="docOperations"> <div class="docOperations">
<h3>Has <?=$html->link($enquiry['Customer']['name'], array('controller'=>'customers','action'=>'view',$enquiry['Customer']['id']));?> placed an order for this Quote?</h3> <h3>Create new Documents based on this</h3>
<button id="createOA">Create Order Acknowledgement</button> <ul class="document-buttons">
<li>
<button id="createOA">Create Order Acknowledgement</button>
</li>
<li>
<button id="createInvoice" data-href="/documents/newDocument/invoice/<?=$enquiry['Enquiry']['id']?>/0/<?=$document['Document']['id']?>">Create Invoice</button>
</li>
</ul>
</div> </div>
<div class="pages"> <div class="pages">

View file

@ -75,6 +75,7 @@
<td class="actions"> <td class="actions">
<?=$html->link('View', '/documents/view/'.$invoice['Invoice']['document_id']);?>
<?=$html->link('Edit', '/invoices/edit/'.$invoice['Invoice']['id']);?> <?=$html->link('Edit', '/invoices/edit/'.$invoice['Invoice']['id']);?>
</td> </td>
</tr> </tr>

View file

@ -1670,3 +1670,10 @@ h2.document-attachments {
tr.archived { tr.archived {
text-decoration: line-through; text-decoration: line-through;
} }
ul.document-buttons {
list-style: none;
}
ul.document-buttons li {
padding: 0.5em;
}

View file

@ -353,19 +353,19 @@ $(function() {
var confirmed = confirm("This will overwrite any changes you have made to the first page"); var confirmed = confirm("This will overwrite any changes you have made to the first page");
} }
if(confirmed) { if(confirmed) {
$.get('/documents/generateFirstPage/'+docID, function(data) { $.get('/documents/generateFirstPage/'+docID, function(data) {
$(".firstPage").val(data); $(".firstPage").val(data);
savePages(); savePages();
}); });
} }
}); });
$("#pdfDocButton").click(function() { $("#pdfDocButton").click(function() {
window.location = $(this).data('url'); window.location = $(this).data('url');
}); });
$("#emailDocButton").click(function() { $("#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?"); var confirmed = confirm("This will email this Quote and all Attachments to the Customer. Are you sure you want to do this?");
if(confirmed) { if(confirmed) {
@ -381,81 +381,33 @@ $(function() {
$("#DocumentShipTo").ckeditor(config); $("#DocumentShipTo").ckeditor(config);
$("#saveInvoiceButton").click(function() { $("#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. //OA View.
$("#saveOAButton").click(function() { $("#saveOAButton").click(function() {
saveDocument('Order Acknowledgement');
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");
}
$('.job-title').html($("#OrderAcknowledgementJobId :selected").text()); $('.job-title').html($("#OrderAcknowledgementJobId :selected").text());
$("#flashMessage").show();
loadLineItems();
});
}); });
$("#createOA").click(function() { $("#createOA").click(function() {
$("#addJobConfirmation").dialog('open'); $("#addJobConfirmation").dialog('open');
}); });
$(".billing_address").click(function() { $("#createInvoice").click(function() {
if($(this).val() > 0 ) { location.href = $(this).data('href');
var address = $(this).next().html(); });
var c = confirm("Set the 'Bill To' field to this address?");
if(c) { //Choosing an address
$("#DocumentBillTo").val(address); $(".billing_address").click(function() {
} var address = $(this).next().html();
} setAddress(address, '#DocumentBillTo', 'Bill To');
}); });
$(".shipping_address").click(function() { $(".shipping_address").click(function() {
if($(this).val() > 0 ) { var address = $(this).next().html();
var address = $(this).next().html(); setAddress(address, '#DocumentShipTo', 'Ship To');
var c = confirm("Set the 'Ship To' field to this address?");
if(c) {
$("#DocumentShipTo").val(address);
}
}
}); });
}); });
@ -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() { function showHideTextPrices() {
if( $('#LineItemHasTextPrices').val() == 1) { if( $('#LineItemHasTextPrices').val() == 1) {
$("#noCosting").hide(); $("#noCosting").hide();