More work cleaning up and testing the use of charge waivers, as well as security deposit utilizations. Much of this was just around the concept of determining balances, which wasn't / isn't very straightforward. It's not hard to calculate a balance for a particular sitation, but it is difficult to generalize. I think it's reasonable as it now stands, but the StatementEntry::stats algorithm is certainly subject to change. Also, I didn't double check every case which called the stats() function, so I highly suspect we'll find cases where the code is not expecting the new return values, either symantically or logically.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@480 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -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,14 +40,14 @@ class StatementEntry extends AppModel {
|
||||
($sum ? ')' : '') . ' AS charge' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
//"IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVE')," .
|
||||
//"IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVER')," .
|
||||
"IF({$entry_name}.type NOT IN('CHARGE', 'VOID')," .
|
||||
" {$entry_name}.amount, NULL)" .
|
||||
($sum ? ')' : '') . ' AS payment' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
//"IF({$entry_name}.type = 'CHARGE', 1," .
|
||||
//" IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVE'), -1, 0))" .
|
||||
//" IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVER'), -1, 0))" .
|
||||
"IF({$entry_name}.type = 'VOID', 0," .
|
||||
" IF({$entry_name}.type = 'CHARGE', 1, -1))" .
|
||||
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
||||
@@ -377,10 +377,13 @@ OPTION 2
|
||||
*
|
||||
*/
|
||||
function assignCredits($query = null, $receipt_id = null,
|
||||
$charge_ids = null, $payment_type = null)
|
||||
$charge_ids = null, $payment_type = null,
|
||||
$customer_id = null, $lease_id = null)
|
||||
{
|
||||
//$this->prFunctionLevel(25);
|
||||
$this->prEnter( compact('query', 'receipt_id', 'charge_ids', 'payment_type'));
|
||||
$this->prEnter(compact('query', 'receipt_id',
|
||||
'charge_ids', 'payment_type',
|
||||
'customer_id', 'lease_id'));
|
||||
$this->queryInit($query);
|
||||
|
||||
if (empty($payment_type))
|
||||
@@ -570,7 +573,8 @@ OPTION 2
|
||||
'amount' => $credit['balance'],
|
||||
'effective_date' => $credit['Transaction']['stamp'],
|
||||
'transaction_id' => $credit['Transaction']['id'],
|
||||
'customer_id' => $credit['Customer']['id'],
|
||||
'customer_id' => $customer_id,
|
||||
'lease_id' => $lease_id,
|
||||
));
|
||||
$ret['Credit'] = $result;
|
||||
if ($result['error'])
|
||||
@@ -597,92 +601,95 @@ OPTION 2
|
||||
if (isset($id))
|
||||
$query['conditions'][] = array('StatementEntry.id' => $id);
|
||||
|
||||
$rquery = $query;
|
||||
unset($rquery['link']['ChargeEntry']);
|
||||
$rquery['link']['PaymentEntry'] = array('fields' => array());
|
||||
// Determine the total in charges
|
||||
$charge_query = $query;
|
||||
unset($charge_query['link']['ChargeEntry']);
|
||||
unset($charge_query['link']['PaymentEntry']);
|
||||
|
||||
$rquery['fields'] = array();
|
||||
$rquery['fields'][] = "StatementEntry.amount";
|
||||
$rquery['fields'][] = "SUM(PaymentEntry.amount) AS reconciled";
|
||||
$charge_query['fields'] = array();
|
||||
$charge_query['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
$charge_query['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
||||
$result = $this->find('first', $charge_query);
|
||||
$stats['Charge'] = $result[0];
|
||||
|
||||
$rquery['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
||||
$rquery['group'] = 'StatementEntry.id';
|
||||
|
||||
$result = $this->find('all', $rquery);
|
||||
$stats['Charge'] = array('total' => 0, 'reconciled' => 0);
|
||||
foreach($result AS $charge) {
|
||||
$stats['Charge']['total'] += $charge['StatementEntry']['amount'];
|
||||
$stats['Charge']['reconciled'] += $charge[0]['reconciled'];
|
||||
}
|
||||
$stats['Charge']['balance'] =
|
||||
$stats['Charge']['total'] - $stats['Charge']['reconciled'];
|
||||
|
||||
$this->pr(17, compact('rquery', 'result'),
|
||||
$this->pr(17, compact('charge_query', 'result'),
|
||||
'Charges');
|
||||
|
||||
$rquery = $query;
|
||||
unset($rquery['link']['ChargeEntry']);
|
||||
$rquery['link']['PaymentEntry'] = array('fields' => array(),
|
||||
/* 'conditions' => */
|
||||
/* array('PaymentEntry.type' => 'WAIVE'), */
|
||||
);
|
||||
|
||||
$rquery['fields'] = array();
|
||||
$rquery['fields'][] = "SUM(PaymentEntry.amount) AS reconciled";
|
||||
|
||||
$rquery['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
||||
$rquery['conditions'][] = array('PaymentEntry.type' => 'WAIVE');
|
||||
$rquery['group'] = 'StatementEntry.id';
|
||||
|
||||
$result = $this->find('first', $rquery);
|
||||
$stats['Charge']['waived'] = $result[0]['reconciled'];
|
||||
/* $stats['Waiver'] = array('total' => 0, 'reconciled' => 0); */
|
||||
/* foreach($result AS $charge) { */
|
||||
/* $stats['Waiver']['total'] += $charge['StatementEntry']['amount']; */
|
||||
/* $stats['Waiver']['reconciled'] += $charge[0]['reconciled']; */
|
||||
/* } */
|
||||
/* $stats['Waiver']['balance'] = */
|
||||
/* $stats['Waiver']['total'] - $stats['Waiver']['reconciled']; */
|
||||
// Tally the amount actually _paid_ to those charges
|
||||
$charge_payment_query = $charge_query;
|
||||
$charge_payment_query['link']['PaymentEntry'] = array('fields' => array());
|
||||
$charge_payment_query['fields'] = array();
|
||||
$charge_payment_query['fields'][] = "COALESCE(SUM(PaymentEntry.amount),0) AS paid";
|
||||
$charge_payment_query['conditions'][] = array('PaymentEntry.type' => 'PAYMENT');
|
||||
$result = $this->find('first', $charge_payment_query);
|
||||
$stats['Charge'] += $result[0];
|
||||
|
||||
$this->pr(17, compact('rquery', 'result'),
|
||||
'Waived');
|
||||
$this->pr(17, compact('charge_payment_query', 'result'),
|
||||
'Charge Payments');
|
||||
|
||||
$rquery = $query;
|
||||
unset($rquery['link']['PaymentEntry']);
|
||||
$rquery['link']['ChargeEntry'] = array('fields' => array());
|
||||
// Tally the amount of charges that have been waived
|
||||
$charge_waiver_query = $charge_query;
|
||||
$charge_waiver_query['link']['PaymentEntry'] = array('fields' => array());
|
||||
$charge_waiver_query['fields'] = array();
|
||||
$charge_waiver_query['fields'][] = "COALESCE(SUM(PaymentEntry.amount),0) AS waived";
|
||||
$charge_waiver_query['conditions'][] = array('PaymentEntry.type' => 'WAIVER');
|
||||
$result = $this->find('first', $charge_waiver_query);
|
||||
$stats['Charge'] += $result[0];
|
||||
|
||||
$this->pr(17, compact('charge_waiver_query', 'result'),
|
||||
'Charge Waivers');
|
||||
|
||||
$rquery['fields'] = array();
|
||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, 0, StatementEntry.amount)) AS reconciled";
|
||||
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, StatementEntry.amount, 0)) AS balance";
|
||||
// Compute some summary information for charges
|
||||
$stats['Charge']['reconciled'] =
|
||||
$stats['Charge']['paid'] + $stats['Charge']['waived'];
|
||||
$stats['Charge']['balance'] =
|
||||
$stats['Charge']['total'] - $stats['Charge']['reconciled'];
|
||||
if (!isset($stats['Charge']['balance']))
|
||||
$stats['Charge']['balance'] = 0;
|
||||
|
||||
$rquery['conditions'][] = array('StatementEntry.type' => 'PAYMENT');
|
||||
$result = $this->find('first', $rquery);
|
||||
if (!isset($result[0]['balance']))
|
||||
$result[0]['balance'] = 0;
|
||||
|
||||
// Determine the total in payments, including those which
|
||||
// are charge waivers and those that do not even reconcile
|
||||
// to charges (i.e. they are surplus payments).
|
||||
$payment_query = $query;
|
||||
unset($payment_query['link']['PaymentEntry']);
|
||||
$payment_query['link']['ChargeEntry'] = array('fields' => array());
|
||||
|
||||
$payment_query['fields'] = array();
|
||||
$payment_query['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
$payment_query['fields'][] = "COALESCE(SUM(IF(ChargeEntry.id IS NULL, 0, StatementEntry.amount)), 0) AS charged";
|
||||
$payment_query['fields'][] = "COALESCE(SUM(IF(ChargeEntry.id IS NULL, StatementEntry.amount, 0)), 0) AS surplus";
|
||||
|
||||
$payment_query['conditions'][] = array('StatementEntry.type' => array('PAYMENT', 'WAIVER', 'SURPLUS'));
|
||||
$result = $this->find('first', $payment_query);
|
||||
$stats['Payment'] = $result[0];
|
||||
|
||||
$this->pr(17, compact('rquery', 'result'),
|
||||
$this->pr(17, compact('payment_query', 'result'),
|
||||
'Payments');
|
||||
|
||||
$rquery = $query;
|
||||
unset($rquery['link']['PaymentEntry']);
|
||||
unset($rquery['link']['ChargeEntry']);
|
||||
// Compute some summary information for payments.
|
||||
// Add a reconciled field just for consistency with Charge.
|
||||
$stats['Payment']['reconciled'] =
|
||||
$stats['Payment']['charged'];
|
||||
$stats['Payment']['balance'] =
|
||||
$stats['Payment']['total'] - $stats['Payment']['reconciled'];
|
||||
if (!isset($stats['Payment']['balance']))
|
||||
$stats['Payment']['balance'] = 0;
|
||||
|
||||
$rquery['fields'] = array();
|
||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
$rquery['fields'][] = "SUM(0) AS reconciled";
|
||||
|
||||
$rquery['conditions'][] = array('StatementEntry.type' => 'SURPLUS');
|
||||
$result = $this->find('first', $rquery);
|
||||
$result[0]['balance'] = $result[0]['total'] - $result[0]['reconciled'];
|
||||
if (!isset($result[0]['balance']))
|
||||
$result[0]['balance'] = 0;
|
||||
$stats['Surplus'] = $result[0];
|
||||
|
||||
$this->pr(17, compact('rquery', 'result'),
|
||||
'Surplus');
|
||||
// 'balance' is simply the difference between
|
||||
// the balances of charges and payments
|
||||
$stats['balance'] = $stats['Charge']['balance'] - $stats['Payment']['balance'];
|
||||
if (!isset($stats['balance']))
|
||||
$stats['balance'] = 0;
|
||||
|
||||
// 'account_balance' is really only relevant to
|
||||
// callers that have requested charge and payment
|
||||
// stats with respect to a particular account.
|
||||
// It represents the difference between inflow
|
||||
// and outflow from that account.
|
||||
$stats['account_balance'] = $stats['Charge']['paid'] - $stats['Payment']['total'];
|
||||
if (!isset($stats['account_balance']))
|
||||
$stats['account_balance'] = 0;
|
||||
|
||||
return $this->prReturn($stats);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user