Definitely not yet what we need for reversing charges, but at least the recursive nature seems to be in the right direction.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@326 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-13 04:41:18 +00:00
parent 059dbd3190
commit 865d9ee617

View File

@@ -241,7 +241,10 @@ class LedgerEntry extends AppModel {
* - Reverses the ledger entry * - Reverses the ledger entry
*/ */
function reverse($id) { function reverse($id, $amount = null, $transaction_id = null, $rec_id = null) {
/* pr(array('LedgerEntry::reverse', */
/* compact('id', 'amount', 'transaction_id', 'rec_id'))); */
// Get the LedgerEntry and related fields // Get the LedgerEntry and related fields
$entry = $this->find $entry = $this->find
('first', ('first',
@@ -251,39 +254,107 @@ class LedgerEntry extends AppModel {
'DebitLedger.account_id', 'DebitLedger.account_id',
'CreditLedger.id', 'CreditLedger.id',
'CreditLedger.account_id', 'CreditLedger.account_id',
'DebitReconciliationLedgerEntry'
/* => */
/* array('DebitLedger.id', */
/* 'DebitLedger.account_id', */
/* 'CreditLedger.id', */
/* 'CreditLedger.account_id', */
/* ) */
,
'CreditReconciliationLedgerEntry'
/* => */
/* array('DebitLedger.id', */
/* 'DebitLedger.account_id', */
/* 'CreditLedger.id', */
/* 'CreditLedger.account_id', */
/* ) */
,
'Customer.id', 'Customer.id',
'Lease.id', 'Lease.id',
), ),
'fields' => array('LedgerEntry.*'), 'fields' => array('LedgerEntry.*'),
'conditions' => array('LedgerEntry.id' => $id), 'conditions' => array(array('LedgerEntry.id' => $id),
/* array('NOT' => */
/* array('OR' => */
/* array(array('DebitReconciliationLedgerEntry.id' => $rec_id), */
/* array('CreditReconciliationLedgerEntry.id' => $rec_id), */
/* ), */
/* ), */
/* ), */
),
)); ));
//pr($entry); //pr($entry);
if (!isset($amount))
$amount = $entry['LedgerEntry']['amount'];
$A = new Account(); $A = new Account();
$ids = $this->Ledger->Account->postLedgerEntry $ids = $this->Ledger->Account->postLedgerEntry
(//array('transaction_id' => $entry['Transaction']['id']), (array('transaction_id' => $transaction_id),
null,
null, null,
array('debit_ledger_id' => $A->currentLedgerID($entry['CreditLedger']['account_id']), array('debit_ledger_id' => $A->currentLedgerID($entry['CreditLedger']['account_id']),
'credit_ledger_id' => $A->currentLedgerID($entry['DebitLedger']['account_id']), 'credit_ledger_id' => $A->currentLedgerID($entry['DebitLedger']['account_id']),
'effective_date' => $entry['LedgerEntry']['effective_date'], 'effective_date' => $entry['LedgerEntry']['effective_date'],
//'effective_date' => $entry['LedgerEntry']['effective_date'], //'effective_date' => $entry['LedgerEntry']['effective_date'],
'amount' => $entry['LedgerEntry']['amount'], 'amount' => $amount,
'lease_id' => $entry['Lease']['id'], 'lease_id' => $entry['Lease']['id'],
'customer_id' => $entry['Customer']['id'], 'customer_id' => $entry['Customer']['id'],
'comment' => "Reversal of Ledger Entry #{$id}", 'comment' => "Reversal of Ledger Entry #{$id}",
), ),
array('debit' => array(array('LedgerEntry' => array('id' => $entry['LedgerEntry']['id'], array('debit' => array(array('LedgerEntry' => array('id' => $entry['LedgerEntry']['id'],
'amount' => $entry['LedgerEntry']['amount'], 'amount' => $amount,
))), ))),
'credit' => array(array('LedgerEntry' => array('id' => $entry['LedgerEntry']['id'], 'credit' => array(array('LedgerEntry' => array('id' => $entry['LedgerEntry']['id'],
'amount' => $entry['LedgerEntry']['amount'], 'amount' => $amount,
))), ))),
)); ));
if ($ids['error'])
return null;
$tid = $ids['transaction_id'];
/* pr(compact('entry')); */
foreach (array('Debit', 'Credit') AS $dc_type) {
foreach ($entry[$dc_type . 'ReconciliationLedgerEntry'] AS $RLE) {
/* pr(array('checkpoint' => "Reverse $dc_type LE", */
/* compact('id', 'RLE'))); */
if ($RLE['id'] == $rec_id)
continue;
if (!$this->reverse($RLE['id'], $RLE['Reconciliation']['amount'], $tid, $id))
$ids['error'] = true;
/* $rids = $this->Ledger->Account->postLedgerEntry */
/* (array('transaction_id' => $tid), */
/* null, */
/* array('debit_ledger_id' => $A->currentLedgerID($RLE['CreditLedger']['account_id']), */
/* 'credit_ledger_id' => $A->currentLedgerID($RLE['DebitLedger']['account_id']), */
/* 'effective_date' => $RLE['effective_date'], */
/* //'effective_date' => $RLE['effective_date'], */
/* 'amount' => $RLE['Reconciliation']['amount'], */
/* 'lease_id' => $entry['Lease']['id'], */
/* 'customer_id' => $entry['Customer']['id'], */
/* 'comment' => "Reversal of Ledger Entry #{$RLE['id']}", */
/* ), */
/* array('debit' => array(array('LedgerEntry' => array('id' => $RLE['id'], */
/* 'amount' => $RLE['Reconciliation']['amount'], */
/* ))), */
/* 'credit' => array(array('LedgerEntry' => array('id' => $RLE['id'], */
/* 'amount' => $RLE['Reconciliation']['amount'], */
/* ))), */
/* )); */
/* if ($rids['error']) */
/* $ids['error'] = true; */
}
}
if ($ids['error']) if ($ids['error'])
return null; return null;