diff --git a/site/models/monetary_source.php b/site/models/monetary_source.php index 7221278..184dad2 100644 --- a/site/models/monetary_source.php +++ b/site/models/monetary_source.php @@ -23,6 +23,10 @@ class MonetarySource extends AppModel { * - Flags the ledger entry as having insufficient funds * - NOTE: nsf only works if given the monetary source id * to transaction e3, below + * - NOTE: In order to show that the rent formerly considered + * "collected" is now recognized in reverse, we must + * credit A/R with a negative amount in order to + * reconcile it against the Rent<->A/R ledger entry. * * FEE RENT A/R RECEIPT CHECK NSF BANK * ------- ------- ------- ------- ------- ------- ------- @@ -34,26 +38,6 @@ class MonetarySource extends AppModel { * | | | | | | | * | | | | |50 | 50| t3 e4 : R e5 : R e3 * | | | | | | | - * | | | | | 50| |50 t4 e5 : R e4 : R e6a/e6b - * | | 30| | | |30 | t5 e6a : : R e5 - * | | 20| | | |20 | t5 e6b : : R e5 - * |35 | 35| | | | | t5 e7 - - * - NOTE - * The above example is great, except it leave no way to resolve - * that the rent has not actually been collected. So, instead - * perhaps we need to run a negative credit to A/R, which we can - * use to reconcile against the Rent<->A/R double entry: - * - * | | | | | 50| |50 t4 e5 : R e4 : R e6a/e6b (?) - * | | |-30 | | -30| | t5 e6a : R e1a : R e5 (?) - * | | |-20 | | -20| | t5 e6b : R e1b : R e5 (?) - * |35 | 35| | | | | t5 e7 - - * OR perhaps even a negative entry all the way through to the - * bank, making the NSF appear not as a withdrawal, but as a - * negative deposit (i.e, and adjustment, just like it really is): - * * | | | | | |-50 -50| t4 e5 : R e4 (?) : R e6a/e6b * | | |-30 | | -30| | t5 e6a : R e1a : R e5 * | | |-20 | | -20| | t5 e6b : R e1b : R e5 @@ -85,8 +69,6 @@ class MonetarySource extends AppModel { 'Account' => /* e.g. CHECK Account */ array('fields' => array('id', 'name'), 'conditions' => -/* array(array('Account.payable' => 1), */ -/* array('Account.type' => 'ASSET')), */ array('Account.payable' => 1, 'Account.type' => 'ASSET'), ), @@ -136,10 +118,7 @@ class MonetarySource extends AppModel { ), ), -/* 'fields' => array('LedgerEntry.*'), */ - - 'conditions' => array(array('MonetarySource.id' => $id), - ), + 'conditions' => array(array('MonetarySource.id' => $id)), )); pr($source); @@ -147,11 +126,7 @@ class MonetarySource extends AppModel { $nsf_fee_account_id = $A->nsfChargeAccountID(); $ar_account_id = $A->accountReceivableAccountID(); - $total = 0; - $e6_debit_rec_ids = array(); - $e6_credit_rec_ids = array(); - - $e5_transaction_id = null; + $t4_id = null; foreach ($source['LedgerEntry'] AS $e3) { // We expect only a single e4 entry $e4 = $e3['CreditReconciliationLedgerEntry']; @@ -164,16 +139,15 @@ class MonetarySource extends AppModel { $e4 = $e4[0]; // e3 amount - $amount = $e3['amount']; - $amount *= -1; + $amount = -1 * $e3['amount']; // e4 account $bank_account_id = $e4['DebitLedger']['account_id']; // post new e5 $e5_ids = $A->postLedgerEntry - (array('transaction_id' => $e5_transaction_id), - null, + (array('transaction_id' => $t4_id), + array('monetary_source_id' => $e3['monetary_source_id']), array('debit_ledger_id' => $A->currentLedgerID($bank_account_id), 'credit_ledger_id' => $A->currentLedgerID($nsf_account_id), 'effective_date' => $stamp, @@ -191,52 +165,57 @@ class MonetarySource extends AppModel { if ($e5_ids['error']) return null; - + $t4_id = $e5_ids['transaction_id']; + pr(array('checkpoint' => 'Posted Ledger Entry e5', - compact('e5_ids'))); - - $e6_debit_rec_ids[] - = array('LedgerEntry' => array('id' => $e5_ids['id'], 'amount' => $amount)); + compact('e5_ids', 'amount'))); + $t5_id = null; foreach ($e3['DebitReconciliationLedgerEntry'] AS $e2) { foreach ($e2['DebitReconciliationLedgerEntry2'] AS $e1) { - $e6_credit_rec_ids[] - = array('LedgerEntry' => array('id' => $e1['id'], 'amount' => -1*$e1['Reconciliation']['amount'])); + $amount = -1*$e1['Reconciliation']['amount']; + + // post new e6 + $e6_ids = $A->postLedgerEntry + (array('transaction_id' => $t5_id), + null, + array('debit_ledger_id' => $A->currentLedgerID($nsf_account_id), + 'credit_ledger_id' => $A->currentLedgerID($ar_account_id), + 'effective_date' => $stamp, + 'amount' => $amount, + 'lease_id' => $e1['lease_id'], + 'customer_id' => $e1['customer_id'], + 'comment' => "NSF back to A/R from Monetary Source #{$id}", + ), + array('debit' => + array(array('LedgerEntry' => + array('id' => $e5_ids['id'], + 'amount' => $amount))), + + 'credit' => + array(array('LedgerEntry' => + array('id' => $e1['id'], + 'amount' => $amount))), + ) + ); + + if ($e6_ids['error']) + return null; + $t5_id = $e6_ids['transaction_id']; + + pr(array('checkpoint' => 'Posted Ledger Entry e6', + compact('e6_ids', 'amount'))); } } - - $total += $amount; } // Cheat for now - $lease_id = $source['LedgerEntry'][0]['lease_id']; $customer_id = $source['LedgerEntry'][0]['customer_id']; - - // post new e6 - $e6_ids = $A->postLedgerEntry - (null, - null, - array('debit_ledger_id' => $A->currentLedgerID($nsf_account_id), - 'credit_ledger_id' => $A->currentLedgerID($ar_account_id), - 'effective_date' => $stamp, - 'amount' => $total, - 'lease_id' => $lease_id, - 'customer_id' => $customer_id, - 'comment' => "NSF back to A/R from Monetary Source #{$id}", - ), - array('debit' => $e6_debit_rec_ids, - 'credit' => $e6_credit_rec_ids) - ); - - if ($e6_ids['error']) - return null; - - pr(array('checkpoint' => 'Posted Ledger Entry e6', - compact('e6_ids', 'e6_debit_rec_ids', 'e6_credit_rec_ids'))); + $lease_id = null; // post new e7 $e7_ids = $A->postLedgerEntry - (array('transaction_id' => $e6_ids['transaction_id']), + (array('transaction_id' => $t5_id), null, array('debit_ledger_id' => $A->currentLedgerID($ar_account_id), 'credit_ledger_id' => $A->currentLedgerID($nsf_fee_account_id),