From b741bb6b013d1e4d746d95e386eeb5bd8d504537 Mon Sep 17 00:00:00 2001 From: abijah Date: Thu, 13 Aug 2009 02:37:37 +0000 Subject: [PATCH] Added cached fields for charged_through and paid_through dates of a lease. Also, added the ability to dynamically determine whether a unit is late or not. In reality, we really need this to be part of the lease, not one of the status types for unit. The sitemap, however, is driven from unit information, so it's not clear whether we should move that information or not. git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@543 97e9348a-65ac-dc4b-aefc-98561f571b83 --- app_controller.php | 2 +- controllers/maps_controller.php | 51 ++++++++++++++++++++++-------- controllers/units_controller.php | 9 ++++++ models/customer.php | 23 ++++++++++++++ models/lease.php | 19 ++++++++++- models/transaction.php | 1 + models/unit.php | 54 ++++++++++++++++++++++++++++++++ views/elements/units.ctp | 2 +- 8 files changed, 145 insertions(+), 16 deletions(-) diff --git a/app_controller.php b/app_controller.php index 2f3cac2..1bf0548 100644 --- a/app_controller.php +++ b/app_controller.php @@ -653,7 +653,7 @@ class AppController extends Controller { foreach ($records AS &$record) { // Add the calculated fields (if any), to the model fields if (isset($record[0])) { - $record[$model_alias] += $record[0]; + $record[$model_alias] = $record[0] + $record[$model_alias]; unset($record[0]); } } diff --git a/controllers/maps_controller.php b/controllers/maps_controller.php index cda6434..bd971db 100644 --- a/controllers/maps_controller.php +++ b/controllers/maps_controller.php @@ -85,10 +85,32 @@ class MapsController extends AppController { 'units' => array()); // Find all of the map/unit information from this SiteArea - $this->Map->recursive = 2; - $this->Map->SiteArea->unbindModel(array('hasOne' => array('Map'))); - $map = $this->Map->read(null, $id); - //pr($map); + $map = $this->Map->find('first', array('contain' => false, + 'conditions' => array('id' => $id))); + + $units = $this->Map->Unit->find + ('all', + array('link' => + array('Map' => + array('fields' => array()), + + 'CurrentLease' => + array('fields' => array()), + + 'UnitSize' => + array('fields' => array('id', 'depth', 'width', + 'MapsUnit.pt_top', + 'MapsUnit.pt_left', + 'MapsUnit.transpose')), + ), + 'fields' => array_merge(array('id', 'name'), + $this->Map->Unit->statusFields()), + 'conditions' => array('Map.id' => $id), + )); + +/* pr(compact('map', 'units')); */ +/* $this->render('/fake'); */ +/* return; */ /***** * The preference would be to leave all things "screen" related @@ -113,7 +135,10 @@ class MapsController extends AppController { $info['depth'] = $bottom * $screen_adjustment_factor; // Go through each unit in the map, calculating the map location - foreach ($map['Unit'] AS $unit) { + foreach ($units AS $unit) { + // Add the calculated fields to the Unit + $unit['Unit'] = $unit[0] + $unit['Unit']; + $lft = $unit['MapsUnit']['pt_left'] + $boundary_adjustment; $top = $unit['MapsUnit']['pt_top'] + $boundary_adjustment; @@ -132,10 +157,9 @@ class MapsController extends AppController { $width *= $screen_adjustment_factor; $depth *= $screen_adjustment_factor; - //$info['units'][$unit['id']] = $info['units'][] = - array( 'id' => $unit['id'], - 'name' => $unit['name'], + array( 'id' => $unit['Unit']['id'], + 'name' => $unit['Unit']['name'], 'left' => $lft, 'right' => $lft + $width, 'top' => $top, @@ -143,11 +167,12 @@ class MapsController extends AppController { 'width' => $width, 'depth' => $depth, 'n-s' => $unit['MapsUnit']['transpose'] ? 0 : 1, - 'status' => $unit['status'] + 'status' => $unit['Unit']['status'] ); } - //pr($info); +/* pr($info); */ +/* $this->render('/fake'); */ return $info; } @@ -241,9 +266,9 @@ class MapsController extends AppController { $info['palate']['unit']['DIRTY']['bg'] = array('red' => 128, 'green' => 192, 'blue' => 192); $info['palate']['unit']['VACANT']['bg'] = array('red' => 0, 'green' => 255, 'blue' => 128); $info['palate']['unit']['OCCUPIED']['bg'] = array('red' => 0, 'green' => 128, 'blue' => 255); - $info['palate']['unit']['LATE']['bg'] = array('red' => 255, 'green' => 64, 'blue' => 64); - $info['palate']['unit']['LOCKED']['bg'] = array('red' => 255, 'green' => 128, 'blue' => 128); - $info['palate']['unit']['LIENED']['bg'] = array('red' => 255, 'green' => 192, 'blue' => 192); + $info['palate']['unit']['LATE']['bg'] = array('red' => 255, 'green' => 192, 'blue' => 192); + $info['palate']['unit']['LOCKED']['bg'] = array('red' => 255, 'green' => 64, 'blue' => 64); + $info['palate']['unit']['LIENED']['bg'] = array('red' => 255, 'green' => 128, 'blue' => 128); // Determine text color to go with each background foreach ($info['palate']['unit'] AS &$code) { diff --git a/controllers/units_controller.php b/controllers/units_controller.php index 05f26ef..e2c11eb 100644 --- a/controllers/units_controller.php +++ b/controllers/units_controller.php @@ -4,6 +4,7 @@ class UnitsController extends AppController { var $sidemenu_links = array(array('name' => 'Units', 'header' => true), + array('name' => 'Delinquent', 'url' => array('controller' => 'units', 'action' => 'delinquent')), array('name' => 'Occupied', 'url' => array('controller' => 'units', 'action' => 'occupied')), array('name' => 'Vacant', 'url' => array('controller' => 'units', 'action' => 'vacant')), array('name' => 'Unavailable', 'url' => array('controller' => 'units', 'action' => 'unavailable')), @@ -33,6 +34,7 @@ class UnitsController extends AppController { function unavailable() { $this->gridView('Unavailable Units'); } function vacant() { $this->gridView('Vacant Units'); } function occupied() { $this->gridView('Occupied Units'); } + function delinquent() { $this->gridView('Delinquent Units'); } function all() { $this->gridView('All Units', 'all'); } @@ -82,6 +84,7 @@ class UnitsController extends AppController { $fields = parent::gridDataFields($params, $model); return array_merge($fields, + $this->Unit->statusFields(), $this->Unit->Lease->StatementEntry->chargeDisbursementFields(true)); } @@ -97,6 +100,9 @@ class UnitsController extends AppController { elseif ($params['action'] === 'occupied') { $conditions[] = $this->Unit->conditionOccupied(); } + elseif ($params['action'] === 'delinquent') { + $conditions[] = $this->Unit->conditionDelinquent(); + } elseif ($params['action'] === 'unoccupied') { $conditions[] = array('NOT' => array($this->Unit->conditionOccupied())); } @@ -210,8 +216,11 @@ class UnitsController extends AppController { 'Lease' => array('Customer'), 'CurrentLease' => array('Customer') ), + 'fields' => array_merge(array('Unit.*'), + $this->Unit->statusFields()), 'conditions' => array('Unit.id' => $id), )); + $unit['Unit'] = $unit[0] + $unit['Unit']; // Get the balance on each lease. foreach ($unit['Lease'] AS &$lease) { diff --git a/models/customer.php b/models/customer.php index cd2272c..b3c6fa1 100644 --- a/models/customer.php +++ b/models/customer.php @@ -201,6 +201,29 @@ class Customer extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: update + * - Update any cached or calculated fields + */ + function update($id) { + + // REVISIT : 20090812 + // updateLeaseCount is handled directly when needed. + // Should we simplify by just doing it anyway? + //$this->updateLeaseCount($id); + + $current_leases = + $this->find('all', + array('link' => array('CurrentLease' => array('type' => 'INNER')), + 'conditions' => array('Customer.id' => $id))); + + foreach ($current_leases AS $lease) + $this->Lease->update($lease['CurrentLease']['id']); + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/lease.php b/models/lease.php index 87e263d..67ffec7 100644 --- a/models/lease.php +++ b/models/lease.php @@ -12,7 +12,7 @@ class Lease extends AppModel { 'StatementEntry', ); - //var $default_log_level = array('log' => 30, 'show' => 15); + //var $default_log_level = array('log' => 30, 'show' => 30); /************************************************************************** ************************************************************************** @@ -745,6 +745,23 @@ class Lease extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: update + * - Update any cached or calculated fields + */ + function update($id) { + $this->id = $id; + $this->saveField('charge_through_date', $this->rentChargeThrough($id)); + $this->saveField('paid_through_date', $this->rentPaidThrough($id)); + + $moveout = $this->field('moveout_date'); + if (empty($moveout)) + $this->Unit->update($this->field('unit_id')); + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/transaction.php b/models/transaction.php index 8813f99..d0ffc19 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -663,6 +663,7 @@ class Transaction extends AppModel { $ret['error'] = true; } + $this->Customer->update($transaction['customer_id']); return $this->prReturn($ret); } diff --git a/models/unit.php b/models/unit.php index bd42691..cbbb5ff 100644 --- a/models/unit.php +++ b/models/unit.php @@ -25,6 +25,7 @@ class Unit extends AppModel { var $hasMany = array( 'Lease', + 'MapsUnit', ); //var $default_log_level = array('log' => 30, 'show' => 15); @@ -87,6 +88,16 @@ class Unit extends AppModel { return $this->prReturn(true); } + function delinquent($enum) { + INTERNAL_ERROR("NOT YET FULLY IMPLEMENTED (Need check for LATE)"); + return $this->statusCheck($enum, 'OCCUPIED', true, null, false); + } + + function conditionDelinquent() { + return ('(Unit.status > ' . $this->statusValue('OCCUPIED') . + " OR " . $this->delinquentTest() . ")"); + } + function occupied($enum) { return $this->statusCheck($enum, 'OCCUPIED', false, null, false); } @@ -138,6 +149,13 @@ class Unit extends AppModel { ($val < $this->occupiedEnumValue())) { unset($enums[$enum]); } + // REVISIT : 20090812 + // LATE is the only temporal condition, and somehow + // feels like it should be treated differently than + // the rest. How can a unit be marked as LATE when + // it is not inherent, but a function of time? + if ($enum === 'LATE') + unset($enums[$enum]); } return $this->prReturn($enums); @@ -181,6 +199,42 @@ class Unit extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: statusField + * - Returns the status field SQL query + */ + function delinquentTest() { + return ("(`status` = 'OCCUPIED' AND " . +/* "(DATE_ADD(CurrentLease.paid_through_date, INTERVAL 10 DAY)" . */ +/* " < CurrentLease.charge_through_date)," . */ + " (DATE_ADD(CurrentLease.paid_through_date, INTERVAL 15 DAY)" . + " < NOW()))"); + } + + function statusFields() { + return array( + //"IF(`status` = 'OCCUPIED' AND " . $this->delinquentTest() . "," . + "IF(" . $this->delinquentTest() . "," . + " 'LATE', `status`) AS 'status'"); + } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: update + * - Update any cached or calculated fields + */ + function update($id) { + $unit = $this->find('first', + array('contain' => array('CurrentLease'), + 'conditions' => array('Unit.id' => $id), + )); + + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/views/elements/units.ctp b/views/elements/units.ctp index b5b5220..4dbb165 100644 --- a/views/elements/units.ctp +++ b/views/elements/units.ctp @@ -9,7 +9,7 @@ $cols['Unit'] = array('index' => 'Unit.name', 'formatter' => 'shortname' $cols['Size'] = array('index' => 'UnitSize.name', 'formatter' => 'shortname'); $cols['Rent'] = array('index' => 'Unit.rent', 'formatter' => 'currency'); $cols['Deposit'] = array('index' => 'Unit.deposit', 'formatter' => 'currency'); -$cols['Status'] = array('index' => 'Unit.status', 'formatter' => 'name'); // We have enough real estate +$cols['Status'] = array('index' => 'status', 'formatter' => 'name'); // We have enough real estate $cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency'); $cols['Comment'] = array('index' => 'Unit.comment', 'formatter' => 'comment');