Files
pmgr/site/models/monetary_source.php
abijah 1d8ef76efa Just updated the comments, and added e2a and e1 into the query. No functional change yet.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@330 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-07-14 21:44:19 +00:00

258 lines
7.9 KiB
PHP

<?php
class MonetarySource extends AppModel {
var $name = 'MonetarySource';
var $validate = array(
'id' => array('numeric'),
'name' => array('notempty'),
'tillable' => array('boolean')
);
var $belongsTo = array(
);
var $hasMany = array(
'LedgerEntry',
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function: nsf
* - Flags the ledger entry as having insufficient funds
* - NOTE: nsf only works if given the monetary source id
* to transaction t2b, below
*
* FEE RENT A/R RECEIPT CHECK NSF BANK
* ------- ------- ------- ------- ------- ------- -------
* | |50 50| | | | | e1
* | | | | | | |
* | | |50 50| | | | e2a
* | | | |50 50| | | e2b
* | | | | | | |
* | | | | |50 | 50| e3
* | | | | | | |
* | | | | | 50| |50 e4
* | | 50| | | |50 | e5a
* |35 | 35| | | | | e5b
* - 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 e4
* | | |-50 | | -50| | e5a
* |35 | 35| | | | | e5b
* 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| e4
* | | |-50 | | -50| | e5a
* |35 | 35| | | | | e5b
*
*/
function nsf($id, $stamp = null) {
pr(array('MonetarySource::nsf',
compact('id')));
$A = new Account();
// Get the LedgerEntries that use this monetary source
$source = $this->find
('first',
array('contain' =>
array(/* e2b */
'LedgerEntry' =>
array('Transaction.id',
'MonetarySource.id',
'Customer.id',
'Lease.id',
/* e2b debit */
'DebitLedger' => /* e.g. CHECK Ledger */
array('fields' => array(),
'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'),
),
),
/* e2b credit */
'CreditLedger' => /* i.e. RECEIPT Ledger */
array('fields' => array('id'),
'Account' => /* i.e. RECEIPT Account */
array('fields' => array('id', 'name'),
'conditions' =>
array('Account.id' => $A->receiptAccountID()),
),
),
/* e2a */
'DebitReconciliationLedgerEntry' =>
array(/* e2a credit */
'CreditLedger' => /* i.e. A/R Ledger */
array('fields' => array(),
'Account' => /* i.e. A/R Account */
array('fields' => array(),
'conditions' =>
array('Account.id' => $A->accountReceivableAccountID()),
),
),
/* e1 */
'DebitReconciliationLedgerEntry' =>
array(/* e1 credit */
'CreditLedger' => /* e.g. Rent Ledger */
array('fields' => array(),
'Account' => /* e.g. Rent Account */
array('fields' => array(),
'conditions' =>
array('Account.id' => $A->accountReceivableAccountID()),
),
),
),
),
/* e3 */
'CreditReconciliationLedgerEntry' =>
array(/* e3 debit */
'DebitLedger' => /* e.g. BANK Ledger */
array('fields' => array('id'),
'Account' => /* e.g. BANK Account */
array('fields' => array('id', 'name'),
'conditions' =>
array('Account.depositable' => 1),
),
),
),
),
),
/* 'fields' => array('LedgerEntry.*'), */
'conditions' => array(array('MonetarySource.id' => $id),
),
));
pr($source);
$nsf_account_id = $A->nsfAccountID();
$nsf_fee_account_id = $A->nsfChargeAccountID();
$ar_account_id = $A->accountReceivableAccountID();
$total = 0;
$nsf_rec_ids = array();
$transaction_id = null;
// FOR EACH e2b
foreach ($source['LedgerEntry'] AS $entry) {
// Count number of e3 entries
if (count($entry['CreditReconciliationLedgerEntry']) < 1)
continue;
if (count($entry['CreditReconciliationLedgerEntry']) > 1)
die('Too many CRLEs');
// e2b amount
$amount = $entry['amount'];
// e3 account
$bank_account_id = $entry['CreditReconciliationLedgerEntry'][0]['DebitLedger']['account_id'];
pr(array('checkpoint' => 'outerloop',
compact('id', $amount, 'bank_account_id', $entry)));
// post new e4
$ids = $A->postLedgerEntry
(array('transaction_id' => $transaction_id),
null,
array('debit_ledger_id' => $A->currentLedgerID($nsf_account_id),
'credit_ledger_id' => $A->currentLedgerID($bank_account_id),
'effective_date' => $stamp,
'amount' => $amount,
'lease_id' => $entry['lease_id'],
'customer_id' => $entry['customer_id'],
'comment' => "NSF of Monetary Source #{$id}",
),
array('credit' => array
(array('LedgerEntry' => array
('id' => $entry['CreditReconciliationLedgerEntry'][0]['id'],
'amount' => $amount,
))),
));
if ($ids['error'])
return null;
pr(array('checkpoint' => 'insert 1',
compact('ids')));
$total += $amount;
$nsf_rec_ids[]
= array('LedgerEntry' => array('id' => $ids['id'], 'amount' => $amount));
}
// Cheat for now
$lease_id = $source['LedgerEntry'][0]['lease_id'];
$customer_id = $source['LedgerEntry'][0]['customer_id'];
// post new e5a
$ids = $A->postLedgerEntry
(null,
null,
array('debit_ledger_id' => $A->currentLedgerID($ar_account_id),
'credit_ledger_id' => $A->currentLedgerID($nsf_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('credit' => $nsf_rec_ids)
);
if ($ids['error'])
return null;
pr(array('checkpoint' => 'insert 2',
compact('ids')));
$tid = $ids['transaction_id'];
// post new e5b
$ids = $A->postLedgerEntry
(array('transaction_id' => $tid),
null,
array('debit_ledger_id' => $A->currentLedgerID($ar_account_id),
'credit_ledger_id' => $A->currentLedgerID($nsf_fee_account_id),
'effective_date' => $stamp,
'amount' => 35,
'lease_id' => $lease_id,
'customer_id' => $customer_id,
'comment' => "NSF Fee for Monetary Source #{$id}",
)
);
if ($ids['error'])
return null;
pr(array('checkpoint' => 'insert 3',
compact('ids')));
return true;
}
}
?>