diff --git a/controllers/leases_controller.php b/controllers/leases_controller.php index 036825b..f32c13a 100644 --- a/controllers/leases_controller.php +++ b/controllers/leases_controller.php @@ -441,6 +441,9 @@ class LeasesController extends AppController { ) ); + $lease['Lease']['paid_through'] = $this->Lease->rentPaidThrough($id); + + $this->set('charge_gaps', $this->Lease->rentChargeGaps($id)); $this->set('charge_through', $this->Lease->rentChargeThrough($id)); diff --git a/models/lease.php b/models/lease.php index 7fd9750..8d69a5c 100644 --- a/models/lease.php +++ b/models/lease.php @@ -223,30 +223,105 @@ class Lease extends AppModel { } -/* /\************************************************************************** */ -/* ************************************************************************** */ -/* ************************************************************************** */ -/* * function: rentPaidThrough */ -/* * - Determines the date that rent has been paid through */ -/* * Returns one of: */ -/* * null: There are gaps in the charges */ -/* * false: There are not yet any charges */ -/* * date: The date rent has been paid through */ -/* *\/ */ + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: rentPaidThrough + * - Determines the date of the first unpaid rent + */ -/* function rentPaidThrough($id) { */ -/* if ($this->rentChargeGaps($id)) */ -/* return null; */ + function rentPaidThrough($id) { -/* $A = new Account(); */ -/* $unrec_entries = $A->findUnreconciledLedgerEntries */ -/* ($A->rentAccountID(), 'INCOME', array('LedgerEntry.lease_id' => $id)); */ + // Income / Receipt / Money + // debit: A/R credit: Income <-- this entry + // debit: Receipt credit: A/R <-- ReceiptLedgerEntry, below + // debit: Money credit: Receipt <-- MoneyLedgerEntry, below -/* $unrec_ids */ -/* = array_map(create_function('$data', */ -/* 'return $data["id"];'), */ -/* $unrec_entries); */ -/* } */ + $query = array + ('link' => array + ( + 'CreditLedger' => + array('fields' => array(), + 'Account' => + array('fields' => array(), + ), + ), + + // 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. + 'DebitReconciliationLedgerEntry' => + array('alias' => 'ReceiptLedgerEntry', + 'fields' => array(), + + /* 'Transaction' => */ + /* array('alias' => 'ReceiptTransaction', */ + /* 'fields' => array(), */ + /* ), */ + + // Credit Ledger should be A/R; + // Debit Ledger should be Receipt + /* 'DebitLedger' => */ + /* array('alias' => 'ReceiptLedger', */ + /* 'fields' => array(), */ + /* 'Account' => array('alias' => 'ReceiptAccount' */ + /* 'fields' => array(), */ + /* ), */ + /* ), */ + + // Finally, the Money (Cash/Check/etc) Entry is the one + // which reconciles our ReceiptLedgerEntry debit + 'DebitReconciliationLedgerEntry' => + array('alias' => 'MoneyLedgerEntry', + 'linkalias' => 'MoneyLedgerEntryR', + 'fields' => array('SUM(COALESCE(MoneyLedgerEntryR.amount,0)) AS paid'), + //'fields' => array('MoneyLedgerEntryR.amount AS paid'), + + /* // Credit Ledger should be Receipt; */ + /* // Debit Ledger should be our Money Account */ + /* 'DebitLedger' => */ + /* array('alias' => 'MoneyLedger', */ + /* 'fields' => array(), */ + + /* 'Account' => */ + /* array('alias' => 'MoneyAccount', */ + /* 'fields' => array(), */ + /* ), */ + /* ), */ + ), + ), + ), + + //'fields' => array('LedgerEntry.*, MoneyLedgerEntryR.*'), + //'MAX(ReceiptTransaction.stamp) AS last_paid'; + + 'fields' => array('LedgerEntry.amount', + 'DATE_SUB(LedgerEntry.effective_date, INTERVAL 1 DAY) AS paid_through', + ), +// 'group' => 'LedgerEntry.id', + 'group' => 'LedgerEntry.id HAVING paid <> LedgerEntry.amount', + 'conditions' => array(array('LedgerEntry.lease_id' => $id), + array('Account.id' => $this->LedgerEntry->Ledger->Account->rentAccountID()), + //array('paid =' => 'LedgerEntry.amount'), + //array('NOT' => array(array('MoneyLedgerEntry.id' => null))), + ), + 'order' => array('LedgerEntry.effective_date', + ), + ); + + $rent = $this->LedgerEntry->find('first', $query); + if ($rent) + return $rent[0]['paid_through']; + + $query['fields'] = 'LedgerEntry.through_date'; + $query['order'] = 'LedgerEntry.through_date DESC'; + $query['group'] = 'LedgerEntry.id'; + $rent = $this->LedgerEntry->find('first', $query); + if ($rent) + return $rent['LedgerEntry']['through_date']; + + return null; + } /************************************************************************** diff --git a/views/elements/ledger_entries.ctp b/views/elements/ledger_entries.ctp index 070737c..2d7894c 100644 --- a/views/elements/ledger_entries.ctp +++ b/views/elements/ledger_entries.ctp @@ -45,6 +45,12 @@ $cols['Sub-Total'] = array('index' => 'subtotal-LedgerEntry.amount', 'form if (!isset($group_by_tx)) $group_by_tx = false; +// REVISIT : 20090715 +// If we really want to group by transaction, we need +// a transaction listing, not a ledger_entry listing. +// switch controllers... don't overload this one. +$group_by_tx = false; + if (isset($transaction_id) || isset($reconcile_id)) $grid->invalidFields('Transaction'); diff --git a/views/leases/view.ctp b/views/leases/view.ctp index d24b9e9..1ef3794 100644 --- a/views/leases/view.ctp +++ b/views/leases/view.ctp @@ -37,6 +37,7 @@ $rows = array(array('ID', $lease['id']), array('Closed', FormatHelper::date($lease['close_date'], true)), array('Deposit', FormatHelper::currency($lease['deposit'])), array('Rent', FormatHelper::currency($lease['rent'])), + array('Paid Through', FormatHelper::date($lease['paid_through'], true)), array('Comment', $lease['comment']));