git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@221 97e9348a-65ac-dc4b-aefc-98561f571b83
452 lines
15 KiB
PHP
452 lines
15 KiB
PHP
<?php
|
|
|
|
class CustomersController extends AppController {
|
|
var $sidemenu_links =
|
|
array(array('name' => 'Tenants', 'header' => true),
|
|
array('name' => 'Current', 'url' => array('controller' => 'customers', 'action' => 'current')),
|
|
array('name' => 'Past', 'url' => array('controller' => 'customers', 'action' => 'past')),
|
|
array('name' => 'All', 'url' => array('controller' => 'customers', 'action' => 'all')),
|
|
array('name' => 'Add Tenant', 'url' => array('controller' => 'customers', 'action' => 'add')),
|
|
);
|
|
|
|
//var $components = array('RequestHandler');
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* override: sideMenuLinks
|
|
* - Generates controller specific links for the side menu
|
|
*/
|
|
function sideMenuLinks() {
|
|
return array_merge(parent::sideMenuLinks(), $this->sidemenu_links);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: index / current / past / all
|
|
* - Creates a list of tenants
|
|
*/
|
|
|
|
function index() { $this->current(); }
|
|
function current() { $this->jqGridView('Current Tenants'); }
|
|
function past() { $this->jqGridView('Past Tenants'); }
|
|
function all() { $this->jqGridView('All Tenants', 'all'); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* virtuals: jqGridData
|
|
* - With the application controller handling the jqGridData action,
|
|
* these virtual functions ensure that the correct data is passed
|
|
* to jqGrid.
|
|
*/
|
|
|
|
function jqGridDataSetup(&$params) {
|
|
parent::jqGridDataSetup($params);
|
|
if (!isset($params['action']))
|
|
$params['action'] = 'all';
|
|
}
|
|
|
|
function jqGridDataTables(&$params, &$model) {
|
|
return array
|
|
('link' =>
|
|
array(// Models
|
|
'PrimaryContact',
|
|
'CurrentLease' => array('fields' => array()),
|
|
),
|
|
);
|
|
}
|
|
|
|
function jqGridDataFields(&$params, &$model) {
|
|
return array('Customer.*',
|
|
'COUNT(CurrentLease.id) AS lease_count');
|
|
}
|
|
|
|
function jqGridDataConditions(&$params, &$model) {
|
|
$conditions = parent::jqGridDataConditions($params, $model);
|
|
|
|
if ($params['action'] === 'current') {
|
|
$conditions[] = 'CurrentLease.id IS NOT NULL';
|
|
}
|
|
elseif ($params['action'] === 'past') {
|
|
$conditions[] = 'CurrentLease.id IS NULL';
|
|
}
|
|
|
|
return $conditions;
|
|
}
|
|
|
|
function jqGridDataOrder(&$params, &$model, $index, $direction) {
|
|
$order = parent::jqGridDataOrder($params, $model, $index, $direction);
|
|
if ($index === 'PrimaryContact.last_name') {
|
|
$order[] = 'PrimaryContact.first_name ' . $direction;
|
|
}
|
|
if ($index === 'PrimaryContact.first_name') {
|
|
$order[] = 'PrimaryContact.last_name ' . $direction;
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
function jqGridDataRecordCount(&$params, &$model, $query) {
|
|
|
|
// We don't have a good way to use the query to obtain
|
|
// our count. The problem is that we're relying on the
|
|
// group by for the query, which will destroy the count,
|
|
// whether we omit the group by or leave it in.
|
|
// So, build a fresh query for counting.
|
|
|
|
$query['conditions'] = parent::jqGridDataConditions($params, $model);
|
|
|
|
$count = $model->find('count',
|
|
array_merge(array('link' => array_diff_key($query['link'],
|
|
array('CurrentLease'=>1))),
|
|
array_diff_key($query, array('link'=>1))));
|
|
|
|
if ($params['action'] === 'all')
|
|
return $count;
|
|
|
|
$query['conditions'][] = 'CurrentLease.id IS NULL';
|
|
$count_past = $model->find('count', $query);
|
|
|
|
// Since we can't easily count 'current' directly, we
|
|
// can quickly derive it since 'current' customers
|
|
// are mutually exclusive to 'past' customers.
|
|
if ($params['action'] == 'current')
|
|
$count = $count - $count_past;
|
|
elseif ($params['action'] == 'past') {
|
|
$count = $count_past;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
function jqGridDataRecords(&$params, &$model, $query) {
|
|
$customers = parent::jqGridDataRecords($params, $model, $query);
|
|
|
|
// Get the balance on each customer.
|
|
foreach ($customers AS &$customer) {
|
|
$stats = $this->Customer->stats($customer['Customer']['id']);
|
|
$customer['Customer']['balance'] = $stats['balance'];
|
|
}
|
|
|
|
return $customers;
|
|
}
|
|
|
|
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
|
|
$links['Customer'] = array('name');
|
|
return parent::jqGridRecordLinks($params, $model, $records, $links);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: move_in
|
|
* - Sets up the move-in page for the given customer.
|
|
*/
|
|
|
|
function move_in($id = null) {
|
|
$customer = array();
|
|
$unit = array();
|
|
|
|
if (isset($id)) {
|
|
$this->Customer->recursive = -1;
|
|
$customer = current($this->Customer->read(null, $id));
|
|
}
|
|
|
|
$title = 'Customer Move-In';
|
|
$this->set(compact('customer', 'unit', 'title'));
|
|
$this->render('/leases/move_in');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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),
|
|
));
|
|
|
|
$redirect = array('controller' => 'customers',
|
|
'action' => 'view',
|
|
$id);
|
|
|
|
$title = $customer['Customer']['name'] . ': Prepare Move-Out';
|
|
$this->set(compact('title', 'customer', 'redirect'));
|
|
$this->render('/leases/move_out');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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'));
|
|
}
|
|
|
|
$customer = $this->Customer->details($id);
|
|
|
|
$outstanding_balance = $customer['stats']['balance'];
|
|
$outstanding_deposit = $customer['deposits']['summary']['balance'];
|
|
|
|
// Figure out if this customer has any non-closed leases
|
|
$show_moveout = false;
|
|
$show_payment = false;
|
|
foreach ($customer['Lease'] AS $lease) {
|
|
if (!isset($lease['close_date']))
|
|
$show_payment = true;
|
|
if (!isset($lease['moveout_date']))
|
|
$show_moveout = true;
|
|
}
|
|
|
|
// Set up dynamic menu items
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Operations', 'header' => true);
|
|
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Edit',
|
|
'url' => array('action' => 'edit',
|
|
$id));
|
|
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Add Contact',
|
|
'url' => array('controller' => 'contacts',
|
|
'action' => 'add',
|
|
$id));
|
|
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Move-In',
|
|
'url' => array('action' => 'move_in',
|
|
$id));
|
|
|
|
if ($show_moveout) {
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Move-Out',
|
|
'url' => array('action' => 'move_out',
|
|
$id));
|
|
}
|
|
|
|
if ($show_payment) {
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Payment',
|
|
'url' => array('action' => 'payment',
|
|
$id));
|
|
}
|
|
|
|
// Prepare to render.
|
|
$title = $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)) {
|
|
|
|
if (isset($this->params['form']['cancel'])) {
|
|
if (isset($this->data['Customer']['id']))
|
|
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
|
else
|
|
$this->redirect(array('action'=>'index'));
|
|
return;
|
|
}
|
|
|
|
$this->data['Customer']['primary_contact_id']
|
|
= $this->data['Contact'][$this->data['Customer']['primary_contact_entry']]['id'];
|
|
|
|
$this->Customer->create();
|
|
if (!$this->Customer->save($this->data, false)) {
|
|
pr("CUSTOMER SAVE FAILED");
|
|
$this->Session->setFlash("CUSTOMER SAVE FAILED", true);
|
|
return;
|
|
}
|
|
$this->data['Customer']['id'] = $this->Customer->id;
|
|
|
|
// Remove all associated Customer Contacts, as it ensures
|
|
// any entries deleted by the user actually get deleted
|
|
// in the system. We'll recreate the needed ones anyway.
|
|
$this->Customer->ContactsCustomer->deleteAll
|
|
(array('customer_id' => $this->data['Customer']['id']), false);
|
|
|
|
if (!isset($this->data['Contact']))
|
|
$this->data['Contact'] = array();
|
|
|
|
// Go through each entry of this customer method
|
|
foreach ($this->data['Contact'] AS &$contact) {
|
|
|
|
// Check if the user has entered a brand new contact
|
|
if (isset($contact['source']) && $contact['source'] === 'new') {
|
|
unset($contact['id']);
|
|
$I = new Contact();
|
|
$I->create();
|
|
if (!$I->save($contact, false)) {
|
|
pr("CONTACT SAVE FAILED");
|
|
$this->Session->setFlash("CONTACT SAVE FAILED", true);
|
|
continue;
|
|
}
|
|
$contact['id'] = $I->id;
|
|
}
|
|
|
|
// Update the ContactsCustomer to reflect the appropriate IDs
|
|
$contact['ContactsCustomer']['customer_id'] = $this->data['Customer']['id'];
|
|
$contact['ContactsCustomer']['contact_id'] = $contact['id'];
|
|
|
|
// Save the relationship between customer and contact
|
|
$CM = new ContactsCustomer();
|
|
if (!$CM->save($contact['ContactsCustomer'], false)) {
|
|
die("ContactsCustomer Save FAILED!");
|
|
}
|
|
}
|
|
|
|
// Now that the work is done, let the user view the updated customer
|
|
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
|
//$this->autoRender = false;
|
|
return;
|
|
}
|
|
|
|
if ($id) {
|
|
$this->data = $this->Customer->details($id);
|
|
$title = $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() {
|
|
$this->edit();
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: payment
|
|
* - Sets up the payment entry page for the given customer.
|
|
*/
|
|
|
|
function payment($id = null) {
|
|
/* if (!$id) { */
|
|
/* $this->Session->setFlash(__('Invalid Item.', true)); */
|
|
/* $this->redirect(array('action'=>'index')); */
|
|
/* } */
|
|
|
|
/* if ($this->RequestHandler->isPost()) { */
|
|
/* pr($this->data); */
|
|
/* //$this->redirect(array('action'=>'index')); */
|
|
/* $customer = $this->data; */
|
|
/* } */
|
|
if (isset($id)) {
|
|
$this->Customer->recursive = -1;
|
|
$customer = $this->Customer->read(null, $id);
|
|
$customer = $customer['Customer'];
|
|
$unreconciled = $this->Customer->findUnreconciledLedgerEntries($id);
|
|
$charges = $unreconciled['debit'];
|
|
}
|
|
else {
|
|
$customer = null;
|
|
$charges = array('balance' => 0, 'entry' => array());
|
|
}
|
|
|
|
$title = 'Payment Entry';
|
|
$this->set(compact('customer', 'charges', 'title'));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: unreconciledEntries
|
|
* - returns the list of unreconciled entries
|
|
*/
|
|
|
|
function unreconciled($id) {
|
|
|
|
//$this->layout = 'ajax';
|
|
$this->layout = null;
|
|
$this->autoLayout = false;
|
|
$this->autoRender = false;
|
|
Configure::write('debug', '0');
|
|
header("Content-type: text/xml;charset=utf-8");
|
|
|
|
App::import('Helper', 'Xml');
|
|
$xml = new XmlHelper();
|
|
|
|
// Find the unreconciled entries, then manipulate the structure
|
|
// slightly to accomodate the format necessary for XML Helper.
|
|
$unreconciled = $this->Customer->findUnreconciledLedgerEntries($id);
|
|
$unreconciled = array('entries' =>
|
|
array_intersect_key($unreconciled['debit'],
|
|
array('entry'=>1, 'balance'=>1)));
|
|
|
|
// XML Helper will dump an empty tag if the array is empty
|
|
if (!count($unreconciled['entries']['entry']))
|
|
unset($unreconciled['entries']['entry']);
|
|
|
|
pr($unreconciled);
|
|
//$reconciled = $cust->reconcileNewLedgerEntry($cust_id, 'credit', $amount);
|
|
|
|
$opts = array();
|
|
//$opts['format'] = 'tags';
|
|
echo $xml->header();
|
|
echo $xml->serialize($unreconciled, $opts);
|
|
}
|
|
|
|
}
|