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));
$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> $type
* @param <type> $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 <int> $id
*/
function convert_to_invoice($id = null) {
if (!$id) {
echo 'Invalid Document ID';
}
else {
}
}
function pdf($id = null) {
//Configure::write('debug',0);
if(!$id) {

View file

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

View file

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

View file

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

View file

@ -9,8 +9,12 @@
</div>
<div class="docOperations">
<h3>Create an Invoice from this OA</h3>
<?=$html->link('Create an Invoice based on this OA', '/documents/convert_to_invoice/'.$document['Document']['id'], array('class'=>'actionLink'));?>
<h3>Create new Documents based on this</h3>
<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>

View file

@ -28,8 +28,16 @@ echo $form->input('Document.id');
<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>
<button id="createOA">Create Order Acknowledgement</button>
<h3>Create new Documents based on this</h3>
<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 class="pages">

View file

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

View file

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

View file

@ -353,12 +353,12 @@ $(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();
});
}
});
@ -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();