Many, many changes, and yet still much to do. Many things are working, but certainly nothing beyond simply data retrieval (no editing or adding of any data). Also, some work is still required to ensure the grids have the right columns; we can strip out certain columns for some views (such as removing customer from the leases grid of the customer view... completely redundant). And of course, there are still many bugs and lots to clean up.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@368 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-23 01:49:46 +00:00
parent 0ba5007438
commit 234999b4d2
46 changed files with 616 additions and 1128 deletions

View File

@@ -25,221 +25,35 @@ class LedgerEntriesController extends AppController {
* to jqGrid.
*/
function gridDataSetup(&$params) {
parent::gridDataSetup($params);
if (isset($params['custom']['collected_account_id']))
$params['custom']['account_id'] = $params['custom']['collected_account_id'];
}
function gridDataTables(&$params, &$model) {
$link =
array(// Models
'Account' =>
array('fields' => array('id', 'name', 'type'),
),
'Transaction' =>
array('fields' => array('id', 'stamp'),
),
'Ledger' =>
array('fields' => array('id', 'sequence'),
),
'Ledger' =>
array('fields' => array('id', 'sequence'),
'Account' =>
array('fields' => array('id', 'name', 'type'),
),
),
'DoubleEntry' => array
('Transaction' =>
array('fields' => array('id', 'stamp'),
),
'Tender' =>
array('fields' => array('id', 'name'),
),
'DebitLedger' =>
array('fields' => array('id'),
'Account' => array('alias' => 'DebitAccount',
'fields' => array('id', 'name'),
),
),
'CreditLedger' =>
array('fields' => array('id'),
'Account' => array('alias' => 'CreditAccount',
'fields' => array('id', 'name'),
),
),
'Customer' =>
array('fields' => array('id', 'name'),
),
'Lease' =>
array('fields' => array('id', 'number'),
'Unit' =>
array('fields' => array('id', 'name'),
),
),
),
/* 'DebitEntry', */
/* 'CreditEntry', */
);
if (count(array_intersect($params['fields'], array('applied'))) == 1) {
$link['Payment'] = array('DoubleEntry' =>
array('alias' => 'PaymentDoubleEntry',
'Receipt'),
'Account' => array('alias' => 'PaymentAccount'),
);
$link['Charge'] = array('DoubleEntry' =>
array('alias' => 'ChargeDoubleEntry',
'Invoice'),
'Account' => array('alias' => 'ChargeAccount'),
);
}
elseif (isset($params['custom']['customer_id']) || isset($params['custom']['lease_id'])) {
$link['Charge'] = array('DoubleEntry' =>
array('alias' => 'ChargeDoubleEntry',
'Invoice'),
'Account' => array('alias' => 'ChargeAccount'),
);
}
return array('link' => $link);
}
function gridDataFields(&$params, &$model) {
$fields = parent::gridDataFields($params, $model);
if (!isset($fields))
$fields = array('Entry.*');
/* if (isset($params['custom']['reconcile_id'])) { */
/* $fields[] = array("IF(Entry.type = 'CHARGE',", */
/* " COALESCE(AppliedCharge.amount,0),", */
/* " COALESCE(AppliedPayment.amount,0))", */
/* " AS 'applied'"); */
/* $fields[] = array("Entry.amount - (", */
/* "IF(Entry.type = 'CHARGE',", */
/* " COALESCE(AppliedCharge.amount,0),", */
/* " COALESCE(AppliedPayment.amount,0))", */
/* ") AS 'balance'"); */
/* } */
if (isset($params['custom']['customer_id']) || isset($params['custom']['lease_id'])) {
$fields[] = "IF(Entry.type = 'CHARGE', DoubleEntry.amount, NULL) AS debit";
$fields[] = "IF(Entry.type = 'PAYMENT', DoubleEntry.amount, NULL) AS credit";
$fields[] = "IF(Entry.type = 'CHARGE', 1, -1) * DoubleEntry.amount AS balance";
}
else {
if (count(array_intersect($params['fields'], array('applied'))) == 1)
$fields[] = ('SUM(COALESCE(AppliedPayment.amount,0)' .
' + COALESCE(AppliedCharge.amount,0)) AS applied');
if (count(array_intersect($params['fields'], array('debit', 'credit'))) >= 1) {
$fields = array_merge($fields,
$this->Entry->DoubleEntry->debitCreditFields());
/* $fields = array_merge($fields, */
/* $this->Entry->Account->debitCreditFields()); */
/* $fields[] = "IF(Entry.crdr = 'CREDIT', DoubleEntry.amount, NULL) AS credit"; */
/* $fields[] = "IF(Entry.crdr = 'DEBIT', DoubleEntry.amount, NULL) AS debit"; */
/* $fields[] = "IF(Account.type IN Entry.crdr = 'DEBIT', DoubleEntry.amount, NULL) AS debit"; */
}
}
if ($params['action'] === 'collected')
$fields[] = 'MAX(Receipt.stamp) AS last_paid';
return $fields;
}
function gridDataConditions(&$params, &$model) {
$conditions = parent::gridDataConditions($params, $model);
if ($params['action'] === 'collected') {
extract($params['custom']);
if (!isset($params['custom']['account_id']))
die("INTERNAL ERROR: ACCOUNT ID NOT SET");
if (!empty($collected_from_date))
$conditions[]
= array('Receipt.stamp >=' =>
$this->Entry->Transaction->dateFormatBeforeSave($collected_from_date));
if (!empty($collected_through_date))
$conditions[]
= array('Receipt.stamp <=' =>
$this->Entry->Transaction->dateFormatBeforeSave($collected_through_date . ' 23:59:59'));
if (isset($collected_payment_accounts))
$conditions[] = array('PaymentAccount.id' => $collected_payment_accounts);
else
$conditions[] = array('NOT' => array(array('PaymentAccount.id' => null)));
}
if (isset($params['custom']['ledger_id'])) {
$ledger_id = $params['custom']['ledger_id'];
$conditions[] = array('Ledger.id' => $ledger_id);
}
if (isset($params['custom']['reconcile_id'])) {
$conditions[] = array('OR' =>
array('AppliedCharge.id' => $reconcile_id),
array('AppliedPayment.id' => $reconcile_id));
}
if (isset($params['custom']['account_id'])) {
$account_id = $params['custom']['account_id'];
$conditions[] = array('Account.id' => $account_id);
}
if (isset($params['custom']['customer_id'])) {
$customer_id = $params['custom']['customer_id'];
$conditions[] =
array('OR' =>
array(array(array('Entry.type' => 'CHARGE'),
array('DoubleEntry.customer_id' => $customer_id)),
array(array('Entry.type' => 'PAYMENT'),
array('ChargeDoubleEntry.customer_id' => $customer_id)),
),
);
}
if (isset($params['custom']['lease_id'])) {
$lease_id = $params['custom']['lease_id'];
$conditions[] =
array('OR' =>
array(array(array('Entry.type' => 'CHARGE'),
array('DoubleEntry.lease_id' => $lease_id)),
array(array('Entry.type' => 'PAYMENT'),
array('ChargeDoubleEntry.lease_id' => $lease_id)),
),
);
/* array('OR' => */
/* array('AND' => */
/* array(array('Entry.type' => 'CHARGE'), */
/* array('DoubleEntry.lease_id' => $lease_id)) */
/* ), */
/* array('AND' => */
/* array(array('Entry.type' => 'PAYMENT'), */
/* array('ChargeDoubleEntry.lease_id' => $lease_id)), */
/* ), */
/* ); */
}
if (isset($params['custom']['transaction_id'])) {
$conditions[] =
array('Transaction.id' => $params['custom']['transaction_id']);
}
return $conditions;
}
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['Transaction'] = array('id');
$links['Entry'] = array('id');
$links['Account'] = array('controller' => 'accounts', 'name');
$links['DebitAccount'] = array('controller' => 'accounts', 'name');
$links['CreditAccount'] = array('controller' => 'accounts', 'name');
$links['MonetarySource'] = array('name');
$links['Customer'] = array('name');
$links['Lease'] = array('number');
$links['Unit'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
}
function gridDataGroup(&$params, &$model) {
return parent::gridDataGroup($params, $model);
return array_merge($fields,
$this->LedgerEntry->debitCreditFields());
}
function gridDataOrder(&$params, &$model, $index, $direction) {
@@ -248,46 +62,19 @@ class LedgerEntriesController extends AppController {
$order = parent::gridDataOrder($params, $model, $index, $direction);
if ($index === 'Transaction.stamp') {
$order[] = 'Entry.id ' . $direction;
$order[] = 'LedgerEntry.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(Entry.type = 'CHARGE',",
" SUM(COALESCE(AppliedCharge.amount,0)),",
" SUM(COALESCE(AppliedPayment.amount,0)))",
" AS 'applied'",
"Charge.amount - (",
"IF(Entry.type = 'CHARGE',",
" SUM(COALESCE(AppliedCharge.amount,0)),",
" SUM(COALESCE(AppliedPayment.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->Entry->reverse($id);
$this->redirect(array('action'=>'view', $id));
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['LedgerEntry'] = array('id');
$links['Transaction'] = array('id');
$links['Ledger'] = array('id');
$links['Account'] = array('controller' => 'accounts', 'name');
$links['Tender'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
}
@@ -305,96 +92,49 @@ class LedgerEntriesController extends AppController {
}
// Get the Entry and related fields
$entry = $this->Entry->find
$entry = $this->LedgerEntry->find
('first',
array('contain' => array
('Account' => array('id', 'name', 'type', 'trackable'),
'DoubleEntry' => array
(//'fields' => array('id'),
'DebitEntry' => array('fields' => array('id', 'crdr')),
'CreditEntry' => array('fields' => array('id', 'crdr')),
'Transaction' => array('fields' => array('id', 'stamp')),
'Customer' => array('fields' => array('id', 'name')),
'Lease' => array('fields' => array('id')),
),
(
'Transaction' =>
array('fields' => array('id', 'stamp'),
),
'Ledger' =>
array('fields' => array('id', 'sequence', 'name'),
'Account' =>
array('fields' => array('id', 'name', 'type', 'trackable'),
),
),
'Tender' =>
array('fields' => array('id', 'name'),
),
'DebitEntry' => array('fields' => array('id', 'crdr')),
'CreditEntry' => array('fields' => array('id', 'crdr')),
),
'conditions' => array('Entry.id' => $id),
'conditions' => array('LedgerEntry.id' => $id),
));
$entry['Entry']['opposite_crdr'] =
ucfirst($this->Entry->Account->fundamentalOpposite($entry['Entry']['crdr']));
$entry['Entry']['matching_entry_id'] =
$entry['DoubleEntry'][
ucfirst(strtolower($entry['Entry']['opposite_crdr'])) .
'Entry']['id'];
/* if ($entry['DoubleEntry']['DebitEntry']['id'] == $entry['Entry']['id']) */
/* $entry['Entry']['matching_entry_id'] = $entry['DoubleEntry']['CreditEntry']['id']; */
/* if ($entry['DoubleEntry']['CreditEntry']['id'] == $entry['Entry']['id']) */
/* $entry['Entry']['matching_entry_id'] = $entry['DoubleEntry']['DebitEntry']['id']; */
//pr(compact('entry'));
$reconciled = $this->Entry->reconciledEntries($id);
//pr(compact('reconciled'));
if (!empty($entry['DebitEntry']) && !empty($entry['CreditEntry']))
die("LedgerEntry has both a matching DebitEntry and CreditEntry");
if (empty($entry['DebitEntry']) && empty($entry['CreditEntry']))
die("LedgerEntry has neither a matching DebitEntry nor a CreditEntry");
if (empty($entry['DebitEntry']) && count($entry['CreditEntry']) != 1)
die("LedgerEntry has more than one CreditEntry");
if (empty($entry['CreditEntry']) && count($entry['DebitEntry']) != 1)
die("LedgerEntry has more than one DebitEntry");
/* // 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->Entry->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)); */
/* } */
if (empty($entry['DebitEntry']))
$entry['MatchingEntry'] = $entry['CreditEntry'][0];
else
$entry['MatchingEntry'] = $entry['DebitEntry'][0];
// Prepare to render.
$title = "Ledger Entry #{$entry['Entry']['id']}";
$this->set(compact('entry', 'title', 'reconciled'));
}
function tst($id = null) {
$entry = $this->Entry->find
('first',
array('contain' => array('Account',
'DoubleEntry',
'Payment' => array('fields' => array('Payment.*'/*, 'AppliedPayment.*'*/),
'DoubleEntry'/* => array('alias' => 'PaymentDoubleEntry')*/),
'Charge' => array('fields' => array('Charge.*'/*, 'AppliedCharge.*'*/),
'DoubleEntry'/* => array('alias' => 'ChargeDoubleEntry')*/),
),
'conditions' => array('Entry.id' => $id),
));
pr($entry);
$title = "Ledger Entry #{$entry['LedgerEntry']['id']}";
$this->set(compact('entry', 'title'));
}
}