Files
pmgr/controllers/ledgers_controller.php
abijah af405a44d5 Yet another place where Ledger.name was being used.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@147 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-06-16 08:38:31 +00:00

150 lines
5.0 KiB
PHP

<?php
class LedgersController extends AppController {
var $sidemenu_links =
array(array('name' => 'Ledgers', 'header' => true),
array('name' => 'Current', 'url' => array('controller' => 'ledgers', 'action' => 'current')),
array('name' => 'Closed', 'url' => array('controller' => 'ledgers', 'action' => 'closed')),
array('name' => 'All', 'url' => array('controller' => 'ledgers', 'action' => 'all')),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* override: sideMenuLinks
* - Generates controller specific links for the side menu
*/
function sideMenuLinks() {
return array_merge(parent::sideMenuLinks(), $this->sidemenu_links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index / current / closed / all
* - Generate a list of ledgers
*/
function index() { $this->all(); }
function current() { $this->jqGridView('Current Ledgers'); }
function closed() { $this->jqGridView('Closed Ledgers'); }
function all() { $this->jqGridView('All Ledgers', 'all'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* virtuals: jqGridData
* - With the application controller handling the jqGridData action,
* these virtual functions ensure that the correct data is passed
* to jqGrid.
*/
function jqGridDataSetup(&$params) {
parent::jqGridDataSetup($params);
if (!isset($params['action']))
$params['action'] = 'all';
}
function jqGridDataCountTables(&$params, &$model) {
return array('contain' => false);
}
function jqGridDataTables(&$params, &$model) {
return array
('link' =>
array(// Models
'Account',
'LedgerEntry',
),
);
}
function jqGridDataFields(&$params, &$model) {
return array
('Ledger.*',
'CONCAT(Account.id, "-", Ledger.sequence) AS id_sequence',
'SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS debits',
'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS credits',
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance",
'COUNT(LedgerEntry.id) AS entries');
}
function jqGridDataConditions(&$params, &$model) {
$conditions = parent::jqGridDataConditions($params, $model);
if ($params['action'] === 'current') {
$conditions[] = array('NOT' => array('Ledger.closed'));
}
elseif ($params['action'] === 'closed') {
$conditions[] = 'Ledger.closed';
}
return $conditions;
}
function jqGridDataOrder(&$params, &$model, $index, $direction) {
$id_sequence = false;
if ($index === 'id_sequence') {
$id_sequence = true;
$index = 'Ledger.account_id';
}
$order = parent::jqGridDataOrder($params, $model, $index, $direction);
if ($id_sequence) {
$order[] = 'Ledger.sequence ' . $direction;
}
return $order;
}
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
$links['Ledger'] = array('id_sequence');
$links['Account'] = array('name');
return parent::jqGridRecordLinks($params, $model, $records, $links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: view
* - Displays information about a specific ledger
*/
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
// Get details about the ledger itself (no entries yet)
$ledger = $this->Ledger->find
('first',
array('contain' =>
array(// Models
'Account',
),
'conditions' => array(array('Ledger.id' => $id)),
)
);
// Get ledger stats for our summary box
$stats = $this->Ledger->stats($id);
// OK, set our view variables and render!
$title = 'Ledger: #' . $ledger['Account']['id'] .'-'. $ledger['Ledger']['sequence'];
$this->set(compact('ledger', 'title', 'stats'));
}
}