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:
@@ -956,7 +956,6 @@ CREATE TABLE `pmgr_transactions` (
|
|||||||
'CLOSE',
|
'CLOSE',
|
||||||
-- 'CREDIT',
|
-- 'CREDIT',
|
||||||
-- 'REFUND',
|
-- 'REFUND',
|
||||||
-- 'WAIVER',
|
|
||||||
'TRANSFER')
|
'TRANSFER')
|
||||||
NOT NULL,
|
NOT NULL,
|
||||||
|
|
||||||
@@ -1065,10 +1064,10 @@ CREATE TABLE `pmgr_statement_entries` (
|
|||||||
`type` ENUM('CHARGE',
|
`type` ENUM('CHARGE',
|
||||||
'PAYMENT',
|
'PAYMENT',
|
||||||
'SURPLUS',
|
'SURPLUS',
|
||||||
|
'WAIVER',
|
||||||
-- REVISIT <AP>: 20090730
|
-- REVISIT <AP>: 20090730
|
||||||
-- VOID is just to test out a theory
|
-- VOID is used for handling NSF and perhaps charge reversals.
|
||||||
-- for handling NSF and charge reversals
|
-- It's not clear this is the best way to handle these things.
|
||||||
'WAIVE',
|
|
||||||
'VOID')
|
'VOID')
|
||||||
NOT NULL,
|
NOT NULL,
|
||||||
|
|
||||||
|
|||||||
@@ -222,109 +222,13 @@ class LeasesController extends AppController {
|
|||||||
* to prevent feature overload on the receipt page.
|
* to prevent feature overload on the receipt page.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function apply_deposit($id = null) {
|
function apply_deposit($id) {
|
||||||
// Create some models for convenience
|
$this->Lease->releaseSecurityDeposits($id);
|
||||||
$A = new Account();
|
pr("PREVENTING REDIRECT");
|
||||||
|
$this->render('fake');
|
||||||
if ($this->data) {
|
$this->redirect(array('controller' => 'leases',
|
||||||
// Handle the move out based on the data given
|
'action' => 'view',
|
||||||
pr($this->data);
|
$id));
|
||||||
$this->Lease->releaseSecurityDeposits($this->data['Lease']['id']);
|
|
||||||
die();
|
|
||||||
|
|
||||||
// Assume this will succeed
|
|
||||||
$ret = true;
|
|
||||||
|
|
||||||
// Go through the entered payments
|
|
||||||
$receipt_transaction = array_intersect_key($this->data,
|
|
||||||
array('Transaction'=>1,
|
|
||||||
'transaction_id'=>1));
|
|
||||||
foreach ($data['StatementEntry'] AS $entry) {
|
|
||||||
// Create the receipt entry, and reconcile the credit side
|
|
||||||
// of the double-entry (which should be A/R) as a payment.
|
|
||||||
$ids = $this->StatementEntry->Ledger->Account->postLedgerEntry
|
|
||||||
($receipt_transaction,
|
|
||||||
array_intersect_key($entry, array('MonetarySource'=>1))
|
|
||||||
+ array_intersect_key($entry, array('account_id'=>1)),
|
|
||||||
array('debit_ledger_id' => $A->currentLedgerID($entry['account_id']),
|
|
||||||
'credit_ledger_id' => $A->currentLedgerID($A->receiptAccountID()),
|
|
||||||
'customer_id' => $customer_id,
|
|
||||||
'lease_id' => $lease_id)
|
|
||||||
+ $entry,
|
|
||||||
'receipt');
|
|
||||||
|
|
||||||
if ($ids['error'])
|
|
||||||
$ret = false;
|
|
||||||
|
|
||||||
$db = &$model->getDataSource();
|
|
||||||
$fields = $db->fields($model, $model->alias);
|
|
||||||
$fields[] = ("SUM(IF(Account.id IS NULL, 0," .
|
|
||||||
" IF(LedgerEntry.debit_ledger_id = Account.id," .
|
|
||||||
" 1, -1))" .
|
|
||||||
" * IF(LedgerEntry.amount IS NULL, 0, LedgerEntry.amount))" .
|
|
||||||
" AS 'balance'");
|
|
||||||
return $fields;
|
|
||||||
$receipt_transaction = array_intersect_key($ids,
|
|
||||||
array('transaction_id'=>1,
|
|
||||||
'split_transaction_id'=>1));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->Lease->moveOut($this->data['Lease']['id'],
|
|
||||||
'VACANT',
|
|
||||||
$this->data['Lease']['moveout_date'],
|
|
||||||
//true // Close this lease, if able
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->redirect(array('controller' => 'leases',
|
|
||||||
'action' => 'view',
|
|
||||||
$this->data['Lease']['id']));
|
|
||||||
$this->autoRender = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$A = new Account();
|
|
||||||
|
|
||||||
$lease = $this->Lease->find
|
|
||||||
('first', array
|
|
||||||
('contain' => array
|
|
||||||
(// Models
|
|
||||||
'Unit' =>
|
|
||||||
array('order' => array('sort_order'),
|
|
||||||
'fields' => array('id', 'name'),
|
|
||||||
),
|
|
||||||
|
|
||||||
'Customer' =>
|
|
||||||
array('fields' => array('id', 'name'),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
|
|
||||||
'conditions' => array(array('Lease.id' => $id),
|
|
||||||
array('Lease.close_date' => null),
|
|
||||||
),
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
// Get the lease balance, part of lease stats
|
|
||||||
$this->Lease->statsMerge($lease['Lease'],
|
|
||||||
array('stats' => $this->Lease->stats($id)));
|
|
||||||
|
|
||||||
// Determine the lease security deposit
|
|
||||||
$deposit_balance = $this->Lease->securityDeposits($lease['Lease']['id']);
|
|
||||||
$this->set(compact('deposit_balance'));
|
|
||||||
$this->set('customer', $lease['Customer']);
|
|
||||||
$this->set('unit', $lease['Unit']);
|
|
||||||
$this->set('lease', $lease['Lease']);
|
|
||||||
$this->set('account', array('id' => $A->securityDepositAccountID()));
|
|
||||||
|
|
||||||
/* $redirect = array('controller' => 'leases', */
|
|
||||||
/* 'action' => 'view', */
|
|
||||||
/* $id); */
|
|
||||||
|
|
||||||
$title = ('Lease #' . $lease['Lease']['number'] . ': ' .
|
|
||||||
$lease['Unit']['name'] . ': ' .
|
|
||||||
$lease['Customer']['name'] . ': Utilize Security Deposit');
|
|
||||||
$this->set(compact('title', 'redirect'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -81,12 +81,7 @@ class Customer extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['Customer']))
|
$query['conditions'][] = array('StatementEntry.customer_id' => $id);
|
||||||
$query['link']['Customer'] = array();
|
|
||||||
if (!isset($query['link']['Customer']['fields']))
|
|
||||||
$query['link']['Customer']['fields'] = array();
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Customer.id' => $id);
|
|
||||||
$query['conditions'][] = array('StatementEntry.account_id' =>
|
$query['conditions'][] = array('StatementEntry.account_id' =>
|
||||||
$this->StatementEntry->Account->securityDepositAccountID());
|
$this->StatementEntry->Account->securityDepositAccountID());
|
||||||
|
|
||||||
@@ -106,18 +101,12 @@ class Customer extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['Customer']))
|
$query['conditions'][] = array('StatementEntry.customer_id' => $id);
|
||||||
$query['link']['Customer'] = array();
|
|
||||||
if (!isset($query['link']['Customer']['fields']))
|
|
||||||
$query['link']['Customer']['fields'] = array();
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Customer.id' => $id);
|
|
||||||
$query['conditions'][] = array('StatementEntry.account_id' =>
|
$query['conditions'][] = array('StatementEntry.account_id' =>
|
||||||
$this->StatementEntry->Account->securityDepositAccountID());
|
$this->StatementEntry->Account->securityDepositAccountID());
|
||||||
|
|
||||||
$stats = $this->StatementEntry->stats(null, $query);
|
$stats = $this->StatementEntry->stats(null, $query);
|
||||||
$balance = $stats['Charge']['reconciled'] - $stats['Payment']['reconciled'];
|
return $this->prReturn($stats['account_balance']);
|
||||||
return $this->prReturn($balance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -132,19 +121,7 @@ class Customer extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['Customer']))
|
$query['conditions'][] = array('StatementEntry.customer_id' => $id);
|
||||||
$query['link']['Customer'] = array();
|
|
||||||
if (!isset($query['link']['Customer']['fields']))
|
|
||||||
$query['link']['Customer']['fields'] = array();
|
|
||||||
/* if (!isset($query['link']['StatementEntry'])) */
|
|
||||||
/* $query['link']['StatementEntry'] = array(); */
|
|
||||||
/* if (!isset($query['link']['StatementEntry']['Customer'])) */
|
|
||||||
/* $query['link']['StatementEntry']['Customer'] = array(); */
|
|
||||||
/* if (!isset($query['link']['StatementEntry']['Customer']['fields'])) */
|
|
||||||
/* $query['link']['StatementEntry']['Customer']['fields'] = array(); */
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Customer.id' => $id);
|
|
||||||
|
|
||||||
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, true);
|
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, true);
|
||||||
return $this->prReturn($set);
|
return $this->prReturn($set);
|
||||||
}
|
}
|
||||||
@@ -161,15 +138,7 @@ class Customer extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['StatementEntry']))
|
$query['conditions'][] = array('StatementEntry.customer_id' => $id);
|
||||||
$query['link']['StatementEntry'] = array();
|
|
||||||
/* if (!isset($query['link']['StatementEntry']['Customer'])) */
|
|
||||||
/* $query['link']['StatementEntry']['Customer'] = array(); */
|
|
||||||
/* if (!isset($query['link']['StatementEntry']['Customer']['fields'])) */
|
|
||||||
/* $query['link']['StatementEntry']['Customer']['fields'] = array(); */
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Customer.id' => $id);
|
|
||||||
|
|
||||||
$set = $this->StatementEntry->reconciledSet('PAYMENT', $query, true);
|
$set = $this->StatementEntry->reconciledSet('PAYMENT', $query, true);
|
||||||
return $this->prReturn($set);
|
return $this->prReturn($set);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class Lease extends AppModel {
|
|||||||
'StatementEntry',
|
'StatementEntry',
|
||||||
);
|
);
|
||||||
|
|
||||||
var $default_log_level = array('log' => 30, 'show' => 15);
|
//var $default_log_level = array('log' => 30, 'show' => 15);
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
@@ -24,21 +24,11 @@ class Lease extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['Lease']))
|
$query['conditions'][] = array('StatementEntry.lease_id' => $id);
|
||||||
$query['link']['Lease'] = array();
|
|
||||||
if (!isset($query['link']['Lease']['fields']))
|
|
||||||
$query['link']['Lease']['fields'] = array();
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Lease.id' => $id);
|
|
||||||
$query['conditions'][] = array('StatementEntry.account_id' =>
|
$query['conditions'][] = array('StatementEntry.account_id' =>
|
||||||
$this->StatementEntry->Account->securityDepositAccountID());
|
$this->StatementEntry->Account->securityDepositAccountID());
|
||||||
|
|
||||||
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, false, true);
|
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, false, true);
|
||||||
|
|
||||||
/* $set['summary'] = array('total' => $set['summary']['Charge']['total'], */
|
|
||||||
/* 'balance' => $set['summary']['Charge']['reconciled'], */
|
|
||||||
/* ); */
|
|
||||||
|
|
||||||
return $this->prReturn($set);
|
return $this->prReturn($set);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,18 +44,12 @@ class Lease extends AppModel {
|
|||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'query'));
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (!isset($query['link']['Lease']))
|
$query['conditions'][] = array('StatementEntry.lease_id' => $id);
|
||||||
$query['link']['Lease'] = array();
|
|
||||||
if (!isset($query['link']['Lease']['fields']))
|
|
||||||
$query['link']['Lease']['fields'] = array();
|
|
||||||
|
|
||||||
$query['conditions'][] = array('Lease.id' => $id);
|
|
||||||
$query['conditions'][] = array('StatementEntry.account_id' =>
|
$query['conditions'][] = array('StatementEntry.account_id' =>
|
||||||
$this->StatementEntry->Account->securityDepositAccountID());
|
$this->StatementEntry->Account->securityDepositAccountID());
|
||||||
|
|
||||||
$stats = $this->StatementEntry->stats(null, $query);
|
$stats = $this->StatementEntry->stats(null, $query);
|
||||||
$balance = $stats['Charge']['reconciled'] - $stats['Payment']['reconciled'];
|
return $this->prReturn($stats['account_balance']);
|
||||||
return $this->prReturn($balance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -78,9 +62,9 @@ class Lease extends AppModel {
|
|||||||
* to pay outstanding customer charges, or simply to become
|
* to pay outstanding customer charges, or simply to become
|
||||||
* a customer surplus (customer credit).
|
* a customer surplus (customer credit).
|
||||||
*/
|
*/
|
||||||
function releaseSecurityDeposits($id, $query = null) {
|
function releaseSecurityDeposits($id, $stamp = null, $query = null) {
|
||||||
$this->prFunctionLevel(30);
|
//$this->prFunctionLevel(30);
|
||||||
$this->prEnter(compact('id', 'query'));
|
$this->prEnter(compact('id', 'stamp', 'query'));
|
||||||
|
|
||||||
$secdeps = $this->securityDeposits($id, $query);
|
$secdeps = $this->securityDeposits($id, $query);
|
||||||
$secdeps = $secdeps['entries'];
|
$secdeps = $secdeps['entries'];
|
||||||
@@ -93,6 +77,7 @@ class Lease extends AppModel {
|
|||||||
|
|
||||||
// Build a transaction
|
// Build a transaction
|
||||||
$release = array('Transaction' => array(), 'Entry' => array());
|
$release = array('Transaction' => array(), 'Entry' => array());
|
||||||
|
$release['Transaction']['stamp'] = $stamp;
|
||||||
$release['Transaction']['comment'] = "Security Deposit Release";
|
$release['Transaction']['comment'] = "Security Deposit Release";
|
||||||
foreach ($secdeps AS $charge) {
|
foreach ($secdeps AS $charge) {
|
||||||
if ($charge['StatementEntry']['type'] !== 'CHARGE')
|
if ($charge['StatementEntry']['type'] !== 'CHARGE')
|
||||||
@@ -102,7 +87,7 @@ class Lease extends AppModel {
|
|||||||
// any unpaid (or only partially paid) security deposit should
|
// any unpaid (or only partially paid) security deposit should
|
||||||
// have the remaining balance simply waived.
|
// have the remaining balance simply waived.
|
||||||
if ($charge['StatementEntry']['balance'] > 0)
|
if ($charge['StatementEntry']['balance'] > 0)
|
||||||
$this->StatementEntry->waive($charge['StatementEntry']['id']);
|
$this->StatementEntry->waive($charge['StatementEntry']['id'], $stamp);
|
||||||
|
|
||||||
$release['Entry'][] =
|
$release['Entry'][] =
|
||||||
array('amount' => $charge['StatementEntry']['reconciled'],
|
array('amount' => $charge['StatementEntry']['reconciled'],
|
||||||
@@ -112,7 +97,7 @@ class Lease extends AppModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$customer_id = $secdeps[0]['StatementEntry']['customer_id'];
|
$customer_id = $secdeps[0]['StatementEntry']['customer_id'];
|
||||||
$lease_id = $secdeps[0]['StatementEntry']['lease_id'];
|
$lease_id = $secdeps[0]['StatementEntry']['lease_id'];
|
||||||
|
|
||||||
$result = $this->StatementEntry->Transaction->addReceipt
|
$result = $this->StatementEntry->Transaction->addReceipt
|
||||||
($release, $customer_id, $lease_id);
|
($release, $customer_id, $lease_id);
|
||||||
@@ -377,6 +362,9 @@ class Lease extends AppModel {
|
|||||||
// Save it!
|
// Save it!
|
||||||
$this->save($this->data, false);
|
$this->save($this->data, false);
|
||||||
|
|
||||||
|
// Release the security deposit(s)
|
||||||
|
$this->releaseSecurityDeposits($id, $stamp);
|
||||||
|
|
||||||
// Close the lease, if so requested
|
// Close the lease, if so requested
|
||||||
if ($close)
|
if ($close)
|
||||||
$this->close($id, $stamp);
|
$this->close($id, $stamp);
|
||||||
@@ -477,40 +465,8 @@ class Lease extends AppModel {
|
|||||||
return $this->prReturn(null);
|
return $this->prReturn(null);
|
||||||
|
|
||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
|
|
||||||
//$query['link'] = array('Lease' => $query['link']);
|
|
||||||
/* if (!isset($query['link']['StatementEntry'])) */
|
|
||||||
/* $query['link']['StatementEntry'] = array(); */
|
|
||||||
/* if (!isset($query['link']['StatementEntry']['ChargeEntry'])) */
|
|
||||||
/* $query['link']['StatementEntry']['ChargeEntry'] = array(); */
|
|
||||||
|
|
||||||
/* $query['link']['StatementEntry']['fields'] = array(); */
|
|
||||||
/* $query['link']['ChargeEntry']['fields'] = array(); */
|
|
||||||
/* $query['link']['ChargeEntry']['Account']['fields'] = array(); */
|
|
||||||
/* $query['link']['ChargeEntry']['StatementEntry']['fields'] = array(); */
|
|
||||||
/* $query['link']['ChargeEntry']['StatementEntry']['Invoice']['fields'] = array(); */
|
|
||||||
|
|
||||||
if (!isset($query['fields']))
|
|
||||||
$query['fields'] = array();
|
|
||||||
|
|
||||||
$query['fields'] = array_merge($query['fields'],
|
|
||||||
$this->StatementEntry->chargePaymentFields(true));
|
|
||||||
|
|
||||||
$query['conditions'][] = array('StatementEntry.lease_id' => $id);
|
$query['conditions'][] = array('StatementEntry.lease_id' => $id);
|
||||||
|
$stats = $this->StatementEntry->stats(null, $query);
|
||||||
$query['group'] = null;
|
|
||||||
|
|
||||||
$stats = $this->StatementEntry->find('first', $query);
|
|
||||||
//$this->pr(20, compact('query', 'stats'));
|
|
||||||
|
|
||||||
// The fields are all tucked into the [0] index,
|
|
||||||
// and the rest of the array is useless (empty).
|
|
||||||
$stats = $stats[0];
|
|
||||||
|
|
||||||
// Make sure we have a non-null balance
|
|
||||||
if (!isset($stats['balance']))
|
|
||||||
$stats['balance'] = 0;
|
|
||||||
|
|
||||||
return $this->prReturn($stats);
|
return $this->prReturn($stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 ? ')' : '') . ' AS charge' . ($sum ? 's' : ''),
|
||||||
|
|
||||||
($sum ? 'SUM(' : '') .
|
($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')," .
|
"IF({$entry_name}.type NOT IN('CHARGE', 'VOID')," .
|
||||||
" {$entry_name}.amount, NULL)" .
|
" {$entry_name}.amount, NULL)" .
|
||||||
($sum ? ')' : '') . ' AS payment' . ($sum ? 's' : ''),
|
($sum ? ')' : '') . ' AS payment' . ($sum ? 's' : ''),
|
||||||
|
|
||||||
($sum ? 'SUM(' : '') .
|
($sum ? 'SUM(' : '') .
|
||||||
//"IF({$entry_name}.type = 'CHARGE', 1," .
|
//"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 = 'VOID', 0," .
|
||||||
" IF({$entry_name}.type = 'CHARGE', 1, -1))" .
|
" IF({$entry_name}.type = 'CHARGE', 1, -1))" .
|
||||||
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
||||||
@@ -377,10 +377,13 @@ OPTION 2
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function assignCredits($query = null, $receipt_id = null,
|
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->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);
|
$this->queryInit($query);
|
||||||
|
|
||||||
if (empty($payment_type))
|
if (empty($payment_type))
|
||||||
@@ -570,7 +573,8 @@ OPTION 2
|
|||||||
'amount' => $credit['balance'],
|
'amount' => $credit['balance'],
|
||||||
'effective_date' => $credit['Transaction']['stamp'],
|
'effective_date' => $credit['Transaction']['stamp'],
|
||||||
'transaction_id' => $credit['Transaction']['id'],
|
'transaction_id' => $credit['Transaction']['id'],
|
||||||
'customer_id' => $credit['Customer']['id'],
|
'customer_id' => $customer_id,
|
||||||
|
'lease_id' => $lease_id,
|
||||||
));
|
));
|
||||||
$ret['Credit'] = $result;
|
$ret['Credit'] = $result;
|
||||||
if ($result['error'])
|
if ($result['error'])
|
||||||
@@ -597,92 +601,95 @@ OPTION 2
|
|||||||
if (isset($id))
|
if (isset($id))
|
||||||
$query['conditions'][] = array('StatementEntry.id' => $id);
|
$query['conditions'][] = array('StatementEntry.id' => $id);
|
||||||
|
|
||||||
$rquery = $query;
|
// Determine the total in charges
|
||||||
unset($rquery['link']['ChargeEntry']);
|
$charge_query = $query;
|
||||||
$rquery['link']['PaymentEntry'] = array('fields' => array());
|
unset($charge_query['link']['ChargeEntry']);
|
||||||
|
unset($charge_query['link']['PaymentEntry']);
|
||||||
|
|
||||||
$rquery['fields'] = array();
|
$charge_query['fields'] = array();
|
||||||
$rquery['fields'][] = "StatementEntry.amount";
|
$charge_query['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||||
$rquery['fields'][] = "SUM(PaymentEntry.amount) AS reconciled";
|
$charge_query['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
||||||
|
$result = $this->find('first', $charge_query);
|
||||||
|
$stats['Charge'] = $result[0];
|
||||||
|
|
||||||
$rquery['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
$this->pr(17, compact('charge_query', 'result'),
|
||||||
$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'),
|
|
||||||
'Charges');
|
'Charges');
|
||||||
|
|
||||||
$rquery = $query;
|
// Tally the amount actually _paid_ to those charges
|
||||||
unset($rquery['link']['ChargeEntry']);
|
$charge_payment_query = $charge_query;
|
||||||
$rquery['link']['PaymentEntry'] = array('fields' => array(),
|
$charge_payment_query['link']['PaymentEntry'] = array('fields' => array());
|
||||||
/* 'conditions' => */
|
$charge_payment_query['fields'] = array();
|
||||||
/* array('PaymentEntry.type' => 'WAIVE'), */
|
$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);
|
||||||
$rquery['fields'] = array();
|
$stats['Charge'] += $result[0];
|
||||||
$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']; */
|
|
||||||
|
|
||||||
$this->pr(17, compact('rquery', 'result'),
|
$this->pr(17, compact('charge_payment_query', 'result'),
|
||||||
'Waived');
|
'Charge Payments');
|
||||||
|
|
||||||
$rquery = $query;
|
// Tally the amount of charges that have been waived
|
||||||
unset($rquery['link']['PaymentEntry']);
|
$charge_waiver_query = $charge_query;
|
||||||
$rquery['link']['ChargeEntry'] = array('fields' => array());
|
$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();
|
// Compute some summary information for charges
|
||||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
$stats['Charge']['reconciled'] =
|
||||||
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, 0, StatementEntry.amount)) AS reconciled";
|
$stats['Charge']['paid'] + $stats['Charge']['waived'];
|
||||||
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, StatementEntry.amount, 0)) AS balance";
|
$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);
|
// Determine the total in payments, including those which
|
||||||
if (!isset($result[0]['balance']))
|
// are charge waivers and those that do not even reconcile
|
||||||
$result[0]['balance'] = 0;
|
// 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];
|
$stats['Payment'] = $result[0];
|
||||||
|
|
||||||
$this->pr(17, compact('rquery', 'result'),
|
$this->pr(17, compact('payment_query', 'result'),
|
||||||
'Payments');
|
'Payments');
|
||||||
|
|
||||||
$rquery = $query;
|
// Compute some summary information for payments.
|
||||||
unset($rquery['link']['PaymentEntry']);
|
// Add a reconciled field just for consistency with Charge.
|
||||||
unset($rquery['link']['ChargeEntry']);
|
$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();
|
// 'balance' is simply the difference between
|
||||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
// the balances of charges and payments
|
||||||
$rquery['fields'][] = "SUM(0) AS reconciled";
|
$stats['balance'] = $stats['Charge']['balance'] - $stats['Payment']['balance'];
|
||||||
|
if (!isset($stats['balance']))
|
||||||
$rquery['conditions'][] = array('StatementEntry.type' => 'SURPLUS');
|
$stats['balance'] = 0;
|
||||||
$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');
|
|
||||||
|
|
||||||
|
// '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);
|
return $this->prReturn($stats);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class Transaction extends AppModel {
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
var $default_log_level = array('log' => 30, 'show' => 15);
|
//var $default_log_level = array('log' => 30, 'show' => 15);
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
@@ -127,7 +127,7 @@ class Transaction extends AppModel {
|
|||||||
|
|
||||||
// Just make sure the payment(s) are marked as waivers
|
// Just make sure the payment(s) are marked as waivers
|
||||||
// and that they go to cover the specific charge.
|
// and that they go to cover the specific charge.
|
||||||
$data['Transaction']['payment_type'] = 'WAIVE';
|
$data['Transaction']['payment_type'] = 'WAIVER';
|
||||||
$data['Transaction']['charge_entry_id'] = $charge_id;
|
$data['Transaction']['charge_entry_id'] = $charge_id;
|
||||||
|
|
||||||
// In all other respects this is just a receipt.
|
// In all other respects this is just a receipt.
|
||||||
@@ -521,7 +521,9 @@ class Transaction extends AppModel {
|
|||||||
: null),
|
: null),
|
||||||
(!empty($transaction['payment_type'])
|
(!empty($transaction['payment_type'])
|
||||||
? $transaction['payment_type']
|
? $transaction['payment_type']
|
||||||
: null)
|
: null),
|
||||||
|
$transaction['customer_id'],
|
||||||
|
$transaction['lease_id']
|
||||||
);
|
);
|
||||||
|
|
||||||
$ret['assigned'] = $result;
|
$ret['assigned'] = $result;
|
||||||
|
|||||||
Reference in New Issue
Block a user