From 019b97fe5325c1d07dbc38ca9dc5628644f95ea6 Mon Sep 17 00:00:00 2001 From: abijah Date: Tue, 7 Jul 2009 07:14:18 +0000 Subject: [PATCH] Modified to dynamically determine eligible charge accounts, instead of the two that were hardcoded. git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@244 97e9348a-65ac-dc4b-aefc-98561f571b83 --- controllers/leases_controller.php | 17 ++++---- models/account.php | 70 +++++++++++++++++++++++++++++++ models/transaction.php | 18 +------- views/leases/invoice.ctp | 30 ++++--------- 4 files changed, 89 insertions(+), 46 deletions(-) diff --git a/controllers/leases_controller.php b/controllers/leases_controller.php index 7513f7c..8e80d97 100644 --- a/controllers/leases_controller.php +++ b/controllers/leases_controller.php @@ -194,13 +194,16 @@ class LeasesController extends AppController { ), )); - $charge['type'] = $type; - if ($type == 'rent') - $charge['amount'] = $lease['Lease']['rent']; - elseif ($type == 'late') - // REVISIT 20090705: - // Of course, the late charge should come from the late_schedule - $charge['amount'] = 10.00; + $A = new Account(); + $charge_accounts = $A->chargeAccounts(); + $default_account = $A->rentAccountID(); + $this->set(compact('charge_accounts', 'default_account')); + + // REVISIT 20090705: + // Of course, the late charge should come from the late_schedule + $default_rent = $lease['Lease']['rent']; + $default_late = 10; + $this->set(compact('default_rent', 'default_late')); $title = ('Lease #' . $lease['Lease']['number'] . ': ' . $lease['Unit']['name'] . ': ' . diff --git a/models/account.php b/models/account.php index da02936..48cd586 100644 --- a/models/account.php +++ b/models/account.php @@ -95,10 +95,32 @@ class Account extends AppModel { function securityDepositAccountID() { return $this->nameToID('Security Deposit'); } function rentAccountID() { return $this->nameToID('Rent'); } function lateChargeAccountID() { return $this->nameToID('Late Charge'); } + function taxAccountID() { return $this->nameToID('Tax'); } function accountReceivableAccountID() { return $this->nameToID('A/R'); } function invoiceAccountID() { return $this->nameToID('Invoice'); } function receiptAccountID() { return $this->nameToID('Receipt'); } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: fundamentalAccounts + * - Returns an array of accounts by their fundamental type + */ + + function fundamentalAccounts($ftype) { + $this->cacheQueries = true; + $account = $this->find('all', array + ('contain' => array('CurrentLedger'), + 'fields' => array('Account.id', 'Account.type', 'Account.name', 'CurrentLedger.id'), + 'conditions' => array('Account.type' => strtoupper($ftype)) + )); + $this->cacheQueries = false; + + return $account; + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -119,6 +141,54 @@ class Account extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: chargeAccounts + * - Returns an array of accounts suitable for charges + */ + + function chargeAccounts() { + // REVISIT : 20090706 + // It's not clear how to authoritatively return accounts + // that are suitable for entering charges. Clearly, INCOME + // accounts are part of the list, but there must be others + // as well. Security Deposit, for example, is clearly not + // an INCOME account. In fact, it is a LIABILITY, yet not + // all liability accounts should be part of this list + // (consider a mortgage account, for example). At the + // moment, I'll just specify the exceptions individually, + // and we'll update the algorithm when we figure out how to + // bettter genericize it. +/* $accounts = array_merge($A->fundamentalAccounts('INCOME'), */ +/* $A->fundamentalAccounts('LIABILITY')); */ +/* usort($accounts, */ +/* create_function('$a, $b', */ +/* 'return strcasecmp($a["Account"]["name"], $b["Account"]["name"]);') */ +/* ); */ + + // Find the accounts suitable for invoicing + $accounts = $this->find + ('all', array + ('contain' => false, + 'conditions' => array + ('OR' => array + (array('type' => 'INCOME'), + array('id' => $this->securityDepositAccountID()), + array('id' => $this->taxAccountID()), + ), + ), + 'order' => array('name'), + )); + + // Rearrange to be of the form (id => name) + $charge_accounts = array(); + foreach ($accounts AS $acct) { + $charge_accounts[$acct['Account']['id']] = $acct['Account']['name']; + } + + return $charge_accounts; + } /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/transaction.php b/models/transaction.php index d23ae0a..5b4543d 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -97,22 +97,8 @@ class Transaction extends AppModel { = $A->currentLedgerID($A->invoiceAccountID()); // ...and credit the charge ledger - // REVISIT 20090706 - // The charge page should have a list of all income types - // and thus the field _should_ contain a valid account - // name. At the moment, however, I'm still cobbling things - // together, and only have two types. -/* $entry['credit_ledger_id'] */ -/* = $A->currentLedgerID($A->nameToID($entry['charge_account'])); */ - if ($entry['charge_type'] === 'rent') { - $entry['credit_ledger_id'] - = $A->currentLedgerID($A->rentAccountID()); - } elseif ($entry['charge_type'] === 'late') { - $entry['credit_ledger_id'] - = $A->currentLedgerID($A->lateChargeAccountID()); - } else { - die("Invalid charge account"); - } + $entry['credit_ledger_id'] + = $A->currentLedgerID($entry['account_id']); $entry['customer_id'] = $customer_id; $entry['lease_id'] = $lease_id; diff --git a/views/leases/invoice.ctp b/views/leases/invoice.ctp index 5e44970..6f50f2c 100644 --- a/views/leases/invoice.ctp +++ b/views/leases/invoice.ctp @@ -154,29 +154,13 @@ function addChargeSource(flash) { //'with_name_after' => ':', 'field_prefix' => 'LedgerEntry.%{id}', 'fields' => array - ("charge_type" => array('opts' => - array('options' => - array('rent' => 'Rent', - 'late' => 'Late Charge', - 'secd' => 'Security Deposit'), - 'separator' => ' ', - 'type' => 'radio', - 'label' => false, - 'div' => false, - 'legend' => false, - 'value' => - (isset($charge['type']) - ? $charge['type'] - : null) - ), - ), - "amount" => array('opts' => - array('value' => - (isset($charge['amount']) - ? $charge['amount'] - : null) - ), - ), + ("account_id" => array('name' => 'Account', + 'opts' => + array('options' => $chargeAccounts, + 'value' => $defaultAccount, + ), + ), + "amount" => null, "comment" => array('opts' => array('size' => 50)), ), ))) . "+\n";