Primarly a cleanup checkin, although a couple minor bugfixes were included as well.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@82 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 05:14:08 +00:00
parent 9f8d4fa9b2
commit 55b896a9f0
15 changed files with 151 additions and 519 deletions

View File

@@ -9,8 +9,10 @@ class AccountsController extends AppController {
(// Models (// Models
'LedgerEntry' => 'LedgerEntry' =>
array('fields' => array('fields' =>
array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS debits', array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id,
'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS credits', 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'), "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1), IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1),
IF(LedgerEntry.credit_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'); $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 // Get all ledger entries of the CURRENT ledger
//$crap = $this->Account->findSecurityDeposits($id); $entries = $this->Account->findLedgerEntries($id);
//$crap = $this->Account->findAccountRelatedEntries($id, 8); $account['CurrentLedger']['LedgerEntry'] = $entries['Entries'];
//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));
// Summarize each ledger // Summarize each ledger
foreach($account['Ledger'] AS &$ledger) foreach($account['Ledger'] AS &$ledger)

View File

@@ -74,8 +74,9 @@ class ContactsController extends AppController {
'Customer') 'Customer')
); );
$contact = $this->Contact->read(null, $id); $contact = $this->Contact->read(null, $id);
//pr($contact); $this->Contact->Behaviors->detach('Containable');
// Prepare to render.
$title = $contact['Contact']['display_name']; $title = $contact['Contact']['display_name'];
$this->set(compact('contact', 'title')); $this->set(compact('contact', 'title'));
} }

View File

@@ -128,11 +128,7 @@ class CustomersController extends AppController {
), ),
'Account', 'Account',
'Lease' => 'Lease' =>
array(//'order' => 'movein_date', array('Unit' =>
//'conditions' => array('Lease.lease_date IS NOT NULL'),
// Models
//'Account',
'Unit' =>
array('order' => array('sort_order'), array('order' => array('sort_order'),
'fields' => array('id', 'name'), 'fields' => array('id', 'name'),
), ),
@@ -142,36 +138,30 @@ class CustomersController extends AppController {
$customer = $this->Customer->read(null, $id); $customer = $this->Customer->read(null, $id);
$this->Customer->Behaviors->detach('Containable'); $this->Customer->Behaviors->detach('Containable');
// Add the lease balance to each lease.
foreach ($customer['Lease'] AS &$lease) { foreach ($customer['Lease'] AS &$lease) {
$stats = $this->Customer->Lease->stats($lease['id']); $stats = $this->Customer->Lease->stats($lease['id']);
//pr($stats);
$lease['balance'] = $stats['Account']['Ledger']['balance']; $lease['balance'] = $stats['Account']['Ledger']['balance'];
} }
//pr($customer); // Figure out the outstanding balance of the current lease.
$stats = $this->Customer->stats($id); $stats = $this->Customer->stats($id);
$deposits = $this->Customer->findSecurityDeposits($id);
$outstanding_balance = $stats['balance']; $outstanding_balance = $stats['balance'];
// Figure out the total security deposit for the current lease.
$deposits = $this->Customer->findSecurityDeposits($id);
$outstanding_deposit = $deposits['summary']['balance']; $outstanding_deposit = $deposits['summary']['balance'];
/* pr(array('Customer Stats', $stats)); */ // Add statistics into the customer account.
/* pr(array('Security Deposits', $deposits)); */ $customer['Account'] = array_merge($customer['Account'],
$stats['Account']['Ledger']);
$this->sidemenu_links[] = $this->sidemenu_links[] =
array('name' => 'Operations', 'header' => true); array('name' => 'Operations', 'header' => true);
$this->sidemenu_links[] = $this->sidemenu_links[] =
array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out'));
// Prepare to render.
$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; */
$title = $customer['Customer']['name']; $title = $customer['Customer']['name'];
$this->set(compact('customer', 'title', $this->set(compact('customer', 'title',
'outstanding_balance', 'outstanding_balance',

View File

@@ -46,10 +46,12 @@ class LedgerEntriesController extends AppController {
) )
); );
$entry = $this->LedgerEntry->read(null, $id); $entry = $this->LedgerEntry->read(null, $id);
pr($entry); $this->LedgerEntry->Behaviors->detach('Containable');
$title = "Entry #{$entry['LedgerEntry']['id']} ({$entry['LedgerEntry']['name']})"; $title = "Entry #{$entry['LedgerEntry']['id']} ({$entry['LedgerEntry']['name']})";
$this->set(compact('entry', 'title')); $this->set(compact('entry', 'title'));
//$this->autoRender = false;
pr($entry);
$this->autoRender = false;
} }
} }

