From aa7a25e5c2dc445827c304f5087bf6680b5f60b3 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Mon, 24 Oct 2011 14:20:07 +1100 Subject: [PATCH] Added some validation to Address model. Closes #29 --- controllers/documents_controller.php | 19 +++++-- models/address.php | 45 +++++++++++++-- views/elements/document_quote_view.ctp | 2 +- views/purchase_orders/add.ctp | 77 +++++++++++++------------- webroot/js/addpurchaseorder.js | 25 +++++++++ 5 files changed, 115 insertions(+), 53 deletions(-) diff --git a/controllers/documents_controller.php b/controllers/documents_controller.php index 4212d804..7086736e 100755 --- a/controllers/documents_controller.php +++ b/controllers/documents_controller.php @@ -6,11 +6,9 @@ class DocumentsController extends AppController { var $paginate = array( - 'contain' => false, 'limit' => 200, 'order'=>array('Document.id' => 'desc') - ); var $components = array('Email'); @@ -28,7 +26,7 @@ class DocumentsController extends AppController { $this->redirect(array('action'=>'index')); } $document = $this->Document->read(null,$id); -//$document = $this->Document->find('first',array('conditions'=>array('Document.id'=>$id), 'recursive'=>2)); + $this->set('document', $document); $this->data = $document; @@ -49,15 +47,12 @@ class DocumentsController extends AppController { break; case 'invoice': $docTypeElement = 'document_invoice_view'; - $this->set('currencies', $this->Document->Invoice->Currency->find('list')); $this->set('invoice', $this->Document->Invoice->find('first', array('conditions'=>array('Invoice.id'=>$document['Invoice']['id'])))); break; - case 'purchaseOrder': $docTypeElement = 'document_purchase_order_view'; break; - case 'orderAck': $this->set('currencies', $this->Document->OrderAcknowledgement->Currency->find('list')); $this->set('job', $this->Document->OrderAcknowledgement->Job->find('first', array('conditions'=>array('Job.id'=>$document['OrderAcknowledgement']['job_id'])))); @@ -170,6 +165,18 @@ class DocumentsController extends AppController { break; case "purchaseOrder": + $count = $this->Document->PurchaseOrder->findCount(); + $count++; + + $offset = 2060; + $count += $offset; + $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'; break; case "orderAck": $enquiry = $this->Document->OrderAcknowledgement->Enquiry->read(null, $enquiryid); diff --git a/models/address.php b/models/address.php index 342e85a4..54795a37 100755 --- a/models/address.php +++ b/models/address.php @@ -30,11 +30,18 @@ class Address extends AppModel { 'allowEmpty' => false ), 'country_id' => array( - 'rule' => array('notEmpty'), - 'message' => 'Please select the Country', - 'required' => true, - 'allowEmpty' => false - ), + 'numeric' => array( + 'rule' => 'numeric', + 'required' => true, + 'message' => 'Please choose a Country for this Address', + 'on' => 'create' + ), + 'checkStates' => array( + 'rule' => array('checkStates'), + 'message' => ' Australian States can only be chosen if Australia is selected. Otherwise choose "Overseas" for other Countries ', + 'on' => 'create')), + + 'type' => array( 'rule' => array('notEmpty'), 'message' => 'Please select the Type of Address', @@ -66,8 +73,34 @@ class Address extends AppModel { 'fields' => '', 'order' => '' - + )); + + /*Custom Validation Rule to Check that Address with Australian States are assigned to Australia. + * and that Overseas enquiries are NOT assigned to Australia. + * Not Portable at all. This code is only for CMC usage + * Should probably change this to use RegExes. + */ + + function checkStates($data) { + if($this->data['Address']['country_id'] != 1) { //This Enquiry is not for Australia. State must be set to overseas. + if($this->data['Address']['state_id'] != 9) { //The State isn't set to Overseas. Return false + return FALSE; + } + else { + return TRUE; + } + } + if($this->data['Address']['country_id'] == 1) { //This enquiry is for Australia. State must be for an Australian State + if($this->data['Address']['state_id'] == 9) { + return FALSE; + } + else { + return TRUE; + } + } + } + } ?> diff --git a/views/elements/document_quote_view.ctp b/views/elements/document_quote_view.ctp index ca67f6c4..b80176b5 100644 --- a/views/elements/document_quote_view.ctp +++ b/views/elements/document_quote_view.ctp @@ -27,7 +27,7 @@ echo $form->input('Document.id');
-

Customer placed an order for this Quote?

+

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

link('Convert this Enquiry to a new Job and Generate an Order Acknowledgement', '/documents/convert_to_oa/'.$document['Document']['id'], array('class'=>'actionLink'));?>
diff --git a/views/purchase_orders/add.ctp b/views/purchase_orders/add.ctp index ed56468d..5b23c337 100755 --- a/views/purchase_orders/add.ctp +++ b/views/purchase_orders/add.ctp @@ -1,54 +1,51 @@
-link('addpurchaseorder'); ?> -create('PurchaseOrder');?> -
- + link('addpurchaseorder'); ?> + create('PurchaseOrder');?> +
+ -
- - -
+
+ + +
- - + +
-
- - +
+ +
- - input('issue_date', array('type'=>'hidden')); - echo $form->input('dispatch_date', array('type'=>'hidden')); - echo $form->input('date_arrived', array('type'=>'hidden')); - echo $form->input('title', array('type'=>'hidden')); - echo $form->input('principle_id'); - echo $form->input('principle_reference'); - echo $form->input('document_id', array('type'=>'hidden', 'value'=>0)); - echo $form->input('Job', array('label'=>'Jobs in this PO', - 'type'=>'select', - 'multiple'=>'checkbox', - 'options'=>$jobs, - 'selected'=>$html->value('Job.Job'))); + input('issue_date', array('type'=>'hidden')); + echo $form->input('dispatch_date', array('type'=>'hidden')); + echo $form->input('date_arrived', array('type'=>'hidden')); + echo $form->input('title', array('type'=>'hidden')); + echo $form->input('principle_id'); + echo $form->input('principle_reference'); + echo $form->input('document_id', array('type'=>'hidden', 'value'=>0)); + echo $form->input('Job', array('label'=>'Jobs in this PO', + 'type'=>'select', + 'multiple'=>'checkbox', + 'options'=>$jobs, + 'selected'=>$html->value('Job.Job'))); ?> -
-end('Submit');?> +
+ end('Submit');?>
- +
diff --git a/webroot/js/addpurchaseorder.js b/webroot/js/addpurchaseorder.js index 60375ec3..bb99d3e0 100755 --- a/webroot/js/addpurchaseorder.js +++ b/webroot/js/addpurchaseorder.js @@ -30,4 +30,29 @@ $(function() { }); + /** + * A more generic way of handling the HABTM + */ + function addToList(modelName, id, value, ULelement) { + var thisLI = $('
  • '); + var thisButton = $(''); + thisButton.addClass('removeFromList'); + thisButton.button(); + + var thisHiddenInput = $(''); + + var modelString = '['+modelName+']'; + + thisHiddenInput.attr('name', 'data'+modelString+modelString+'[]'); + + thisHiddenInput.attr('value', id); + + thisLI.attr('id', modelName+'ID_'+id); + thisLI.html(value); + thisLI.prepend(thisButton); + thisLI.append(thisHiddenInput); + ULelement.append(thisLI); + + } + }); \ No newline at end of file