git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@131 97e9348a-65ac-dc4b-aefc-98561f571b83
158 lines
5.3 KiB
PHP
158 lines
5.3 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 jqGridDataExtraTables(&$params, &$model, &$query) {
|
|
unset($query['contain']);
|
|
$query['link'] =
|
|
array(// Models
|
|
'Account',
|
|
'LedgerEntry' =>
|
|
array('conditions' =>
|
|
array('OR' =>
|
|
array('LedgerEntry.debit_ledger_id = Ledger.id',
|
|
'LedgerEntry.credit_ledger_id = Ledger.id'),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
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 all ledger entries of this ledger
|
|
$ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries
|
|
($id, $ledger['Account']['type']);
|
|
|
|
// Summarize the entries, and obtain the ledger balance
|
|
$ledger['Ledger'] =
|
|
array_merge($ledger['Ledger'],
|
|
array('stats' => $this->Ledger->stats($id)));
|
|
$balance = $ledger['Ledger']['stats']['balance'];
|
|
|
|
// OK, set our view variables and render!
|
|
$title = 'Ledger: ' . $ledger['Ledger']['name'];
|
|
$this->set(compact('ledger', 'title', 'balance'));
|
|
}
|
|
}
|