Files
pmgr/controllers/accounts_controller.php
abijah e0d9edc4a8 Primarly a cleanup checkin, although a couple minor bugfixes were included as well.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@82 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-06-10 05:14:08 +00:00

209 lines
7.5 KiB
PHP

<?php
class AccountsController extends AppController {
var $paginate = array
('limit' => 100,
'link' =>
array(// Models
'CurrentLedger' => array
(// Models
'LedgerEntry' =>
array('fields' =>
array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id,
LedgerEntry.amount, NULL)) AS debits',
'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id,
LedgerEntry.amount, NULL)) AS credits',
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1),
IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, 1, -1)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance",
'COUNT(LedgerEntry.id) AS entries'),
'conditions' =>
array('OR' =>
array('LedgerEntry.debit_ledger_id = CurrentLedger.id',
'LedgerEntry.credit_ledger_id = CurrentLedger.id'),
),
),
),
),
'group' => 'Account.id',
'order' => array('Account.name' => 'ASC'));
var $uses = array('Account', 'LedgerEntry');
var $sidemenu_links =
array(array('name' => 'Accounts', 'header' => true),
array('name' => 'All', 'url' => array('controller' => 'accounts', 'action' => 'all')),
array('name' => 'Asset', 'url' => array('controller' => 'accounts', 'action' => 'asset')),
array('name' => 'Liability', 'url' => array('controller' => 'accounts', 'action' => 'liability')),
array('name' => 'Equity', 'url' => array('controller' => 'accounts', 'action' => 'equity')),
array('name' => 'Income', 'url' => array('controller' => 'accounts', 'action' => 'income')),
array('name' => 'Expense', 'url' => array('controller' => 'accounts', 'action' => 'expense')),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* override: sideMenuLinks
* - Generates controller specific links for the side menu
*/
function sideMenuLinks() {
return array_merge(parent::sideMenuLinks(), $this->sidemenu_links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index
* - Lists all accounts
*/
function index() {
$this->all();
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: asset
* - Lists all asset accounts
*/
function asset() {
$title = 'Asset Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'ASSET')));
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: liability
* - Lists all liability accounts
*/
function liability() {
$title = 'Liability Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'LIABILITY')));
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: equity
* - Lists all equity accounts
*/
function equity() {
$title = 'Equity Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'EQUITY')));
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: income
* - Lists all income accounts
*/
function income() {
$title = 'Income Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'INCOME')));
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: expense
* - Lists all expense accounts
*/
function expense() {
$title = 'Expense Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'EXPENSE')));
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: all
* - Lists all accounts
*/
function all() {
$title = 'All Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: view
* - Displays information about a specific account
*/
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
// Get details about the account and its ledgers (no ledger entries yet)
$this->Account->Behaviors->attach('Containable');
$account = $this->Account->find
('first',
array('contain' =>
array(// Models
'CurrentLedger' =>
array('fields' => array('id', 'sequence')),
'Ledger' =>
array('order' => array('Ledger.open_stamp' => 'DESC')),
),
'conditions' => array(array('Account.id' => $id)),
)
);
$this->Account->Behaviors->detach('Containable');
// Get all ledger entries of the CURRENT ledger
$entries = $this->Account->findLedgerEntries($id);
$account['CurrentLedger']['LedgerEntry'] = $entries['Entries'];
// Summarize each ledger
foreach($account['Ledger'] AS &$ledger)
$ledger = array_merge($ledger,
$this->Account->Ledger->stats($ledger['id']));
// Obtain the overall account balance
$account['Account'] =
array_merge($account['Account'],
array('stats' => $this->Account->stats($id)));
$balance = $account['Account']['stats']['Ledger']['balance'];
// Prepare to render
$title = 'Account: ' . $account['Account']['name'];
$this->set(compact('account', 'title', 'balance'));
}
}