Discovered that the use of the term 'Payment' has been a misnomer. A company uses the term payment to indicate the monies it pays to _others_. Changing this leaves us without a good replacement term, but disbursement really seems to fit the bill. As comfortable as the term payment was, it was odd in many respects and disbursement does seem more appropriate. I'm sure there are several lingering bugs from this massive search/replace exercise, but this is a decent baseline for the change.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@488 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -7,15 +7,15 @@ class StatementEntry extends AppModel {
|
||||
'Lease',
|
||||
'Account',
|
||||
|
||||
// The charge to which this payment applies (if it is one)
|
||||
// The charge to which this disbursement applies (if it is one)
|
||||
'ChargeEntry' => array(
|
||||
'className' => 'StatementEntry',
|
||||
),
|
||||
);
|
||||
|
||||
var $hasMany = array(
|
||||
// The payments that apply to this charge (if it is one)
|
||||
'PaymentEntry' => array(
|
||||
// The disbursements that apply to this charge (if it is one)
|
||||
'DisbursementEntry' => array(
|
||||
'className' => 'StatementEntry',
|
||||
'foreignKey' => 'charge_entry_id',
|
||||
),
|
||||
@@ -27,11 +27,11 @@ class StatementEntry extends AppModel {
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: chargePaymentFields
|
||||
* function: chargeDisbursementFields
|
||||
*/
|
||||
|
||||
|
||||
function chargePaymentFields($sum = false, $entry_name = 'StatementEntry') {
|
||||
function chargeDisbursementFields($sum = false, $entry_name = 'StatementEntry') {
|
||||
$fields = array
|
||||
(
|
||||
($sum ? 'SUM(' : '') .
|
||||
@@ -40,14 +40,14 @@ class StatementEntry extends AppModel {
|
||||
($sum ? ')' : '') . ' AS charge' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
//"IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVER')," .
|
||||
//"IF({$entry_name}.type IN('DISBURSEMENT', 'SURPLUS', 'WAIVER')," .
|
||||
"IF({$entry_name}.type NOT IN('CHARGE', 'VOID')," .
|
||||
" {$entry_name}.amount, NULL)" .
|
||||
($sum ? ')' : '') . ' AS payment' . ($sum ? 's' : ''),
|
||||
($sum ? ')' : '') . ' AS disbursement' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
//"IF({$entry_name}.type = 'CHARGE', 1," .
|
||||
//" IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVER'), -1, 0))" .
|
||||
//" IF({$entry_name}.type IN('DISBURSEMENT', '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)" .
|
||||
@@ -154,7 +154,7 @@ class StatementEntry extends AppModel {
|
||||
* function: reverse
|
||||
* - Reverses the charges
|
||||
*
|
||||
* SAMPLE MOVE IN w/ PRE PAYMENT
|
||||
* SAMPLE MOVE IN w/ PRE DISBURSEMENT
|
||||
* DEPOSIT RENT A/R RECEIPT CHECK PETTY BANK
|
||||
* ------- ------- ------- ------- ------- ------- -------
|
||||
* |25 | 25| | | | |
|
||||
@@ -271,20 +271,20 @@ OPTION 2
|
||||
function reconciledSetQuery($set, $query) {
|
||||
$this->queryInit($query);
|
||||
|
||||
if ($set == 'CHARGE' || $set == 'PAYMENT')
|
||||
if ($set == 'CHARGE' || $set == 'DISBURSEMENT')
|
||||
$query['conditions'][] = array('StatementEntry.type' => $set);
|
||||
else
|
||||
die("INVALID RECONCILE SET");
|
||||
|
||||
if ($set == 'CHARGE')
|
||||
$query['link']['PaymentEntry'] = array('fields' => array("SUM(PaymentEntry.amount) AS reconciled"));
|
||||
if ($set == 'PAYMENT')
|
||||
$query['link']['DisbursementEntry'] = array('fields' => array("SUM(DisbursementEntry.amount) AS reconciled"));
|
||||
if ($set == 'DISBURSEMENT')
|
||||
$query['link']['ChargeEntry'] = array('fields' => array("SUM(ChargeEntry.amount) AS reconciled"));
|
||||
|
||||
$query['group'] = 'StatementEntry.id';
|
||||
|
||||
// REVISIT: TESTING
|
||||
//$query['link']['PaymentEntry'] = array('fields' => array("(`PaymentEntry.amount`+0) AS reconciled"));
|
||||
//$query['link']['DisbursementEntry'] = array('fields' => array("(`DisbursementEntry.amount`+0) AS reconciled"));
|
||||
//$query['group'] = null;
|
||||
// END REVISIT
|
||||
|
||||
@@ -316,7 +316,7 @@ OPTION 2
|
||||
$reconciled = true;
|
||||
elseif ($entry['StatementEntry']['reconciled'] == 0)
|
||||
$reconciled = false;
|
||||
else // Partial payment; depends on unrec
|
||||
else // Partial disbursement; depends on unrec
|
||||
$reconciled = (!$unrec && $if_rec_include_partial);
|
||||
|
||||
// Add to the set, if it's been requested
|
||||
@@ -334,7 +334,7 @@ OPTION 2
|
||||
**************************************************************************
|
||||
* function: reconciledEntries
|
||||
* - Returns a list of entries that reconcile against the given entry.
|
||||
* (such as payments towards a charge).
|
||||
* (such as disbursements towards a charge).
|
||||
*/
|
||||
function reconciledEntriesQuery($id, $query = null) {
|
||||
$this->queryInit($query, false);
|
||||
@@ -346,8 +346,8 @@ OPTION 2
|
||||
$query['conditions'][] = array('StatementEntry.id' => $id);
|
||||
|
||||
if ($this->data['StatementEntry']['type'] == 'CHARGE')
|
||||
$query['link']['PaymentEntry'] = array();
|
||||
if ($this->data['StatementEntry']['type'] == 'PAYMENT')
|
||||
$query['link']['DisbursementEntry'] = array();
|
||||
if ($this->data['StatementEntry']['type'] == 'DISBURSEMENT')
|
||||
$query['link']['ChargeEntry'] = array();
|
||||
|
||||
return $query;
|
||||
@@ -373,24 +373,24 @@ OPTION 2
|
||||
*
|
||||
* REVISIT <AP>: 20090726
|
||||
* This algorithm shouldn't be hardcoded. We need to allow
|
||||
* the user to specify how payments should be applied.
|
||||
* the user to specify how disbursements should be applied.
|
||||
*
|
||||
*/
|
||||
function assignCredits($query = null, $receipt_id = null,
|
||||
$charge_ids = null, $payment_type = null,
|
||||
$charge_ids = null, $disbursement_type = null,
|
||||
$customer_id = null, $lease_id = null)
|
||||
{
|
||||
$this->prFunctionLevel(25);
|
||||
$this->prEnter(compact('query', 'receipt_id',
|
||||
'charge_ids', 'payment_type',
|
||||
'charge_ids', 'disbursement_type',
|
||||
'customer_id', 'lease_id'));
|
||||
$this->queryInit($query);
|
||||
|
||||
if (!empty($customer_id))
|
||||
$query['conditions'][] = array('StatementEntry.customer_id' => $customer_id);
|
||||
|
||||
if (empty($payment_type))
|
||||
$payment_type = 'PAYMENT';
|
||||
if (empty($disbursement_type))
|
||||
$disbursement_type = 'DISBURSEMENT';
|
||||
|
||||
$ret = array();
|
||||
|
||||
@@ -461,7 +461,7 @@ OPTION 2
|
||||
// Initialize our list of used credits
|
||||
$used_credits = array();
|
||||
|
||||
// Work through all unpaid charges, applying payments as we go
|
||||
// Work through all unpaid charges, applying disbursements as we go
|
||||
foreach ($charges['entries'] AS $charge) {
|
||||
|
||||
$this->pr(20, compact('charge'),
|
||||
@@ -489,9 +489,9 @@ OPTION 2
|
||||
if (count($credits)) {
|
||||
// Peel off the first credit available
|
||||
$credit =& $credits[0];
|
||||
$payment_date = $credit['StatementEntry']['effective_date'];
|
||||
$payment_transaction_id = $credit['StatementEntry']['transaction_id'];
|
||||
$payment_account_id = $credit['StatementEntry']['account_id'];
|
||||
$disbursement_date = $credit['StatementEntry']['effective_date'];
|
||||
$disbursement_transaction_id = $credit['StatementEntry']['transaction_id'];
|
||||
$disbursement_account_id = $credit['StatementEntry']['account_id'];
|
||||
|
||||
if (!isset($credit['balance']))
|
||||
$credit['balance'] = $credit['StatementEntry']['amount'];
|
||||
@@ -499,22 +499,22 @@ OPTION 2
|
||||
elseif (!empty($receipt_credit['balance'])) {
|
||||
// Use our only receipt credit
|
||||
$credit =& $receipt_credit;
|
||||
$payment_date = $credit['Transaction']['stamp'];
|
||||
$payment_transaction_id = $credit['Transaction']['id'];
|
||||
$payment_account_id = $credit['LedgerEntry']['account_id'];
|
||||
$disbursement_date = $credit['Transaction']['stamp'];
|
||||
$disbursement_transaction_id = $credit['Transaction']['id'];
|
||||
$disbursement_account_id = $credit['LedgerEntry']['account_id'];
|
||||
}
|
||||
else {
|
||||
die("HOW DID WE GET HERE WITH NO SURPLUS?");
|
||||
}
|
||||
|
||||
// Set the payment amount to the maximum amount
|
||||
// Set the disbursement amount to the maximum amount
|
||||
// possible without exceeding the charge or credit balance
|
||||
$payment_amount = min($charge['balance'], $credit['balance']);
|
||||
$disbursement_amount = min($charge['balance'], $credit['balance']);
|
||||
if (!isset($credit['applied']))
|
||||
$credit['applied'] = 0;
|
||||
|
||||
$credit['applied'] += $payment_amount;
|
||||
$credit['balance'] -= $payment_amount;
|
||||
$credit['applied'] += $disbursement_amount;
|
||||
$credit['balance'] -= $disbursement_amount;
|
||||
|
||||
$this->pr(20, compact('credit'),
|
||||
($credit['balance'] > 0 ? 'Utilized' : 'Exhausted') .
|
||||
@@ -528,28 +528,28 @@ OPTION 2
|
||||
if ($credit['balance'] <= 0 && count($credits))
|
||||
$used_credits[] = array_shift($credits);
|
||||
|
||||
// Add a payment that uses the available credit to pay the charge
|
||||
$payment = array('type' => $payment_type,
|
||||
'account_id' => $payment_account_id,
|
||||
'amount' => $payment_amount,
|
||||
'effective_date' => $payment_date,
|
||||
'transaction_id' => $payment_transaction_id,
|
||||
// Add a disbursement that uses the available credit to pay the charge
|
||||
$disbursement = array('type' => $disbursement_type,
|
||||
'account_id' => $disbursement_account_id,
|
||||
'amount' => $disbursement_amount,
|
||||
'effective_date' => $disbursement_date,
|
||||
'transaction_id' => $disbursement_transaction_id,
|
||||
'customer_id' => $charge['StatementEntry']['customer_id'],
|
||||
'lease_id' => $charge['StatementEntry']['lease_id'],
|
||||
'charge_entry_id' => $charge['StatementEntry']['id'],
|
||||
'comment' => null,
|
||||
);
|
||||
|
||||
$this->pr(20, compact('payment'),
|
||||
'New Payment Entry');
|
||||
$this->pr(20, compact('disbursement'),
|
||||
'New Disbursement Entry');
|
||||
|
||||
$result = $this->addStatementEntry($payment);
|
||||
$ret['Payment'][] = $result;
|
||||
$result = $this->addStatementEntry($disbursement);
|
||||
$ret['Disbursement'][] = $result;
|
||||
if ($result['error'])
|
||||
$ret['error'] = true;
|
||||
|
||||
// Adjust the charge balance to reflect the new payment
|
||||
$charge['balance'] -= $payment_amount;
|
||||
// Adjust the charge balance to reflect the new disbursement
|
||||
$charge['balance'] -= $disbursement_amount;
|
||||
if ($charge['balance'] < 0)
|
||||
die("HOW DID WE GET A NEGATIVE CHARGE AMOUNT?");
|
||||
|
||||
@@ -564,7 +564,7 @@ OPTION 2
|
||||
$used_credits[] = array_shift($credits);
|
||||
|
||||
$this->pr(18, compact('credits', 'used_credits', 'receipt_credit'),
|
||||
'Payments added');
|
||||
'Disbursements added');
|
||||
|
||||
// Clean up any explicit credits that have been used
|
||||
foreach ($used_credits AS $credit) {
|
||||
@@ -627,7 +627,7 @@ OPTION 2
|
||||
// Determine the total in charges
|
||||
$charge_query = $query;
|
||||
unset($charge_query['link']['ChargeEntry']);
|
||||
unset($charge_query['link']['PaymentEntry']);
|
||||
unset($charge_query['link']['DisbursementEntry']);
|
||||
|
||||
$charge_query['fields'] = array();
|
||||
$charge_query['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
@@ -639,23 +639,23 @@ OPTION 2
|
||||
'Charges');
|
||||
|
||||
// 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);
|
||||
$charge_disbursement_query = $charge_query;
|
||||
$charge_disbursement_query['link']['DisbursementEntry'] = array('fields' => array());
|
||||
$charge_disbursement_query['fields'] = array();
|
||||
$charge_disbursement_query['fields'][] = "COALESCE(SUM(DisbursementEntry.amount),0) AS paid";
|
||||
$charge_disbursement_query['conditions'][] = array('DisbursementEntry.type' => 'DISBURSEMENT');
|
||||
$result = $this->find('first', $charge_disbursement_query);
|
||||
$stats['Charge'] += $result[0];
|
||||
|
||||
$this->pr(17, compact('charge_payment_query', 'result'),
|
||||
'Charge Payments');
|
||||
$this->pr(17, compact('charge_disbursement_query', 'result'),
|
||||
'Charge Disbursements');
|
||||
|
||||
// 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['link']['DisbursementEntry'] = 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');
|
||||
$charge_waiver_query['fields'][] = "COALESCE(SUM(DisbursementEntry.amount),0) AS waived";
|
||||
$charge_waiver_query['conditions'][] = array('DisbursementEntry.type' => 'WAIVER');
|
||||
$result = $this->find('first', $charge_waiver_query);
|
||||
$stats['Charge'] += $result[0];
|
||||
|
||||
@@ -671,46 +671,46 @@ OPTION 2
|
||||
$stats['Charge']['balance'] = 0;
|
||||
|
||||
|
||||
// Determine the total in payments, including those which
|
||||
// Determine the total in disbursements, 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());
|
||||
// to charges (i.e. they are surplus disbursements).
|
||||
$disbursement_query = $query;
|
||||
unset($disbursement_query['link']['DisbursementEntry']);
|
||||
$disbursement_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";
|
||||
$disbursement_query['fields'] = array();
|
||||
$disbursement_query['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||
$disbursement_query['fields'][] = "COALESCE(SUM(IF(ChargeEntry.id IS NULL, 0, StatementEntry.amount)), 0) AS charged";
|
||||
$disbursement_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];
|
||||
$disbursement_query['conditions'][] = array('StatementEntry.type' => array('DISBURSEMENT', 'WAIVER', 'SURPLUS'));
|
||||
$result = $this->find('first', $disbursement_query);
|
||||
$stats['Disbursement'] = $result[0];
|
||||
|
||||
$this->pr(17, compact('payment_query', 'result'),
|
||||
'Payments');
|
||||
$this->pr(17, compact('disbursement_query', 'result'),
|
||||
'Disbursements');
|
||||
|
||||
// Compute some summary information for payments.
|
||||
// Compute some summary information for disbursements.
|
||||
// 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;
|
||||
$stats['Disbursement']['reconciled'] =
|
||||
$stats['Disbursement']['charged'];
|
||||
$stats['Disbursement']['balance'] =
|
||||
$stats['Disbursement']['total'] - $stats['Disbursement']['reconciled'];
|
||||
if (!isset($stats['Disbursement']['balance']))
|
||||
$stats['Disbursement']['balance'] = 0;
|
||||
|
||||
// 'balance' is simply the difference between
|
||||
// the balances of charges and payments
|
||||
$stats['balance'] = $stats['Charge']['balance'] - $stats['Payment']['balance'];
|
||||
// the balances of charges and disbursements
|
||||
$stats['balance'] = $stats['Charge']['balance'] - $stats['Disbursement']['balance'];
|
||||
if (!isset($stats['balance']))
|
||||
$stats['balance'] = 0;
|
||||
|
||||
// 'account_balance' is really only relevant to
|
||||
// callers that have requested charge and payment
|
||||
// callers that have requested charge and disbursement
|
||||
// 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'];
|
||||
$stats['account_balance'] = $stats['Charge']['paid'] - $stats['Disbursement']['total'];
|
||||
if (!isset($stats['account_balance']))
|
||||
$stats['account_balance'] = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user