Significant changes to work with the new account/ledger structure. Removed much of the auto-generated model association code. Added helper functions into the models to perform model related work, such as model 'stats' (a bad name for a function to return a summary of pertinent financial information from a given model instance). There is a ton of cleanup to do, but first I want to get it all captured.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@81 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 02:41:23 +00:00
parent 3dcd83229b
commit ffd1b64580
41 changed files with 1441 additions and 916 deletions

View File

@@ -9,12 +9,13 @@ class AccountsController extends AppController {
(// Models
'LedgerEntry' =>
array('fields' =>
array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) AS debits',
'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) 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)
)* LedgerEntry.amount) AS balance",
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance",
'COUNT(LedgerEntry.id) AS entries'),
'conditions' =>
array('OR' =>
@@ -166,99 +167,51 @@ class AccountsController extends AppController {
$this->redirect(array('action'=>'index'));
}
/* $this->Account->bindModel(array('hasMany' => array */
/* ('LedgerEntry' => array */
/* ('className' => 'LedgerEntry', */
/* 'foreignKey' => false, */
/* 'finderQuery' => 'SELECT `LedgerEntry`.* */
/* FROM pmgr_entries AS `LedgerEntry` */
/* LEFT JOIN pmgr_transactions AS `Transaction` */
/* ON `Transaction`.id = `LedgerEntry`.transaction_id */
/* WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) */
/* OR LedgerEntry.credit_ledger_id = ({$__cakeID__$}) */
/* ORDER BY Transaction.stamp', */
/* )))); */
// Get details about the account and its ledgers (no ledger entries yet)
$this->Account->Behaviors->attach('Containable');
$account = $this->Account->find
('first',
array('contain' =>
array(// Models
'CurrentLedger' =>
array('fields' => array('id', 'sequence')),
/* $this->Account->Behaviors->attach('Containable'); */
/* $this->Account->Contain(array('LedgerEntry' => array */
/* ( */
/* // Models */
/* 'Transaction' => array */
/* ( */
/* // Models */
/* 'Customer', */
/* )), */
/* )); */
/* } */
/* $account = $this->Account->read(null, $id); */
'Ledger' =>
array('order' => array('Ledger.open_stamp' => 'DESC')),
),
'conditions' => array(array('Account.id' => $id)),
)
);
$this->Account->Behaviors->detach('Containable');
// Simply debug stuff... testing.
$cond = null;
//$cond = array('Transaction.stamp >' => '2009-05-16');
$this->Account->recursive = -1;
$account = $this->Account->read(null, $id);
// 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; */
$account['Ledger'] = $this->Account->find
('all', array
('link' => array
(// Models
'Ledger' => array
(// Models
'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'),
'conditions' =>
array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id',
'LedgerEntry.credit_ledger_id = Ledger.id'),
),
),
),
),
'fields' => array(),
'conditions' => array('Account.id' => $id),
'order' => 'Ledger.name',
'group' => 'Ledger.id',
'limit' => 4,
));
// Get all ledger entries of the CURRENT ledger
$account['CurrentLedger']['LedgerEntry']
= $this->Account->findCurrentLedgerEntries($id, $cond);
//pr(array('Account summary', $account));
$account['CurrentLedger'] = array_shift($this->Account->Ledger->find
('first', array
('link' => array
(// Models
'Account' => array('fields' => array()),
'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'),
'conditions' =>
array('OR' =>
array("LedgerEntry.debit_ledger_id = Ledger.id",
"LedgerEntry.credit_ledger_id = Ledger.id"),
),
),
),
'fields' => array(),
'conditions' => array(array('Ledger.account_id' => $id),
'NOT' => array('Ledger.closed')),
//'order' => 'Ledger.name',
'group' => 'Ledger.id',
//'limit' => 4,
)));
//pr($account);
// Summarize each ledger
foreach($account['Ledger'] AS &$ledger)
$ledger = array_merge($ledger,
$this->Account->Ledger->stats($ledger['id']));
$balance = $account['CurrentLedger']['balance'];
// Obtain the overall account balance
$account['Account'] =
array_merge($account['Account'],
array('stats' => $this->Account->stats($id)));
$balance = $account['Account']['stats']['Ledger']['balance'];
// Prepare to render
$title = 'Account: ' . $account['Account']['name'];
$this->set(compact('account', 'title', 'balance'));
}

