git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@201 97e9348a-65ac-dc4b-aefc-98561f571b83
276 lines
9.6 KiB
PHP
276 lines
9.6 KiB
PHP
<?php
|
|
|
|
class AccountsController extends AppController {
|
|
|
|
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')),
|
|
array('name' => 'Bank Deposit', 'url' => array('controller' => 'accounts', 'action' => 'deposit')),
|
|
);
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* override: sideMenuLinks
|
|
* - Generates controller specific links for the side menu
|
|
*/
|
|
function sideMenuLinks() {
|
|
return array_merge(parent::sideMenuLinks(), $this->sidemenu_links);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: index / asset / liability / equity / income / expense / all
|
|
* - Generate a chart of accounts
|
|
*/
|
|
|
|
function index() { $this->all(); }
|
|
function asset() { $this->jqGridView('Asset Accounts'); }
|
|
function liability() { $this->jqGridView('Liability Accounts'); }
|
|
function equity() { $this->jqGridView('Equity Accounts'); }
|
|
function income() { $this->jqGridView('Income Accounts'); }
|
|
function expense() { $this->jqGridView('Expense Accounts'); }
|
|
function all() { $this->jqGridView('All Accounts', '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 parent::jqGridDataTables($params, $model);
|
|
}
|
|
|
|
function jqGridDataTables(&$params, &$model) {
|
|
return array
|
|
('link' =>
|
|
array(// Models
|
|
'CurrentLedger' => array
|
|
(// Models
|
|
'LedgerEntry'
|
|
/* REVISIT <AP> 20090615:
|
|
* I'll remove this 'conditions' section on a future checkin,
|
|
* after I've proven out the %{MODEL_ALIAS} feature will be
|
|
* sticking around.
|
|
|
|
=> array
|
|
('conditions' =>
|
|
array('OR' =>
|
|
array('LedgerEntry.debit_ledger_id = CurrentLedger.id',
|
|
'LedgerEntry.credit_ledger_id = CurrentLedger.id'),
|
|
),
|
|
),
|
|
|
|
* END REVISIT
|
|
*/
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
function jqGridDataFields(&$params, &$model) {
|
|
return array
|
|
('Account.*',
|
|
'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');
|
|
}
|
|
|
|
function jqGridDataConditions(&$params, &$model) {
|
|
$conditions = parent::jqGridDataConditions($params, $model);
|
|
|
|
if (in_array($params['action'], array('asset', 'liability', 'equity', 'income', 'expense'))) {
|
|
$conditions[] = array('Account.type' => strtoupper($params['action']));
|
|
}
|
|
|
|
return $conditions;
|
|
}
|
|
|
|
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
|
|
$links['Account'] = array('name');
|
|
return parent::jqGridRecordLinks($params, $model, $records, $links);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: newledger
|
|
* - Close the current account ledger and create a new one,
|
|
* carrying forward any balance if necessary.
|
|
*/
|
|
|
|
function newledger($id = null) {
|
|
if (!$this->Account->closeCurrentLedger($id)) {
|
|
$this->Session->setFlash(__('Unable to create new Ledger.', true));
|
|
}
|
|
if ($id)
|
|
$this->redirect(array('action'=>'view', $id));
|
|
else
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: deposit
|
|
* - Prepares the books for a bank deposit
|
|
*/
|
|
function deposit() {
|
|
if ($this->data) {
|
|
// Action the close based on provided data
|
|
//pr($this->data);
|
|
|
|
// Get data about each closed ledger.
|
|
$deposit = array('total' => 0, 'ledgers' => array());
|
|
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
|
|
if (!$ledger['checked'])
|
|
continue;
|
|
|
|
$ledger_entries =
|
|
$this->Account->Ledger->find
|
|
('all',
|
|
array('link' => array
|
|
('Account' =>
|
|
array('fields' => array('name')),
|
|
|
|
'LedgerEntry' =>
|
|
array('fields' => array('amount'),
|
|
|
|
'MonetarySource' =>
|
|
array('fields' => array('name')),
|
|
|
|
'Customer' =>
|
|
array('fields' => array('name')),
|
|
|
|
//'Transaction' =>
|
|
//array('fields' => array('stamp')),
|
|
),
|
|
),
|
|
'fields' => false,
|
|
'conditions' => array(array('Ledger.id' => $ledger_id),
|
|
array('LedgerEntry.id IS NOT NULL'),
|
|
),
|
|
));
|
|
|
|
$deposit['total'] += $ledger['amount'];
|
|
$deposit['ledgers'][] = array('name' => $ledger['account_name'],
|
|
'total' => $ledger['amount'],
|
|
'entries' => $ledger_entries);
|
|
}
|
|
|
|
// Estabish a set of ledgers to close
|
|
$set = array();
|
|
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
|
|
if (!$ledger['checked'] || $ledger['amount'] == 0)
|
|
continue;
|
|
$set[] = array('ledger_id' => $ledger_id, 'amount' => $ledger['amount']);
|
|
}
|
|
|
|
// Perform the accounting work necessary to close the
|
|
// monetary ledgers and deposit into the bank account.
|
|
if (count($set) > 0)
|
|
$this->Account->closeAndDeposit($set, $this->data['Deposit']['Account']['id']);
|
|
|
|
$title = 'Account: Deposit Slip';
|
|
$this->set(compact('title', 'deposit'));
|
|
$this->render('deposit_slip');
|
|
return;
|
|
}
|
|
|
|
// Prepare a close page...
|
|
$tillable_account = $this->Account->relatedAccounts('tillable');
|
|
$depositable_account = $this->Account->relatedAccounts('depositable');
|
|
|
|
foreach ($tillable_account AS &$acct) {
|
|
$acct['Account']['stats'] = $this->Account->stats($acct['Account']['id']);
|
|
}
|
|
|
|
$title = 'Account: Prepare Deposit';
|
|
$this->set(compact('title', 'tillable_account', 'depositable_account'));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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)
|
|
$account = $this->Account->find
|
|
('first',
|
|
array('contain' =>
|
|
array(// Models
|
|
'CurrentLedger' =>
|
|
array('fields' => array('id', 'sequence')),
|
|
|
|
'Ledger' =>
|
|
array('Close' => array
|
|
('order' => array('Close.stamp' => 'DESC'))),
|
|
),
|
|
'conditions' => array(array('Account.id' => $id)),
|
|
)
|
|
);
|
|
|
|
// 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 stats across ALL ledgers for the summary infobox
|
|
$stats = $this->Account->stats($id, true);
|
|
$stats = $stats['Ledger'];
|
|
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Operations', 'header' => true);
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'New Ledger', 'url' => array('action' => 'newledger', $id));
|
|
|
|
// Prepare to render
|
|
$title = 'Account: ' . $account['Account']['name'];
|
|
$this->set(compact('account', 'title', 'stats'));
|
|
}
|
|
|
|
}
|