diff --git a/site/controllers/transactions_controller.php b/site/controllers/transactions_controller.php index 71e4e3c..3939155 100644 --- a/site/controllers/transactions_controller.php +++ b/site/controllers/transactions_controller.php @@ -165,25 +165,54 @@ class TransactionsController extends AppController { // Go through each type of tender presented to the user // Determine which are to be deposited, and which are to // have their corresponding account ledgers closed. - $type_ids = $close_type_ids = array(); + $deposit_tender_ids = array(); + $deposit_type_ids = array(); + $close_type_ids = array(); foreach ($this->data['TenderType'] AS $type_id => $type) { - if (empty($type['checked'])) + $type['items'] = unserialize($type['items']); + if (empty($type['selection']) || + $type['selection'] === 'none' || + ($type['selection'] === 'subset' && count($type['items']) == 0)) continue; - $type_ids[] = $type_id; - if (!empty($type['close'])) + // The deposit includes either the whole type, or just certain tenders + if ($type['selection'] === 'all') + $deposit_type_ids[] = $type_id; + else + $deposit_tender_ids = array_merge($deposit_tender_ids, $type['items']); + + // Should we close the ledger for this tender type? + // First, the user would have to request that we do so, + // but additionally, we shouldn't close a ledger unless + // all the tenders are included in this deposit. That + // doesn't guarantee that the ledger has a zero balance, + // but it does carry the balance forward, and a total + // deposit would imply a fresh start, so go for it. + if (!empty($type['close']) && $type['selection'] === 'all') $close_type_ids[] = $type_id; } - // Find all items which are actually to be deposited + if (empty($deposit_type_ids) && empty($deposit_tender_ids)) { + $this->Session->setFlash(__('Nothing to Deposit', true)); + $this->redirect(array('controller' => 'tenders', 'action'=>'deposit')); + } + + $deposit_conditions = array(); + if (!empty($deposit_type_ids)) + $deposit_conditions[] = array('TenderType.id' => $deposit_type_ids); + if (!empty($deposit_tender_ids)) + $deposit_conditions[] = array('DepositTender.id' => $deposit_tender_ids); + + $deposit_conditions = + array(array('DepositTender.deposit_transaction_id' => null), + array('OR' => $deposit_conditions)); + $tenders = $this->Transaction->DepositTender->find ('all', array('contain' => array('TenderType', 'LedgerEntry'), - 'conditions' => array(array('DepositTender.deposit_transaction_id' => null), - array('TenderType.id' => $type_ids)), + 'conditions' => $deposit_conditions, )); - // Prepare for the deposit by building a list of entries $deposit = array('Transaction' => array(), 'Entry' => array()); foreach ($tenders AS $tender) { $deposit['Entry'][] = @@ -193,6 +222,8 @@ class TransactionsController extends AppController { ); } + //pr(compact('deposit_type_ids', 'deposit_tender_ids', 'close_type_ids', 'deposit_conditions', 'deposit')); + // OK, perform the deposit and associated accounting $result = $this->Transaction->addDeposit ($deposit, $this->data['Deposit']['Account']['id']); diff --git a/site/views/tenders/deposit.ctp b/site/views/tenders/deposit.ctp index 2cc4d8d..cf19501 100644 --- a/site/views/tenders/deposit.ctp +++ b/site/views/tenders/deposit.ctp @@ -8,23 +8,32 @@ echo '
' . "\n";
//pr(compact('depositTypes', 'depositAccounts'));
echo $form->create(null, array('id' => 'deposit-form',
+ 'onsubmit' => 'return verifyRequest();',
'url' => array('controller' => 'transactions',
'action' => 'postDeposit')));
foreach ($depositTypes AS $type) {
//$acct = $acct['Account'];
+ $names = Inflector::pluralize($type['name']);
+
+ $radioOptions =
+ array('none' => " No {$names} will be deposited",
+ 'all' => (" Deposit all {$names} (" .
+ FormatHelper::currency($type['stats']['undeposited']) .
+ ")"),
+ 'subset' => " Deposit {$names} from the list below",
+ );
echo "\n";
- echo $form->input("TenderType.{$type['id']}.checked",
- array(//'label' => $type['name'],
- 'type' => 'checkbox',
- 'checked' => $type['stats']['undeposited'] > 0 ? true : false,
- 'disabled' => $type['stats']['undeposited'] > 0 ? false : true,
- 'value' => true,
- 'label' => (" I have exactly " .
- FormatHelper::currency($type['stats']['undeposited']) .
- " in " . Inflector::pluralize($type['name']) .
- " and will be depositing it all.")
+ echo $form->input("TenderType.{$type['id']}.selection",
+ array('type' => 'radio',
+ 'class' => "type-selection-{$type['id']}",
+ 'separator' => '
',
+ 'onclick' => "switchSelection({$type['id']})",
+ 'legend' => false,
+ 'value' => $type['stats']['undeposited'] > 0 ? 'all' : 'none',
+ 'disabled' => $type['stats']['undeposited'] <= 0,
+ 'options' => $radioOptions,
));
// REVISIT