View File

@@ -29,94 +29,11 @@ class ContactsController extends AppController {
**************************************************************************
**************************************************************************
* action: index
* - Lists all current tenants
* - Lists all contacts
*/
function index() {
$this->current();
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: past
* - Lists all tenants, past and present
*/
function tenants() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('ContactsLease.type !=' => 'ALTERNATE')
));
$title = 'All Tenants';
$this->set('title', $title); $this->set('heading', $title);
$this->set('contacts', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: current
* - Lists all current tenants
*/
function current() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('ContactsLease.type !=' => 'ALTERNATE',
'Lease.close_date IS NULL')
));
$title = 'Current Tenants';
$this->set('title', $title); $this->set('heading', $title);
$this->set('contacts', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: past
* - Lists all past tenants
*/
function past() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('ContactsLease.type !=' => 'ALTERNATE',
'Lease.close_date IS NOT NULL')
));
$title = 'Past Tenants';
$this->set('title', $title); $this->set('heading', $title);
$this->set('contacts', $this->paginate());
$this->render('index');
$this->all();
}
@@ -124,7 +41,7 @@ class ContactsController extends AppController {
**************************************************************************
**************************************************************************
* action: all
* - Lists all contacts, including non-tenants
* - Lists all contacts
*/
function all() {

View File

@@ -2,15 +2,15 @@
class CustomersController extends AppController {
var $paginate = array('limit' => 100,
'group' => 'Customer.id',
'order' => array('Customer.name' => 'ASC'));
'group' => 'Customer.id',
'order' => array('Customer.name' => 'ASC'));
var $sidemenu_links =
array(array('name' => 'Tenants', 'header' => true),
array('name' => 'Current', 'url' => array('controller' => 'customers', 'action' => 'current')),
array('name' => 'Past', 'url' => array('controller' => 'customers', 'action' => 'past')),
array('name' => 'All', 'url' => array('controller' => 'customers', 'action' => 'all')),
);
array('name' => 'Current', 'url' => array('controller' => 'customers', 'action' => 'current')),
array('name' => 'Past', 'url' => array('controller' => 'customers', 'action' => 'past')),
array('name' => 'All', 'url' => array('controller' => 'customers', 'action' => 'all')),
);
/**************************************************************************
**************************************************************************
@@ -46,14 +46,14 @@ class CustomersController extends AppController {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NULL')
));
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NULL')
));
$title = 'Current Tenants';
$this->set('title', $title); $this->set('heading', $title);
@@ -73,14 +73,14 @@ class CustomersController extends AppController {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NOT NULL')
));
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NOT NULL')
));
$title = 'Past Tenants';
$this->set('title', $title); $this->set('heading', $title);
@@ -120,58 +120,61 @@ class CustomersController extends AppController {
$this->Customer->Behaviors->attach('Containable');
$this->Customer->contain
(array(// Models
'Contact' =>
array(// Models
'ContactPhone',
'ContactEmail',
'ContactAddress',
),
'Transaction' =>
array('order' => array('stamp'),
// Models
'Entry' =>
array(// Models
'DebitAccount',
'CreditAccount'),
),
'Lease' =>
array('order' => 'movein_date',
'conditions' => array('Lease.lease_date IS NOT NULL'),
// Models
'Contact' =>
array(// Models
'ContactPhone',
'ContactEmail',
'ContactAddress',
),
'Account',
'Lease' =>
array(//'order' => 'movein_date',
//'conditions' => array('Lease.lease_date IS NOT NULL'),
// Models
//'Account',
'Unit' =>
array('order' => array('sort_order'),
'fields' => array('id', 'name'),
),
),
)
array('order' => array('sort_order'),
'fields' => array('id', 'name'),
),
),
)
);
$customer = $this->Customer->read(null, $id);
$this->Customer->Behaviors->detach('Containable');
foreach ($customer['Lease'] AS &$lease) {
$stats = $this->Customer->Lease->stats($lease['id']);
//pr($stats);
$lease['balance'] = $stats['Account']['Ledger']['balance'];
}
//pr($customer);
$outstanding_deposit = 0;
$outstanding_balance = 0;
foreach($customer['Transaction'] AS $transaction) {
foreach($transaction['Entry'] AS $entry) {
if ($entry['DebitAccount']['name'] === 'A/R')
$outstanding_balance += $entry['amount'];
if ($entry['CreditAccount']['name'] === 'A/R')
$outstanding_balance -= $entry['amount'];
$stats = $this->Customer->stats($id);
$deposits = $this->Customer->findSecurityDeposits($id);
if ($entry['DebitAccount']['name'] === 'Security Deposit')
$outstanding_deposit -= $entry['amount'];
if ($entry['CreditAccount']['name'] === 'Security Deposit')
$outstanding_deposit += $entry['amount'];
}
}
$outstanding_balance = $stats['balance'];
$outstanding_deposit = $deposits['summary']['balance'];
/* pr(array('Customer Stats', $stats)); */
/* pr(array('Security Deposits', $deposits)); */
$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; */
$title = $customer['Customer']['name'];
$this->set(compact('customer', 'title',
'outstanding_balance',
'outstanding_deposit'));
'outstanding_balance',
'outstanding_deposit'));
}
}