View File

@@ -8,12 +8,16 @@ class LedgersController extends AppController {
'Account', 'Account',
'LedgerEntry' => 'LedgerEntry' =>
array('fields' => array('fields' =>
array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits', array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id,
'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS credits', LedgerEntry.amount, NULL)) AS debits',
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), 'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id,
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), LedgerEntry.amount, NULL)) AS credits',
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)) * LedgerEntry.amount) AS balance", "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
'COUNT(LedgerEntry.id) AS entries'), 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' => 'conditions' =>
array('OR' => array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id', array('LedgerEntry.debit_ledger_id = Ledger.id',
@@ -23,7 +27,7 @@ class LedgersController extends AppController {
), ),
'group' => 'Ledger.id', 'group' => 'Ledger.id',
'order' => array('Ledger.name' => 'ASC')); 'order' => array('Ledger.account_id', 'Ledger.sequence'));
var $sidemenu_links = var $sidemenu_links =
array(array('name' => 'Ledgers', 'header' => true), array(array('name' => 'Ledgers', 'header' => true),
@@ -130,13 +134,6 @@ class LedgersController extends AppController {
$ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries
($id, $ledger['Account']['type']); ($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 // Summarize the entries, and obtain the ledger balance
$ledger['Ledger'] = $ledger['Ledger'] =
array_merge($ledger['Ledger'], array_merge($ledger['Ledger'],

View File

@@ -107,23 +107,15 @@ class TransactionsController extends AppController {
) )
); );
$transaction = $this->Transaction->read(null, $id); $transaction = $this->Transaction->read(null, $id);
pr($transaction); $this->Transaction->Behaviors->detach('Containable');
$debit_amount = 0; // Figure out the transaction total
$credit_amount = 0; $total = 0;
foreach($transaction['LedgerEntry'] AS $entry) { foreach($transaction['LedgerEntry'] AS $entry)
$debit_amount += $entry['amount']; $total += $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')); */
// OK, prepare to render.
$title = 'Transaction #' . $transaction['Transaction']['id']; $title = 'Transaction #' . $transaction['Transaction']['id'];
$this->set(compact('transaction', 'title', $this->set(compact('transaction', 'title', 'total'));
'debit_amount',
'credit_amount'));
} }
} }

View File

