256 lines
11 KiB
PHP
Executable file
256 lines
11 KiB
PHP
Executable file
<?php
|
|
|
|
App::import('Core', 'Sanitize');
|
|
class EnquiriesController extends AppController {
|
|
|
|
var $name = 'Enquiries';
|
|
var $helpers = array('Html', 'Form', 'Javascript', 'Time', 'Ajax');
|
|
|
|
var $paginate = array(
|
|
'limit' => 250,
|
|
'order'=>array('Enquiry.id' => 'desc'));
|
|
|
|
|
|
function index() {
|
|
$this->Enquiry->recursive = 0;
|
|
$this->set('enquiries', $this->paginate());
|
|
}
|
|
|
|
function view($id = null) {
|
|
if (!$id) {
|
|
$this->Session->setFlash(__('Invalid Enquiry.', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
$this->set('enquiry', $this->Enquiry->read(null, $id));
|
|
$this->set('quotes', $this->Enquiry->Quote->find('all', array('conditions'=>array('Quote.enquiry_id'=>$id), 'order'=>'Quote.revision DESC')));
|
|
}
|
|
|
|
|
|
|
|
function add() {
|
|
|
|
|
|
$states = $this->Enquiry->State->find('list');
|
|
$countries = $this->Enquiry->Country->find('list');
|
|
$principles = $this->Enquiry->Principle->find('list');
|
|
$statuses = $this->Enquiry->Status->find('list');
|
|
$user_id = $this->Auth->user('id');
|
|
$this->set(compact('user_id','users', 'customers', 'states', 'countries', 'principles', 'statuses'));
|
|
|
|
|
|
if(empty($this->data)) {
|
|
$this->set('step', 1);
|
|
$users = $this->Enquiry->User->find('list', array('fields' => array('User.id', 'User.username')));
|
|
$this->set('users', $users);
|
|
}
|
|
|
|
elseif( (!empty($this->data)) && ($this->data['Enquiry']['step'] == 1) ) {
|
|
$customer = $this->Enquiry->Customer->findByName($this->data['Customer']['name']);
|
|
$this->set('customer', $customer);
|
|
$contacts = $this->Enquiry->Contact->find('list', array('conditions' => array('Contact.customer_id' => $customer['Customer']['id'])));
|
|
/* Check if the customer exists in the database - otherwise send the user back to step 1*/
|
|
if(!$customer) {
|
|
$this->Session->setFlash(__('The customer must already exist in the database. Please try again', true));
|
|
$this->redirect(array('action'=>'add'));
|
|
}
|
|
$this->set('step',2);
|
|
$user_id = $this->Auth->user('id');
|
|
$this->set('user_id', $user_id);
|
|
$this->set('contacts', $contacts);
|
|
|
|
$customerAddresses = $this->Enquiry->BillingAddress->find('all', array('conditions'=>array('BillingAddress.customer_id' => $customer['Customer']['id'])));
|
|
$this->set('billing_addresses', $customerAddresses);
|
|
$this->set('shipping_addresses', $customerAddresses);
|
|
|
|
/* Feels like an ugly hack. But it gets the desired effect and it will do for now */
|
|
|
|
if(isset($customerAddresses[0]['BillingAddress'])) {
|
|
foreach ($customerAddresses as $address) {
|
|
$i = $address['BillingAddress']['id'];
|
|
$billing_address_list[$i] = $address['BillingAddress']['address'].'<br>'.$address['BillingAddress']['city'].' '.$address['State']['name'].' '.$address['BillingAddress']['postcode'].'<br>'.up($address['Country']['name']).'<br><br>';
|
|
}
|
|
$this->set('billing_addresses_list', $billing_address_list);
|
|
$this->set('shipping_addresses_list', $billing_address_list);
|
|
}
|
|
else {
|
|
$this->set('billing_addresses_list', 'No Addresses exist for this Customer. Please add one');
|
|
$this->set('shipping_addresses_list', 'No Addresses exist for this Customer. Please add one');
|
|
}
|
|
$this->render();
|
|
|
|
}
|
|
|
|
if ( (!empty($this->data)) && ($this->data['Enquiry']['finished'] == 'true') ) {
|
|
$this->Enquiry->set($this->data);
|
|
if( !($this->Enquiry->validates()) ) {
|
|
$customer = $this->Enquiry->Customer->findByName($this->data['Customer']['name']);
|
|
$this->set('customer', $customer);
|
|
$contacts = $this->Enquiry->Contact->find('list', array('conditions' => array('Contact.customer_id' => $customer['Customer']['id'])));
|
|
$this->set('contacts', $contacts);
|
|
$this->set('billing_addresses', $this->Enquiry->BillingAddress->find('list', array('conditions'=>array('BillingAddress.customer_id' => $customer['Customer']['id']))));
|
|
$this->set('shipping_addresses', $this->Enquiry->ShippingAddress->find('list', array('conditions'=>array('ShippingAddress.customer_id' => $customer['Customer']['id']))));
|
|
$this->set('errors', $this->Enquiry->invalidFields());
|
|
$this->set('step',2);
|
|
$this->render();
|
|
|
|
|
|
}
|
|
else {
|
|
$this->Enquiry->create();
|
|
$state = $this->Enquiry->State->findById($this->data['Enquiry']['state_id']);
|
|
$principle = $this->Enquiry->Principle->findById($this->data['Enquiry']['principle_id']);
|
|
/* Generate the enquiry number for this enquiry */
|
|
/* CMC Enquiry number format is:
|
|
* CMC<INCREMENTAL_NUMBER><STATE_INITIAL>E<PRINCIPLE_CODE><NO_OF_ENQ_FOR_THIS_PRINCIPLE_CODE>-<NO_OF_ENQ_FOR_THIS_PRINCIPLE_CODE_IN_THIS_STATE>
|
|
* eg. CMC1245NE351234-456 */
|
|
|
|
$enquiryid = $this->Enquiry->findCount(); /* Find what number we are - CMCXXXX */
|
|
$enquiryoffset = 7203; //What Number Enquiry we were at before using this package.
|
|
$enquiryid += $enquiryoffset;
|
|
$enquiryid++;
|
|
/* Find how many enquiries have been for this principle code */
|
|
$principleenquiries = $this->Enquiry->findCount('principle_code ='. $principle['Principle']['code']);
|
|
|
|
/* Principle Code Offsets - Existing Number of Enquiries for each Principle Code */
|
|
$principlecode_offset = array( 15 => 204, //ELMAC, AMAL
|
|
20 => 49, //T and B Electronic
|
|
25 => 2021, //ATEX,Brilex,Marston
|
|
35 => 2030, //Mid-West Instrument, DP
|
|
36 => 46, //Reading Technologies
|
|
37 => 2020, //Eldridge, Smart Measurement, Alia
|
|
65 => 2189, //Papenmeier, Herberts Industrieglas
|
|
85 => 585, //Analytical Systems, Misc
|
|
95 => 171); //Comet, T and D.
|
|
$principleenquiries += $principlecode_offset[$principle['Principle']['code']]; //Add the offset.
|
|
$principleenquiries++;
|
|
|
|
|
|
|
|
/* Find how many enquiries have been for this principle code in this state */
|
|
/* This aspect of the enquiry number is not useful anymore. Made redundant thanks to mySQL queries.
|
|
* But this has been done for backwards-compatablity with existing enquiry numbers */
|
|
$principleconditions = array
|
|
(
|
|
"Enquiry.principle_code" => $principle['Principle']['code'],
|
|
"Enquiry.state_id" => $this->data['Enquiry']['state_id']
|
|
);
|
|
|
|
$stateprincipleenquiries = $this->Enquiry->findCount($principleconditions);
|
|
|
|
/*Add the offsets for the State Principle Enquiries for each Code */
|
|
$stateprinciple_offset = array(
|
|
//Elmac, Amal
|
|
15 => array("NSW" => 39, "VIC" => 36, "TAS" => 58,
|
|
"ACT" => 0, "QLD" => 30, "NT" => 0,
|
|
"WA" => 18, "SA" => 0, "Overseas" => 0),
|
|
//T and B Electronic
|
|
20 => array("NSW" => 13, "VIC" => 6, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 8, "NT" => 0,
|
|
"WA" => 12, "SA" => 0, "Overseas" => 4),
|
|
//ATEX,Brilex,Marston
|
|
25 => array("NSW" => 569, "VIC" => 589, "TAS" => 186,
|
|
"ACT" => 0, "QLD" => 250, "NT" => 3,
|
|
"WA" => 540, "SA" => 71, "Overseas" => 612),
|
|
//Mid-West Instrument, DP
|
|
35 => array("NSW" => 530, "VIC" => 844, "TAS" => 9,
|
|
"ACT" => 0, "QLD" => 250, "NT" => 14,
|
|
"WA" => 464, "SA" => 507, "Overseas" => 149),
|
|
//Reading Technologies ****TO DO****
|
|
36 => array("NSW" => 0, "VIC" => 0, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 0, "NT" => 0,
|
|
"WA" => 0, "SA" => 0, "Overseas" => 0),
|
|
//Eldridge, Smart Measurement, Alia *** TO DO***
|
|
37 => array("NSW" => 0, "VIC" => 0, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 0, "NT" => 0,
|
|
"WA" => 0, "SA" => 0, "Overseas" => 0),
|
|
//Papenmeier, Herberts Industrieglas *** TO DO***
|
|
65 => array("NSW" => 0, "VIC" => 0, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 0, "NT" => 0,
|
|
"WA" => 0, "SA" => 0, "Overseas" => 0),
|
|
//Analytical Systems, Misc *** TO DO***
|
|
85 => array("NSW" => 0, "VIC" => 0, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 0, "NT" => 0,
|
|
"WA" => 0, "SA" => 0, "Overseas" => 0),
|
|
//Comet, T and D. *** TO DO***
|
|
95 => array("NSW" => 0, "VIC" => 0, "TAS" => 0,
|
|
"ACT" => 0, "QLD" => 0, "NT" => 0,
|
|
"WA" => 0, "SA" => 0, "Overseas" => 0),
|
|
);
|
|
$stateprincipleenquiries += $stateprinciple_offset[$principle['Principle']['code']][$state['State']['shortform']];
|
|
$stateprincipleenquiries++;
|
|
/* Generate/set the enquiry number */
|
|
$enquiryno = 'CMC'.$enquiryid.$state['State']['enqform'].'E'.$principle['Principle']['code'].$principleenquiries.
|
|
'-'.$stateprincipleenquiries;
|
|
|
|
|
|
$this->data['Enquiry']['title'] = $enquiryno;
|
|
$this->data['Enquiry']['principle_code'] = $principle['Principle']['code']; //Store which principle code this enquiry belongs to.
|
|
Sanitize::clean($this->data);
|
|
}
|
|
|
|
if ($this->Enquiry->save($this->data)) {
|
|
$this->Session->setFlash(__('The Enquiry has been saved', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
} else {
|
|
$this->Session->setFlash(__('The Enquiry could not be saved. Please, try again.', true));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/* Autocomplete the customer name - Used in: add.ctp */
|
|
function completeCustomer() {
|
|
|
|
$this->set('customers', $this->Enquiry->Customer->find('all', array(
|
|
'conditions' => array(
|
|
'Customer.name LIKE' => $this->data['Customer']['name'].'%'
|
|
),'fields' => array('Customer.name')
|
|
)));
|
|
$this->layout = 'ajax';
|
|
|
|
}
|
|
|
|
|
|
function edit($id = null) {
|
|
if (!$id && empty($this->data)) {
|
|
$this->Session->setFlash(__('Invalid Enquiry', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
if (!empty($this->data)) {
|
|
if ($this->Enquiry->save($this->data)) {
|
|
$this->Session->setFlash(__('The Enquiry has been saved', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
} else {
|
|
$this->Session->setFlash(__('The Enquiry could not be saved. Please, try again.', true));
|
|
$this->set('dump', $this->data);
|
|
}
|
|
}
|
|
if (empty($this->data)) {
|
|
$this->data = $this->Enquiry->read(null, $id);
|
|
}
|
|
$enquiry = $this->Enquiry->read(null, $id);
|
|
$users = $this->Enquiry->User->find('list', array('fields' => array('User.id', 'User.username')));
|
|
$customer = $this->Enquiry->Customer->findById($enquiry['Enquiry']['customer_id']);
|
|
$contacts = $this->Enquiry->Contact->find('list', array('conditions' => array('Contact.customer_id' => $enquiry['Enquiry']['customer_id'] )));
|
|
$state = $this->Enquiry->State->findById($enquiry['Enquiry']['state_id']);
|
|
$country = $this->Enquiry->Country->findById($enquiry['Enquiry']['country_id']);
|
|
$principle = $this->Enquiry->Principle->findById($enquiry['Enquiry']['principle_id']);
|
|
$statuses = $this->Enquiry->Status->find('list');
|
|
|
|
$this->set(compact('enquiry', 'users','customer','contacts','state','country','principle','statuses', 'addresses'));
|
|
}
|
|
|
|
function delete($id = null) {
|
|
if (!$id) {
|
|
$this->Session->setFlash(__('Invalid id for Enquiry', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
if ($this->Enquiry->del($id)) {
|
|
$this->Session->setFlash(__('Enquiry deleted', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
}
|
|
|
|
}
|
|
?>
|