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', array('order' => 'Principle.name asc')); $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'].'
'.$address['BillingAddress']['city'].' '.$address['State']['name'].' '.$address['BillingAddress']['postcode'].'
'.up($address['Country']['name']).'

'; } $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: * CMCE- * eg. CMC1245NE351234-456 */ $enquiryid = $this->Enquiry->findCount(); /* Find what number we are - CMCXXXX */ $enquiryoffset = 7882; //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 => 207, //ELMAC, AMAL 20 => 51, //T and B Electronic 25 => 2062, //ATEX,Brilex,Marston 35 => 2067, //Mid-West Instrument, DP 36 => 47, //Reading Technologies 37 => 2040, //Eldridge, Smart Measurement, Alia 65 => 2212, //Papenmeier, Herberts Industrieglas 85 => 586, //Analytical Systems, Misc 95 => 181); //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" => 38, "TAS" => 58, "ACT" => 0, "QLD" => 30, "NT" => 0, "WA" => 19, "SA" => 8, "Overseas" => 1), //T and B Electronic 20 => array("NSW" => 14, "VIC" => 6, "TAS" => 0, "ACT" => 0, "QLD" => 8, "NT" => 0, "WA" => 12, "SA" => 1, "Overseas" => 5), //ATEX,Brilex,Marston 25 => array("NSW" => 580, "VIC" => 599, "TAS" => 186, "ACT" => 0, "QLD" => 254, "NT" => 3, "WA" => 541, "SA" => 72, "Overseas" => 625), //Mid-West Instrument, DP 35 => array("NSW" => 549, "VIC" => 851, "TAS" => 9, "ACT" => 0, "QLD" => 245, "NT" => 15, "WA" => 470, "SA" => 508, "Overseas" => 154), //Reading Technologies 36 => array("NSW" => 1, "VIC" => 3, "TAS" => 0, "ACT" => 0, "QLD" => 7, "NT" => 0, "WA" => 0, "SA" => 4, "Overseas" => 2), //Eldridge, Smart Measurement, Alia 37 => array("NSW" => 204, "VIC" => 60, "TAS" => 1, "ACT" => 0, "QLD" => 130, "NT" => 1, "WA" => 44, "SA" => 25, "Overseas" => 18), //Papenmeier, Herberts Industrieglas 65 => array("NSW" => 498, "VIC" => 537, "TAS" => 44, "ACT" => 0, "QLD" => 158, "NT" => 12, "WA" => 138, "SA" => 25, "Overseas" => 174), //Analytical Systems, Misc 85 => array("NSW" => 3, "VIC" => 6, "TAS" => 0, "ACT" => 0, "QLD" => 1, "NT" => 0, "WA" => 2, "SA" => 2, "Overseas" => 0), //Comet, T and D. 95 => array("NSW" => 34, "VIC" => 28, "TAS" => 6, "ACT" => 0, "QLD" => 29, "NT" => 0, "WA" => 9, "SA" => 8, "Overseas" => 31), ); $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'); $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'].'
'.$address['BillingAddress']['city'].' '.$address['State']['name'].' '.$address['BillingAddress']['postcode'].'
'.up($address['Country']['name']).'

'; } $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->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')); } } } ?>