View File

@@ -25,8 +25,6 @@ class LedgersController extends AppController {
'group' => 'Ledger.id',
'order' => array('Ledger.name' => 'ASC'));
var $uses = array('Ledger', 'LedgerEntry');
var $sidemenu_links =
array(array('name' => 'Ledgers', 'header' => true),
array('name' => 'Current', 'url' => array('controller' => 'ledgers', 'action' => 'current')),
@@ -115,73 +113,37 @@ class LedgersController extends AppController {
$this->redirect(array('action'=>'index'));
}
/* $this->Ledger->bindModel(array('hasMany' => array */
/* ('LedgerEntry' => array */
/* ('className' => 'LedgerEntry', */
/* 'foreignKey' => false, */
/* 'finderQuery' => 'SELECT `LedgerEntry`.* */
/* FROM pmgr_entries AS `LedgerEntry` */
/* LEFT JOIN pmgr_transactions AS `Transaction` */
/* ON `Transaction`.id = `LedgerEntry`.transaction_id */
/* WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) */
/* OR LedgerEntry.credit_ledger_id = ({$__cakeID__$}) */
/* ORDER BY Transaction.stamp', */
/* )))); */
/* $this->Ledger->Behaviors->attach('Containable'); */
/* $this->Ledger->Contain(array('LedgerEntry' => array */
/* ( */
/* // Models */
/* 'Transaction' => array */
/* ( */
/* // Models */
/* 'Customer', */
/* )), */
/* )); */
/* } */
/* $ledger = $this->Ledger->read(null, $id); */
// Get details about the ledger itself (no entries yet)
$this->Ledger->Behaviors->attach('Containable');
$this->Ledger->Contain(array('Account'));
$ledger = $this->Ledger->read(null, $id);
//pr($ledger);
$ledger = $this->Ledger->find
('first',
array('contain' =>
array(// Models
'Account',
),
'conditions' => array(array('Ledger.id' => $id)),
)
);
$this->Ledger->Behaviors->detach('Containable');
if (in_array($ledger['Account']['type'], array('ASSET', 'EXPENSE')))
$ledger_type = 'debit';
else
$ledger_type = 'credit';
// Get all ledger entries of this ledger
$ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries
($id, $ledger['Account']['type']);
$ledger['LedgerEntry'] = $this->LedgerEntry->find
('all',
array('link' => array
(// Models
'Transaction' =>
array(// Models
'Customer',
)),
'fields' =>
array('id', 'name', 'comment',
"IF(LedgerEntry.debit_ledger_id = $id, LedgerEntry.amount, NULL) AS debit",
"IF(LedgerEntry.credit_ledger_id = $id, LedgerEntry.amount, NULL) AS credit",
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $id, 1, -1) * LedgerEntry.amount) AS balance"),
'conditions' => array('OR' =>
array("LedgerEntry.debit_ledger_id = $id",
"LedgerEntry.credit_ledger_id = $id")),
'order' => array('Transaction.stamp'),
//'limit' => 15,
//'limit' => 2,
));
//pr($ledger);
/* $ledger['LedgerEntry'] = $this->Ledger->LedgerEntry->findInLedgerContext */
/* ($id,
/* $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries */
/* ($id); */
//($id, $ledger['Account']['type']);
$balance = 0;
foreach($ledger['LedgerEntry'] AS &$entry) {
if (isset($entry[0]))
$entry = array_merge($entry[0], array_diff_key($entry, array(0)));
$balance += $entry['balance'];
}
// Summarize the entries, and obtain the ledger balance
$ledger['Ledger'] =
array_merge($ledger['Ledger'],
array('stats' => $this->Ledger->stats($id)));
$balance = $ledger['Ledger']['stats']['balance'];
// OK, set our view variables and render!
$title = 'Ledger: ' . $ledger['Ledger']['name'];
$this->set(compact('ledger', 'title', 'balance'));
}

