Several changes in an effort to support charge reversals. I can't imagine this is all working flawlessly, as I'm not quite sure how it even _should_ work.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@490 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-05 07:54:57 +00:00
parent 5247bb8db6
commit cca698d437
6 changed files with 236 additions and 220 deletions

View File

@@ -22,7 +22,7 @@ class StatementEntry extends AppModel {
);
var $default_log_level = array('log' => 30, 'show' => 15);
//var $default_log_level = array('log' => 30, 'show' => 15);
/**************************************************************************
**************************************************************************
@@ -40,15 +40,12 @@ class StatementEntry extends AppModel {
($sum ? ')' : '') . ' AS charge' . ($sum ? 's' : ''),
($sum ? 'SUM(' : '') .
//"IF({$entry_name}.type IN('DISBURSEMENT', 'SURPLUS', 'WAIVER')," .
"IF({$entry_name}.type NOT IN('CHARGE', 'VOID')," .
"IF({$entry_name}.type NOT IN('CHARGE', 'PAYMENT', 'VOID')," .
" {$entry_name}.amount, NULL)" .
($sum ? ')' : '') . ' AS disbursement' . ($sum ? 's' : ''),
($sum ? 'SUM(' : '') .
//"IF({$entry_name}.type = 'CHARGE', 1," .
//" IF({$entry_name}.type IN('DISBURSEMENT', 'SURPLUS', 'WAIVER'), -1, 0))" .
"IF({$entry_name}.type = 'VOID', 0," .
"IF({$entry_name}.type IN ('VOID', 'PAYMENT'), 0," .
" IF({$entry_name}.type = 'CHARGE', 1, -1))" .
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
($sum ? ')' : '') . ' AS balance',
@@ -154,110 +151,80 @@ class StatementEntry extends AppModel {
* function: reverse
* - Reverses the charges
*
* SAMPLE MOVE IN w/ PRE DISBURSEMENT
* DEPOSIT RENT A/R RECEIPT CHECK PETTY BANK
* ------- ------- ------- ------- ------- ------- -------
* |25 | 25| | | | |
* | |20 20| | | | |
* | |20 20| | | | |
* | |20 20| | | | |
* | | |25 25| | | |
* | | |20 20| | | |
* | | |20 20| | | |
* | | |20 20| | | |
* | | | |85 85| | |
* | | | | |85 | 85|
* MOVE OUT and REFUND FINAL MONTH
* DEPOSIT RENT C/P RECEIPT CHECK PETTY BANK
* ------- ------- ------- ------- ------- ------- -------
* 25| | |25 | | | | t20 e20a
* | 20| |20 | | | | t20 e20b
* -ONE REFUND CHECK-
* | | 25| |25 | | | t30 e30a
* | | 20| |20 | | | t30 e30b
* | | | 45| | | |45 t40 e40
* -OR MULTIPLE-
* | | 15| |15 | | | t50a e50a
* | | | 15| | |15 | t60a e60a
* | | 30| |30 | | | t50b e50b
* | | | 30| | | |30 t60b e60b
* | | | | | | |
OPTION 1
* |-25 | -25| | | | |
* | |-20 -20| | | | |
* | | |-25 -25| | | |
* | | |-20 -20| | | |
OPTION 2
* |-25 | | -25| | | |
* | |-20 | -20| | | |
* | | | |-15 | -15| |
* | | | |-30 | | -30|
* | | | | | | |
*
*/
function reverse($ledger_entries, $stamp = null) {
$this->prEnter(compact('ledger_entries', 'stamp'));
function reverse($id, $stamp = null) {
$this->prEnter(compact('id', 'stamp'));
// If the user only wants to reverse one ID, we'll allow it
if (!is_array($ledger_entries))
$ledger_entries = $this->find
('all', array
('contain' => false,
'conditions' => array('Entry.id' => $ledger_entries)));
$ret = array();
$A = new Account();
// Get the basic information about the entry to be reversed.
$this->recursive = -1;
$charge = $this->read(null, $id);
$charge = $charge['StatementEntry'];
$ar_account_id = $A->accountReceivableAccountID();
$receipt_account_id = $A->receiptAccountID();
$voided_entry_transactions = array();
$reconciled = $this->reconciledEntries($id);
$this->pr(21, compact('reconciled'));
$transaction_id = null;
foreach ($ledger_entries AS $entry) {
$entry = $entry['Entry'];
$amount = -1*$entry['amount'];
if ($reconciled) {
foreach ($reconciled['entries'] AS $entry) {
$voided_entry_transactions[$entry['DisbursementEntry']['transaction_id']]
= array_intersect_key($entry['DisbursementEntry'],
array('customer_id'=>1, 'lease_id'=>1));
if (isset($entry['credit_account_id']))
$refund_account_id = $entry['credit_account_id'];
elseif (isset($entry['CreditLedger']['Account']['id']))
$refund_account_id = $entry['CreditLedger']['Account']['id'];
elseif (isset($entry['credit_ledger_id']))
$refund_account_id = $this->Ledger->accountID($entry['credit_ledger_id']);
else
return $this->prReturn(null);
$this->del($entry['DisbursementEntry']['id']);
continue;
// post new refund in the income account
$ids = $A->postEntry
(array('transaction_id' => $transaction_id),
null,
array('debit_ledger_id' => $A->currentLedgerID($ar_account_id),
'credit_ledger_id' => $A->currentLedgerID($refund_account_id),
'effective_date' => $entry['effective_date'],
'through_date' => $entry['through_date'],
'amount' => $amount,
'lease_id' => $entry['lease_id'],
'customer_id' => $entry['customer_id'],
'comment' => "Refund; Entry #{$entry['id']}",
),
array('debit' => array
(array('Entry' =>
array('id' => $entry['id'],
'amount' => $amount))),
)
);
if ($ids['error'])
return $this->prReturn(null);
$transaction_id = $ids['transaction_id'];
$this->pr(15, compact('ids', 'amount', 'refund_account_id', 'ar_account_id'),
'Posted Refund Ledger Entry');
$DE = new StatementEntry();
$DE->id = $entry['DisbursementEntry']['id'];
$DE->saveField('type', 'VOID');
$DE->saveField('charge_entry_id', null);
}
$this->pr(17, compact('voided_entry_transactions'));
}
return $this->prReturn(true);
// Query the stats to get the remaining balance
$stats = $this->stats($id);
// Build a transaction
$reversal = array('Transaction' => array(), 'Entry' => array());
$reversal['Transaction']['stamp'] = $stamp;
$reversal['Transaction']['comment'] = "Credit Note: Charge Reversal";
if ($charge['type'] !== 'CHARGE')
die("INTERNAL ERROR: REVERSAL ITEM IS NOT CHARGE");
// Add the charge reversal
$reversal['Entry'][] =
array('amount' => $stats['Charge']['total'],
'account_id' => $charge['account_id'],
'comment' => 'Charge Reversal',
);
// Record the reversal transaction
$result = $this->Transaction->addReversal
($reversal, $id, $charge['customer_id'], $charge['lease_id']);
$this->pr(21, compact('result'));
$ret['reversal'] = $result;
if ($result['error'])
$ret['error'] = true;
foreach ($voided_entry_transactions AS $transaction_id => $tx) {
$result = $this->assignCredits
(null,
$transaction_id,
null,
null,
$tx['customer_id'],
$tx['lease_id']
);
$this->pr(21, compact('result'));
$ret['assigned'][] = $result;
if ($result['error'])
$ret['error'] = true;
}
return $this->prReturn($ret + array('error' => false));
}
@@ -421,23 +388,27 @@ OPTION 2
// Next, establish credit from the newly added receipt
$receipt_credit = null;
if (!empty($receipt_id)) {
$lquery = $query;
$lquery['link'] = array('StatementEntry' => $lquery['link']);
$lquery['link'] += array('LedgerEntry' =>
array('conditions' =>
//array(LedgerEntry.'crdr'=>'DEBIT'),
array('LedgerEntry.account_id !=' => $this->Account->accountReceivableAccountID()),
));
$lquery['fields'] = array('Transaction.id', 'Transaction.stamp', 'Transaction.amount',
'LedgerEntry.account_id');
// Very specific case here... no extra conditions
unset($lquery['conditions']);
$this->Transaction->id = $receipt_id;
$lquery =
array('link' =>
array('StatementEntry',
'LedgerEntry' =>
array('conditions' =>
array('LedgerEntry.account_id !=' =>
$this->Account->accountReceivableAccountID()),
),
),
'conditions' => array('Transaction.id' => $receipt_id),
'fields' => array('Transaction.id', 'Transaction.stamp', 'Transaction.amount'),
);
$receipt_credit = $this->Transaction->find('first', $lquery);
if (!$receipt_credit)
die("INTERNAL ERROR: UNABLE TO LOCATE RECEIPT");
$receipt_credit['balance'] = $receipt_credit['Transaction']['amount'];
//$reconciled = $this->reconciledEntries($id);
$stats = $this->Transaction->stats($receipt_id);
$receipt_credit['balance'] =
$receipt_credit['Transaction']['amount'] - $stats['Disbursement']['total'];
$this->pr(18, compact('receipt_credit'),
"Receipt Credit Added");