diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 38d608d..9d5e813 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -203,8 +203,7 @@ class CustomersController extends AppController { //pr($customer); // Figure out the outstanding balances for this customer - $stats = $this->Customer->stats($id); - $outstanding_balance = $stats['balance']; + $outstanding_balance = $this->Customer->balance($id); $outstanding_deposit = $this->Customer->securityDepositBalance($id); // Figure out if this customer has any non-closed leases @@ -407,7 +406,7 @@ class CustomersController extends AppController { )); pr(compact('entries')); - $this->Customer->StatementEntry->reverse($entries); + $this->Customer->refund($entries); } diff --git a/controllers/leases_controller.php b/controllers/leases_controller.php index edc1231..75d6474 100644 --- a/controllers/leases_controller.php +++ b/controllers/leases_controller.php @@ -239,12 +239,14 @@ class LeasesController extends AppController { */ function refund($id) { - // Obtain the overall lease balance - $stats = $this->Lease->stats($id); - $outstanding_balance = $stats['balance']; + $this->Lease->refund($id); + $this->render('/fake'); +/* // Obtain the overall lease balance */ +/* $stats = $this->Lease->stats($id); */ +/* $outstanding_balance = $stats['balance']; */ - $this->set(compact('lease', 'title', - 'outstanding_balance')); +/* $this->set(compact('lease', 'title', */ +/* 'outstanding_balance')); */ } @@ -381,13 +383,9 @@ class LeasesController extends AppController { $this->set('charge_gaps', $this->Lease->rentChargeGaps($id)); $this->set('charge_through', $this->Lease->rentChargeThrough($id)); - // Obtain the overall lease balance - $this->Lease->statsMerge($lease['Lease'], - array('stats' => $this->Lease->stats($id))); - $outstanding_balance = $lease['Lease']['stats']['balance']; - - // Determine the lease security deposit - $outstanding_deposit = $this->Lease->securityDepositBalance($lease['Lease']['id']); + // Figure out the outstanding balances for this lease + $outstanding_balance = $this->Lease->balance($id); + $outstanding_deposit = $this->Lease->securityDepositBalance($id); // Set up dynamic menu items if (!isset($lease['Lease']['close_date'])) { diff --git a/models/account.php b/models/account.php index b32844c..c841db1 100644 --- a/models/account.php +++ b/models/account.php @@ -128,6 +128,7 @@ class Account extends AppModel { function nsfChargeAccountID() { return $this->nameToID('NSF Charge'); } function taxAccountID() { return $this->nameToID('Tax'); } function accountReceivableAccountID() { return $this->nameToID('A/R'); } + function accountPayableAccountID() { return $this->nameToID('A/P'); } function cashAccountID() { return $this->nameToID('Cash'); } function checkAccountID() { return $this->nameToID('Check'); } function moneyOrderAccountID() { return $this->nameToID('Money Order'); } diff --git a/models/customer.php b/models/customer.php index aa01afd..42ac10e 100644 --- a/models/customer.php +++ b/models/customer.php @@ -127,49 +127,6 @@ class Customer extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: details - * - Returns detail information for the customer - */ - - function details($id = null) { - $this->prEnter(compact('id')); - - // Query the DB for need information. - $customer = $this->find - ('first', array - ('contain' => array - (// Models - 'Contact' => - array('order' => array('Contact.display_name'), - // Models - 'ContactPhone', - 'ContactEmail', - 'ContactAddress', - ), - 'Lease' => - array('Unit' => - array('order' => array('sort_order'), - 'fields' => array('id', 'name'), - ), - ), - ), - - 'conditions' => array('Customer.id' => $id), - )); - - // Figure out the outstanding balance for this customer - $customer['stats'] = $this->stats($id); - - // Figure out the total security deposit for the current lease. - $customer['deposits'] = $this->securityDeposits($id); - - return $this->prReturn($customer); - } - - /************************************************************************** ************************************************************************** ************************************************************************** @@ -271,6 +228,19 @@ class Customer extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: balance + * - Returns the balance of money owed on the lease + */ + + function balance($id) { + $stats = $this->stats($id); + return $stats['balance']; + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/lease.php b/models/lease.php index 42a7195..1b72947 100644 --- a/models/lease.php +++ b/models/lease.php @@ -464,6 +464,51 @@ class Lease extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: refund + * - Marks any lease balance as payable to the customer. + */ + + function refund($id, $stamp = null) { + $this->prEnter(compact('id')); + $balance = $this->balance($id); + + if ($balance >= 0) + return $this->prReturn(array('error' => true)); + + $balance *= -1; + + // Build a transaction + $refund = array('Transaction' => array(), 'Entry' => array()); + $refund['Transaction']['stamp'] = $stamp; + $refund['Transaction']['comment'] = "Lease Refund"; + + $refund['Entry'][] = + array('amount' => $balance); + + $result = $this->StatementEntry->Transaction->addRefund + ($refund, null, $id); + + return $this->prReturn($result); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: balance + * - Returns the balance of money owed on the lease + */ + + function balance($id) { + $this->prEnter(compact('id')); + $stats = $this->stats($id); + return $this->prReturn($stats['balance']); + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/statement_entry.php b/models/statement_entry.php index 6495f72..ef02fdc 100644 --- a/models/statement_entry.php +++ b/models/statement_entry.php @@ -24,29 +24,54 @@ class StatementEntry extends AppModel { //var $default_log_level = array('log' => 30, 'show' => 15); + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: debit/creditTypes + */ + + function debitTypes() { + return array('CHARGE', 'VOUCHER'); + } + + function creditTypes() { + return array('DISBURSEMENT', 'WAIVER', 'SURPLUS'); + } + + /************************************************************************** ************************************************************************** ************************************************************************** * function: chargeDisbursementFields */ - function chargeDisbursementFields($sum = false, $entry_name = 'StatementEntry') { + $charges = array('CHARGE', 'VOUCHER'); + $nulls = array('PAYMENT', 'VOID'); + + foreach ($charges AS &$enum) + $enum = "'" . $enum . "'"; + foreach ($nulls AS &$enum) + $enum = "'" . $enum . "'"; + + $charge_set = implode(", ", $charges); + $null_set = implode(", ", $nulls); + $fields = array ( ($sum ? 'SUM(' : '') . - "IF({$entry_name}.type = 'CHARGE'," . + "IF({$entry_name}.type IN ({$charge_set})," . " {$entry_name}.amount, NULL)" . ($sum ? ')' : '') . ' AS charge' . ($sum ? 's' : ''), ($sum ? 'SUM(' : '') . - "IF({$entry_name}.type NOT IN('CHARGE', 'PAYMENT', 'VOID')," . + "IF({$entry_name}.type NOT IN({$charge_set}, ${null_set})," . " {$entry_name}.amount, NULL)" . ($sum ? ')' : '') . ' AS disbursement' . ($sum ? 's' : ''), ($sum ? 'SUM(' : '') . - "IF({$entry_name}.type IN ('VOID', 'PAYMENT'), 0," . - " IF({$entry_name}.type = 'CHARGE', 1, -1))" . + "IF({$entry_name}.type IN ({$null_set}), 0," . + " IF({$entry_name}.type IN ({$charge_set}), 1, -1))" . " * IF({$entry_name}.amount, {$entry_name}.amount, 0)" . ($sum ? ')' : '') . ' AS balance', ); @@ -602,7 +627,7 @@ class StatementEntry extends AppModel { $charge_query['fields'] = array(); $charge_query['fields'][] = "SUM(StatementEntry.amount) AS total"; - $charge_query['conditions'][] = array('StatementEntry.type' => 'CHARGE'); + $charge_query['conditions'][] = array('StatementEntry.type' => array('CHARGE', 'VOUCHER')); $result = $this->find('first', $charge_query); $stats['Charge'] = $result[0]; @@ -688,4 +713,4 @@ class StatementEntry extends AppModel { return $this->prReturn($stats); } -} +} \ No newline at end of file diff --git a/models/transaction.php b/models/transaction.php index 8cb3666..f7df39f 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -332,17 +332,17 @@ class Transaction extends AppModel { $refund =& $data['Transaction']; $refund += array('type' => 'CREDIT_NOTE', - 'crdr' => 'CREDIT', - 'account_id' => $this->Account->accountPayableAccountID(), + 'crdr' => 'DEBIT', + 'account_id' => $this->Account->accountReceivableAccountID(), 'customer_id' => $customer_id, 'lease_id' => $lease_id, ); // Go through the statement entries and flag as vouchers foreach ($data['Entry'] AS &$entry) - $entry += array('type' => 'VOUCHER', - 'crdr' => 'DEBIT', - 'account_id' => $this->Account->accountReceivableAccountID(), + $entry += array('type' => 'VOUCHER', + 'crdr' => 'CREDIT', + 'account_id' => $this->Account->accountPayableAccountID(), ); $ids = $this->addTransaction($data['Transaction'], $data['Entry']); @@ -575,9 +575,9 @@ class Transaction extends AppModel { // (DISBURSEMENTS will have statement entries created below, when // assigning credits, and DEPOSITS don't have statement entries) - if (($transaction['account_id'] != $this->Account->accountReceivableAccountID() && - $transaction['account_id'] != $this->Account->nsfAccountID()) || - $transaction['crdr'] != 'DEBIT') + if (empty($transaction['customer_id']) || + ($transaction['account_id'] == $this->Account->accountReceivableAccountID() && + $transaction['crdr'] == 'CREDIT')) $se = null; // NSF transactions don't use LedgerEntries