diff --git a/site/controllers/accounts_controller.php b/site/controllers/accounts_controller.php index ed044af..49a4c44 100644 --- a/site/controllers/accounts_controller.php +++ b/site/controllers/accounts_controller.php @@ -164,7 +164,7 @@ class AccountsController extends AppController { array('fields' => array('name')), 'LedgerEntry' => - array('fields' => array('amount'), + array('fields' => array('id', 'amount'), 'MonetarySource' => array('fields' => array('name')), @@ -183,23 +183,15 @@ class AccountsController extends AppController { )); $deposit['total'] += $ledger['amount']; - $deposit['ledgers'][] = array('name' => $ledger['account_name'], + $deposit['ledgers'][] = array('id' => $ledger_id, + 'name' => $ledger['account_name'], 'total' => $ledger['amount'], 'entries' => $ledger_entries); } - // Estabish a set of ledgers to close - $set = array(); - foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) { - if (!$ledger['checked'] || $ledger['amount'] == 0) - continue; - $set[] = array('ledger_id' => $ledger_id, 'amount' => $ledger['amount']); - } - // Perform the accounting work necessary to close the // monetary ledgers and deposit into the bank account. - if (count($set) > 0) - $this->Account->closeAndDeposit($set, $this->data['Deposit']['Account']['id']); + $this->Account->closeAndDeposit($deposit['ledgers'], $this->data['Deposit']['Account']['id']); $title = 'Account: Deposit Slip'; $this->set(compact('title', 'deposit')); diff --git a/site/models/account.php b/site/models/account.php index 0c0968c..bd61252 100644 --- a/site/models/account.php +++ b/site/models/account.php @@ -155,7 +155,8 @@ class Account extends AppModel { $account = $this->find('all', array ('contain' => array('CurrentLedger'), 'fields' => array('Account.id', 'Account.type', 'Account.name', 'CurrentLedger.id'), - 'conditions' => array('Account.'.$attribute => true) + 'conditions' => array('Account.'.$attribute => true), + 'order' => array('Account.name'), ) + (isset($extra) ? $extra : array()) ); $this->cacheQueries = false; @@ -490,7 +491,8 @@ class Account extends AppModel { function postLedgerEntry($transaction_data, $monetary_data, - $entry_data) { + $entry_data, + $reconcile = null) { /* if (!isset($entry_data) || */ /* !isset($entry_data['amount']) || */ @@ -608,18 +610,69 @@ class Account extends AppModel { //pr(array('pre-save', compact('entry_data'))); // Create it! - $entry = new LedgerEntry(); - $entry->create(); - if (!$entry->saveAll($entry_data, array('validate'=>false))) { - return false; + $new_entry = new LedgerEntry(); + $new_entry->create(); + if (!$new_entry->saveAll($entry_data, array('validate'=>false))) { + return array('error' => true); } - $entry->recursive = -1; - $entry->read(); + + // 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) || !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 $dr => $reconcile_set) { + if (!isset($reconcile_set) || (is_bool($reconcile_set) && !$reconcile_set)) + continue; + + if (is_bool($reconcile_set) && $reconcile_set) { + // REVISIT : 20090710 + // Take the reconcile code from the Transaction model, + // which utilizes reconcileNewLedgerEntry, and insert + // it here so that we can migrate the bulk of the + // functions addReceipt and addInvoice + } + + if (is_array($reconcile_set)) { + foreach ($reconcile_set AS $reconcile_entry) { + $amount = $reconcile_entry['LedgerEntry']['amount']; + if (!$amount) + continue; + + if ($dr == '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)); - return array('transaction_id' => $entry->data['LedgerEntry']['transaction_id'], - 'monetary_source_id' => $entry->data['LedgerEntry']['monetary_source_id'], - 'id' => $entry->data['LedgerEntry']['id']); + return 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']); } @@ -640,14 +693,28 @@ class Account extends AppModel { $transaction = array(); foreach ($set AS $ledger) { + // REVISIT : 20090710 + // If the user said to include a ledger in the + // set, should we really be excluding it? + if ($ledger['total'] == 0) + continue; + $ids = $this->postLedgerEntry ($transaction, null, array('debit_account_id' => $deposit_account_id, - 'credit_ledger_id' => $ledger['ledger_id'], - 'amount' => $ledger['amount'])); + 'credit_ledger_id' => $ledger['id'], + 'amount' => $ledger['total']), + // Reconcile the account for cash/check/etc, + // which is the credit side of this entry. + array('credit' => $ledger['entries'])); + + if ($ids['error']) + die("closeAndDeposit : postLedgerEntry returned error!"); + $transaction = array_intersect_key($ids, array('transaction_id'=>1)); - $this->Ledger->closeLedger($ledger['ledger_id'], $close->id); + + $this->Ledger->closeLedger($ledger['id'], $close->id); } }