cmc-sales/controllers/customers_controller.php

177 lines
6 KiB
PHP
Raw Normal View History

<?php
class CustomersController extends AppController {
var $name = 'Customers';
var $helpers = array('Html', 'Form', 'Time', 'Ajax');
var $components = array('RequestHandler');
2009-07-09 00:09:59 -07:00
var $paginate = array(
'Customer' => array('order' => array('Customer.name' => 'asc'),
'limit' => 200
),
'Enquiry' => array('order' => array('Enquiry.id' => 'desc'))
);
function index() {
$this->Customer->recursive = 0;
$this->set('customers', $this->paginate());
$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'])));
}
}
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)));
$this->set('contacts', $this->Customer->Contact->find('all', array('conditions' => array('customer_id' => $id), 'order' => 'Contact.last_name ASC')));
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);
$this->set('parent_industry_list', $this->Customer->Industry->find('list', array('conditions' => array('parent_id' => null))));
}
function add() {
if (!empty($this->data)) {
/* Remove all whitespace and non-digit characters from the user input */
$this->data['Customer']['abn'] = preg_replace("/\s*\D*/", "", $this->data['Customer']['abn']);
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));
$this->redirect(array('action'=>'view', 'id'=>$newcustomerid));
}
2010-02-18 21:38:43 -08:00
}
$this->set('customer_categories', $this->Customer->CustomerCategory->find('list'));
$this->set('states', $this->Customer->Address->State->find('list'));
2010-03-10 15:32:00 -08:00
//$this->set('countries', $this->Customer->Country->find('list', array('order'=>array('Country.name ASC'))));
}
function edit($id = null) {
2009-07-09 00:09:59 -07:00
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Customer', true));
$this->redirect(array('action'=>'index'));
}
2009-07-09 00:09:59 -07:00
if (!empty($this->data)) {
/* Remove all whitespace and non-digit characters from the user input */
$this->data['Customer']['abn'] = preg_replace("/\s*\D*/", "", $this->data['Customer']['abn']);
if ($this->Customer->saveAll($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));
} else {
$this->Session->setFlash(__('The Customer could not be saved. Please, try again.', true));
$this->set('customer_categories', $this->Customer->CustomerCategory->find('list'));
$this->set('industries', $this->Customer->Industry->find('list', array('fields'=>array('Industry.id', 'Industry.name', 'ParentIndustry.name'),'recursive' => 0,
'order'=>'ParentIndustry.name ASC, Industry.name ASC')));
}
}
if (empty($this->data)) {
$this->data = $this->Customer->read(null, $id);
$this->set('customer_categories', $this->Customer->CustomerCategory->find('list'));
$this->set('industries', $this->Customer->Industry->find('list', array('fields'=>array('Industry.id', 'Industry.name', 'ParentIndustry.name'),'recursive' => 0,
'order'=>'ParentIndustry.name ASC, Industry.name ASC')));
}
}
2009-02-03 21:28:46 -08:00
/* Autocomplete the customer name - Used in: add.ctp */
function completeCustomer() {
2010-02-18 21:38:43 -08:00
$query = strtolower($_GET["term"]);
if($query == null) {
return;
}
App::import('Core', 'Sanitize');
$query = Sanitize::clean($query);
$customers = $this->Customer->find('all', array(
2009-02-03 21:28:46 -08:00
'conditions' => array(
2010-02-18 21:38:43 -08:00
'Customer.name LIKE' => '%'.$query.'%'
2009-02-03 21:28:46 -08:00
),'fields' => array('Customer.name')
2010-02-18 21:38:43 -08:00
));
foreach ($customers as $customer) {
$custid = $customer['Customer']['id'];
$cust_list[] = array('id'=> $custid, 'label' => $customer['Customer']['name']);
}
$customers_json = json_encode($cust_list);
$this->set('customers_json', $customers_json);
$this->layout = 'ajax';
2009-02-03 21:28:46 -08:00
}
2009-03-19 22:37:24 -07:00
function find() {
if(!empty($this->data)) {
$customer = $this->Customer->findByName($this->data['Customer']['name']);
if(!$customer) {
$this->Session->setFlash(__('Could not find the Requested Customer. Please select one from the drop down box as you type.', true));
}
else {
$this->redirect(array('action'=>'view/'.$customer['Customer']['id']));
}
}
}
2009-07-09 00:09:59 -07:00
function tagindustries($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->saveAll($this->data)) {
$this->Session->setFlash(__('The Industry Tags have been Saved to the Customer', true));
$this->redirect(array('action'=>'view/'.$this->data['Customer']['id']));
}
else {
$this->Session->setFlash(__('Something went wrong saving the Tags. Call for help!', true));
$this->redirect(array('action'=>'index'));
}
}
else {
$this->set('industries', $this->Customer->Industry->find('list', array('fields'=>array('Industry.id', 'Industry.name', 'ParentIndustry.name'),'recursive' => 0,
'order'=>'ParentIndustry.name ASC, Industry.name ASC')));
$this->data = $this->Customer->read(null, $id);
}
}
}
?>