diff --git a/controllers/statement_entries_controller.php b/controllers/statement_entries_controller.php index b94cb39..41e1d91 100644 --- a/controllers/statement_entries_controller.php +++ b/controllers/statement_entries_controller.php @@ -183,6 +183,18 @@ class StatementEntriesController extends AppController { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: waive the ledger entry + */ + + function waive($id) { + $this->StatementEntry->waive($id); + //$this->redirect(array('action'=>'view', $id)); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -256,6 +268,19 @@ class StatementEntriesController extends AppController { else $stats = $stats['Payment']; + + if (strtoupper($entry['StatementEntry']['type']) === 'CHARGE' && + $stats['reconciled'] == 0) { + // Set up dynamic menu items + $this->sidemenu_links[] = + array('name' => 'Operations', 'header' => true); + + $this->sidemenu_links[] = + array('name' => 'Waive', + 'url' => array('action' => 'waive', + $id)); + } + // Prepare to render. $title = "Statement Entry #{$entry['StatementEntry']['id']}"; $this->set(compact('entry', 'title', 'reconciled', 'stats')); diff --git a/models/account.php b/models/account.php index 0d6a51a..afc854a 100644 --- a/models/account.php +++ b/models/account.php @@ -132,6 +132,7 @@ class Account extends AppModel { function checkAccountID() { return $this->nameToID('Check'); } function moneyOrderAccountID() { return $this->nameToID('Money Order'); } function concessionAccountID() { return $this->nameToID('Concession'); } + function waiverAccountID() { return $this->nameToID('Waiver'); } function pettyCashAccountID() { return $this->nameToID('Petty Cash'); } function invoiceAccountID() { return $this->nameToID('Invoice'); } function receiptAccountID() { return $this->nameToID('Receipt'); } diff --git a/models/statement_entry.php b/models/statement_entry.php index 0339488..72a3fbb 100644 --- a/models/statement_entry.php +++ b/models/statement_entry.php @@ -22,7 +22,7 @@ class StatementEntry extends AppModel { ); - //var $default_log_level = 30; + var $default_log_level = 30; /************************************************************************** ************************************************************************** @@ -106,6 +106,39 @@ class StatementEntry extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: waive + * - Waives the charges + * + */ + function waive($id, $stamp = null) { + $this->prEnter(compact('id', 'stamp')); + + $this->recursive = -1; + $charge = $this->read(null, $id); + $charge = $charge['StatementEntry']; + + // Build a transaction + $waiver = array('Transaction' => array(), 'Entry' => array()); + $waiver['Transaction']['stamp'] = $stamp; + $waiver['Transaction']['comment'] = "Charge Waiver"; + + if ($charge['type'] !== 'CHARGE') + die("INTERNAL ERROR: WAIVER ITEM IS NOT CHARGE"); + + $waiver['Entry'][] = + array('amount' => $charge['amount'], + 'account_id' => $this->Account->waiverAccountID(), + 'comment' => null, + ); + + return $this->prReturn($this->Transaction->addWaiver + ($waiver, $id, $charge['customer_id'], $charge['lease_id'])); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -334,7 +367,7 @@ OPTION 2 * the user to specify how payments should be applied. * */ - function assignCredits($query = null, $receipt_id = null) { + function assignCredits($query = null, $receipt_id = null, $charge_ids = null) { //$this->prFunctionLevel(25); $this->prEnter( compact('query', 'receipt_id')); $this->queryInit($query); @@ -374,7 +407,12 @@ OPTION 2 } // Now find all unpaid charges - $lquery = $query; + if (isset($charge_ids)) { + $lquery = array('contain' => false, + 'conditions' => array('StatementEntry.id' => $charge_ids)); + } else { + $lquery = $query; + } $lquery['order'] = 'StatementEntry.effective_date ASC'; $charges = $this->reconciledSet('CHARGE', $lquery, true); $this->pr(18, compact('charges'), diff --git a/models/transaction.php b/models/transaction.php index c67954c..c33ad7e 100644 --- a/models/transaction.php +++ b/models/transaction.php @@ -51,23 +51,25 @@ class Transaction extends AppModel { // Establish the transaction as an invoice $invoice =& $data['Transaction']; - $invoice['type'] = 'INVOICE'; - $invoice['crdr'] = 'DEBIT'; - $invoice['account_id'] = $this->Account->accountReceivableAccountID(); - $invoice['customer_id'] = $customer_id; - $invoice['lease_id'] = $lease_id; + $invoice += + array('type' => 'INVOICE', + 'crdr' => 'DEBIT', + 'account_id' => $this->Account->accountReceivableAccountID(), + 'customer_id' => $customer_id, + 'lease_id' => $lease_id, + ); // Go through the statement entries and flag as charges - foreach ($data['Entry'] AS &$entry) { - $entry['type'] = 'CHARGE'; - $entry['crdr'] = 'CREDIT'; - } + foreach ($data['Entry'] AS &$entry) + $entry += array('type' => 'CHARGE', + 'crdr' => 'CREDIT', + ); $ids = $this->addTransaction($data['Transaction'], $data['Entry']); if (isset($ids['transaction_id'])) $ids['invoice_id'] = $ids['transaction_id']; - return $ids; + return $this->prReturn($ids); } @@ -83,26 +85,30 @@ class Transaction extends AppModel { // Establish the transaction as a receipt $receipt =& $data['Transaction']; - $receipt['type'] = 'RECEIPT'; - $receipt['crdr'] = 'CREDIT'; - $receipt['account_id'] = $this->Account->accountReceivableAccountID(); - $receipt['customer_id'] = $customer_id; - $receipt['lease_id'] = $lease_id; + $receipt += + array('type' => 'RECEIPT', + 'crdr' => 'CREDIT', + 'account_id' => $this->Account->accountReceivableAccountID(), + 'customer_id' => $customer_id, + 'lease_id' => $lease_id, + ); // Go through the statement entries and flag as payments - foreach ($data['Entry'] AS &$entry) { - $entry['crdr'] = 'DEBIT'; - if (empty($entry['account_id']) && isset($entry['Tender']['tender_type_id'])) { - $entry['account_id'] = $this->LedgerEntry->Tender->TenderType-> - accountID($entry['Tender']['tender_type_id']); - } - } + foreach ($data['Entry'] AS &$entry) + $entry += array('type' => 'PAYMENT', // not used + 'crdr' => 'DEBIT', + 'account_id' => + (isset($entry['Tender']['tender_type_id']) + ? ($this->LedgerEntry->Tender->TenderType-> + accountID($entry['Tender']['tender_type_id'])) + : null), + ); $ids = $this->addTransaction($data['Transaction'], $data['Entry']); if (isset($ids['transaction_id'])) $ids['receipt_id'] = $ids['transaction_id']; - return $ids; + return $this->prReturn($ids); } @@ -113,36 +119,22 @@ class Transaction extends AppModel { * - Adds a new waiver */ - function addWaiver($data, $customer_id, $lease_id = null) { + function addWaiver($data, $charge_id, $customer_id, $lease_id = null) { $this->prEnter(compact('data', 'customer_id', 'lease_id')); - // REVISIT : 20090802 - // Completely un-implemented. Just copied from addReceipt - // and search-replace receipt with waiver. - return array('error' => true); - // Establish the transaction as a waiver - $waiver =& $data['Transaction']; - $waiver['type'] = 'RECEIPT'; - $waiver['crdr'] = 'CREDIT'; - $waiver['account_id'] = $this->Account->accountReceivableAccountID(); - $waiver['customer_id'] = $customer_id; - $waiver['lease_id'] = $lease_id; - - // Go through the statement entries and flag as payments - foreach ($data['Entry'] AS &$entry) { + // Just make sure the entries are marked as waivers... + foreach ($data['Entry'] AS &$entry) $entry['type'] = 'WAIVE'; - $entry['crdr'] = 'DEBIT'; - if (empty($entry['account_id']) && isset($entry['Tender']['tender_type_id'])) { - $entry['account_id'] = $this->LedgerEntry->Tender->TenderType-> - accountID($entry['Tender']['tender_type_id']); - } - } - $ids = $this->addTransaction($data['Transaction'], $data['Entry']); + // ... and the charge statement entry is recorded... + $data['Transaction']['charge_entry_id'] = $charge_id; + + // ... and in all other respects this is just a receipt. + $ids = $this->addReceipt($data, $customer_id, $lease_id); if (isset($ids['transaction_id'])) $ids['waiver_id'] = $ids['transaction_id']; - return $ids; + return $this->prReturn($ids); } @@ -192,7 +184,7 @@ class Transaction extends AppModel { ); } - return $ids; + return $this->prReturn($ids); } @@ -249,7 +241,7 @@ class Transaction extends AppModel { ); } - return $ids; + return $this->prReturn($ids); } @@ -476,6 +468,7 @@ class Transaction extends AppModel { $this->create(); if (!$this->save($transaction)) return $this->prReturn(array('error' => true) + $ret); + $transaction_stamp = $this->field('stamp'); // Set up our return ids array $ret['transaction_id'] = $this->id; @@ -503,6 +496,7 @@ class Transaction extends AppModel { if (!empty($se)) { $se['transaction_id'] = $ret['transaction_id']; + $se += array('effective_date' => $transaction_stamp); $result = $this->StatementEntry->addStatementEntry($se); $ret['entries'][$e_index]['StatementEntry'] = $result; if ($result['error']) { @@ -520,7 +514,11 @@ class Transaction extends AppModel { 'conditions' => array('Customer.id' => $transaction['customer_id'])), ($transaction['type'] == 'RECEIPT' ? $ret['transaction_id'] - : null)); + : null), + ($transaction['type'] == 'RECEIPT' && !empty($transaction['charge_entry_id']) + ? $transaction['charge_entry_id'] + : null) + ); $ret['assigned'] = $result; if ($result['error'])