git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@368 97e9348a-65ac-dc4b-aefc-98561f571b83
255 lines
8.5 KiB
PHP
255 lines
8.5 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 gridDataSetup(&$params) {
|
|
parent::gridDataSetup($params);
|
|
}
|
|
|
|
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 (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);
|
|
extract($params['post']['custom']);
|
|
|
|
/* if (isset($params['post']['custom']['reconcile_id'])) { */
|
|
/* $fields[] = array("IF(StatementEntry.type = 'CHARGE',", */
|
|
/* " COALESCE(AppliedCharge.amount,0),", */
|
|
/* " COALESCE(AppliedPayment.amount,0))", */
|
|
/* " AS 'applied'"); */
|
|
/* $fields[] = array("StatementEntry.amount - (", */
|
|
/* "IF(StatementEntry.type = 'CHARGE',", */
|
|
/* " COALESCE(AppliedCharge.amount,0),", */
|
|
/* " COALESCE(AppliedPayment.amount,0))", */
|
|
/* ") AS 'balance'"); */
|
|
/* } */
|
|
|
|
$fields = array_merge($fields,
|
|
$this->StatementEntry->chargePaymentFields());
|
|
|
|
if ($params['action'] === 'collected')
|
|
$fields[] = 'MAX(Receipt.stamp) AS last_paid';
|
|
|
|
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($payment_accounts))
|
|
$conditions[] = array('PaymentEntry.account_id' => $payment_accounts);
|
|
|
|
if (isset($charge_accounts))
|
|
$conditions[] = array('ChargeEntry.account_id' => $charge_accounts);
|
|
|
|
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);
|
|
|
|
if ($index === 'Transaction.stamp')
|
|
$order[] = 'StatementEntry.id ' . $direction;
|
|
|
|
return $order;
|
|
}
|
|
|
|
function gridDataRecords(&$params, &$model, $query) {
|
|
if ($params['action'] === 'collected') {
|
|
$tquery = array_diff_key($query, array(/*'fields'=>1,*/'group'=>1,'limit'=>1,'order'=>1));
|
|
$tquery['group'] = array('AppliedPayment.id');
|
|
$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'",
|
|
);
|
|
|
|
$total = $model->find('first', $tquery);
|
|
$params['userdata']['total'] = $total[0]['applied'];
|
|
$params['userdata']['balance'] = $total[0]['balance'];
|
|
}
|
|
|
|
return parent::gridDataRecords($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
|
|
$entry = $this->StatementEntry->find
|
|
('first',
|
|
array('contain' => array
|
|
('Transaction' => array('fields' => array('id', 'stamp')),
|
|
'Account' => array('id', 'name', 'type', 'trackable'),
|
|
'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);
|
|
//pr($stats);
|
|
|
|
// Prepare to render.
|
|
$title = "Statement Entry #{$entry['StatementEntry']['id']}";
|
|
$this->set(compact('entry', 'title', 'reconciled', 'stats'));
|
|
}
|
|
|
|
}
|