Files
pmgr/controllers/customers_controller.php
abijah 1b940d2adf Merge in v0.2.2 fixes
git-svn-id: file:///svn-source/pmgr/branches/v0.3_work/site@966 97e9348a-65ac-dc4b-aefc-98561f571b83
2010-03-12 04:05:00 +00:00

543 lines
18 KiB
PHP

<?php
class CustomersController extends AppController {
// DEBUG FUNCTION ONLY!
// Call without id to update ALL customers
function force_update($id = null) {
$this->Customer->update($id);
$this->redirect(array('action'=>'index'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* override: addGridViewSideMenuLinks
* - Adds grid view navigation side menu links
*/
function addGridViewSideMenuLinks() {
parent::addGridViewSideMenuLinks();
$this->addSideMenuLink('Current',
array('controller' => 'customers', 'action' => 'current'), null,
'CONTROLLER');
$this->addSideMenuLink('Past',
array('controller' => 'customers', 'action' => 'past'), null,
'CONTROLLER');
$this->addSideMenuLink('All',
array('controller' => 'customers', 'action' => 'all'), null,
'CONTROLLER');
/* $this->addSideMenuLink('New Customer', */
/* array('controller' => 'customers', 'action' => 'add'), null, */
/* 'CONTROLLER', $this->new_area); */
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index / current / past / all
* - Creates a list of customers
*/
function index() { $this->current(); }
function current() { $this->gridView('Current Tenants', 'current'); }
function past() { $this->gridView('Past Tenants'); }
function all() { $this->gridView('All Customers'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* virtuals: gridData
* - With the application controller handling the gridData action,
* these virtual functions ensure that the correct data is passed
* to jqGrid.
*/
function gridDataCountTables(&$params, &$model) {
return array
('link' =>
array(// Models
'PrimaryContact',
),
);
}
function gridDataTables(&$params, &$model) {
$link = $this->gridDataCountTables($params, $model);
// StatementEntry is needed to determine customer balance
$link['link']['StatementEntry'] = array('fields' => array());
return $link;
}
function gridDataFields(&$params, &$model) {
$fields = parent::gridDataFields($params, $model);
return array_merge($fields,
$this->Customer->StatementEntry->chargeDisbursementFields(true));
}
function gridDataConditions(&$params, &$model) {
$conditions = parent::gridDataConditions($params, $model);
if ($params['action'] === 'current') {
$conditions[] = array('Customer.current_lease_count >' => 0);
}
elseif ($params['action'] === 'past') {
$conditions[] = array('Customer.current_lease_count' => 0);
$conditions[] = array('Customer.past_lease_count >' => 0);
}
return $conditions;
}
function gridDataFilterTablesConfig(&$params, &$model, $table) {
$config = parent::gridDataFilterTablesConfig($params, $model, $table);
// Special case for Contact; We need the Contact/Customer relationship
if ($table == 'Contact')
$config = array('fields' => array('ContactsCustomer.type',
'ContactsCustomer.active'),
'conditions' => array('ContactsCustomer.active' => true),
);
return $config;
}
function gridDataOrder(&$params, &$model, $index, $direction) {
$order = parent::gridDataOrder($params, $model, $index, $direction);
// After sorting by whatever the user wants, add these
// defaults into the sort mechanism. If we're already
// sorting by one of them, it will only be redundant,
// and should cause no harm (possible a longer query?)
$order[] = 'PrimaryContact.last_name ' . $direction;
$order[] = 'PrimaryContact.first_name ' . $direction;
$order[] = 'Customer.id ' . $direction;
return $order;
}
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['Customer'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: move_in
* - Sets up the move-in page for the given customer.
*/
function move_in($id = null, $unit_id = null) {
$customer = array();
$unit = array();
if (!empty($id)) {
$this->Customer->recursive = -1;
$customer = current($this->Customer->read(null, $id));
}
if (!empty($unit_id)) {
$this->Customer->Lease->Unit->recursive = -1;
$unit = current($this->Customer->Lease->Unit->read(null, $unit_id));
}
$this->set(compact('customer', 'unit'));
$title = 'Customer Move-In';
$this->set(compact('title'));
$this->render('/leases/move');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: move_out
* - prepare to move a customer out of one of their units
*/
function move_out($id) {
$customer = $this->Customer->find
('first', array
('contain' => array
(// Models
'Lease' =>
array('conditions' => array('Lease.moveout_date' => null),
// Models
'Unit' =>
array('order' => array('sort_order'),
'fields' => array('id', 'name'),
),
),
),
'conditions' => array('Customer.id' => $id),
));
$this->set('customer', $lease['Customer']);
$this->set('unit', array());
$redirect = array('controller' => 'customers',
'action' => 'view',
$id);
$title = $customer['Customer']['name'] . ': Prepare Move-Out';
$this->set(compact('title', 'customer', 'redirect'));
$this->render('/leases/move');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: view
* - Displays information about a specific customer
*/
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
// Get details on this customer, its contacts and leases
$customer = $this->Customer->find
('first', array
('contain' => array
(// Models
'Contact' =>
array('order' => array('Contact.display_name'),
// Models
'ContactPhone',
'ContactEmail',
'ContactAddress',
),
'Lease' =>
array('Unit' =>
array('order' => array('sort_order'),
'fields' => array('id', 'name'),
),
),
),
'conditions' => array('Customer.id' => $id),
));
//pr($customer);
// Determine how long this customer has been with us.
$leaseinfo = $this->Customer->find
('first', array
('link' => array('Lease' => array('fields' => array())),
'fields' => array('MIN(Lease.movein_date) AS since',
'IF(Customer.current_lease_count = 0, MAX(Lease.moveout_date), NULL) AS until'),
'conditions' => array('Customer.id' => $id),
'group' => 'Customer.id',
));
$this->set($leaseinfo[0]);
// Figure out the outstanding balances for this customer
//$this->set('stats', $this->Customer->stats($id));
$outstanding_balance = $this->Customer->balance($id);
$outstanding_deposit = $this->Customer->securityDepositBalance($id);
// Figure out if this customer has any non-closed leases
$show_moveout = false; $moveout_lease_id = null;
$show_payment = false; $payment_lease_id = null;
foreach ($customer['Lease'] AS $lease) {
if (!isset($lease['close_date'])) {
if ($show_payment)
$payment_lease_id = null;
else
$payment_lease_id = $lease['id'];
$show_payment = true;
}
if (!isset($lease['moveout_date'])) {
if ($show_moveout)
$moveout_lease_id = null;
else
$moveout_lease_id = $lease['id'];
$show_moveout = true;
}
}
// Set up dynamic menu items
if ($show_payment || $outstanding_balance > 0)
$this->addSideMenuLink('New Receipt',
array('action' => 'receipt', $id), null,
'ACTION');
if ($show_payment) {
/* $ids = $this->Customer->leaseIds($id, true); */
/* if (count($ids) == 1) */
/* $lease_id = $ids[0]; */
/* else */
/* $lease_id = null; */
$this->addSideMenuLink('New Invoice',
array('controller' => 'leases',
'action' => 'invoice',
$payment_lease_id), null,
'ACTION');
}
$this->addSideMenuLink('Move-In',
array('action' => 'move_in', $id), null,
'ACTION');
if ($show_moveout) {
$this->addSideMenuLink('Move-Out',
array('controller' => 'leases',
'action' => 'move_out',
$moveout_lease_id), null,
'ACTION');
}
if (!$show_moveout && $outstanding_balance > 0)
$this->addSideMenuLink('Write-Off',
array('action' => 'bad_debt', $id), null,
'ACTION');
if ($outstanding_balance < 0)
$this->addSideMenuLink('Issue Refund',
array('action' => 'refund', $id), null,
'ACTION');
$this->addSideMenuLink('Edit',
array('action' => 'edit', $id), null,
'ACTION');
// Prepare to render.
$title = 'Customer: ' . $customer['Customer']['name'];
$this->set(compact('customer', 'title',
'outstanding_balance',
'outstanding_deposit'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: edit
* - Edit customer information
*/
function edit($id = null) {
if (isset($this->data)) {
// Check to see if the operation was cancelled.
if (isset($this->params['form']['cancel'])) {
if (isset($this->data['Customer']['id']))
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
$this->redirect(array('action'=>'index'));
}
// Make sure we have at least one contact
if (!isset($this->data['Contact']) || count($this->data['Contact']) == 0) {
$this->Session->setFlash("MUST SPECIFY AT LEAST ONE CONTACT", true);
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
}
// Make sure there is a primary contact
if (!isset($this->data['Customer']['primary_contact_entry'])) {
$this->Session->setFlash("MUST SPECIFY A PRIMARY CONTACT", true);
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
}
// Go through each customer and strip the bogus ID if new
foreach ($this->data['Contact'] AS &$contact) {
if (isset($contact['source']) && $contact['source'] === 'new')
unset($contact['id']);
}
// Save the customer and all associated data
if (!$this->Customer->saveCustomer($this->data['Customer']['id'],
$this->data,
$this->data['Customer']['primary_contact_entry'])) {
$this->Session->setFlash("CUSTOMER SAVE FAILED", true);
pr("CUSTOMER SAVE FAILED");
}
// If existing customer, then view it.
if ($this->data['Customer']['id'])
$this->redirect(array('action'=>'view', $this->Customer->id));
// Since this is a new customer, go to the move in screen.
// First set the move-in unit id, if there is one, ...
if (empty($this->data['movein']['Unit']['id']))
$unit_id = null;
else
$unit_id = $this->data['movein']['Unit']['id'];
// ... then redirect
$this->redirect(array('action'=>'move_in',
$this->Customer->id,
$unit_id,
));
}
if ($id) {
// REVISIT <AP>: 20090816
// This should never need to be done by a controller.
// However, until things stabilize, this gives the
// user a way to update any cached items on the
// customer, by just clicking Edit then Cancel.
$this->Customer->update($id);
// Get details on this customer, its contacts and leases
$customer = $this->Customer->find
('first', array
('contain' => array
(// Models
'Contact' =>
array('order' => array('Contact.display_name'),
// Models
'ContactPhone',
'ContactEmail',
'ContactAddress',
),
'Lease' =>
array('Unit' =>
array('order' => array('sort_order'),
'fields' => array('id', 'name'),
),
),
),
'conditions' => array('Customer.id' => $id),
));
$this->data = $customer;
$title = 'Customer: ' . $this->data['Customer']['name'] . " : Edit";
}
else {
$title = "Enter New Customer";
$this->data = array('Contact' => array(), 'PrimaryContact' => null);
}
$contact_types = array_flip($this->Customer->ContactsCustomer->getEnumValues('type'));
unset($contact_types[0]);
$contact_types = array_combine($contact_types, $contact_types);
$this->set(compact('contact_types'));
$contacts = $this->Customer->Contact->contactList();
$this->set(compact('contacts'));
// Prepare to render.
//pr($this->data);
$this->set(compact('title'));
$this->render('edit');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: add
* - Add a new customer
*/
function add($unit_id = null) {
$this->set('movein', array('Unit' => array('id' => $unit_id)));
$this->edit();
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: receipt
* - Sets up the receipt entry page for the given customer.
*/
function receipt($id = null) {
if (isset($id)) {
$this->Customer->recursive = -1;
$customer = $this->Customer->read(null, $id);
$customer = $customer['Customer'];
}
else {
$customer = null;
}
$TT = new TenderType();
$payment_types = $TT->paymentTypes();
$default_type = $TT->defaultPaymentType();
$this->set(compact('payment_types', 'default_type'));
$title = ($customer['name'] . ': Receipt Entry');
$this->set(compact('customer', 'title'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: refund
* - Refunds customer charges
*/
function refund($id) {
$customer = $this->Customer->find
('first', array
('contain' => false,
'conditions' => array(array('Customer.id' => $id),
),
));
if (empty($customer)) {
$this->redirect(array('action'=>'view', $id));
}
// Determine the customer balance, bailing if the customer owes money
$balance = $this->Customer->balance($id);
if ($balance >= 0) {
$this->redirect(array('action'=>'view', $id));
}
// The refund will be for a positive amount
$balance *= -1;
// Get the accounts capable of paying the refund
$refundAccounts = $this->Customer->StatementEntry->Account->refundAccounts();
$defaultAccount = current($refundAccounts);
$this->set(compact('refundAccounts', 'defaultAccount'));
// Prepare to render
$title = ($customer['Customer']['name'] . ': Refund');
$this->set(compact('title', 'customer', 'balance'));
$this->render('/transactions/refund');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: bad_debt
* - Sets up the write-off entry page, so that the
* user can write off remaining charges of a customer.
*/
function bad_debt($id) {
$this->Customer->id = $id;
$customer = $this->Customer->find
('first', array
('contain' => false,
));
// Make sure we have a valid customer to write off
if (empty($customer))
$this->redirect(array('action' => 'index'));
// Get the customer balance
$balance = $this->Customer->balance($id);
// Prepare to render
$title = ($customer['Customer']['name'] . ': Write Off Bad Debt');
$this->set(compact('title', 'customer', 'balance'));
$this->render('/transactions/bad_debt');
}
}