git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@472 97e9348a-65ac-dc4b-aefc-98561f571b83
290 lines
9.8 KiB
PHP
290 lines
9.8 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 gridDataTables(&$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'),
|
|
),
|
|
),
|
|
|
|
'Account' =>
|
|
array('fields' => array('id', 'name', 'type'),
|
|
),
|
|
);
|
|
|
|
if (isset($params['post']['custom']['statement_entry_id'])) {
|
|
$link['PaymentEntry'] = array();
|
|
$link['ChargeEntry'] = array();
|
|
}
|
|
|
|
/* if ($params['action'] === 'collected') { */
|
|
/* $link['PaymentEntry'] = array('Receipt' => array('class' => 'Transaction')); */
|
|
/* $link['ChargeEntry'] = array('Invoice' => array('class' => 'Transaction')); */
|
|
/* } */
|
|
|
|
/* if (count(array_intersect($params['fields'], array('applied'))) == 1) { */
|
|
/* $link['PaymentEntry'] = array(); */
|
|
/* $link['ChargeEntry'] = array(); */
|
|
/* } */
|
|
/* elseif (isset($params['post']['custom']['customer_id']) || isset($params['post']['custom']['lease_id'])) { */
|
|
/* $link['PaymentEntry'] = array(); */
|
|
/* } */
|
|
|
|
return array('link' => $link);
|
|
}
|
|
|
|
function gridDataFields(&$params, &$model) {
|
|
$fields = parent::gridDataFields($params, $model);
|
|
|
|
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('StatementEntry.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 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(PaymentEntry.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: waive the ledger entry
|
|
*/
|
|
|
|
function waive($id) {
|
|
$this->StatementEntry->waive($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
|
|
$entry = $this->StatementEntry->find
|
|
('first',
|
|
array('contain' => array
|
|
('Transaction' => array('fields' => array('id', 'stamp')),
|
|
'Account' => array('id', 'name', 'type'),
|
|
'Customer' => array('fields' => array('id', 'name')),
|
|
'Lease' => array('fields' => array('id')),
|
|
),
|
|
|
|
'conditions' => array('StatementEntry.id' => $id),
|
|
));
|
|
|
|
$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'];
|
|
|
|
|
|
if (strtoupper($entry['StatementEntry']['type']) === 'CHARGE' &&
|
|
$stats['balance'] > 0) {
|
|
// Set up dynamic menu items
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Operations', 'header' => true);
|
|
|
|
$this->sidemenu_links[] =
|
|
array('name' => 'Waive',
|
|
'url' => array('action' => 'waive',
|
|
$id));
|
|
}
|
|
|
|
// Prepare to render.
|
|
$title = "Statement Entry #{$entry['StatementEntry']['id']}";
|
|
$this->set(compact('entry', 'title', 'reconciled', 'stats'));
|
|
}
|
|
|
|
}
|