@@ -163,18 +163,19 @@ class UnitsController extends AppController {
$unit = $this->Unit->read(null, $id); $unit = $this->Unit->read(null, $id);
$this->Unit->Behaviors->detach('Containable'); $this->Unit->Behaviors->detach('Containable');
pr($unit); // Get the balance on each lease.
foreach ($unit['Lease'] AS &$lease) { foreach ($unit['Lease'] AS &$lease) {
$stats = $this->Unit->Lease->stats($lease['id']); $stats = $this->Unit->Lease->stats($lease['id']);
$lease['balance'] = $stats['Account']['Ledger']['balance']; $lease['balance'] = $stats['Account']['Ledger']['balance'];
} }
// Figure out the outstanding balance of the current lease.
$stats = $this->Unit->stats($id); $stats = $this->Unit->stats($id);
$deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']); $outstanding_balance =
$stats['CurrentLease']['Account']['Ledger']['balance'];
//pr($stats); // Figure out the total security deposit for the current lease.
$outstanding_balance = $stats['CurrentLease']['Account']['Ledger']['balance']; $deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']);
$outstanding_deposit = $deposits['summary']['balance']; $outstanding_deposit = $deposits['summary']['balance'];
$this->sidemenu_links[] = $this->sidemenu_links[] =
@@ -182,6 +183,7 @@ class UnitsController extends AppController {
$this->sidemenu_links[] = $this->sidemenu_links[] =
array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out'));
// Prepare to render.
$title = 'Unit ' . $unit['Unit']['name']; $title = 'Unit ' . $unit['Unit']['name'];
$this->set(compact('unit', 'title', $this->set(compact('unit', 'title',
'outstanding_balance', 'outstanding_balance',

View File

@@ -11,7 +11,6 @@ class Account extends AppModel {
var $hasOne = array( var $hasOne = array(
'CurrentLedger' => array( 'CurrentLedger' => array(
'className' => 'Ledger', 'className' => 'Ledger',
//'foreignKey' => 'account_id',
'conditions' => array('NOT' => array('CurrentLedger.closed')) 'conditions' => array('NOT' => array('CurrentLedger.closed'))
), ),
); );
@@ -21,19 +20,6 @@ class Account extends AppModel {
); );
var $cache; 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() { function securityDepositAccountID() {
$account = $this->find('first', array $account = $this->find('first', array
('recursive' => -1, ('recursive' => -1,
'conditions' => array(array('name' => 'Security Deposit')), 'conditions' => array
(array('name' => 'Security Deposit')),
)); ));
return $account['Account']['id']; return $account['Account']['id'];
} }
@@ -60,7 +47,8 @@ class Account extends AppModel {
function rentAccountID() { function rentAccountID() {
$account = $this->find('first', array $account = $this->find('first', array
('recursive' => -1, ('recursive' => -1,
'conditions' => array(array('name' => 'Rent')), 'conditions' => array
(array('name' => 'Rent')),
)); ));
return $account['Account']['id']; return $account['Account']['id'];
} }
@@ -74,10 +62,6 @@ class Account extends AppModel {
*/ */
function type($id) { function type($id) {
if (isset($this->cache[$id]['type'])) { 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']; return $this->cache[$id]['type'];
} }
@@ -87,7 +71,9 @@ class Account extends AppModel {
'conditions' => array(array('Account.id' => $id)), 'conditions' => array(array('Account.id' => $id)),
)); ));
// Save the account type in our cache for future reference
$this->cache[$id]['type'] = $account['Account']['type']; $this->cache[$id]['type'] = $account['Account']['type'];
/* pr(array('function' => 'Account::type', */ /* pr(array('function' => 'Account::type', */
/* 'args' => compact('id'), */ /* 'args' => compact('id'), */
/* 'return' => $this->cache[$id]['type'])); */ /* 'return' => $this->cache[$id]['type'])); */
@@ -105,10 +91,6 @@ class Account extends AppModel {
function ledgers($id, $all = false) { function ledgers($id, $all = false) {
$cachekey = $all ? 'all' : 'current'; $cachekey = $all ? 'all' : 'current';
if (isset($this->cache[$id]['ledgers'][$cachekey])) { 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]; return $this->cache[$id]['ledgers'][$cachekey];
} }
@@ -135,6 +117,7 @@ class Account extends AppModel {
$ledger_ids = array($account['CurrentLedger']['id']); $ledger_ids = array($account['CurrentLedger']['id']);
} }
// Save the ledgers in our cache for future reference
$this->cache[$id]['ledgers'][$cachekey] = $ledger_ids; $this->cache[$id]['ledgers'][$cachekey] = $ledger_ids;
/* pr(array('function' => 'Account::ledgers', */ /* pr(array('function' => 'Account::ledgers', */
@@ -159,19 +142,22 @@ class Account extends AppModel {
$entries = array(); $entries = array();
foreach ($this->ledgers($id, $all) AS $ledger_id) { 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, $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', */ /* pr(array('function' => 'Account::findLedgerEntries', */
/* 'args' => compact('id', 'all', 'cond', 'link'), */ /* 'args' => compact('id', 'all', 'cond', 'link'), */
/* 'vars' => compact('stats'), */
/* 'return' => compact('entries'), */ /* 'return' => compact('entries'), */
/* )); */ /* )); */
$stats = $this->stats($id, $all, $cond); return $entries;
return array('Entries' => $entries, 'summary' => $stats['Ledger']);
} }
@@ -187,9 +173,9 @@ class Account extends AppModel {
/* pr(array('function' => 'Account::findLedgerEntriesRelatedToAccount', */ /* pr(array('function' => 'Account::findLedgerEntriesRelatedToAccount', */
/* 'args' => compact('id', 'rel_ids', 'all', 'cond', 'link'), */ /* 'args' => compact('id', 'rel_ids', 'all', 'cond', 'link'), */
/* )); */ /* )); */
if (!isset($cond)) if (!isset($cond))
$cond = array(); $cond = array();
if (!is_array($rel_ids)) if (!is_array($rel_ids))
$rel_ids = 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; return null;
// All old, closed ledgers MUST balance to 0. // 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'); $this->Behaviors->attach('Containable');
$account = $this->find('first', $account = $this->find('first',
array('contain' => array('contain' =>
($all ($all
? array('Ledger' => array('fields' => array('id'))) ? array('Ledger' => array
: array('CurrentLedger' => array('fields' => array('id'))) ('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'); $this->Behaviors->detach('Containable');
if ($all) $stats = array();
$ledgers = $account['Ledger']; if ($all) {
else foreach ($account['Ledger'] AS $ledger)
$ledgers = array($account['CurrentLedger']); $this->statsMerge($stats['Ledger'],
$this->Ledger->stats($ledger['id'], $cond));
foreach ($ledgers AS $ledger) { }
$this->statsMerge($stats['Ledger'], $this->Ledger->stats($ledger['id'], $cond)); else {
$stats['Ledger'] =
$this->Ledger->stats($account['CurrentLedger']['id'], $cond);
} }
return $stats; return $stats;

View File

@@ -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 * - Returns an array of security deposit entries
*/ */
function findSecurityDeposits($id, $link = null) { function findSecurityDeposits($id, $link = null) {
/* pr(array_merge(array('function' => 'Customer::findSecurityDeposits', */ /* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'checkpoint' => 'begin'), */ /* 'args' => compact('id', 'link'), */
/* compact('id', 'link'))); */ /* )); */
$this->Behaviors->attach('Containable'); $this->Behaviors->attach('Containable');
$customer = $this->find('first', $customer = $this->find('first',
@@ -122,16 +51,16 @@ class Customer extends AppModel {
foreach ($customer['Lease'] AS $lease) { foreach ($customer['Lease'] AS $lease) {
$ledger_entries = $this->Lease->findSecurityDeposits($lease['id'], $link); $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']); $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', */ /* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */ /* 'args' => compact('id', 'link'), */
/* 'vars' => compact('customer'), */ /* 'vars' => compact('customer'), */
/* 'return' => compact('entries'))); */ /* 'return' => compact('entries'), */
/* )); */
return $entries; return $entries;
} }
@@ -148,48 +77,28 @@ class Customer extends AppModel {
if (!$id) if (!$id)
return null; return null;
// Get the basic information necessary
$this->Behaviors->attach('Containable'); $this->Behaviors->attach('Containable');
$customer = $this->find('first', $customer = $this->find('first',
array('contain' => array('contain' =>
array('Account' => array('fields' => array('Account.id')), array('Account' => array
'Lease' => array('fields' => array('Lease.id')) ('fields' => array('Account.id')),
'Lease' => array
('fields' => array('Lease.id'))
), ),
/* array('Account' => array */ 'conditions' => array
/* ('fields' => array('id'), */ (array('Customer.id' => $id))));
/* /\* 'CurrentLedger' => array *\/ */
/* /\* ('fields => *\/ */
/* /\* 'Lease' => array('fields' => array('id') *\/ */
/* ), */
/* ), */
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable'); $this->Behaviors->detach('Containable');
// Get stats from the customer account, and each lease
$stats['Account'] = $this->Account->stats($customer['Account']['id']); $stats['Account'] = $this->Account->stats($customer['Account']['id']);
foreach ($customer['Lease'] AS $lease) { foreach ($customer['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id'])); $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 // 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['Account']['Ledger']);
$this->statsMerge($stats, $stats['Lease']['Account']['Ledger']); $this->statsMerge($stats, $stats['Lease']['Account']['Ledger']);

View File

@@ -77,10 +77,6 @@ class Lease extends AppModel {
'conditions' => array(array('id' => $id)), '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 $entries = $this->Account->findLedgerEntriesRelatedToAccount
($lease['Lease']['account_id'], ($lease['Lease']['account_id'],
$this->Account->securityDepositAccountID(), $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);
}
/************************************************************************** /**************************************************************************
************************************************************************** **************************************************************************
************************************************************************** **************************************************************************

View File

@@ -15,18 +15,21 @@ class Ledger extends AppModel {
'LedgerEntry' => array( 'LedgerEntry' => array(
'className' => 'LedgerEntry', 'className' => 'LedgerEntry',
'foreignKey' => false, 'foreignKey' => false,
// conditions will be used when JOINing tables // conditions will be used when JOINing tables
// (such as find with LinkableBehavior) // (such as find with LinkableBehavior)
'conditions' => array('OR' => 'conditions' => array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id', array('LedgerEntry.debit_ledger_id = Ledger.id',
'LedgerEntry.credit_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. // finderQuery will be used when tables are put
// (such as find with ContainableBehavior) // together across several querys, not with JOIN.
// (such as find with ContainableBehavior)
'finderQuery' => 'SELECT `LedgerEntry`.* 'finderQuery' => 'SELECT `LedgerEntry`.*
FROM pmgr_ledger_entries AS `LedgerEntry` FROM pmgr_ledger_entries AS `LedgerEntry`
WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$})
OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})', OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})',
'counterQuery' => '' 'counterQuery' => ''
), ),
'DebitLedgerEntry' => array( 'DebitLedgerEntry' => array(
@@ -71,7 +74,7 @@ class Ledger extends AppModel {
// If the requested entries are limited by date, we must calculate // If the requested entries are limited by date, we must calculate
// a balance forward, or the resulting balance will be thrown off. // a balance forward, or the resulting balance will be thrown off.
//
// REVISIT <AP>: This obviously is more general than date. // REVISIT <AP>: This obviously is more general than date.
// As such, it will not work (or, only work if the // As such, it will not work (or, only work if the
// condition only manages to exclude the first parts // condition only manages to exclude the first parts
@@ -105,10 +108,6 @@ class Ledger extends AppModel {
/* 'return' => compact('entries'), */ /* 'return' => compact('entries'), */
/* )); */ /* )); */
return $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' => 'fields' =>
array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, 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, "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'), "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1) IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance", ) AS balance",
"COUNT(LedgerEntry.id) AS entries"), "COUNT(LedgerEntry.id) AS entries"),
'conditions' => array(isset($cond) ? $cond : array(), 'conditions' => array(isset($cond) ? $cond : array(),
array('Ledger.id' => $id)), array('Ledger.id' => $id)),

View File

@@ -26,8 +26,9 @@ 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) { function conditionEntryAsCreditOrDebit($ledger_ids) {
return array('OR' => return array('OR' =>
@@ -35,6 +36,7 @@ class LedgerEntry extends AppModel {
array('credit_ledger_id' => $ledger_ids))); array('credit_ledger_id' => $ledger_ids)));
} }
/************************************************************************** /**************************************************************************
************************************************************************** **************************************************************************
************************************************************************** **************************************************************************
@@ -78,18 +80,6 @@ class LedgerEntry extends AppModel {
array('Transaction.stamp'), 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; return $entries;
} }
} }

View File

@@ -19,17 +19,12 @@ class Unit extends AppModel {
var $hasOne = array( var $hasOne = array(
'CurrentLease' => array( 'CurrentLease' => array(
'className' => 'Lease', 'className' => 'Lease',
//'foreignKey' => 'unit_id',
'conditions' => 'CurrentLease.close_date IS NULL', 'conditions' => 'CurrentLease.close_date IS NULL',
), ),
); );
var $hasMany = array( var $hasMany = array(
'Lease' => array( 'Lease',
'className' => 'Lease',
'foreignKey' => 'unit_id',
'dependent' => false,
)
); );
function statusEnums() { function statusEnums() {
@@ -74,20 +69,30 @@ class Unit extends AppModel {
if (!$id) if (!$id)
return null; return null;
// Get the basic information necessary
$this->Behaviors->attach('Containable'); $this->Behaviors->attach('Containable');
$unit = $this->find('first', $unit = $this->find('first',
array('contain' => array array('contain' => array
('Lease' => array('fields' => array('Lease.id')), ('Lease' => array
'CurrentLease' => array('fields' => array('CurrentLease.id'))), ('fields' => array('Lease.id')),
'conditions' => array(array('Unit.id' => $id))));
'CurrentLease' => array
('fields' => array('CurrentLease.id'))),
'conditions' => array
(array('Unit.id' => $id)),
));
$this->Behaviors->detach('Containable'); $this->Behaviors->detach('Containable');
// Get the stats for the current lease
$stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']); $stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']);
// Sum the stats for all leases together
foreach ($unit['Lease'] AS $lease) { foreach ($unit['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id'])); $this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
} }
// Return the collection
return $stats; return $stats;
} }

View File

@@ -52,7 +52,7 @@ foreach ($ledgers as $ledger) {
if (isset($ledger['Ledger'])) if (isset($ledger['Ledger']))
$ledger = $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', array('controller' => 'ledgers',
'action' => 'view', 'action' => 'view',
$ledger['id']))), $ledger['id']))),

View File

@@ -29,11 +29,8 @@ echo $this->element('table',
?> ?>
<DIV CLASS="infobox"> <DIV CLASS="infobox">
<DIV CLASS="summary grand debit"> <DIV CLASS="summary grand total">
Debits: <?php echo FormatHelper::currency($debitAmount); ?> Total: <?php echo FormatHelper::currency($total); ?>
</DIV>
<DIV CLASS="summary grand credit">
Credits: <?php echo FormatHelper::currency($creditAmount); ?>
</DIV> </DIV>
</DIV> </DIV>
@@ -53,13 +50,27 @@ echo $this->element('table',
* Entries * 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(); $rows = array();
$running_total = 0; $running_total = 0;
foreach($transaction['LedgerEntry'] AS $entry) { foreach($transaction['LedgerEntry'] AS $entry) {
$amount = $entry['amount']; $amount = $entry['amount'];
$running_total += $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'], $html->link($entry['DebitLedger']['name'],
array('controller' => 'ledgers', array('controller' => 'ledgers',
'action' => 'view', 'action' => 'view',
@@ -79,7 +90,7 @@ echo $this->element('table',
'caption' => 'Entries in Transaction', 'caption' => 'Entries in Transaction',
'headers' => $headers, 'headers' => $headers,
'rows' => $rows, 'rows' => $rows,
'column_class' => $headers)); 'column_class' => $column_class));
/* End "detail supporting" DIV */ ?> /* End "detail supporting" DIV */ ?>