From 046cf5fe633cdea35f3f53327852f6d461dbf522 Mon Sep 17 00:00:00 2001 From: abijah Date: Tue, 25 Aug 2009 17:07:28 +0000 Subject: [PATCH] Added the ability to list unit sizes appropriate for a specific need. Since some of the code was taken from Account, I tidied up that model just a bit as well. git-svn-id: file:///svn-source/pmgr/branches/pre_0.1_work_20090819@784 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/unit_sizes_controller.php | 108 ++++++++++++++++++++- site/models/account.php | 22 +---- site/models/unit_type.php | 54 +++++++++-- 3 files changed, 156 insertions(+), 28 deletions(-) diff --git a/site/controllers/unit_sizes_controller.php b/site/controllers/unit_sizes_controller.php index 1047e16..3f7bbcb 100644 --- a/site/controllers/unit_sizes_controller.php +++ b/site/controllers/unit_sizes_controller.php @@ -3,6 +3,61 @@ class UnitSizesController extends AppController { + /************************************************************************** + ************************************************************************** + ************************************************************************** + * override: addGridViewSideMenuLinks + * - Adds grid view navigation side menu links + */ + + function addGridViewSideMenuLinks() { + parent::addGridViewSideMenuLinks(); + + $this->addSideMenuLink('1 Bedroom', + array('controller' => 'unit_sizes', 'action' => 'bd1'), null, + 'CONTROLLER'); + $this->addSideMenuLink('2 Bedroom', + array('controller' => 'unit_sizes', 'action' => 'bd2'), null, + 'CONTROLLER'); + $this->addSideMenuLink('3 Bedroom', + array('controller' => 'unit_sizes', 'action' => 'bd3'), null, + 'CONTROLLER'); + $this->addSideMenuLink('4+ Bedroom', + array('controller' => 'unit_sizes', 'action' => 'bd4'), null, + 'CONTROLLER'); + $this->addSideMenuLink('Auto', + array('controller' => 'unit_sizes', 'action' => 'auto'), null, + 'CONTROLLER'); + $this->addSideMenuLink('Boat', + array('controller' => 'unit_sizes', 'action' => 'boat'), null, + 'CONTROLLER'); + $this->addSideMenuLink('RV', + array('controller' => 'unit_sizes', 'action' => 'rv'), null, + 'CONTROLLER'); + $this->addSideMenuLink('All', + array('controller' => 'unit_sizes', 'action' => 'all'), null, + 'CONTROLLER'); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: index / unavailable / vacant / occupied / all + * - Generate a listing of units + */ + + function index() { $this->all(); } + function bd1() { $this->gridView('Sizes for 1 Bedroom'); } + function bd2() { $this->gridView('Sizes for 2 Bedrooms'); } + function bd3() { $this->gridView('Sizes for 3 Bedroom'); } + function bd4() { $this->gridView('Sizes for 4+ Bedroom'); } + function auto() { $this->gridView('Sizes for an Automobile'); } + function boat() { $this->gridView('Sizes for a Boat'); } + function rv() { $this->gridView('Sizes for an RV'); } + function all() { $this->gridView('All Unit Sizes', 'all'); } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -13,7 +68,7 @@ class UnitSizesController extends AppController { */ function gridDataTables(&$params, &$model) { - return array(); + return array('link' => array('UnitType')); } function gridDataFields(&$params, &$model) { @@ -28,6 +83,57 @@ class UnitSizesController extends AppController { return $fields; } + function gridDataConditions(&$params, &$model) { + $conditions = parent::gridDataConditions($params, $model); + + // REVISIT : 20090825 + // Sizes should come from the database. + // For now, I took an assumed average need, then bracketed + // with +/- 50 sqft. This gives a 100sqft range for each. + if ($params['action'] === 'bd1') { // 75 sqft + $conditions[] = array('UnitType.id' => array_keys($this->UnitSize->UnitType->enclosedTypes())); + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) <= 125'; + } + elseif ($params['action'] === 'bd2') { // 125 sqft + $conditions[] = array('UnitType.id' => array_keys($this->UnitSize->UnitType->enclosedTypes())); + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) >= 75'; + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) <= 175'; + } + elseif ($params['action'] === 'bd3') { // 175 sqft + $conditions[] = array('UnitType.id' => array_keys($this->UnitSize->UnitType->enclosedTypes())); + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) >= 125'; + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) <= 225'; + } + elseif ($params['action'] === 'bd4') { // 225 sqft + $conditions[] = array('UnitType.id' => array_keys($this->UnitSize->UnitType->enclosedTypes())); + $conditions[] = '(UnitSize.width/12 * UnitSize.depth/12) >= 175'; + } + elseif (in_array($params['action'], array('auto', 'boat', 'rv'))) { + $conditions[] = array('UnitType.id' => + array_merge(array_keys($this->UnitSize->UnitType->enclosedTypes()), + array_keys($this->UnitSize->UnitType->outdoorTypes()))); + list($width, $depth, $height) = array(8, 15, null); + if ($params['action'] === 'auto') + $depth = 15; + elseif ($params['action'] === 'boat') + $depth = 15; + elseif ($params['action'] === 'rv') + list($width, $depth, $height) = array(10, 25, 12); + + $conditions[] = "(UnitSize.width/12) >= $width"; + $conditions[] = "(UnitSize.depth/12) >= $depth"; + if (isset($height)) + $conditions[] = array('OR' => + array("(UnitSize.height/12) >= $height", + //"UnitSize.height IS NULL", + array('UnitType.id' => + array_keys($this->UnitSize->UnitType->outdoorTypes())), + )); + } + + return $conditions; + } + function gridDataPostProcessLinks(&$params, &$model, &$records, $links) { $links['UnitSize'] = array('name'); return parent::gridDataPostProcessLinks($params, $model, $records, $links); diff --git a/site/models/account.php b/site/models/account.php index 36a9d2b..1033fbd 100644 --- a/site/models/account.php +++ b/site/models/account.php @@ -188,8 +188,7 @@ class Account extends AppModel { function relatedAccounts($attribute, $extra = null) { $this->cacheQueries = true; $accounts = $this->find('all', array - ('contain' => array('CurrentLedger'), - 'fields' => array('Account.id', 'Account.type', 'Account.name', 'CurrentLedger.id'), + ('fields' => array('Account.id', 'Account.name'), 'conditions' => array('Account.'.$attribute => true), 'order' => array('Account.name'), ) + (isset($extra) ? $extra : array()) @@ -213,21 +212,10 @@ class Account extends AppModel { * - Returns an array of accounts suitable for activity xxx */ - function invoiceAccounts() { - return $this->relatedAccounts('invoices', array('order' => 'name')); - } - - function receiptAccounts() { - return $this->relatedAccounts('receipts', array('order' => 'name')); - } - - function depositAccounts() { - return $this->relatedAccounts('deposits', array('order' => 'name')); - } - - function refundAccounts() { - return $this->relatedAccounts('refunds', array('order' => 'name')); - } + function invoiceAccounts() { return $this->relatedAccounts('invoices'); } + function receiptAccounts() { return $this->relatedAccounts('receipts'); } + function depositAccounts() { return $this->relatedAccounts('deposits'); } + function refundAccounts() { return $this->relatedAccounts('refunds'); } /************************************************************************** diff --git a/site/models/unit_type.php b/site/models/unit_type.php index d2bd90d..50d27b3 100644 --- a/site/models/unit_type.php +++ b/site/models/unit_type.php @@ -1,16 +1,50 @@ array('numeric'), - 'code' => array('notempty'), - 'name' => array('notempty') - ); + var $hasMany = + array( + 'UnitSize', + ); - var $hasMany = array( - 'UnitSize', - ); + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: relatedTypes + * - Returns an array of types related by similar attributes + */ + + function relatedTypes($attribute, $extra = null) { + $this->cacheQueries = true; + $types = $this->find('all', array + ('fields' => array('UnitType.id', 'UnitType.name'), + 'conditions' => array('UnitType.'.$attribute => true), + 'order' => array('UnitType.name'), + ) + (isset($extra) ? $extra : array()) + ); + $this->cacheQueries = false; + + // Rearrange to be of the form (id => name) + $rel_types = array(); + foreach ($types AS $type) { + $rel_types[$type['UnitType']['id']] = $type['UnitType']['name']; + } + + return $rel_types; + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: xxxTypes + * - Returns an array of types suitable for activity xxx + */ + + function residentialTypes() { return $this->relatedTypes('residential'); } + function enclosedTypes() { return $this->relatedTypes('enclosed'); } + function climateTypes() { return $this->relatedTypes('climate'); } + function outdoorTypes() { return $this->relatedTypes('outdoor'); } + function coveredTypes() { return $this->relatedTypes('covered'); } } -?> \ No newline at end of file