Files
pmgr/controllers/tenders_controller.php
abijah 23cdd0e497 Merge in from surplus_account_20090815 r592
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@593 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-08-17 02:22:54 +00:00

259 lines
9.0 KiB
PHP

<?php
class TendersController 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);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index / all
* - Generate a listing of Tenders
*/
function index() { $this->all(); }
function all() { $this->gridView('All Legal Tender', 'all'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* 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) {
return array
('link' =>
array('TenderType',
'Customer',
'LedgerEntry' =>
array('Transaction',
),
),
);
}
function gridDataRecordsExecute(&$params, &$model, $query) {
$tquery = array_diff_key($query, array('fields'=>1,'group'=>1,'limit'=>1,'order'=>1));
$tquery['fields'] = array("SUM(COALESCE(LedgerEntry.amount,0)) AS 'total'");
$total = $model->find('first', $tquery);
$params['userdata']['total'] = $total[0]['total'];
return parent::gridDataRecordsExecute($params, $model, $query);
}
function gridDataPostProcessCalculatedFields(&$params, &$model, &$records) {
parent::gridDataPostProcessCalculatedFields($params, $model, $records);
foreach ($records AS &$record) {
// REVISIT <AP>: 20090730
// We really need the grid to handle this. We probably need to
// either create a hidden column with the nsf id, or pass back
// a list of nsf items as user data. We can then add an onload
// function to sweep through the nsf items and format them.
// For now... this works.
if (!empty($record['Tender']['nsf_transaction_id']))
$record['Tender']['name'] =
'<SPAN class="nsf-tender">' . $record['Tender']['name'] . '</SPAN>';
}
}
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['Tender'] = array('name', 'id');
$links['Customer'] = array('name');
$links['TenderType'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: deposit
* - Prepares the books for a bank deposit
*/
function deposit() {
// Prepare a close page...
$deposit_types = $this->Tender->TenderType->depositTypes(
// Testing... limit to only one type
//array('limit' => 1)
);
$deposit_accounts = $this->Tender->TenderType->Account->depositAccounts();
foreach ($deposit_types AS $type_id => &$type)
$type = array('id' => $type_id,
'name' => $type,
'stats' => $this->Tender->TenderType->stats($type_id));
//pr(compact('deposit_types', 'deposit_accounts'));
$title = 'Prepare Deposit';
$this->set(compact('title', 'deposit_types', 'deposit_accounts'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: nsf
* - Marks a tender as having insufficient funds.
*/
function nsf($id = null) {
if ($this->data) {
$result = $this->Tender->nsf
($this->data['Tender']['id'],
$this->data['Transaction']['stamp'],
$this->data['Transaction']['comment']);
$this->redirect(array('controller' => 'tenders',
'action' => 'view',
$this->data['Tender']['id']));
}
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tender->id = $id;
$tender = $this->Tender->find
('first', array
('contain' => array('Customer', 'LedgerEntry' => array('Transaction')),
));
// Prepare to render.
$title = "Tender #{$tender['Tender']['id']} : {$tender['Tender']['name']} : NSF";
$this->set(compact('tender', 'title'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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 Tender and related fields
$this->Tender->id = $id;
$tender = $this->Tender->find
('first', array
('contain' => array('TenderType', 'Customer', 'LedgerEntry' => array('Transaction')),
));
// Set up dynamic menu items
$this->sidemenu_links[] =
array('name' => 'Operations', 'header' => true);
// Watch out for the special "Closing" entries
if (!empty($tender['TenderType']['id']))
$this->sidemenu_links[] =
array('name' => 'Edit',
'url' => array('action' => 'edit',
$id));
if (!empty($tender['Tender']['deposit_transaction_id'])
&& empty($tender['Tender']['nsf_transaction_id'])
// Hard to tell what types of items can come back as NSF.
// For now, assume iff it is a named item, it can be NSF.
// (or if we're in development mode)
&& (!empty($tender['TenderType']['data1_name']) || !empty($this->params['dev']))
) {
$this->sidemenu_links[] =
array('name' => 'NSF',
'url' => array('action' => 'nsf',
$id));
}
// Prepare to render.
$title = "Tender #{$tender['Tender']['id']} : {$tender['Tender']['name']}";
$this->set(compact('tender', 'title'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: edit
* - Edit tender information
*/
function edit($id = null) {
if (isset($this->data)) {
// Check to see if the operation was cancelled.
if (isset($this->params['form']['cancel'])) {
if (empty($this->data['Tender']['id']))
$this->redirect(array('action'=>'index'));
$this->redirect(array('action'=>'view', $this->data['Tender']['id']));
}
// Make sure we have tender data
if (empty($this->data['Tender']) || empty($this->data['Tender']['id']))
$this->redirect(array('action'=>'index'));
// Figure out which tender type was chosen
// REVISIT <AP>: 20090810; Not ready to change tender type
// $tender_type_id = $this->data['Tender']['tender_type_id'];
$tender_type_id = $this->Tender->field('tender_type_id');
if (empty($tender_type_id))
$this->redirect(array('action'=>'view', $this->data['Tender']['id']));
// Get data fields from the selected tender type
$this->data['Tender'] += $this->data['type'][$tender_type_id];
unset($this->data['type']);
// Save the tender and all associated data
$this->Tender->create();
$this->Tender->id = $this->data['Tender']['id'];
if (!$this->Tender->save($this->data, false)) {
$this->Session->setFlash("TENDER SAVE FAILED", true);
pr("TENDER SAVE FAILED");
}
$this->redirect(array('action'=>'view', $this->Tender->id));
}
if ($id) {
$this->data = $this->Tender->findById($id);
} else {
$this->redirect(array('action'=>'index'));
}
$tender_types = $this->Tender->TenderType->find
('list', array('order' => array('name')));
$this->set(compact('tender_types'));
$types = $this->Tender->TenderType->find('all', array('contain' => false));
$this->set(compact('types'));
// Prepare to render.
$title = ('Tender #' . $this->data['Tender']['id'] .
' : ' . $this->data['Tender']['name'] .
" : Edit");
$this->set(compact('title'));
}
}