cmc-sales/controllers/customers_controller.php

69 lines
2.1 KiB
PHP
Executable file

<?php
class CustomersController extends AppController {
var $name = 'Customers';
var $helpers = array('Html', 'Form', 'Time');
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());
}
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)));
}
function add() {
if (!empty($this->data)) {
$this->data['Contact'][0]['name'] = $this->data['Contact'][0]['first_name'].' '.$this->data['Contact'][0]['last_name'];
if($this->Customer->saveAll($this->data, array('validate'=>'first')) ) {
$newcustomerid = $this->Customer->id;
$this->Session->setFlash(__('The Customer and Contact have been saved', true));
$this->redirect(array('action'=>'view', 'id'=>$newcustomerid));
}
else {
$this->Session->setFlash(__('The Customer could not be saved. Please try again.', true));
}
}
$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));
$this->redirect(array('action'=>'index'));
} 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);
}
}
}
?>