diff --git a/controllers/accounts_controller.php b/controllers/accounts_controller.php index c1080ad..f317b0c 100644 --- a/controllers/accounts_controller.php +++ b/controllers/accounts_controller.php @@ -9,8 +9,10 @@ class AccountsController extends AppController { (// Models 'LedgerEntry' => array('fields' => - array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS debits', - 'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS credits', + array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, + LedgerEntry.amount, NULL)) AS debits', + 'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, + LedgerEntry.amount, NULL)) AS credits', "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1), IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, 1, -1) @@ -184,21 +186,9 @@ class AccountsController extends AppController { ); $this->Account->Behaviors->detach('Containable'); - // Simply debug stuff... testing. - $cond = null; - //$cond = array('Transaction.stamp >' => '2009-05-16'); - // Get all ledger entries of the CURRENT ledger - //$crap = $this->Account->findSecurityDeposits($id); - //$crap = $this->Account->findAccountRelatedEntries($id, 8); - //pr(array('crap', $crap)); -/* $this->autoRender = false; */ -/* return; */ - - // Get all ledger entries of the CURRENT ledger - $account['CurrentLedger']['LedgerEntry'] - = $this->Account->findCurrentLedgerEntries($id, $cond); - //pr(array('Account summary', $account)); + $entries = $this->Account->findLedgerEntries($id); + $account['CurrentLedger']['LedgerEntry'] = $entries['Entries']; // Summarize each ledger foreach($account['Ledger'] AS &$ledger) diff --git a/controllers/contacts_controller.php b/controllers/contacts_controller.php index 097b927..360429a 100644 --- a/controllers/contacts_controller.php +++ b/controllers/contacts_controller.php @@ -74,8 +74,9 @@ class ContactsController extends AppController { 'Customer') ); $contact = $this->Contact->read(null, $id); - //pr($contact); + $this->Contact->Behaviors->detach('Containable'); + // Prepare to render. $title = $contact['Contact']['display_name']; $this->set(compact('contact', 'title')); } diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index b4a47d1..f98d1d6 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -128,11 +128,7 @@ class CustomersController extends AppController { ), 'Account', 'Lease' => - array(//'order' => 'movein_date', - //'conditions' => array('Lease.lease_date IS NOT NULL'), - // Models - //'Account', - 'Unit' => + array('Unit' => array('order' => array('sort_order'), 'fields' => array('id', 'name'), ), @@ -142,36 +138,30 @@ class CustomersController extends AppController { $customer = $this->Customer->read(null, $id); $this->Customer->Behaviors->detach('Containable'); + // Add the lease balance to each lease. foreach ($customer['Lease'] AS &$lease) { $stats = $this->Customer->Lease->stats($lease['id']); - //pr($stats); $lease['balance'] = $stats['Account']['Ledger']['balance']; } - //pr($customer); - + // Figure out the outstanding balance of the current lease. $stats = $this->Customer->stats($id); - $deposits = $this->Customer->findSecurityDeposits($id); - $outstanding_balance = $stats['balance']; + + // Figure out the total security deposit for the current lease. + $deposits = $this->Customer->findSecurityDeposits($id); $outstanding_deposit = $deposits['summary']['balance']; -/* pr(array('Customer Stats', $stats)); */ -/* pr(array('Security Deposits', $deposits)); */ + // Add statistics into the customer account. + $customer['Account'] = array_merge($customer['Account'], + $stats['Account']['Ledger']); $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); $this->sidemenu_links[] = array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); - - $customer['Account'] = array_merge($customer['Account'], $stats['Account']['Ledger']); - //pr($stats); - //unset($stats['Lease'], $stats['Account']); -/* $customer['Account']['debits'] = 10; */ -/* $customer['Account']['credits'] = 20; */ -/* $customer['Account']['balance'] = 55; */ - + // Prepare to render. $title = $customer['Customer']['name']; $this->set(compact('customer', 'title', 'outstanding_balance', diff --git a/controllers/ledger_entries_controller.php b/controllers/ledger_entries_controller.php index 5a7c11a..a389a56 100644 --- a/controllers/ledger_entries_controller.php +++ b/controllers/ledger_entries_controller.php @@ -46,10 +46,12 @@ class LedgerEntriesController extends AppController { ) ); $entry = $this->LedgerEntry->read(null, $id); - pr($entry); + $this->LedgerEntry->Behaviors->detach('Containable'); $title = "Entry #{$entry['LedgerEntry']['id']} ({$entry['LedgerEntry']['name']})"; $this->set(compact('entry', 'title')); - //$this->autoRender = false; + + pr($entry); + $this->autoRender = false; } } diff --git a/controllers/ledgers_controller.php b/controllers/ledgers_controller.php index 644f2b4..965270f 100644 --- a/controllers/ledgers_controller.php +++ b/controllers/ledgers_controller.php @@ -8,12 +8,16 @@ class LedgersController extends AppController { 'Account', 'LedgerEntry' => array('fields' => - array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits', - 'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS credits', - "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), - IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), - IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)) * LedgerEntry.amount) AS balance", - 'COUNT(LedgerEntry.id) AS entries'), + array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, + LedgerEntry.amount, NULL)) AS debits', + 'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, + LedgerEntry.amount, NULL)) AS credits', + "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), + IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), + IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1) + ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) + ) AS balance", + 'COUNT(LedgerEntry.id) AS entries'), 'conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = Ledger.id', @@ -23,7 +27,7 @@ class LedgersController extends AppController { ), 'group' => 'Ledger.id', - 'order' => array('Ledger.name' => 'ASC')); + 'order' => array('Ledger.account_id', 'Ledger.sequence')); var $sidemenu_links = array(array('name' => 'Ledgers', 'header' => true), @@ -130,13 +134,6 @@ class LedgersController extends AppController { $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries ($id, $ledger['Account']['type']); -/* $ledger['LedgerEntry'] = $this->Ledger->LedgerEntry->findInLedgerContext */ -/* ($id, -/* $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries */ -/* ($id); */ - //($id, $ledger['Account']['type']); - - // Summarize the entries, and obtain the ledger balance $ledger['Ledger'] = array_merge($ledger['Ledger'], diff --git a/controllers/transactions_controller.php b/controllers/transactions_controller.php index a5d0da3..8641019 100644 --- a/controllers/transactions_controller.php +++ b/controllers/transactions_controller.php @@ -107,23 +107,15 @@ class TransactionsController extends AppController { ) ); $transaction = $this->Transaction->read(null, $id); - pr($transaction); + $this->Transaction->Behaviors->detach('Containable'); - $debit_amount = 0; - $credit_amount = 0; - foreach($transaction['LedgerEntry'] AS $entry) { - $debit_amount += $entry['amount']; - $credit_amount += $entry['amount']; - } - -/* $this->sidemenu_links[] = */ -/* array('name' => 'Operations', 'header' => true); */ -/* $this->sidemenu_links[] = */ -/* array('name' => 'Move-Out', 'url' => array('controller' => 'transactions', 'action' => 'move-out')); */ + // Figure out the transaction total + $total = 0; + foreach($transaction['LedgerEntry'] AS $entry) + $total += $entry['amount']; + // OK, prepare to render. $title = 'Transaction #' . $transaction['Transaction']['id']; - $this->set(compact('transaction', 'title', - 'debit_amount', - 'credit_amount')); + $this->set(compact('transaction', 'title', 'total')); } } diff --git a/controllers/units_controller.php b/controllers/units_controller.php index 805a3aa..59515db 100644 --- a/controllers/units_controller.php +++ b/controllers/units_controller.php @@ -163,18 +163,19 @@ class UnitsController extends AppController { $unit = $this->Unit->read(null, $id); $this->Unit->Behaviors->detach('Containable'); - pr($unit); - + // Get the balance on each lease. foreach ($unit['Lease'] AS &$lease) { $stats = $this->Unit->Lease->stats($lease['id']); $lease['balance'] = $stats['Account']['Ledger']['balance']; } + // Figure out the outstanding balance of the current lease. $stats = $this->Unit->stats($id); - $deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']); + $outstanding_balance = + $stats['CurrentLease']['Account']['Ledger']['balance']; - //pr($stats); - $outstanding_balance = $stats['CurrentLease']['Account']['Ledger']['balance']; + // Figure out the total security deposit for the current lease. + $deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']); $outstanding_deposit = $deposits['summary']['balance']; $this->sidemenu_links[] = @@ -182,6 +183,7 @@ class UnitsController extends AppController { $this->sidemenu_links[] = array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); + // Prepare to render. $title = 'Unit ' . $unit['Unit']['name']; $this->set(compact('unit', 'title', 'outstanding_balance', diff --git a/models/account.php b/models/account.php index 1487c3e..add07bf 100644 --- a/models/account.php +++ b/models/account.php @@ -11,7 +11,6 @@ class Account extends AppModel { var $hasOne = array( 'CurrentLedger' => array( 'className' => 'Ledger', - //'foreignKey' => 'account_id', 'conditions' => array('NOT' => array('CurrentLedger.closed')) ), ); @@ -21,19 +20,6 @@ class Account extends AppModel { ); var $cache; -/* var $instantiated; */ - - -/* /\************************************************************************** */ -/* ************************************************************************** */ -/* ************************************************************************** */ -/* * function: constructor */ -/* *\/ */ -/* function __constructor($id = null) { */ -/* parent::__contstructor(); */ -/* $this->id = $id; */ -/* $this->instantiated = true; */ -/* } */ /************************************************************************** @@ -45,7 +31,8 @@ class Account extends AppModel { function securityDepositAccountID() { $account = $this->find('first', array ('recursive' => -1, - 'conditions' => array(array('name' => 'Security Deposit')), + 'conditions' => array + (array('name' => 'Security Deposit')), )); return $account['Account']['id']; } @@ -60,7 +47,8 @@ class Account extends AppModel { function rentAccountID() { $account = $this->find('first', array ('recursive' => -1, - 'conditions' => array(array('name' => 'Rent')), + 'conditions' => array + (array('name' => 'Rent')), )); return $account['Account']['id']; } @@ -74,10 +62,6 @@ class Account extends AppModel { */ function type($id) { if (isset($this->cache[$id]['type'])) { -/* pr(array('function' => 'Account::type', */ -/* 'args' => compact('id'), */ -/* 'cache_hit' => true, */ -/* 'return' => $this->cache[$id]['type'])); */ return $this->cache[$id]['type']; } @@ -87,7 +71,9 @@ class Account extends AppModel { 'conditions' => array(array('Account.id' => $id)), )); + // Save the account type in our cache for future reference $this->cache[$id]['type'] = $account['Account']['type']; + /* pr(array('function' => 'Account::type', */ /* 'args' => compact('id'), */ /* 'return' => $this->cache[$id]['type'])); */ @@ -105,10 +91,6 @@ class Account extends AppModel { function ledgers($id, $all = false) { $cachekey = $all ? 'all' : 'current'; if (isset($this->cache[$id]['ledgers'][$cachekey])) { -/* pr(array('function' => 'Account::ledgers', */ -/* 'args' => compact('id', 'all'), */ -/* 'cache_hit' => true, */ -/* 'return' => $this->cache[$id]['ledgers'][$cachekey])); */ return $this->cache[$id]['ledgers'][$cachekey]; } @@ -135,6 +117,7 @@ class Account extends AppModel { $ledger_ids = array($account['CurrentLedger']['id']); } + // Save the ledgers in our cache for future reference $this->cache[$id]['ledgers'][$cachekey] = $ledger_ids; /* pr(array('function' => 'Account::ledgers', */ @@ -159,19 +142,22 @@ class Account extends AppModel { $entries = array(); foreach ($this->ledgers($id, $all) AS $ledger_id) { - $ledger_entries = $this->Ledger->findLedgerEntries($ledger_id, $this->type($id), $cond, $link); + $ledger_entries = $this->Ledger->findLedgerEntries + ($ledger_id, $this->type($id), $cond, $link); $entries = array_merge($entries, $ledger_entries); - //$entries = array_merge($entries, array_diff_key($ledger_entries, array('summary'=>1))); - //$this->statsMerge($entries['summary'], $ledger_entries['summary']); } + $stats = $this->stats($id, $all, $cond); + $entries = array('Entries' => $entries, + 'summary' => $stats['Ledger']); + /* pr(array('function' => 'Account::findLedgerEntries', */ /* 'args' => compact('id', 'all', 'cond', 'link'), */ +/* 'vars' => compact('stats'), */ /* 'return' => compact('entries'), */ /* )); */ - $stats = $this->stats($id, $all, $cond); - return array('Entries' => $entries, 'summary' => $stats['Ledger']); + return $entries; } @@ -187,9 +173,9 @@ class Account extends AppModel { /* pr(array('function' => 'Account::findLedgerEntriesRelatedToAccount', */ /* 'args' => compact('id', 'rel_ids', 'all', 'cond', 'link'), */ /* )); */ + if (!isset($cond)) $cond = array(); - if (!is_array($rel_ids)) $rel_ids = array($rel_ids); @@ -209,196 +195,6 @@ class Account extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findCurrentLedgerEntries - * - Returns an array of ledger entries that belong to the current - * ledger of the given account. There is extra work done... see the - * LedgerEntry model. - */ - function findCurrentLedgerEntries($id, $cond = null, $link = null) { - $this->Behaviors->attach('Containable'); - $account = $this->find('first', array - ('contain' => array('CurrentLedger'), - 'fields' => array('Account.type', 'CurrentLedger.id'), - 'conditions' => array(array('Account.id' => $id)), - )); - $this->Behaviors->detach('Containable'); - - $ledger_id = $account['CurrentLedger']['id']; - $account_type = $account['Account']['type']; - - return $this->Ledger->findLedgerEntries($ledger_id, $account_type, $cond, $link); - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findLedgerRelatedEntries - * - Returns an array of ledger entries from this account that are - * related to one of the given ledgers. - */ - function findLedgerRelatedEntries($id, $ledgers, $link = null) { - pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries', - 'checkpoint' => 'begin'), - compact('id', 'ledgers', 'link'))); - $this->Behaviors->attach('Containable'); - $account = $this->find('first', array - ('contain' => array - ('Ledger' => array('fields' => array('Ledger.id')), - ), - 'fields' => array('Account.type'), - 'conditions' => array(array('Account.id' => $id)), - )); - $this->Behaviors->detach('Containable'); - - $cond = array('OR' => - array(array('debit_ledger_id' => $ledgers), - array('credit_ledger_id' => $ledgers))); - - pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries', - 'checkpoint' => 'get-account-ledgers'), - compact('account', 'cond'))); - - $entries = array(); - foreach($account['Ledger'] AS $ledger) { - //pr(array('find', $ledger, $account, $cond, $link)); - $entries = array_merge - ($entries, - $this->Ledger->findLedgerEntries($ledger['id'], - $account['Account']['type'], - $cond, $link)); - } - - foreach($entries AS $entry) - $this->statsMerge($entries['summary'], $entry[0]); - - //$entries['summary'] - - pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries', - 'checkpoint' => 'return'), - compact('entries'))); - return $entries; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findAccountRelatedEntries - * - Returns an array of ledger entries from this account that are - * related to the given account. - */ - function findAccountRelatedEntries($id, $relid, $link = null) { - pr(array_merge(array('function' => 'Account::findAccountRelatedEntries', - 'checkpoint' => 'begin'), - compact('id', 'relid', 'link'))); - $this->Behaviors->attach('Containable'); - $related = $this->find('first', array - ('contain' => array - ('Ledger' => array('fields' => array('Ledger.id')), - ), - 'fields' => array(), - 'conditions' => array(array('Account.id' => $relid)), - )); - $this->Behaviors->detach('Containable'); - - $ledger_ids = array(); - foreach ($related['Ledger'] AS $ledger) - array_push($ledger_ids, $ledger['id']); - - pr(array_merge(array('function' => 'Account::findAccountRelatedEntries', - 'checkpoint' => 'get-related'), - compact('related', 'ledger_ids'))); - - $entries = $this->findLedgerRelatedEntries($id, $ledger_ids, $link); - pr(array_merge(array('function' => 'Account::findAccountRelatedEntries', - 'checkpoint' => 'return'), - compact('entries'))); - return $entries; - //return $this->findLedgerRelatedEntries($id, $ledger_ids, $link); - } - - -/* /\************************************************************************** */ -/* ************************************************************************** */ -/* ************************************************************************** */ -/* * function: findSecurityDeposits */ -/* * - Returns an array of security deposit entries */ -/* *\/ */ -/* function findSecurityDeposits($id, $link = null) { */ -/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */ -/* 'checkpoint' => 'begin'), */ -/* compact('id', 'link'))); */ -/* $sd_account = $this->find('first', array */ -/* ('recursive' => -1, */ -/* 'conditions' => array(array('name' => 'Security Deposit')), */ -/* )); */ -/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */ -/* 'checkpoint' => 'get-security-deposit-account'), */ -/* compact('sd_account'))); */ - -/* $deposits = $this->findAccountRelatedEntries($id, $sd_account['Account']['id'], $link); */ -/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */ -/* 'checkpoint' => 'return'), */ -/* compact('deposits'))); */ -/* return $deposits; */ -/* //return $this->findAccountRelatedEntries($id, $sd_account['Account']['id'], $link); */ -/* } */ - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findSecurityDeposits - * - Returns an array of security deposit entries - */ - function zzfindSecurityDeposits($id, $cond = array(), $link = null) { - $this->Behaviors->attach('Containable'); - $account = $this->find('first', array - ('contain' => array - ('Ledger' => array('fields' => array('Ledger.id')), - ), - 'fields' => array('Account.type'), - 'conditions' => array(array('Account.id' => $id)), - )); - - $sd_account = $this->find('first', array - ('contain' => array - ('Ledger' => array('fields' => array('Ledger.id')), - ), - 'conditions' => array(array('name' => 'Security Deposit')), - )); - $this->Behaviors->detach('Containable'); - - pr(array('sd_account', $sd_account)); - $sd_ledger_ids = array(); - foreach ($sd_account['Ledger'] AS $ledger) - array_push($sd_ledger_ids, $ledger['id']); - - pr(array('sd_ledger_ids', $sd_ledger_ids)); - array_push($cond, - array('OR' => - array(array('debit_ledger_id' => $sd_ledger_ids), - array('credit_ledger_id' => $sd_ledger_ids)))); - - $sd_entries = array(); - foreach($account['Ledger'] AS $ledger) { - pr(array('find', $ledger, $account, $cond, $link)); - $add = $this->Ledger->findLedgerEntries($ledger['id'], - $account['Account']['type'], - $cond, $link); - $sd_entries = array_merge($sd_entries, $add); - } - - pr(array('sd_entries', $sd_entries)); - - //return $this->Ledger->findLedgerEntries($ledger_id, $account_type, $cond, $link); - } - - /************************************************************************** ************************************************************************** ************************************************************************** @@ -411,24 +207,33 @@ class Account extends AppModel { return null; // All old, closed ledgers MUST balance to 0. - // However, the user may want the ENTIRE running totals. + // However, the user may want the ENTIRE running totals, + // (not just the balance), so we may have to query all + // ledgers, as dictated by the $all parameter. + $this->Behaviors->attach('Containable'); $account = $this->find('first', array('contain' => ($all - ? array('Ledger' => array('fields' => array('id'))) - : array('CurrentLedger' => array('fields' => array('id'))) + ? array('Ledger' => array + ('fields' => array('id'))) + : array('CurrentLedger' => array + ('fields' => array('id'))) ), - 'conditions' => array(array('Account.id' => $id)))); + 'conditions' => array + (array('Account.id' => $id)) + )); $this->Behaviors->detach('Containable'); - if ($all) - $ledgers = $account['Ledger']; - else - $ledgers = array($account['CurrentLedger']); - - foreach ($ledgers AS $ledger) { - $this->statsMerge($stats['Ledger'], $this->Ledger->stats($ledger['id'], $cond)); + $stats = array(); + if ($all) { + foreach ($account['Ledger'] AS $ledger) + $this->statsMerge($stats['Ledger'], + $this->Ledger->stats($ledger['id'], $cond)); + } + else { + $stats['Ledger'] = + $this->Ledger->stats($account['CurrentLedger']['id'], $cond); } return $stats; diff --git a/models/customer.php b/models/customer.php index 80c6cb6..af777a1 100644 --- a/models/customer.php +++ b/models/customer.php @@ -25,77 +25,6 @@ class Customer extends AppModel { ); - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findAccountEntries - * - Returns an array of ledger entries that belong to the current - * ledger of the account for the given customer. There is extra work - * done... see the LedgerEntry model. - */ -/* function findAccountEntries($id, $date = null, $link = null) { */ -/* $result = $this->find('first', array */ -/* ('recursive' => -1, */ -/* 'fields' => array('account_id'), */ -/* 'conditions' => array(array('id' => $id)), */ -/* )); */ - -/* return $this->Account->findCurrentLedgerEntries($result['account_id'], */ -/* $date, $link); */ -/* } */ - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findSecurityDeposits - * - Returns an array of security deposit entries - */ - function zfindSecurityDeposits($id, $link = null) { - pr(array('function' => 'Customer::findSecurityDeposits', - 'args' => compact('id', 'link'), - )); - - $this->Behaviors->attach('Containable'); - $customer = $this->find('first', - array('contain' => - array('Lease' => array - ('fields' => array('Lease.account_id')), - ), - 'fields' => array('Customer.account_id'), - 'conditions' => array(array('Customer.id' => $id)))); - $this->Behaviors->detach('Containable'); - - $account_ids = array($customer['Customer']['account_id']); - foreach ($customer['Lease'] AS $lease) - array_push($account_ids, $lease['account_id']); - - $acct = new Account(); - $entries = $acct->findLedgerEntriesRelatedToAccount - ($acct->securityDepositAccountID(), - $account_ids, - //$acct->rentAccountID(), - //6, - //array_merge(array(1), $account_ids), - true, null, $link); - - // OK, we cheated by finding the entries of the security deposit account, - // and not by finding the security deposit entries of the customer - // account(s). Therefore, we have to invert the credit/debit business. - $entries['summary']['debits'] = $entries['summary']['credit']; - $entries['summary']['credits'] = $entries['summary']['debit']; - unset($entries['summary']['credit']); - unset($entries['summary']['debit']); - - pr(array('function' => 'Customer::findSecurityDeposits', - 'args' => compact('id', 'link'), - 'vars' => compact('customer', 'account_ids'), - 'return' => compact('entries'), - )); - return $entries; - } - - /************************************************************************** ************************************************************************** ************************************************************************** @@ -103,9 +32,9 @@ class Customer extends AppModel { * - Returns an array of security deposit entries */ function findSecurityDeposits($id, $link = null) { -/* pr(array_merge(array('function' => 'Customer::findSecurityDeposits', */ -/* 'checkpoint' => 'begin'), */ -/* compact('id', 'link'))); */ +/* pr(array('function' => 'Customer::findSecurityDeposits', */ +/* 'args' => compact('id', 'link'), */ +/* )); */ $this->Behaviors->attach('Containable'); $customer = $this->find('first', @@ -122,16 +51,16 @@ class Customer extends AppModel { foreach ($customer['Lease'] AS $lease) { $ledger_entries = $this->Lease->findSecurityDeposits($lease['id'], $link); - //$this->statsMerge($ledger_entries['summary'], $entries['summary']); - //unset($entries['summary']); $this->statsMerge($entries['summary'], $ledger_entries['summary']); - $entries['Entries'] = array_merge($entries['Entries'], $ledger_entries['Entries']); + $entries['Entries'] = array_merge($entries['Entries'], + $ledger_entries['Entries']); } /* pr(array('function' => 'Customer::findSecurityDeposits', */ /* 'args' => compact('id', 'link'), */ /* 'vars' => compact('customer'), */ -/* 'return' => compact('entries'))); */ +/* 'return' => compact('entries'), */ +/* )); */ return $entries; } @@ -148,48 +77,28 @@ class Customer extends AppModel { if (!$id) return null; + // Get the basic information necessary $this->Behaviors->attach('Containable'); $customer = $this->find('first', array('contain' => - array('Account' => array('fields' => array('Account.id')), - 'Lease' => array('fields' => array('Lease.id')) + array('Account' => array + ('fields' => array('Account.id')), + + 'Lease' => array + ('fields' => array('Lease.id')) ), -/* array('Account' => array */ -/* ('fields' => array('id'), */ -/* /\* 'CurrentLedger' => array *\/ */ -/* /\* ('fields => *\/ */ -/* /\* 'Lease' => array('fields' => array('id') *\/ */ -/* ), */ -/* ), */ - 'conditions' => array(array('Customer.id' => $id)))); + 'conditions' => array + (array('Customer.id' => $id)))); $this->Behaviors->detach('Containable'); + // Get stats from the customer account, and each lease $stats['Account'] = $this->Account->stats($customer['Account']['id']); foreach ($customer['Lease'] AS $lease) { $this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id'])); } -/* foreach($lease['Customer']['Transaction'] AS $transaction) { */ -/* foreach($transaction['LedgerEntry'] AS $entry) { */ -/* if ($entry['DebitLedger']['Account']['name'] === 'A/R') */ -/* $outstanding_balance += $entry['amount']; */ -/* if ($entry['CreditLedger']['Account']['name'] === 'A/R') */ -/* $outstanding_balance -= $entry['amount']; */ - -/* if ($entry['DebitLedger']['Account']['name'] === 'Security Deposit') */ -/* $outstanding_deposit -= $entry['amount']; */ -/* if ($entry['CreditLedger']['Account']['name'] === 'Security Deposit') */ -/* $outstanding_deposit += $entry['amount']; */ -/* } */ -/* } */ -/* } */ -/* if ($entry['DebitLedger']['Account']['name'] === 'Security Deposit') */ -/* $outstanding_deposit -= $entry['amount']; */ -/* if ($entry['CreditLedger']['Account']['name'] === 'Security Deposit') */ -/* $outstanding_deposit += $entry['amount']; */ - // Merge the stats from both the customer specific account, as - // well as the lease. This will provide current customer standing. + // well as the leases. This will provide current customer standing. $this->statsMerge($stats, $stats['Account']['Ledger']); $this->statsMerge($stats, $stats['Lease']['Account']['Ledger']); diff --git a/models/lease.php b/models/lease.php index 6ce5cc8..b482b2a 100644 --- a/models/lease.php +++ b/models/lease.php @@ -77,10 +77,6 @@ class Lease extends AppModel { 'conditions' => array(array('id' => $id)), )); -/* $sd_account_id = $this->Account->securityDepositAccountID(); */ -/* $sd_ledger_ids = $this->Account->ledgers($sd_account_id); */ -/* $cond = conditionEntryAsCreditOrDebit($sd_ledger_ids); */ - $entries = $this->Account->findLedgerEntriesRelatedToAccount ($lease['Lease']['account_id'], $this->Account->securityDepositAccountID(), @@ -95,63 +91,6 @@ class Lease extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findSecurityDeposits - * - Returns an array of security deposit entries - */ - function findAccountDeposits($id, $link = null) { - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'begin'), - compact('id', 'link'))); - $lease = $this->find('first', array - ('recursive' => -1, - 'fields' => array('account_id'), - 'conditions' => array(array('id' => $id)), - )); - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'get-lease'), - compact('lease'))); - - $deposits = $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link); - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'return'), - compact('deposits'))); - return $deposits; - //return $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link); - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findSecurityDeposits - * - Returns an array of security deposit entries - */ - function qqfindSecurityDeposits($id, $link = null) { - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'begin'), - compact('id', 'link'))); - $lease = $this->find('first', array - ('recursive' => -1, - 'fields' => array('account_id'), - 'conditions' => array(array('id' => $id)), - )); - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'get-lease'), - compact('lease'))); - - return $this->Account->findAccountRelatedEntries($id, $relaccount, $link); - $deposits = $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link); - pr(array_merge(array('function' => 'Lease::findSecurityDeposits', - 'checkpoint' => 'return'), - compact('deposits'))); - return $deposits; - //return $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link); - } - - /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/ledger.php b/models/ledger.php index 02c509b..956d9c2 100644 --- a/models/ledger.php +++ b/models/ledger.php @@ -15,18 +15,21 @@ class Ledger extends AppModel { 'LedgerEntry' => array( 'className' => 'LedgerEntry', 'foreignKey' => false, + // conditions will be used when JOINing tables // (such as find with LinkableBehavior) 'conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = Ledger.id', 'LedgerEntry.credit_ledger_id = Ledger.id')), - // finderQuery will be used when tables are put - // together across several querys, not with JOIN. - // (such as find with ContainableBehavior) + + // finderQuery will be used when tables are put + // together across several querys, not with JOIN. + // (such as find with ContainableBehavior) 'finderQuery' => 'SELECT `LedgerEntry`.* FROM pmgr_ledger_entries AS `LedgerEntry` WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})', + 'counterQuery' => '' ), 'DebitLedgerEntry' => array( @@ -71,7 +74,7 @@ class Ledger extends AppModel { // If the requested entries are limited by date, we must calculate // a balance forward, or the resulting balance will be thrown off. - + // // REVISIT : This obviously is more general than date. // As such, it will not work (or, only work if the // condition only manages to exclude the first parts @@ -105,10 +108,6 @@ class Ledger extends AppModel { /* 'return' => compact('entries'), */ /* )); */ return $entries; - - // Return entries from the ledger, along with our balance forward. -/* return array_merge($bf, */ -/* $this->LedgerEntry->findInLedgerContext($id, $account_type, $cond, $link)); */ } @@ -133,14 +132,14 @@ class Ledger extends AppModel { ), 'fields' => array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, - LedgerEntry.amount, NULL)) AS debits", + LedgerEntry.amount, NULL)) AS debits", "SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, - LedgerEntry.amount, NULL)) AS credits", + LedgerEntry.amount, NULL)) AS credits", "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), - IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), - IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1) - ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) - ) AS balance", + IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), + IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1) + ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) + ) AS balance", "COUNT(LedgerEntry.id) AS entries"), 'conditions' => array(isset($cond) ? $cond : array(), array('Ledger.id' => $id)), diff --git a/models/ledger_entry.php b/models/ledger_entry.php index 254e411..970efa4 100644 --- a/models/ledger_entry.php +++ b/models/ledger_entry.php @@ -26,14 +26,16 @@ class LedgerEntry extends AppModel { /************************************************************************** ************************************************************************** ************************************************************************** - * function: - * - + * function: conditionEntryAsCreditOrDebit + * - returns the condition necessary to match a set of + * Ledgers to all related LedgerEntries */ function conditionEntryAsCreditOrDebit($ledger_ids) { return array('OR' => array(array('debit_ledger_id' => $ledger_ids), array('credit_ledger_id' => $ledger_ids))); } + /************************************************************************** ************************************************************************** @@ -78,18 +80,6 @@ class LedgerEntry extends AppModel { array('Transaction.stamp'), )); -/* $entries['summary'] = array('balance' => null, 'debit' => null, 'credit' => null); */ -/* foreach($entries AS $entry) */ -/* $this->statsMerge($entries['summary'], $entry[0]); */ - -/* //if (isset($entries['summary']['debit']) || isset($entries['summary']['credit'])) { */ -/* $entries['summary']['debits'] = $entries['summary']['debit']; */ -/* $entries['summary']['credits'] = $entries['summary']['credit']; */ -/* unset($entries['summary']['debit']); */ -/* unset($entries['summary']['credit']); */ -/* //} */ - - return $entries; } } diff --git a/models/unit.php b/models/unit.php index 17d9197..f7aa047 100644 --- a/models/unit.php +++ b/models/unit.php @@ -19,17 +19,12 @@ class Unit extends AppModel { var $hasOne = array( 'CurrentLease' => array( 'className' => 'Lease', - //'foreignKey' => 'unit_id', 'conditions' => 'CurrentLease.close_date IS NULL', ), ); var $hasMany = array( - 'Lease' => array( - 'className' => 'Lease', - 'foreignKey' => 'unit_id', - 'dependent' => false, - ) + 'Lease', ); function statusEnums() { @@ -74,20 +69,30 @@ class Unit extends AppModel { if (!$id) return null; + // Get the basic information necessary $this->Behaviors->attach('Containable'); $unit = $this->find('first', array('contain' => array - ('Lease' => array('fields' => array('Lease.id')), - 'CurrentLease' => array('fields' => array('CurrentLease.id'))), - 'conditions' => array(array('Unit.id' => $id)))); + ('Lease' => array + ('fields' => array('Lease.id')), + + 'CurrentLease' => array + ('fields' => array('CurrentLease.id'))), + + 'conditions' => array + (array('Unit.id' => $id)), + )); $this->Behaviors->detach('Containable'); + // Get the stats for the current lease $stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']); + // Sum the stats for all leases together foreach ($unit['Lease'] AS $lease) { $this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id'])); } + // Return the collection return $stats; } diff --git a/views/elements/ledgers.ctp b/views/elements/ledgers.ctp index 8013036..a9818b4 100644 --- a/views/elements/ledgers.ctp +++ b/views/elements/ledgers.ctp @@ -52,7 +52,7 @@ foreach ($ledgers as $ledger) { if (isset($ledger['Ledger'])) $ledger = $ledger['Ledger']; - $rows[] = array_merge(array($html->link('#'.$ledger['sequence'],//$ledger['name'], + $rows[] = array_merge(array($html->link('#'.$ledger['account_id'].'-'.$ledger['sequence'], array('controller' => 'ledgers', 'action' => 'view', $ledger['id']))), diff --git a/views/transactions/view.ctp b/views/transactions/view.ctp index 30c07b5..f15f03d 100644 --- a/views/transactions/view.ctp +++ b/views/transactions/view.ctp @@ -29,11 +29,8 @@ echo $this->element('table', ?>
-
- Debits: -
-
- Credits: +
+ Total:
@@ -53,13 +50,27 @@ echo $this->element('table', * Entries */ -$headers = array('Name', 'Debit', 'Credit', 'Comment', 'Amount', 'Total'); +$headers = array('Entry', 'Debit', 'Credit', 'Comment', 'Amount', 'Total'); +$column_class = array(); +foreach (array_intersect($headers, array('Entry')) AS $k => $v) { + $column_class[$k] = 'id'; +} +foreach (array_intersect($headers, array('Amount', 'Total')) AS $k => $v) { + $column_class[$k] = 'currency'; +} +foreach (array_intersect($headers, array('Comment')) AS $k => $v) { + $column_class[$k] = 'slack'; +} + $rows = array(); $running_total = 0; foreach($transaction['LedgerEntry'] AS $entry) { $amount = $entry['amount']; $running_total += $amount; - $rows[] = array($entry['name'], + $rows[] = array($html->link('#'.$entry['id'], + array('controller' => 'ledger_entries', + 'action' => 'view', + $entry['id'])), $html->link($entry['DebitLedger']['name'], array('controller' => 'ledgers', 'action' => 'view', @@ -79,7 +90,7 @@ echo $this->element('table', 'caption' => 'Entries in Transaction', 'headers' => $headers, 'rows' => $rows, - 'column_class' => $headers)); + 'column_class' => $column_class)); /* End "detail supporting" DIV */ ?>