2009-01-12 21:13:18 -08:00
|
|
|
<?php
|
|
|
|
|
class CustomersController extends AppController {
|
|
|
|
|
|
|
|
|
|
var $name = 'Customers';
|
2009-02-03 17:49:44 -08:00
|
|
|
var $helpers = array('Html', 'Form', 'Time', 'Ajax');
|
|
|
|
|
var $components = array('RequestHandler');
|
2009-01-12 21:13:18 -08:00
|
|
|
var $paginate = array(
|
|
|
|
|
'Customer' => array('order' => array('Customer.name' => 'asc'),
|
|
|
|
|
'limit' => 200
|
|
|
|
|
),
|
|
|
|
|
'Enquiry' => array('order' => array('Enquiry.id' => 'asc'))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function index() {
|
|
|
|
|
$this->Customer->recursive = 0;
|
|
|
|
|
$this->set('customers', $this->paginate());
|
2009-02-11 20:02:05 -08:00
|
|
|
$this->set('customer_categories', $this->Customer->CustomerCategory->find('all'));
|
|
|
|
|
/* Show only customers from a particular category name */
|
|
|
|
|
if(isset($this->params['named']['showonly'])) {
|
|
|
|
|
$this->set('customers', $this->paginate('Customer', array('Customer.customer_category_id' => $this->params['named']['showonly'])));
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-12 21:13:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function view($id = null) {
|
|
|
|
|
if (!$id) {
|
|
|
|
|
$this->Session->setFlash(__('Invalid Customer.', true));
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
}
|
|
|
|
|
$this->set('customer', $this->Customer->read(null, $id));
|
|
|
|
|
$this->set('addresses', $this->Customer->Address->findAllByCustomerId($id));
|
|
|
|
|
$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.customer_id' => $id)));
|
2009-02-12 22:11:12 -08:00
|
|
|
|
|
|
|
|
$statuses = $this->Customer->Enquiry->Status->find('all');
|
|
|
|
|
$status_list = array();
|
|
|
|
|
foreach ($statuses as $status) {
|
|
|
|
|
$status_list[] = array($status['Status']['id'], $status['Status']['name']);
|
|
|
|
|
}
|
|
|
|
|
$this->set('status_list', $status_list);
|
2009-01-12 21:13:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function add() {
|
|
|
|
|
if (!empty($this->data)) {
|
2009-02-03 17:49:44 -08:00
|
|
|
|
2009-01-12 21:13:18 -08:00
|
|
|
if($this->Customer->saveAll($this->data, array('validate'=>'first')) ) {
|
|
|
|
|
$newcustomerid = $this->Customer->id;
|
2009-01-18 21:06:41 -08:00
|
|
|
$this->Session->setFlash(__('The Customer has been saved', true));
|
2009-01-12 21:13:18 -08:00
|
|
|
$this->redirect(array('action'=>'view', 'id'=>$newcustomerid));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$this->Session->setFlash(__('The Customer could not be saved. Please try again.', true));
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-11 20:02:05 -08:00
|
|
|
$this->set('customer_categories', $this->Customer->CustomerCategory->find('list'));
|
2009-01-12 21:13:18 -08:00
|
|
|
$this->set('states', $this->Customer->Address->State->find('list'));
|
|
|
|
|
$this->set('countries', $this->Customer->Address->Country->find('list'));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function edit($id = null) {
|
|
|
|
|
if (!$id && empty($this->data)) {
|
|
|
|
|
$this->Session->setFlash(__('Invalid Customer', true));
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
}
|
|
|
|
|
if (!empty($this->data)) {
|
|
|
|
|
if ($this->Customer->save($this->data)) {
|
|
|
|
|
$this->Session->setFlash(__('The Customer has been saved', true));
|
2009-02-03 21:28:46 -08:00
|
|
|
$id = $this->Customer->id;
|
|
|
|
|
$this->redirect(array('action'=>'view/'.$id));
|
2009-01-12 21:13:18 -08:00
|
|
|
} else {
|
|
|
|
|
$this->Session->setFlash(__('The Customer could not be saved. Please, try again.', true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (empty($this->data)) {
|
|
|
|
|
$this->data = $this->Customer->read(null, $id);
|
2009-02-11 20:02:05 -08:00
|
|
|
$this->set('customer_categories', $this->Customer->CustomerCategory->find('list'));
|
2009-01-12 21:13:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-03 21:28:46 -08:00
|
|
|
/* Autocomplete the customer name - Used in: add.ctp */
|
|
|
|
|
function completeCustomer() {
|
|
|
|
|
|
|
|
|
|
$this->set('customers', $this->Customer->find('all', array(
|
|
|
|
|
'conditions' => array(
|
|
|
|
|
'Customer.name LIKE' => '%'.$this->data['Customer']['name'].'%'
|
|
|
|
|
),'fields' => array('Customer.name')
|
|
|
|
|
)));
|
|
|
|
|
$this->layout = 'ajax';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-12 21:13:18 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
?>
|