diff --git a/controllers/transactions_controller.php b/controllers/transactions_controller.php index eca9677..576eb85 100644 --- a/controllers/transactions_controller.php +++ b/controllers/transactions_controller.php @@ -493,4 +493,19 @@ class TransactionsController extends AppController { return; } + function foo() { + $all_tenders = $this->Transaction->LedgerEntry->Tender->find + ('all', array('link' => array('TenderType', + 'LedgerEntry' => + array('Transaction')), + 'conditions' => array(array('TenderType.auto_deposit' => true), + array('Tender.deposit_transaction_id' => null)), + )); + + foreach ($all_tenders AS $cur_tender) { + $tender_ids = array($cur_tender['Tender']['id']); + $ids = array('transaction_id' => $cur_tender['Transaction']['id']); + $this->Transaction->_autoDeposit($tender_ids, $ids); + } + } } diff --git a/models/tender.php b/models/tender.php index 55920f7..37a127a 100644 --- a/models/tender.php +++ b/models/tender.php @@ -8,6 +8,9 @@ class Tender extends AppModel { 'DepositTransaction' => array( 'className' => 'Transaction', ), + 'DepositLedgerEntry' => array( + 'className' => 'LedgerEntry', + ), 'NsfTransaction' => array( 'className' => 'Transaction', 'dependent' => true, @@ -131,6 +134,7 @@ class Tender extends AppModel { ('contain' => array('LedgerEntry', 'DepositTransaction', + 'DepositLedgerEntry', 'NsfTransaction'), )); $this->pr(20, compact('tender')); diff --git a/models/transaction.php b/models/transaction.php index 15dd709..dc7a790 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -159,6 +159,14 @@ class Transaction extends AppModel { $tender_ids[] = $entry1['Tender']['tender_id']; } + $ids = $this->_autoDeposit($tender_ids, $ids); + + return $this->prReturn($ids); + } + + // REVISIT : 20090817 + // Delete after rolling up the old items + function _autoDeposit($tender_ids, $ids) { $deposit_tenders = $this->LedgerEntry->Tender->find ('all', array('contain' => array('TenderType' => array('fields' => array()), 'LedgerEntry' => array('fields' => array()), @@ -211,90 +219,20 @@ class Transaction extends AppModel { if (!empty($deposit_ids['error'])) return $this->prReturn(array('error' => true) + $ids); - if (!empty($tender_ids)) + if (!empty($tender_ids)) { + $entry_id = $deposit_ids['entries'][0]['DoubleEntry']['Entry2']['ledger_entry_id']; + $this->pr(10, compact('tender_ids', 'entry_id')); $this->LedgerEntry->Tender->updateAll - (array('Tender.deposit_transaction_id' => $ids['transaction_id']), + (array('Tender.deposit_transaction_id' => $ids['transaction_id'], + 'Tender.deposit_ledger_entry_id' => $entry_id), array('Tender.id' => $tender_ids) ); + } } return $this->prReturn($ids); } - // REVISIT : 20090817 - // Delete after rolling up the old items - function temp_auto_deposit_old_ach_items() { - $all_tenders = $this->LedgerEntry->Tender->find - ('all', array('link' => array('TenderType', - 'LedgerEntry' => - array('Transaction')), - 'conditions' => array(array('TenderType.auto_deposit' => true), - array('Tender.deposit_transaction_id' => null)), - )); - - //$this->pr(10, compact('all_tenders')); - - foreach ($all_tenders AS $cur_tender) { - $tender_ids = array($cur_tender['Tender']['id']); - $ids = array('transaction_id' => $cur_tender['Transaction']['id']); - - $tenders = $this->LedgerEntry->Tender->find - ('all', array('contain' => array('TenderType' => array('fields' => array()), - 'LedgerEntry' => array('fields' => array()), - ), - 'fields' => array('TenderType.deposit_account_id', - 'TenderType.account_id', - 'CONCAT("CREDIT") AS crdr', - 'CONCAT("Auto Deposit") AS comment', - 'SUM(LedgerEntry.amount) AS amount'), - 'conditions' => array('Tender.id' => $tender_ids, - 'TenderType.auto_deposit' => true, - ), - 'group' => 'TenderType.deposit_account_id', - )); - - foreach ($tenders AS &$tender) - $tender = $tender[0] + array_diff_key($tender['TenderType'], array('id'=>1)); - - $this->pr(10, compact('tender_ids', 'tenders')); - - // REVISIT : 20090817 - // Multiple tenders could result in deposits to more than one - // account. We're already mucking with things by having a - // ledger entry that's not involved with the account_id of the - // transaction. We could handle this by not using the helper - // _splitEntries function, and just building or individual - // entries right here (which we should probably do anyway). - // However, I'm ignoring the issue for now... - if (count($tenders) > 1) - $this->INTERNAL_ERROR("Only expecting one tender type"); - - $deposit_ids = $this->addTransactionEntries - (array('include_ledger_entry' => true, - 'include_statement_entry' => false, - ), - - array('id' => $ids['transaction_id'], - // REVISIT : 20090817 - // This is an awful cheat, and we're going to - // get burned from it someday. - 'type' => 'DEPOSIT', - 'crdr' => 'DEBIT', - 'account_id' => $tenders[0]['deposit_account_id'], - ), - - $tenders); - - if (empty($deposit_ids['error'])) { - if (!empty($tender_ids)) - $this->LedgerEntry->Tender->updateAll - (array('Tender.deposit_transaction_id' => $ids['transaction_id']), - array('Tender.id' => $tender_ids) - ); - } - } - } - /************************************************************************** ************************************************************************** @@ -378,6 +316,7 @@ class Transaction extends AppModel { array('assign' => false, 'include_ledger_entry' => true, 'include_statement_entry' => false, + 'update_tender' => true, ); // Establish the transaction as a deposit @@ -396,12 +335,14 @@ class Transaction extends AppModel { // Go through the statement entries and re-group by account id $group = array(); - foreach ($data['Entry'] AS &$entry) { + $tender_groups = array(); + foreach ($data['Entry'] AS $entry) { if (!isset($group[$entry['account_id']])) $group[$entry['account_id']] = array('account_id' => $entry['account_id'], 'amount' => 0); $group[$entry['account_id']]['amount'] += $entry['amount']; + $tender_groups[$entry['account_id']][] = $entry['tender_id']; } $data['Entry'] = $group; @@ -409,11 +350,16 @@ class Transaction extends AppModel { if (isset($ids['transaction_id'])) $ids['deposit_id'] = $ids['transaction_id']; - if (!empty($ids['deposit_id'])) { - $this->LedgerEntry->Tender->updateAll - (array('Tender.deposit_transaction_id' => $ids['deposit_id']), - array('Tender.id' => $tender_ids) - ); + if (!empty($ids['deposit_id']) && !empty($control['update_tender'])) { + foreach ($tender_groups AS $group => $tender_ids) { + $entry_id = $ids['entries'][$group]['DoubleEntry']['Entry2']['ledger_entry_id']; + $this->pr(10, compact('group', 'tender_ids', 'entry_id')); + $this->LedgerEntry->Tender->updateAll + (array('Tender.deposit_transaction_id' => $ids['deposit_id'], + 'Tender.deposit_ledger_entry_id' => $entry_id), + array('Tender.id' => $tender_ids) + ); + } } return $this->prReturn($ids); @@ -712,7 +658,9 @@ class Transaction extends AppModel { $ret['error'] = true; } - $this->Customer->update($transaction['customer_id']); + if (!empty($transaction['customer_id'])) { + $this->Customer->update($transaction['customer_id']); + } return $this->prReturn($ret); } @@ -978,7 +926,13 @@ class Transaction extends AppModel { // and recording it in the NSF account. It has nothing to do // with the customer statement (charges, disbursements, credits, etc). $bounce_result = $this->addDeposit - (array('Transaction' => + (array('control' => + // This is not a "normal" deposit, so we don't + // want to update the tender deposit transaction id + // (it already has the correct one). + array('update_tender' => false), + + 'Transaction' => array('stamp' => $stamp, 'type' => 'WITHDRAWAL', 'crdr' => 'CREDIT'), @@ -988,7 +942,7 @@ class Transaction extends AppModel { 'account_id' => $this->Account->nsfAccountID(), 'amount' => $tender['LedgerEntry']['amount'], ))), - $tender['Transaction']['account_id']); + $tender['DepositLedgerEntry']['account_id']); $this->pr(20, compact('bounce_result')); $ret['bounce'] = $bounce_result;