git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@81 97e9348a-65ac-dc4b-aefc-98561f571b83
200 lines
7.3 KiB
PHP
200 lines
7.3 KiB
PHP
<?php
|
|
class Customer extends AppModel {
|
|
|
|
var $name = 'Customer';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'name' => array('notempty'),
|
|
);
|
|
|
|
var $belongsTo = array(
|
|
'Account',
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'CurrentLease' => array(
|
|
'className' => 'Lease',
|
|
'conditions' => 'CurrentLease.close_date IS NULL',
|
|
),
|
|
'Lease',
|
|
'Transaction',
|
|
);
|
|
|
|
var $hasAndBelongsToMany = array(
|
|
'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;
|
|
}
|
|
|
|
}
|
|
?>
|