diff --git a/controllers/statement_entries_controller.php b/controllers/statement_entries_controller.php index 3762791..b260699 100644 --- a/controllers/statement_entries_controller.php +++ b/controllers/statement_entries_controller.php @@ -203,9 +203,42 @@ class StatementEntriesController extends AppController { * action: reverse the ledger entry */ - function reverse($id) { - $this->StatementEntry->reverse($id); - $this->redirect(array('action'=>'view', $id)); + function reverse($id = null) { + if ($this->data) { + //pr($this->data); die(); + + $this->StatementEntry->reverse + ($this->data['StatementEntry']['id'], + $this->data['Transaction']['stamp'], + $this->data['Transaction']['comment']); + + $this->redirect(array('action'=>'view', + $this->data['StatementEntry']['id'])); + $this->INTERNAL_ERROR('SHOULD HAVE REDIRECTED'); + } + + $this->StatementEntry->id = $id; + $entry = $this->StatementEntry->find + ('first', array + ('contain' => array('Customer', 'Transaction', 'Account'), + )); + + if (empty($entry)) { + $this->Session->setFlash(__('Invalid Item.', true)); + $this->redirect(array('controller' => 'customers', + 'action'=>'index')); + } + + if (!$this->StatementEntry->reversable($id)) { + $this->Session->setFlash(__('Item not reversable.', true)); + $this->redirect(array('action'=>'view', $id)); + } + + // Prepare to render. + $title = ("Charge #{$entry['StatementEntry']['id']}" . + " : {$entry['StatementEntry']['amount']}" . + " : Reverse"); + $this->set(compact('entry', 'title')); } diff --git a/models/statement_entry.php b/models/statement_entry.php index 764eadb..9d58f56 100644 --- a/models/statement_entry.php +++ b/models/statement_entry.php @@ -185,6 +185,9 @@ class StatementEntry extends AppModel { function reversable($id) { $this->prEnter(compact('id')); + if (empty($id)) + return $this->prReturn(false); + // Verify the item is an actual charge $this->id = $id; $charge_type = $this->field('type'); @@ -205,7 +208,7 @@ class StatementEntry extends AppModel { * function: reverse * - Reverses the charges */ - function reverse($id, $stamp = null) { + function reverse($id, $stamp = null, $comment) { $this->prEnter(compact('id', 'stamp')); // Verify the item can be reversed @@ -222,7 +225,7 @@ class StatementEntry extends AppModel { // Record the reversal transaction $result = $this->Transaction->addReversal - ($charge, $stamp, 'Charge Reversal'); + ($charge, $stamp, $comment ? $comment : 'Charge Reversal'); if (empty($result['error'])) { // Mark the charge as reversed diff --git a/views/statement_entries/reverse.ctp b/views/statement_entries/reverse.ctp new file mode 100644 index 0000000..0fbb3d9 --- /dev/null +++ b/views/statement_entries/reverse.ctp @@ -0,0 +1,74 @@ +' . "\n"; + +$customer = $entry['Customer']; +$transaction = $entry['Transaction']; +$account = $entry['Account']; + +if (isset($entry['StatementEntry'])) + $entry = $entry['StatementEntry']; + +// We're not actually using a grid to select the customer, +// but selection-text makes for reasonable formatting +echo ('
| ' . $customer['name'] . ' | ' . + '' . '(Customer #' . $customer['id'] . ')' . ' | ' . + '
| ' . $account['name'] . ' | ' . + '' . '(StatementEntry #' . $entry['id'] . ')' . ' | ' . + '
| Amount: | ' . + '' . FormatHelper::currency($entry['amount']) . ' | ' . + '