git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@71 97e9348a-65ac-dc4b-aefc-98561f571b83
245 lines
8.9 KiB
PHP
245 lines
8.9 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_account_id = Account.id, LedgerEntry.amount, 0)) AS debits',
|
|
'SUM(IF(LedgerEntry.credit_account_id = Account.id, LedgerEntry.amount, 0)) AS credits',
|
|
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
|
|
IF(LedgerEntry.debit_account_id = Account.id, 1, -1),
|
|
IF(LedgerEntry.credit_account_id = Account.id, 1, -1)
|
|
)* LedgerEntry.amount) AS balance",
|
|
'COUNT(LedgerEntry.id) AS entries'),
|
|
'conditions' =>
|
|
array('OR' =>
|
|
array('LedgerEntry.debit_account_id = Account.id',
|
|
'LedgerEntry.credit_account_id = Account.id'),
|
|
),
|
|
),
|
|
),
|
|
|
|
'group' => 'Account.id',
|
|
'order' => array('Account.name' => 'ASC'));
|
|
|
|
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'));
|
|
}
|
|
|
|
/* $this->Account->bindModel(array('hasMany' => array */
|
|
/* ('LedgerEntry' => array */
|
|
/* ('className' => 'LedgerEntry', */
|
|
/* 'foreignKey' => false, */
|
|
/* 'finderQuery' => 'SELECT `LedgerEntry`.* */
|
|
/* FROM pmgr_entries AS `LedgerEntry` */
|
|
/* LEFT JOIN pmgr_transactions AS `Transaction` */
|
|
/* ON `Transaction`.id = `LedgerEntry`.transaction_id */
|
|
/* WHERE LedgerEntry.debit_account_id = ({$__cakeID__$}) */
|
|
/* OR LedgerEntry.credit_account_id = ({$__cakeID__$}) */
|
|
/* ORDER BY Transaction.stamp', */
|
|
/* )))); */
|
|
|
|
/* $this->Account->Behaviors->attach('Containable'); */
|
|
/* $this->Account->Contain(array('LedgerEntry' => array */
|
|
/* ( */
|
|
/* // Models */
|
|
/* 'Transaction' => array */
|
|
/* ( */
|
|
/* // Models */
|
|
/* 'Customer', */
|
|
/* )), */
|
|
|
|
/* )); */
|
|
/* } */
|
|
/* $account = $this->Account->read(null, $id); */
|
|
|
|
$this->Account->recursive = -1;
|
|
$account = $this->Account->read(null, $id);
|
|
|
|
if (in_array($account['Account']['type'], array('ASSET', 'EXPENSE')))
|
|
$account_type = 'debit';
|
|
else
|
|
$account_type = 'credit';
|
|
|
|
$ledger_id = $account['current_ledger_id'];
|
|
$account['CurrentLedger']['LedgerEntry'] =
|
|
$this->Account->Ledger->LedgerEntry->find
|
|
('all',
|
|
array('link' => array
|
|
(// Models
|
|
'fields' =>
|
|
array('id', 'name', 'comment',
|
|
"IF(LedgerEntry.debit_ledger_id = $id, LedgerEntry.amount, NULL) AS debit",
|
|
"IF(LedgerEntry.credit_ledger_id = $id, LedgerEntry.amount, NULL) AS credit",
|
|
"(IF(LedgerEntry.{$account_type}_ledger_id = $id, 1, -1)
|
|
* LedgerEntry.amount) AS balance"),
|
|
'conditions' =>
|
|
array('OR' =>
|
|
array('LedgerEntry.debit_ledger_id = Account.id',
|
|
'LedgerEntry.credit_ledger_id = Account.id'),
|
|
|
|
|
|
'Transaction' =>
|
|
array(// Models
|
|
'Customer',
|
|
)),
|
|
'conditions' => array('OR' =>
|
|
array("LedgerEntry.debit_account_id = $id",
|
|
"LedgerEntry.credit_account_id = $id")),
|
|
'order' => array('Transaction.stamp'),
|
|
//'limit' => 15,
|
|
//'limit' => 2,
|
|
));
|
|
//pr($account);
|
|
|
|
$balance = 0;
|
|
foreach($account['LedgerEntry'] AS &$entry) {
|
|
if (isset($entry[0]))
|
|
$entry = array_merge($entry[0], array_diff_key($entry, array(0)));
|
|
|
|
$balance += $entry['balance'];
|
|
}
|
|
|
|
$title = 'Account: ' . $account['Account']['name'];
|
|
$this->set(compact('account', 'title', 'balance'));
|
|
}
|
|
}
|