diff --git a/models/ledger_entry.php b/models/ledger_entry.php index 4f6dafd..e40a90f 100644 --- a/models/ledger_entry.php +++ b/models/ledger_entry.php @@ -241,7 +241,10 @@ class LedgerEntry extends AppModel { * - 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 $entry = $this->find ('first', @@ -251,39 +254,107 @@ class LedgerEntry extends AppModel { 'DebitLedger.account_id', 'CreditLedger.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', 'Lease.id', ), '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); + if (!isset($amount)) + $amount = $entry['LedgerEntry']['amount']; + $A = new Account(); $ids = $this->Ledger->Account->postLedgerEntry - (//array('transaction_id' => $entry['Transaction']['id']), - null, + (array('transaction_id' => $transaction_id), null, array('debit_ledger_id' => $A->currentLedgerID($entry['CreditLedger']['account_id']), 'credit_ledger_id' => $A->currentLedgerID($entry['DebitLedger']['account_id']), 'effective_date' => $entry['LedgerEntry']['effective_date'], //'effective_date' => $entry['LedgerEntry']['effective_date'], - 'amount' => $entry['LedgerEntry']['amount'], + 'amount' => $amount, 'lease_id' => $entry['Lease']['id'], 'customer_id' => $entry['Customer']['id'], 'comment' => "Reversal of Ledger Entry #{$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'], - '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']) return null;