From be2865b4d7a37d8c95b8bb40984efa8907f096bb Mon Sep 17 00:00:00 2001 From: abijah Date: Thu, 30 Jul 2009 02:39:09 +0000 Subject: [PATCH] Cleanup of old lingering functions and some comment blocks git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@423 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/customers_controller.php | 2 +- site/models/account.php | 437 ---------------------- site/models/close.php | 12 - site/models/customer.php | 72 ---- site/models/ledger.php | 21 -- site/models/statement_entry.php | 72 ---- 6 files changed, 1 insertion(+), 615 deletions(-) delete mode 100644 site/models/close.php diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index efdd40d..8de75cf 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -418,7 +418,7 @@ class CustomersController extends AppController { /************************************************************************** ************************************************************************** ************************************************************************** - * action: unreconciledEntries + * action: unreconciled * - returns the list of unreconciled entries */ diff --git a/site/models/account.php b/site/models/account.php index 21c42fc..0d6a51a 100644 --- a/site/models/account.php +++ b/site/models/account.php @@ -323,412 +323,6 @@ class Account extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: findUnreconciledLedgerEntries - * - Returns ledger entries that are not yet reconciled - * (such as charges not paid). - */ - - function unreconciledEntries($id, $set, $cond = null, $link = null) { - if (!isset($cond)) - $cond = array(); - if (!isset($link)) - $link = array(); - $link['Account'] = array('fields' => array()); - $cond[] = array('Account.id' => $id); - $set = $this->Ledger->Entry->reconciledSet($set, $cond, $link, true); - //pr(compact('set')); - return $set; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: paymentWouldReconcile - * - Returns which ledger entries a new credit/debit would - * reconcile, and how much. - * - * - REVISIT 20090617 - * This should be subject to different algorithms, such - * as apply to oldest charges first, newest first, to fees - * before rent, etc. Until we get there, I'll hardcode - * whatever algorithm is simplest. - */ - - function paymentWouldReconcile($id, $amount, $cond = null, $link = null) { - $unreconciled = array($ofund => array('entry'=>array(), 'balance'=>0)); - $applied = 0; - - if ($amount <= 0) - return; - - $unreconciled = $this->unreconciledEntries($id, 'CHARGE', $cond, $link); - - foreach ($unreconciled AS $i => &$item) { - $entry =& $item['LedgerEntry']; - // Determine if amount is sufficient to cover the entry - if ($amount > $entry['balance']) - $apply = $entry['balance']; - elseif ($amount > 0) - $apply = $amount; - else { - unset($unreconciled[$i]); - continue; - } - - $entry['applied'] = $apply; - $entry['reconciled'] += $apply; - $entry['balance'] -= $apply; - $applied += $apply; - $amount -= $apply; - } - - $unreconciled['unapplied'] = $amount; - $unreconciled['applied'] = $applied; - $unreconciled['balance'] -= $applied; - return $unreconciled; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: reconcileLedgerEntries - * - Returns which ledger entries a new credit/debit would - * reconcile, and how much. - * - * - REVISIT 20090617 - * This should be subject to different algorithms, such - * as apply to oldest charges first, newest first, to fees - * before rent, etc. Until we get there, I'll hardcode - * whatever algorithm is simplest. - */ - - function reconcileLedgerEntries($id, $cond = null) { - $unreconciled = $this->findUnreconciledLedgerEntries($id, null, $cond); - //pr(compact('unreconciled')); - - $entry = array(); - foreach (array('debit', 'credit') AS $dc) - $entry[$dc] = array('balance' => 0); - - while ($entry['debit'] && $entry['credit']) { - - // If/When we've exhausted this/these entries, move the next - foreach (array('debit', 'credit') AS $dc) { - if ($entry[$dc]['balance'] <= 0) { - $entry[$dc] =& current($unreconciled[$dc]['entry']); - next($unreconciled[$dc]['entry']); - $entry[$dc]['applied'] = 0; - continue 2; - } - } - - // At this point, both entries are valid with a positive balance - $apply = min($entry['debit']['balance'], $entry['credit']['balance']); - - - // REVISIT : 20090716 - // NOT YET ENTERING THE RECONCILIATION SO THAT WE CAN TEST - // MUST ADD THE RECONCILIATION ENTRY!!!! - - foreach (array('debit', 'credit') AS $dc) { - $entry[$dc]['applied'] += $apply; - $entry[$dc]['reconciled'] += $apply; - $entry[$dc]['balance'] -= $apply; - } - } - - foreach (array('debit', 'credit') AS $dc) { - $unreconciled[$dc]['applied'] = 0; - //$unreconciled[$dc]['unapplied'] = 0; - foreach ($unreconciled[$dc]['entry'] AS $i => $entry) { - if (isset($entry[$dc]['applied'])) - $unreconciled[$dc]['applied'] += $entry[$dc]['applied']; - else - unset($unreconciled[$dc]['entry'][$i]); - - //$unreconciled[$dc]['unapplied'] += $entry[$dc]['balance']; - } - $unreconciled[$dc]['balance'] -= $unreconciled[$dc]['applied']; - } - - $unreconciled['debit'] ['unapplied'] = $unreconciled['credit']['balance']; - $unreconciled['credit']['unapplied'] = $unreconciled['debit'] ['balance']; - //pr(compact('unreconciled')); - - return $unreconciled; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: postLedgerEntry - * - - * transaction_data - * - transaction_id (optional... if set all else is ignored) - * - Transaction - * - stamp (optional... otherwise NOW is used) - * - comment - * - * monetary_source_data - * - monetary_source_id (optional... if set all else is ignored) - * - account_name - * - MonetarySource - * - name - */ - - function postLedgerEntry($transaction_data, - $monetary_data, - $entry_data, - $reconcile = null) { - //pr(compact('transaction_data', 'monetary_data', 'entry_data', 'reconcile')); - - // Automatically figure out the customer if we have the lease - if (isset($entry_data['lease_id']) && !isset($entry_data['customer_id'])) { - $L = new Lease(); - $L->recursive = -1; - $lease = $L->read(null, $entry_data['lease_id']); - $entry_data['customer_id'] = $lease['Lease']['customer_id']; - } - - if (!isset($entry_data['lease_id'])) - $entry_data['lease_id'] = null; - - if (!isset($entry_data['customer_id'])) - $entry_data['customer_id'] = null; - - // Get the Transaction squared away - if (isset($transaction_data['transaction_id'])) { - $transaction_data - = array_intersect_key($transaction_data, - array('transaction_id'=>1, - 'split_transaction_id'=>1)); - } - elseif (isset($transaction_data['Transaction'])) { - $transaction_data - = array_intersect_key($transaction_data, - array('Transaction'=>1, - 'split_transaction_id'=>1)); - } - else { - $transaction_data = array('Transaction'=>array('stamp' => null)); - } - - - // Get the Monetary Source squared away - if (isset($monetary_data)) { - if (!isset($monetary_data['monetary_source_id'])) { - - // Convert Account ID to name or vice versa - if (isset($monetary_data['account_id'])) { - $monetary_data['account_name'] = $this->name($monetary_data['account_id']); - } elseif (isset($monetary_data['account_name'])) { - $monetary_data['account_id'] = $this->nameToID($monetary_data['account_name']); - } - - if ($monetary_data['account_id'] == $this->cashAccountID()) { - // No distinguishing features of Cash, just - // use the shared monetary source - $monetary_data['monetary_source_id'] = - $this->Ledger->LedgerEntry->MonetarySource->nameToID('Cash'); - } - } - - if (isset($monetary_data['monetary_source_id'])) { - $monetary_data - = array_intersect_key($monetary_data, - array('monetary_source_id'=>1)); - } - else { - // The monetary source needs to be unique - // Create a new one dedicated to this entry - // Give it a fancy name based on the check number - $monetary_data['MonetarySource']['name'] = $monetary_data['account_name']; - if ($monetary_data['account_name'] === $this->name($this->checkAccountID()) || - $monetary_data['account_name'] === $this->name($this->moneyOrderAccountID())) { - $monetary_data['MonetarySource']['name'] .= - ' #' . $monetary_data['MonetarySource']['data1']; - } - - $monetary_data - = array_intersect_key($monetary_data, - array('MonetarySource'=>1)); - } - } - else { - $monetary_data = array(); - } - - // Make sure to clean out any unwanted data from the entry - $entry_data - = array_diff_key($entry_data, - array('transaction_id'=>1, 'Transaction'=>1, - 'monetary_source_id'=>1, 'MonetarySource'=>1)); - - // Then add in the transaction and monetary source data - //pr(compact('transaction_data', 'monetary_data', 'entry_data')); - if (isset($transaction_data)) - $entry_data += $transaction_data; - if (isset($monetary_data)) - $entry_data += $monetary_data; - - // Set up the debit ledger id - if (!isset($entry_data['debit_ledger_id'])) { - $entry_data['debit_ledger_id'] = - (isset($entry_data['debit_account_id']) - ? $this->currentLedgerID($entry_data['debit_account_id']) - : (isset($entry_data['debit_account_name']) - ? $this->currentLedgerID($this->nameToID($entry_data['debit_account_name'])) - : null - ) - ); - } - - // Set up the credit ledger id - if (!isset($entry_data['credit_ledger_id'])) { - $entry_data['credit_ledger_id'] = - (isset($entry_data['credit_account_id']) - ? $this->currentLedgerID($entry_data['credit_account_id']) - : (isset($entry_data['credit_account_name']) - ? $this->currentLedgerID($this->nameToID($entry_data['credit_account_name'])) - : null - ) - ); - } - - //pr(array('pre-save', compact('entry_data'))); - // Create it! - $new_entry = new LedgerEntry(); - $new_entry->create(); - if (!$new_entry->saveAll($entry_data, array('validate'=>false))) { - return array('error' => true); - } - - // See if the user has entered some sort of non-array - // for the reconcile parameter. - if (isset($reconcile) && is_bool($reconcile) && $reconcile) { - $reconcile = array('debit' => true, 'credit' => true); - } - elseif (isset($reconcile) && $reconcile == 'invoice') { - $reconcile = array('credit' => 'invoice'); - } - elseif (isset($reconcile) && $reconcile == 'receipt') { - $reconcile = array('debit' => 'receipt'); - } - elseif (!isset($reconcile) || !is_array($reconcile)) { - $reconcile = array(); - } - - // Reconcile the new entry... assume we'll have success - $err = false; - foreach (array_intersect_key($reconcile, array('credit'=>1,'debit'=>1)) - AS $dc_type => $reconcile_set) { - if (!isset($reconcile_set) || (is_bool($reconcile_set) && !$reconcile_set)) - continue; - - if ($reconcile_set === 'receipt') { - $C = new Customer(); - $reconciled = $C->amountWouldReconcile($entry_data['customer_id'], - $this->fundamentalOpposite($dc_type), - $entry_data['amount']); - -/* pr(array("reconcile receipt", */ -/* compact('reconciled', 'split_transaction', 'transaction_data'))); */ - $split_transaction = array_intersect_key($transaction_data, - array('Transaction'=>1, - 'split_transaction_id'=>1)); - - if (isset($split_transaction['split_transaction_id'])) - $split_transaction['transaction_id'] = $split_transaction['split_transaction_id']; - - if (is_array($reconciled) && count($reconciled[$dc_type]['entry'])) { - foreach ($reconciled[$dc_type]['entry'] AS $rec) { - //pr(compact('rec', 'split_transaction')); - if (!$rec['applied']) - continue; - - // Create an entry to handle the splitting of the funds ("Payment") - // and reconcile against the new cash/check/etc entry created above, - // as well as the A/R account. - - // Payment must debit the Receipt ledger, and credit the A/R ledger - // debit: Receipt credit: A/R - $ids = $this->postLedgerEntry - ($split_transaction, - null, - array('debit_ledger_id' => $this->currentLedgerID($this->receiptAccountID()), - 'credit_ledger_id' => $this->currentLedgerID($this->accountReceivableAccountID()), - 'amount' => $rec['applied'], - 'lease_id' => $rec['lease_id'], - 'customer_id' => $rec['customer_id'], - ), - array('debit' => array(array('LedgerEntry' => array('id' => $new_entry->id, - 'amount' => $rec['applied']))), - 'credit' => array(array('LedgerEntry' => array('id' => $rec['id'], - 'amount' => $rec['applied'])))) - ); - // Keep using the same split transaction for all reconciled entries - $split_transaction = array_intersect_key($ids, array('transaction_id'=>1)); - //pr(compact('ids', 'split_transaction')); - } - - //pr("end reconciled is array"); - } - - //pr("end reconcile receipt"); - } - - if (is_array($reconcile_set)) { - //pr("reconcile_set is array"); - foreach ($reconcile_set AS $reconcile_entry) { - if (!isset($reconcile_entry['LedgerEntry']['id'])) - continue; - - $amount = $reconcile_entry['LedgerEntry']['amount']; - if (!$amount) - continue; - - if ($dc_type == 'debit') { - $debit_ledger_entry_id = $new_entry->id; - $credit_ledger_entry_id = $reconcile_entry['LedgerEntry']['id']; - } - else { - $debit_ledger_entry_id = $reconcile_entry['LedgerEntry']['id']; - $credit_ledger_entry_id = $new_entry->id; - } - - $R = new Reconciliation(); - $R->create(); - if (!$R->save(compact('amount', - 'debit_ledger_entry_id', - 'credit_ledger_entry_id'), false)) - $err = true; - } - } - } - - $new_entry->recursive = -1; - $new_entry->read(); - //pr(array('post-save', $entry->data)); - - $ret = array - ('error' => $err, - 'id' => $new_entry->data['LedgerEntry']['id'], - 'transaction_id' => $new_entry->data['LedgerEntry']['transaction_id'], - 'monetary_source_id' => $new_entry->data['LedgerEntry']['monetary_source_id']); - - if (isset($split_transaction['transaction_id'])) - $ret['split_transaction_id'] = $split_transaction['transaction_id']; - - return $ret; - } - - /************************************************************************** ************************************************************************** ************************************************************************** @@ -741,43 +335,12 @@ class Account extends AppModel { return null; $this->queryInit($query); - -/* if ($all) { */ -/* if (!isset($query['link']['Ledger'])) */ -/* $query['link']['Ledger'] = array(); */ -/* if (!isset($query['link']['Ledger']['fields'])) */ -/* $query['link']['Ledger']['fields'] = array(); */ -/* $query['link']['Ledger']['fields'][] = 'id'; */ -/* } */ -/* else { */ -/* if (!isset($query['link']['CurrentLedger'])) */ -/* $query['link']['CurrentLedger'] = array(); */ -/* if (!isset($query['link']['CurrentLedger']['fields'])) */ -/* $query['link']['CurrentLedger']['fields'] = array(); */ -/* $query['link']['CurrentLedger']['fields'][] = 'id'; */ -/* } */ - -/* $query['conditions'][] = array('Account.id' => $id); */ - -/* $account = $this->find('first', $query); */ - $query['link'] = array('Account' => $query['link']); foreach ($this->ledgers($id, $all) AS $ledger) $this->statsMerge($stats['Ledger'], $this->Ledger->stats($ledger, $query)); -/* $stats = array(); */ -/* if ($all) { */ -/* foreach ($account['Ledger'] AS $ledger) */ -/* $this->statsMerge($stats['Ledger'], */ -/* $this->Ledger->stats($ledger['id'], $query)); */ -/* } */ -/* else { */ -/* $stats['Ledger'] = */ -/* $this->Ledger->stats($account['CurrentLedger']['id'], $query); */ -/* } */ - return $stats; } diff --git a/site/models/close.php b/site/models/close.php deleted file mode 100644 index 8704f50..0000000 --- a/site/models/close.php +++ /dev/null @@ -1,12 +0,0 @@ - \ No newline at end of file diff --git a/site/models/customer.php b/site/models/customer.php index 08056dd..269f2fb 100644 --- a/site/models/customer.php +++ b/site/models/customer.php @@ -149,78 +149,6 @@ class Customer extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: paymentWouldReconcile - * - Returns which charges a new payment would reconcile - * - * - REVISIT 20090617 - * This should be subject to different algorithms, such - * as apply to oldest charges first, newest first, to fees - * before rent, etc. Until we get there, I'll hardcode - * whatever algorithm is simplest. - */ - - function paymentWouldReconcile($id, $amount, $query = null) { - $unreconciled = array($ofund => array('entry'=>array(), 'balance'=>0)); - $applied = 0; - - if ($amount <= 0) - return; - - $unreconciled = array(); - $unreconciled['entries'] = $this->unreconciledCharges($id, $query); - - foreach ($unreconciled['entries'] AS $i => &$item) { - $entry =& $item['StatementEntry']; - // Determine if amount is sufficient to cover the entry - if ($amount > $entry['balance']) - $apply = $entry['balance']; - elseif ($amount > 0) - $apply = $amount; - else { - unset($unreconciled[$i]); - continue; - } - - $entry['applied'] = $apply; - $entry['reconciled'] += $apply; - $entry['balance'] -= $apply; - $applied += $apply; - $amount -= $apply; - } - - $unreconciled['unapplied'] = $amount; - $unreconciled['applied'] = $applied; - $unreconciled['balance'] -= $applied; - return $unreconciled; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: reconcileLedgerEntries - * - Returns which ledger entries a new credit/debit would - * reconcile, and how much. - * - * - REVISIT 20090617 - * This should be subject to different algorithms, such - * as apply to oldest charges first, newest first, to fees - * before rent, etc. Until we get there, I'll hardcode - * whatever algorithm is simplest. - */ - - function reconcileLedgerEntries($id) { - $A = new Account(); - $reconciled = $A->reconcileLedgerEntries - ($A->accountReceivableAccountID(), - array('StatementEntry.customer_id' => $id)); - - return $reconciled; - } - /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/site/models/ledger.php b/site/models/ledger.php index 0025bcf..491c02a 100644 --- a/site/models/ledger.php +++ b/site/models/ledger.php @@ -144,20 +144,6 @@ class Ledger extends AppModel { $this->queryInit($query); -/* if (!isset($query['link']['Ledger'])) */ -/* $query['link']['Ledger'] = array(); */ -/* if (!isset($query['link']['Ledger']['fields'])) */ -/* $query['link']['Ledger']['fields'] = array(); */ - if (!isset($query['link']['Account'])) - $query['link']['Account'] = array(); - if (!isset($query['link']['Account']['fields'])) - $query['link']['Account']['fields'] = array(); -/* if (!isset($query['link']['Transaction'])) */ -/* $query['link']['Transaction'] = array(); */ -/* if (!isset($query['link']['Transaction']['fields'])) */ -/* $query['link']['Transaction']['fields'] = array(); */ -/* $query['link']['Transaction']['fields'][] = 'stamp'; */ - if (!isset($query['fields'])) $query['fields'] = array(); @@ -168,13 +154,6 @@ class Ledger extends AppModel { $query['group'][] = 'LedgerEntry.ledger_id'; $stats = $this->LedgerEntry->find('first', $query); - //pr(compact('stats')); - - -/* unset($query['group']); */ -/* $query['fields'] = $this->debitCreditFields($id, false); */ -/* $stats = $this->find('all', $query); */ -/* pr(compact('query', 'stats')); */ // The fields are all tucked into the [0] index, // and the rest of the array is useless (empty). diff --git a/site/models/statement_entry.php b/site/models/statement_entry.php index 26e2181..edcb1be 100644 --- a/site/models/statement_entry.php +++ b/site/models/statement_entry.php @@ -108,78 +108,6 @@ class StatementEntry extends AppModel { } - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: addCharge - * - Adds a new charge - */ - - function addCharge($data, $transaction, $customer_id, $lease_id = null) { - // Create some models for convenience - $A = new Account(); - - // Assume this will succeed - $ret = true; - - // Establish the key charge parameters - $charge = array_intersect_key($data, array('stamp'=>1, 'amount'=>1, 'account_id'=>1)); - - $charge['customer_id'] = $customer_id; - $charge['lease_id'] = $lease_id; - $charge['type'] = 'CHARGE'; - - $ids = $this->Entry->Ledger->Account->postLedgerEntry - ($transaction, - $charge, - array('debit_ledger_id' => $A->currentLedgerID($data['account_id']), - 'credit_ledger_id' => $A->currentLedgerID($A->accountReceivableAccountID()) - ) + $data - ); - - if ($ids['error']) - $ret = false; - - return $ids['charge_id']; - } - - - /************************************************************************** - ************************************************************************** - ************************************************************************** - * function: addPayment - * - Adds a new payment - */ - - function addPayment($data, $transaction, $customer_id, $lease_id = null) { - // Create some models for convenience - $A = new Account(); - - // Assume this will succeed - $ret = true; - - // Establish the key payment parameters - $payment = array_intersect_key($data, array('stamp'=>1, 'name'=>1, 'monetary_type'=>1, - 'data1'=>1, 'data2'=>1, 'data3'=>1, 'data4'=>1, - 'amount'=>1, 'account_id'=>1)); - $payment['customer_id'] = $customer_id; - $payment['type'] = 'PAYMENT'; - - $ids = $this->Entry->Ledger->Account->postLedgerEntry - ($transaction, - $payment, - array('debit_ledger_id' => $A->currentLedgerID($data['account_id']), - 'credit_ledger_id' => $A->currentLedgerID($A->accountReceivableAccountID()) - ) + $data - ); - - if ($ids['error']) - $ret = false; - - return $ids['payment_id']; - } - - /************************************************************************** ************************************************************************** **************************************************************************