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

@@ -7,52 +7,194 @@ class Customer extends AppModel {
'name' => array('notempty'),
);
var $belongsTo = array(
'Account',
);
var $hasMany = array(
'Lease' => array(
'CurrentLease' => array(
'className' => 'Lease',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
'conditions' => 'CurrentLease.close_date IS NULL',
),
'Transaction' => array(
'className' => 'Transaction',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'Lease',
'Transaction',
);
var $hasAndBelongsToMany = array(
'Contact' => array(
'className' => 'Contact',
'joinTable' => 'contacts_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Contact',
);
/**************************************************************************
**************************************************************************
**************************************************************************
* 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;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function findSecurityDeposits($id, $link = null) {
/* pr(array_merge(array('function' => 'Customer::findSecurityDeposits', */
/* 'checkpoint' => 'begin'), */
/* compact('id', 'link'))); */
$this->Behaviors->attach('Containable');
$customer = $this->find('first',
array('contain' =>
array('Lease' => array('fields' => array('id'))),
'fields' => array('account_id'),
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
$entries = $this->Account->findLedgerEntriesRelatedToAccount
($customer['Customer']['account_id'],
$this->Account->securityDepositAccountID(),
true, null, $link);
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']);
}
/* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
/* 'vars' => compact('customer'), */
/* 'return' => compact('entries'))); */
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested customer.
*/
function stats($id = null) {
if (!$id)
return null;
$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('id'), */
/* /\* 'CurrentLedger' => array *\/ */
/* /\* ('fields => *\/ */
/* /\* 'Lease' => array('fields' => array('id') *\/ */
/* ), */
/* ), */
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
$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.
$this->statsMerge($stats, $stats['Account']['Ledger']);
$this->statsMerge($stats, $stats['Lease']['Account']['Ledger']);
return $stats;
}
}
?>