Another snapshot
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@356 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -16,66 +16,27 @@ class Lease extends AppModel {
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: accountId
|
||||
* - Returns the accountId of the given lease
|
||||
*/
|
||||
function accountId($id) {
|
||||
$A = new Account();
|
||||
return $A->invoiceAccountID();
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: findAccountEntries
|
||||
* - Returns an array of ledger entries from the account of the given
|
||||
* lease.
|
||||
*/
|
||||
function findAccountEntries($id, $all = false, $cond = null, $link = null) {
|
||||
/* pr(array('function' => 'Lease::findAccountEntries', */
|
||||
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
||||
/* )); */
|
||||
|
||||
if (!isset($cond))
|
||||
$cond = array();
|
||||
$cond[] = array('DoubleEntry.lease_id' => $id);
|
||||
|
||||
$A = new Account();
|
||||
$entries = $A->findLedgerEntries($this->accountId($id),
|
||||
$all, $cond, $link);
|
||||
|
||||
/* pr(array('function' => 'Lease::findAccountEntries', */
|
||||
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
||||
/* 'vars' => compact('lease'), */
|
||||
/* 'return' => compact('entries'), */
|
||||
/* )); */
|
||||
return $entries;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: findSecurityDeposits
|
||||
* function: securityDeposits
|
||||
* - Returns an array of security deposit entries
|
||||
*/
|
||||
function findSecurityDeposits($id, $link = null) {
|
||||
/* pr(array('function' => 'Lease::findSecurityDeposits', */
|
||||
/* 'args' => compact('id', 'link'), */
|
||||
/* )); */
|
||||
function securityDeposits($id, $query = null) {
|
||||
$this->queryInit($query);
|
||||
|
||||
$A = new Account();
|
||||
$entries = $A->findLedgerEntries
|
||||
($A->securityDepositAccountID(),
|
||||
true, array('DoubleEntry.lease_id' => $id), $link);
|
||||
$this->id = $id;
|
||||
$this->recursive = -1;
|
||||
$this->read();
|
||||
|
||||
/* pr(array('function' => 'Lease::findSecurityDeposits', */
|
||||
/* 'args' => compact('id', 'link'), */
|
||||
/* 'vars' => compact('lease'), */
|
||||
/* 'return' => compact('entries'), */
|
||||
/* )); */
|
||||
return $entries;
|
||||
if (!isset($query['link']['DoubleEntry']))
|
||||
$query['link']['DoubleEntry'] = array();
|
||||
if (!isset($query['link']['DoubleEntry']['Lease']))
|
||||
$query['link']['DoubleEntry']['Lease'] = array();
|
||||
if (!isset($query['link']['DoubleEntry']['Lease']['fields']))
|
||||
$query['link']['DoubleEntry']['Lease']['fields'] = array();
|
||||
|
||||
$query['conditions'][] = array('Lease.id' => $id);
|
||||
|
||||
return $this->Customer->securityDeposits
|
||||
($this->data['Lease']['customer_id'], $query);
|
||||
}
|
||||
|
||||
|
||||
@@ -91,38 +52,40 @@ class Lease extends AppModel {
|
||||
*/
|
||||
|
||||
function rentLastCharges($id) {
|
||||
$A = new Account();
|
||||
$rent_account_id = $this->DoubleEntry->Entry->Account->rentAccountID();
|
||||
$entries = $this->find
|
||||
('all',
|
||||
array('link' =>
|
||||
array(// Models
|
||||
'DoubleEntry' => array
|
||||
('Ledger' => array
|
||||
('fields' => array(),
|
||||
'Account' => array
|
||||
('fields' => array(),
|
||||
'Ledger' => array
|
||||
('alias' => 'Lx',
|
||||
'fields' => array(),
|
||||
'DoubleEntry' => array
|
||||
('alias' => 'LEx',
|
||||
'fields' => array(),
|
||||
'conditions' => array
|
||||
('LEx.effective_date = DATE_ADD(DoubleEntry.through_date, INTERVAL 1 day)',
|
||||
'LEx.lease_id = DoubleEntry.lease_id',
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
'DoubleEntry' =>
|
||||
array('Entry'// =>
|
||||
//array('fields'),
|
||||
),
|
||||
|
||||
'DEx' =>
|
||||
array('class' => 'DoubleEntry',
|
||||
'conditions' => array
|
||||
('DEx.effective_date = DATE_ADD(Entry.through_date, INTERVAL 1 day)',
|
||||
'DEx.lease_id = DoubleEntry.lease_id',
|
||||
),
|
||||
|
||||
'Ex' =>
|
||||
array('class' => 'Entry',
|
||||
'type' => 'INNER',
|
||||
'conditions' => array
|
||||
('Ex.account_id = Entry.account_id',
|
||||
'Ex.type = Entry.type',
|
||||
'Ex.crdr = Entry.crdr'),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
//'fields' => array('id', 'amount', 'effective_date', 'through_date'),
|
||||
'fields' => array(),
|
||||
'conditions' => array(array('Lease.id' => $id),
|
||||
array('Account.id' => $A->rentAccountID()),
|
||||
array('LEx.id' => null),
|
||||
array('Entry.type' => 'CHARGE'),
|
||||
array('Entry.account_id' => $rent_account_id),
|
||||
array('DEx.id' => null),
|
||||
),
|
||||
)
|
||||
);
|
||||
@@ -174,62 +137,76 @@ class Lease extends AppModel {
|
||||
*/
|
||||
|
||||
function rentPaidThrough($id) {
|
||||
$rent_account_id = $this->DoubleEntry->Entry->Account->rentAccountID();
|
||||
|
||||
// Income / Receipt / Money
|
||||
// debit: A/R credit: Income <-- this entry
|
||||
// debit: Receipt credit: A/R <-- ReceiptDoubleEntry, below
|
||||
// debit: Money credit: Receipt <-- MoneyDoubleEntry, below
|
||||
$rent = $this->DoubleEntry->Entry->reconciledSet
|
||||
('CHARGE',
|
||||
array('fields' =>
|
||||
array('DATE_SUB(DoubleEntry.effective_date, INTERVAL 1 DAY) AS paid_through',
|
||||
),
|
||||
'conditions' =>
|
||||
array(array('DoubleEntry.lease_id' => $id),
|
||||
array('Entry.account_id' => $rent_account_id)),
|
||||
|
||||
'order' => array('DoubleEntry.effective_date'),
|
||||
),
|
||||
true);
|
||||
|
||||
$query = array
|
||||
('link' => array
|
||||
(
|
||||
'CreditLedger' =>
|
||||
array('fields' => array(),
|
||||
'Account' =>
|
||||
array('fields' => array(),
|
||||
),
|
||||
),
|
||||
/* $query = array */
|
||||
/* ('link' => */
|
||||
/* array(// Models */
|
||||
/* 'DoubleEntry' => */
|
||||
/* array('Entry' => */
|
||||
/* array(), */
|
||||
/* 'fields' => array('SUM(COALESCE(MoneyDoubleEntryR.amount,0)) AS paid'), */
|
||||
/* ), */
|
||||
/* ), */
|
||||
|
||||
// We're searching for the Receipt<->A/R entries,
|
||||
// which are debits on the A/R account. Find the
|
||||
// reconciling entries to that A/R debit.
|
||||
'DebitReconciliationDoubleEntry' =>
|
||||
array('alias' => 'ReceiptDoubleEntry',
|
||||
'fields' => array(),
|
||||
/* // Finally, the Money (Cash/Check/etc) Entry is the one */
|
||||
/* // which reconciles our ReceiptDoubleEntry debit */
|
||||
/* 'DebitReconciliationDoubleEntry' => */
|
||||
/* array('alias' => 'MoneyDoubleEntry', */
|
||||
/* 'linkalias' => 'MoneyDoubleEntryR', */
|
||||
/* ), */
|
||||
/* ), */
|
||||
/* ), */
|
||||
|
||||
// Finally, the Money (Cash/Check/etc) Entry is the one
|
||||
// which reconciles our ReceiptDoubleEntry debit
|
||||
'DebitReconciliationDoubleEntry' =>
|
||||
array('alias' => 'MoneyDoubleEntry',
|
||||
'linkalias' => 'MoneyDoubleEntryR',
|
||||
'fields' => array('SUM(COALESCE(MoneyDoubleEntryR.amount,0)) AS paid'),
|
||||
),
|
||||
),
|
||||
),
|
||||
/* 'fields' => array('DoubleEntry.amount', */
|
||||
/* 'DATE_SUB(DoubleEntry.effective_date, INTERVAL 1 DAY) AS paid_through', */
|
||||
/* ), */
|
||||
|
||||
'fields' => array('DoubleEntry.amount',
|
||||
'DATE_SUB(DoubleEntry.effective_date, INTERVAL 1 DAY) AS paid_through',
|
||||
),
|
||||
/* 'group' => 'DoubleEntry.id HAVING paid <> DoubleEntry.amount', */
|
||||
|
||||
'group' => 'DoubleEntry.id HAVING paid <> DoubleEntry.amount',
|
||||
/* 'conditions' => array(array('DoubleEntry.lease_id' => $id), */
|
||||
/* array('Account.id' => $this->DoubleEntry->Ledger->Account->rentAccountID()), */
|
||||
/* ), */
|
||||
/* 'order' => array('DoubleEntry.effective_date', */
|
||||
/* ), */
|
||||
/* ); */
|
||||
|
||||
'conditions' => array(array('DoubleEntry.lease_id' => $id),
|
||||
array('Account.id' => $this->DoubleEntry->Ledger->Account->rentAccountID()),
|
||||
),
|
||||
'order' => array('DoubleEntry.effective_date',
|
||||
),
|
||||
);
|
||||
/* $rent = $this->DoubleEntry->find('first', $query); */
|
||||
//pr($rent);
|
||||
if ($rent['entries'])
|
||||
return $rent['entries'][0]['DoubleEntry']['paid_through'];
|
||||
|
||||
$rent = $this->DoubleEntry->find('first', $query);
|
||||
$rent = $this->DoubleEntry->Entry->reconciledSet
|
||||
('CHARGE',
|
||||
array('conditions' =>
|
||||
array(array('DoubleEntry.lease_id' => $id),
|
||||
array('Entry.account_id' => $rent_account_id)),
|
||||
|
||||
'order' => array('Entry.through_date DESC'),
|
||||
),
|
||||
false);
|
||||
if ($rent)
|
||||
return $rent[0]['paid_through'];
|
||||
return $rent[0]['Entry']['through_date'];
|
||||
|
||||
$query['fields'] = 'DoubleEntry.through_date';
|
||||
$query['order'] = 'DoubleEntry.through_date DESC';
|
||||
$query['group'] = 'DoubleEntry.id';
|
||||
$rent = $this->DoubleEntry->find('first', $query);
|
||||
if ($rent)
|
||||
return $rent['DoubleEntry']['through_date'];
|
||||
/* $query['fields'] = 'Entry.through_date'; */
|
||||
/* $query['order'] = 'Entry.through_date DESC'; */
|
||||
/* $query['group'] = 'DoubleEntry.id'; */
|
||||
/* $rent = $this->DoubleEntry->find('first', $query); */
|
||||
/* if ($rent) */
|
||||
/* return $rent['DoubleEntry']['through_date']; */
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -432,16 +409,60 @@ class Lease extends AppModel {
|
||||
* - Returns summary data from the requested lease.
|
||||
*/
|
||||
|
||||
function stats($id = null) {
|
||||
function stats($id = null, $query = null) {
|
||||
if (!$id)
|
||||
return null;
|
||||
|
||||
$A = new Account();
|
||||
$stats = $A->stats($A->accountReceivableAccountID(), true,
|
||||
array('DoubleEntry.lease_id' => $id));
|
||||
$this->queryInit($query);
|
||||
|
||||
//$query['link'] = array('Lease' => $query['link']);
|
||||
if (!isset($query['link']['DoubleEntry']))
|
||||
$query['link']['DoubleEntry'] = array();
|
||||
if (!isset($query['link']['Charge']))
|
||||
$query['link']['Charge'] = array();
|
||||
if (!isset($query['link']['Charge']['DoubleEntry']))
|
||||
$query['link']['Charge']['DoubleEntry'] = array();
|
||||
if (!isset($query['link']['Charge']['DoubleEntry']['Invoice']))
|
||||
$query['link']['Charge']['DoubleEntry']['Invoice'] = array();
|
||||
if (!isset($query['link']['Charge']['Account']))
|
||||
$query['link']['Charge']['Account'] = array();
|
||||
|
||||
/* $query['link']['DoubleEntry']['fields'] = array(); */
|
||||
/* $query['link']['Charge']['fields'] = array(); */
|
||||
/* $query['link']['Charge']['Account']['fields'] = array(); */
|
||||
/* $query['link']['Charge']['DoubleEntry']['fields'] = array(); */
|
||||
/* $query['link']['Charge']['DoubleEntry']['Invoice']['fields'] = array(); */
|
||||
|
||||
$query['link']['Charge']['Account']['alias'] = 'ChargeAccount';
|
||||
$query['link']['Charge']['DoubleEntry']['alias'] = 'ChargeDoubleEntry';
|
||||
|
||||
if (!isset($query['fields']))
|
||||
$query['fields'] = array();
|
||||
|
||||
$query['fields'] = array_merge($query['fields'],
|
||||
$this->DoubleEntry->Entry->chargePaymentFields(false));
|
||||
|
||||
$query['conditions'][] = array('OR' =>
|
||||
array(array(array('Entry.type' => 'CHARGE'),
|
||||
array('DoubleEntry.lease_id' => $id)),
|
||||
array(array('Entry.type' => 'PAYMENT'),
|
||||
array('ChargeDoubleEntry.lease_id' => $id)),
|
||||
),
|
||||
);
|
||||
|
||||
$query['group'] = null;
|
||||
|
||||
$stats = $this->DoubleEntry->Entry->find('all', $query);
|
||||
pr(compact('query', 'stats'));
|
||||
|
||||
// The fields are all tucked into the [0] index,
|
||||
// and the rest of the array is useless (empty).
|
||||
$stats = $stats[0];
|
||||
|
||||
// Make sure we have a non-null balance
|
||||
if (!isset($stats['balance']))
|
||||
$stats['balance'] = 0;
|
||||
|
||||
// Pull to the top level and return
|
||||
$stats = $stats['Ledger'];
|
||||
return $stats;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user