View File

@@ -156,74 +156,27 @@ class UnitsController extends AppController {
$this->Unit->contain
(array(// Models
'UnitSize',
//'CurrentLease' =>
'Lease' =>
array(//'order' => 'movein_date',
//'conditions' => array('Lease.lease_date IS NOT NULL',
//),
// Models
//'Contact' => array('alias' => 'PrimaryContact'),
// 'PrimaryContact',
'Customer' => array('fields' => array('id', 'name'),
// Models
'Transaction' =>
array('order' => array('stamp'),
// Models
'LedgerEntry' => array('DebitLedger' => array('Account'),
'CreditLedger' => array('Account'),
),
),
),
/* array(//'order' => array('sort_order'), */
/* 'fields' => array('id', 'display_name'), */
/* ), */
)
'Lease' => array('Customer'),
'CurrentLease' => array('Customer')
)
);
$unit = $this->Unit->read(null, $id);
$this->Unit->Behaviors->detach('Containable');
//$unit = $this->Unit->read(null, $id);
//pr($unit);
pr($unit);
$this->Unit->Lease->Customer->Transaction->LedgerEntry->Behaviors->attach('Containable');
$entries = $this->Unit->Lease->Customer->Transaction->LedgerEntry->find
('all',
array
(
'contain' =>
array('Transaction' =>
array('Customer' =>
array('Lease' =>
array('Unit' =>
array('conditions' => array('Unit.id' => $id)),
),
),
),
),
//'conditions' => 'xyz',
'limit' => 2,
));
pr($entries);
$this->autoRender = false;
$outstanding_deposit = 0;
$outstanding_balance = 0;
foreach($unit['Lease'] AS $lease) {
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'];
}
}
foreach ($unit['Lease'] AS &$lease) {
$stats = $this->Unit->Lease->stats($lease['id']);
$lease['balance'] = $stats['Account']['Ledger']['balance'];
}
$stats = $this->Unit->stats($id);
$deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']);
//pr($stats);
$outstanding_balance = $stats['CurrentLease']['Account']['Ledger']['balance'];
$outstanding_deposit = $deposits['summary']['balance'];
$this->sidemenu_links[] =
array('name' => 'Operations', 'header' => true);
$this->sidemenu_links[] =