From 861eabc6b59e57ea9339b181a5528626e832b7a2 Mon Sep 17 00:00:00 2001 From: abijah Date: Wed, 24 Jun 2009 17:18:25 +0000 Subject: [PATCH] Forgot the new files on the last checkin git-svn-id: file:///svn-source/pmgr/branches/statements_20090623@185 97e9348a-65ac-dc4b-aefc-98561f571b83 --- .../statement_entries_controller.php | 116 +++++++++++++++++ site/models/statement.php | 123 ++++++++++++++++++ site/models/statement_entry.php | 9 ++ site/views/elements/statement_entries.ctp | 50 +++++++ 4 files changed, 298 insertions(+) create mode 100644 site/controllers/statement_entries_controller.php create mode 100644 site/models/statement.php create mode 100644 site/models/statement_entry.php create mode 100644 site/views/elements/statement_entries.ctp diff --git a/site/controllers/statement_entries_controller.php b/site/controllers/statement_entries_controller.php new file mode 100644 index 0000000..32e389e --- /dev/null +++ b/site/controllers/statement_entries_controller.php @@ -0,0 +1,116 @@ +sidemenu_links); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * virtuals: jqGridData + * - With the application controller handling the jqGridData action, + * these virtual functions ensure that the correct data is passed + * to jqGrid. + */ + + function jqGridDataTables(&$params, &$model) { + $link = + array(// Models + 'LedgerEntry' => + array('fields' => array('id'), + // Models + 'Transaction' => + array('fields' => array('id', 'stamp'), + ), + + 'Ledger' => + array('fields' => array('id', 'sequence'), + 'conditions' => ("Ledger.id = IF(StatementEntry.type = 'CHARGE'," . + " LedgerEntry.credit_ledger_id," . + " LedgerEntry.debit_ledger_id)"), + // Models + 'Account' => array('fields' => array('id', 'name'), + ), + ), + ), + ); + + return array('link' => $link); + } + + function jqGridDataFields(&$params, &$model) { + $fields = array('id', 'type', 'comment'); + $fields[] = "IF(StatementEntry.type = 'CHARGE', StatementEntry.amount, NULL) AS 'charge'"; + $fields[] = "IF(StatementEntry.type = 'PAYMENT', StatementEntry.amount, NULL) AS 'payment'"; + $fields[] = "IF(StatementEntry.type = 'CHARGE', 1, -1) * StatementEntry.amount AS 'amount'"; + return $fields; + } + + function jqGridDataConditions(&$params, &$model) { + $conditions = parent::jqGridDataConditions($params, $model); + + if (isset($params['custom']['statement_id'])) { + $conditions[] = + array('StatementEntry.statement_id' => $params['custom']['statement_id']); + } + + return $conditions; + } + + function jqGridRecordLinks(&$params, &$model, &$records, $links) { + $links['Transaction'] = array('id'); + $links['StatementEntry'] = array('id'); + $links['Account'] = array('controller' => 'accounts', 'name'); + $links['LedgerEntry'] = array('id'); + return parent::jqGridRecordLinks($params, $model, $records, $links); + } + + function jqGridRecordsPostProcess(&$params, &$model, &$records) { + parent::jqGridRecordsPostProcess($params, $model, $records); + + $subtotal = 0; + foreach ($records AS &$record) { + $amount = $record['StatementEntry']['amount']; + $record['StatementEntry']['amount'] = $amount; + $record['StatementEntry']['subtotal'] = ($subtotal += $amount); + } + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: view + * - Displays information about a specific entry + */ + + function view($id = null) { + if (!$id) { + $this->Session->setFlash(__('Invalid Item.', true)); + $this->redirect(array('controller' => 'accounts', 'action'=>'index')); + } + + // Get the StatementEntry and related fields + $entry = $this->StatementEntry->find + ('first', + array('contain' => array(), + 'conditions' => array('StatementEntry.id' => $id), + )); + + // Prepare to render. + $title = "Statement Entry #{$entry['StatementEntry']['id']}"; + $this->set(compact('entry', 'title')); + } +} diff --git a/site/models/statement.php b/site/models/statement.php new file mode 100644 index 0000000..ccb917e --- /dev/null +++ b/site/models/statement.php @@ -0,0 +1,123 @@ + array( + 'className' => 'StatementEntry', + 'conditions' => array('type' => 'CHARGE'), + ), + 'PaymentStatementEntry' => array( + 'className' => 'StatementEntry', + 'conditions' => array('type' => 'PAYMENT'), + ), + ); + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: findStatementEntries + * - Returns an array of statement entries that belong to a given + * statement. There is extra work done... see the StatementEntry model. + */ + function findStatementEntries($id, $cond = null, $link = null) { +/* pr(array('function' => 'Statement::findStatementEntries', */ +/* 'args' => compact('id', 'cond', 'link'), */ +/* )); */ + + if (!isset($cond)) + $cond = array(); + + $cond[] = array('Statement.id' => $id); + $entries = $this->find('all', array('link' => $link, 'conditions' => $cond)); + +/* pr(array('function' => 'Statement::findStatementEntries', */ +/* 'args' => compact('id', 'cond', 'link'), */ +/* 'return' => compact('entries'), */ +/* )); */ + return $entries; + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: findEntriesRelatedToAccount + * - Returns an array of statement entries that belong to the given + * account, and are related to a specific account. + */ + function findEntriesRelatedToAccount($id, $rel_ids, $cond = null, $link = null) { +/* pr(array('function' => 'Statement::findEntriesRelatedToAccount', */ +/* 'args' => compact('id', 'rel_ids', 'cond', 'link'), */ +/* )); */ + + if (!isset($cond)) + $cond = array(); + if (!isset($link)) + $link = array(); + if (!is_array($rel_ids)) + $rel_ids = array($rel_ids); + + $link['StatementEntry'] = array('LedgerEntry' => array('Ledger' => array('Account'))); + $cond[] = array('Account.id' => $rel_ids); + + $entries = $this->findStatementEntries($id, $cond, $link); + + $stats = $this->stats($id, $cond, $link); + $entries = array('Entries' => $entries, + 'summary' => $stats); + +/* pr(array('function' => 'Statement::findEntriesRelatedToAccount', */ +/* 'args' => compact('id', 'relid', 'cond', 'link'), */ +/* 'return' => compact('entries'), */ +/* )); */ + return $entries; + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: stats + * - Returns summary data from the requested statement. + */ + function stats($id, $cond = null, $link = null) { + if (!isset($cond)) + $cond = array(); + if (!isset($link)) + $link = array(); + + if (!isset($link['StatementEntry'])) + $link['StatementEntry'] = array('fields' => array()); + + $cond[] = array('Statement.id' => $id); + + $stats = $this->find + ('first', array + ('link' => $link, + + 'fields' => + array("SUM(IF(StatementEntry.type = 'CHARGE', + StatementEntry.amount, NULL)) AS charges", + "SUM(IF(StatementEntry.type = 'PAYMENT', + StatementEntry.amount, NULL)) AS payments", + "SUM(IF(StatementEntry.type = 'CHARGE', 1, -1) + * IF(StatementEntry.amount, StatementEntry.amount, 0) + ) AS balance", + "COUNT(StatementEntry.id) AS entries"), + + 'conditions' => $cond, + 'group' => 'Statement.id', + )); + + // The fields are all tucked into the [0] index, + // and the rest of the array is useless (empty). + return $stats[0]; + } + +} +?> \ No newline at end of file diff --git a/site/models/statement_entry.php b/site/models/statement_entry.php new file mode 100644 index 0000000..f5d5f25 --- /dev/null +++ b/site/models/statement_entry.php @@ -0,0 +1,9 @@ + 'StatementEntry.id', 'formatter' => 'id'); +$cols['Entry'] = array('index' => 'LedgerEntry.id', 'formatter' => 'id'); +$cols['Date'] = array('index' => 'Transaction.stamp', 'formatter' => 'date'); +//$cols['Type'] = array('index' => 'StatementEntry.type', 'formatter' => 'name'); +$cols['Account'] = array('index' => 'Account.name', 'formatter' => 'longname'); +//$cols['Comment'] = array('index' => 'StatementEntry.comment', 'formatter' => 'comment', 'width'=>150); + +//$cols['Amount'] = array('index' => 'StatementEntry.amount', 'formatter' => 'currency'); +$cols['Charge'] = array('index' => 'charge', 'formatter' => 'currency'); +$cols['Payment'] = array('index' => 'payment', 'formatter' => 'currency'); + +if ($subtotal_amount) { + $cols['Sub-Total'] = array('index' => 'subtotal', 'formatter' => 'currency', 'sortable' => false); +} + +$custom_post_data = compact('statement_id'); + +$jqGrid_options = array('jqGridColumns' => $cols, + 'controller' => 'statement_entries', + ); + +$jqGrid_options += compact('grid_div_id', 'grid_id', 'caption', 'grid_setup', 'limit'); + +if (isset($statement_entries)) { + $jqGrid_options += array('custom_ids' => + array_map(create_function('$data', + 'return $data["id"];'), + $statement_entries), + 'limit' => 10); +} +else { + $jqGrid_options += array('action' => 'statement', + 'limit' => 50); +} + +$jqGrid_options += compact('custom_post_data'); +$jqGrid_options['sort_column'] = 'Date'; +echo $this->element('jqGrid', $jqGrid_options); +