Files
pmgr/site/controllers/statement_entries_controller.php

280 lines
9.6 KiB
PHP

<?php
class StatementEntriesController extends AppController {
var $sidemenu_links = array();
/**************************************************************************
**************************************************************************
**************************************************************************
* override: sideMenuLinks
* - Generates controller specific links for the side menu
*/
function sideMenuLinks() {
return array_merge(parent::sideMenuLinks(), $this->sidemenu_links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* virtuals: gridData
* - With the application controller handling the gridData action,
* these virtual functions ensure that the correct data is passed
* to jqGrid.
*/
function gridDataCountTables(&$params, &$model) {
$link =
array(// Models
'Transaction' =>
array('fields' => array('id', 'stamp'),
),
'Customer' =>
array('fields' => array('id', 'name'),
),
'Lease' =>
array('fields' => array('id', 'number'),
'Unit' =>
array('fields' => array('id', 'name'),
),
),
);
if (isset($params['post']['custom']['statement_entry_id'])) {
$link['PaymentEntry'] = array();
$link['ChargeEntry'] = array();
}
if (isset($params['post']['custom']['account_id'])) {
$link['LedgerEntry'] = array('fields' => array('id'));
$link['LedgerEntry']['Account'] = array('fields' => array('id', 'name', 'type'));
}
/* if (count(array_intersect($params['fields'], array('applied'))) == 1) { */
/* $link['PaymentEntry'] = array(); */
/* $link['ChargeEntry'] = array(); */
/* } */
return array('link' => $link);
}
function gridDataTables(&$params, &$model) {
$tables = $this->gridDataCountTables($params, $model);
$tables['link']['LedgerEntry'] = array('fields' => array('id'));
$tables['link']['LedgerEntry']['Account'] = array('fields' => array('id', 'name', 'type'));
return $tables;
}
function gridDataFields(&$params, &$model) {
//foreach(
$fields = parent::gridDataFields($params, $model);
$fields[] = "COUNT(LedgerEntry.id) AS ledger_entry_count";
if (in_array('applied', $params['post']['fields'])) {
$fields[] = ("IF(StatementEntry.type = 'CHARGE'," .
" SUM(COALESCE(PaymentEntry.amount,0))," .
" SUM(COALESCE(ChargeEntry.amount,0)))" .
" AS 'applied'");
$fields[] = ("StatementEntry.amount - (" .
"IF(StatementEntry.type = 'CHARGE'," .
" SUM(COALESCE(PaymentEntry.amount,0))," .
" SUM(COALESCE(ChargeEntry.amount,0)))" .
") AS 'balance'");
}
$fields = array_merge($fields,
$this->StatementEntry->chargePaymentFields());
return $fields;
}
function gridDataConditions(&$params, &$model) {
$conditions = parent::gridDataConditions($params, $model);
extract($params['post']['custom']);
if (!empty($from_date))
$conditions[]
= array('Transaction.stamp >=' =>
$this->StatementEntry->Transaction->dateFormatBeforeSave($from_date));
if (!empty($through_date))
$conditions[]
= array('Transaction.stamp <=' =>
$this->StatementEntry->Transaction->dateFormatBeforeSave($through_date . ' 23:59:59'));
if (isset($account_id))
$conditions[] = array('LedgerEntry.account_id' => $account_id);
if (isset($statement_entry_id)) {
$conditions[] = array('OR' =>
array(array('ChargeEntry.id' => $statement_entry_id),
array('PaymentEntry.id' => $statement_entry_id)));
}
return $conditions;
}
function gridDataPostProcessCalculatedFields(&$params, &$model, &$records) {
parent::gridDataPostProcessCalculatedFields($params, $model, $records);
foreach ($records AS &$record) {
if ($record['StatementEntry']['ledger_entry_count'] > 1)
$record['Account']['name'] = 'Multiple';
}
}
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['StatementEntry'] = array('id');
$links['Transaction'] = array('id');
$links['Account'] = array('name');
$links['Customer'] = array('name');
$links['Lease'] = array('number');
$links['Unit'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
}
function gridDataOrder(&$params, &$model, $index, $direction) {
$order = parent::gridDataOrder($params, $model, $index, $direction);
// After sorting by whatever the user wants, add these
// defaults into the sort mechanism. If we're already
// sorting by one of them, it will only be redundant,
// and should cause no harm (possible a longer query?)
$order[] = 'Transaction.stamp ' . $direction;
$order[] = 'StatementEntry.effective_date ' . $direction;
$order[] = 'StatementEntry.id ' . $direction;
return $order;
}
function gridDataRecordsExecute(&$params, &$model, $query) {
if (in_array('applied', $params['post']['fields'])) {
$tquery = array_diff_key($query, array('fields'=>1,'group'=>1,'limit'=>1,'order'=>1));
$tquery['fields'] = array("IF(StatementEntry.type = 'CHARGE'," .
" SUM(COALESCE(StatementFraction.amount,0))," .
" SUM(COALESCE(ChargeEntry.amount,0)))" .
" AS 'applied'",
"StatementEntry.amount - (" .
"IF(StatementEntry.type = 'CHARGE'," .
" SUM(COALESCE(PaymentEntry.amount,0))," .
" SUM(COALESCE(ChargeEntry.amount,0)))" .
") AS 'balance'",
);
//pr(compact('tquery'));
$total = $model->find('first', $tquery);
$params['userdata']['total'] = $total[0]['applied'];
$params['userdata']['balance'] = $total[0]['balance'];
}
else {
$tquery = array_diff_key($query, array('fields'=>1,'group'=>1,'limit'=>1,'order'=>1));
$tquery['fields'] = array("SUM(COALESCE(StatementEntry.amount,0)) AS 'total'");
$total = $model->find('first', $tquery);
$params['userdata']['total'] = $total[0]['total'];
}
return parent::gridDataRecordsExecute($params, $model, $query);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: reverse the ledger entry
*/
function reverse($id) {
$this->StatementEntry->reverse($id);
$this->redirect(array('action'=>'view', $id));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: view
* - Displays information about a specific entry
*/
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('controller' => 'accounts', 'action'=>'index'));
}
// Get the StatementEntry and related fields
$this->StatementEntry->prClassLevel(30, 'Model');
$entry = $this->StatementEntry->find
('first',
array('contain' => array
('Transaction' => array('fields' => array('id', 'stamp')),
'Customer' => array('fields' => array('id', 'name')),
'Lease' => array('fields' => array('id')),
'LedgerEntry' => array('fields' => array('id'),
'Account' => array('id', 'name', 'type')),
),
'conditions' => array('StatementEntry.id' => $id),
));
pr($entry);
//die();
$reconciled = $this->StatementEntry->reconciledEntries($id);
/* // REVISIT <AP>: 20090711 */
/* // It's not clear whether we should be able to reverse charges that have */
/* // already been paid/cleared/reconciled. Certainly, that will be the */
/* // case when someone has pre-paid and then moves out early. However, this */
/* // will work well for items accidentally charged but not yet paid for. */
/* if ((!$entry['DebitLedger']['Account']['trackable'] || */
/* $stats['debit']['amount_reconciled'] == 0) && */
/* (!$entry['CreditLedger']['Account']['trackable'] || */
/* $stats['credit']['amount_reconciled'] == 0) */
/* && 0 */
/* ) */
/* { */
/* // Set up dynamic menu items */
/* $this->sidemenu_links[] = */
/* array('name' => 'Operations', 'header' => true); */
/* $this->sidemenu_links[] = */
/* array('name' => 'Undo', */
/* 'url' => array('action' => 'reverse', */
/* $id)); */
/* } */
/* if ($this->StatementEntry->Ledger->Account->type */
/* ($entry['CreditLedger']['Account']['id']) == 'INCOME') */
/* { */
/* // Set up dynamic menu items */
/* $this->sidemenu_links[] = */
/* array('name' => 'Operations', 'header' => true); */
/* $this->sidemenu_links[] = */
/* array('name' => 'Reverse', */
/* 'url' => array('action' => 'reverse', */
/* $id)); */
/* } */
$stats = $this->StatementEntry->stats($id);
if (strtoupper($entry['StatementEntry']['type']) === 'CHARGE')
$stats = $stats['Charge'];
else
$stats = $stats['Payment'];
// Prepare to render.
$title = "Statement Entry #{$entry['StatementEntry']['id']}";
$this->set(compact('entry', 'title', 'reconciled', 'stats'));
}
}