diff --git a/models/lease.php b/models/lease.php index 0e4c924..4d83e46 100644 --- a/models/lease.php +++ b/models/lease.php @@ -12,6 +12,7 @@ class Lease extends AppModel { 'StatementEntry', ); + var $default_log_level = array('log' => 30, 'show' => 15); /************************************************************************** ************************************************************************** @@ -85,9 +86,6 @@ class Lease extends AppModel { $secdeps = $secdeps['entries']; $this->pr(20, compact('secdeps')); - $this->securityDepositBalance($id, $query); - die(); - // If there are no paid security deposits, then // we can consider all security deposits released. if (count($secdeps) == 0) @@ -101,17 +99,10 @@ class Lease extends AppModel { die("INTERNAL ERROR: SECURITY DEPOSIT IS NOT CHARGE"); // Since security deposits are being released, this also means - // we're reducing any oustanding amount on a security deposit - // since we no longer expect it to be owed. - // REVISIT : 20090730 - // This is kludgy, and I really don't like it. However, this - // is not presently something that even happens at the moment, - // so this solution will have to work until we come up with - // something more robust, like flagging those charges as defunct. - if ($charge['StatementEntry']['balance'] > 0) { - $this->StatementEntry->id = $charge['StatementEntry']['id']; - $this->StatementEntry->saveField('amount', $charge['StatementEntry']['reconciled']); - } + // any unpaid (or only partially paid) security deposit should + // have the remaining balance simply waived. + if ($charge['StatementEntry']['balance'] > 0) + $this->StatementEntry->waive($charge['StatementEntry']['id']); $release['Entry'][] = array('amount' => $charge['StatementEntry']['reconciled'], diff --git a/models/statement_entry.php b/models/statement_entry.php index 682bc32..6563eee 100644 --- a/models/statement_entry.php +++ b/models/statement_entry.php @@ -22,7 +22,7 @@ class StatementEntry extends AppModel { ); - var $default_log_level = 30; + var $default_log_level = array('log' => 30, 'show' => 15); /************************************************************************** ************************************************************************** @@ -40,13 +40,16 @@ class StatementEntry extends AppModel { ($sum ? ')' : '') . ' AS charge' . ($sum ? 's' : ''), ($sum ? 'SUM(' : '') . - "IF({$entry_name}.type = 'PAYMENT' OR {$entry_name}.type = 'SURPLUS'," . + //"IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVE')," . + "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 = 'PAYMENT' OR {$entry_name}.type = 'SURPLUS', -1, 0))" . + //"IF({$entry_name}.type = 'CHARGE', 1," . + //" IF({$entry_name}.type IN('PAYMENT', 'SURPLUS', 'WAIVE'), -1, 0))" . + "IF({$entry_name}.type = 'VOID', 0," . + " IF({$entry_name}.type = 'CHARGE', 1, -1))" . " * IF({$entry_name}.amount, {$entry_name}.amount, 0)" . ($sum ? ')' : '') . ' AS balance', ); @@ -373,11 +376,16 @@ OPTION 2 * the user to specify how payments should be applied. * */ - function assignCredits($query = null, $receipt_id = null, $charge_ids = null) { + function assignCredits($query = null, $receipt_id = null, + $charge_ids = null, $payment_type = null) + { //$this->prFunctionLevel(25); - $this->prEnter( compact('query', 'receipt_id', 'charge_ids')); + $this->prEnter( compact('query', 'receipt_id', 'charge_ids', 'payment_type')); $this->queryInit($query); + if (empty($payment_type)) + $payment_type = 'PAYMENT'; + $ret = array(); // First, find all known credits @@ -439,7 +447,7 @@ OPTION 2 // just saves extra processing if/when there is no // means to resolve a charge anyway. if (count($credits) == 0 && empty($receipt_credit['balance'])) { - $this->pr(15, 'No more available credits'); + $this->pr(17, 'No more available credits'); break; } @@ -495,7 +503,7 @@ OPTION 2 $used_credits[] = array_shift($credits); // Add a payment that uses the available credit to pay the charge - $payment = array('type' => 'PAYMENT', + $payment = array('type' => $payment_type, 'account_id' => $payment_account_id, 'amount' => $payment_amount, 'effective_date' => $payment_date, @@ -612,6 +620,33 @@ OPTION 2 $this->pr(17, compact('rquery', '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']; */ + + $this->pr(17, compact('rquery', 'result'), + 'Waived'); + $rquery = $query; unset($rquery['link']['PaymentEntry']); $rquery['link']['ChargeEntry'] = array('fields' => array()); @@ -630,6 +665,25 @@ OPTION 2 $this->pr(17, compact('rquery', 'result'), 'Payments'); + $rquery = $query; + unset($rquery['link']['PaymentEntry']); + unset($rquery['link']['ChargeEntry']); + + $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'); + + return $this->prReturn($stats); } diff --git a/models/transaction.php b/models/transaction.php index 98dbd7d..2001852 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -37,7 +37,7 @@ class Transaction extends AppModel { ); - var $default_log_level = 30; + var $default_log_level = array('log' => 30, 'show' => 15); /************************************************************************** ************************************************************************** @@ -122,14 +122,15 @@ class Transaction extends AppModel { function addWaiver($data, $charge_id, $customer_id, $lease_id = null) { $this->prEnter(compact('data', 'charge_id', 'customer_id', 'lease_id')); - // Just make sure the entries are marked as waivers... - foreach ($data['Entry'] AS &$entry) - $entry['type'] = 'WAIVE'; + if (count($data['Entry']) != 1) + die("INTERNAL ERROR: Should be one Entry for addWaiver"); - // ... and the charge statement entry is recorded... + // Just make sure the payment(s) are marked as waivers + // and that they go to cover the specific charge. + $data['Transaction']['payment_type'] = 'WAIVE'; $data['Transaction']['charge_entry_id'] = $charge_id; - // ... and in all other respects this is just a receipt. + // In all other respects this is just a receipt. $ids = $this->addReceipt($data, $customer_id, $lease_id); if (isset($ids['transaction_id'])) $ids['waiver_id'] = $ids['transaction_id']; @@ -517,6 +518,9 @@ class Transaction extends AppModel { : null), ($transaction['type'] == 'RECEIPT' && !empty($transaction['charge_entry_id']) ? $transaction['charge_entry_id'] + : null), + (!empty($transaction['payment_type']) + ? $transaction['payment_type'] : null) );