git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@425 97e9348a-65ac-dc4b-aefc-98561f571b83
179 lines
6.0 KiB
PHP
179 lines
6.0 KiB
PHP
<?php
|
|
class Ledger extends AppModel {
|
|
|
|
var $belongsTo = array(
|
|
'Account',
|
|
'PriorLedger' => array('className' => 'Ledger'),
|
|
'CloseTransaction' => array('className' => 'Transaction'),
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'Transaction',
|
|
'LedgerEntry',
|
|
);
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: accountID
|
|
* - Returns the account ID for the given ledger
|
|
*/
|
|
function accountID($id) {
|
|
$this->cacheQueries = true;
|
|
$item = $this->find('first', array
|
|
('link' => array('Account'),
|
|
'conditions' => array('Ledger.id' => $id),
|
|
));
|
|
$this->cacheQueries = false;
|
|
//pr(compact('id', 'item'));
|
|
return $item['Account']['id'];
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: currentLedgerID
|
|
* - Returns the current ledger ID of the account for the given ledger.
|
|
*/
|
|
function currentLedgerID($id) {
|
|
return $this->Account->currentLedgerID($this->accountID($id));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: closeLedger
|
|
* - Closes the current ledger, and returns a fresh one
|
|
*/
|
|
function closeLedgers($ids) {
|
|
$ret = array('new_ledger_ids' => array());
|
|
|
|
$entries = array();
|
|
foreach ($ids AS $id) {
|
|
// Query stats to get the balance forward
|
|
$stats = $this->stats($id);
|
|
|
|
// Populate fields from the current ledger
|
|
$this->recursive = -1;
|
|
$this->id = $id;
|
|
$this->read();
|
|
|
|
// Build a new ledger to replace the current one
|
|
$this->data['Ledger']['id'] = null;
|
|
$this->data['Ledger']['close_transaction_id'] = null;
|
|
$this->data['Ledger']['prior_ledger_id'] = $id;
|
|
$this->data['Ledger']['comment'] = null;
|
|
++$this->data['Ledger']['sequence'];
|
|
$this->data['Ledger']['name'] =
|
|
($this->data['Ledger']['account_id'] .
|
|
'-' .
|
|
$this->data['Ledger']['sequence']);
|
|
|
|
// Save the new ledger
|
|
$this->id = null;
|
|
if (!$this->save($this->data, false))
|
|
return array('error' => true, 'new_ledger_data' => $this->data) + $ret;
|
|
$ret['new_ledger_ids'][] = $this->id;
|
|
|
|
$entries[] = array('old_ledger_id' => $id,
|
|
'new_ledger_id' => $this->id,
|
|
'amount' => $stats['balance']);
|
|
}
|
|
|
|
// Perform the close
|
|
$result = $this->Transaction->addClose(array('Transaction' => array(),
|
|
'Ledger' => $entries));
|
|
$ret['Transaction'] = $result;
|
|
if ($result['error'])
|
|
return array('error' => true) + $ret;
|
|
|
|
return $ret + array('error' => false);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: debitCreditFields
|
|
* - Returns the fields necessary to determine whether the queried
|
|
* entries are a debit, or a credit, and also the effect each have
|
|
* on the overall balance of the ledger.
|
|
*/
|
|
function debitCreditFields($sum = false, $balance = true,
|
|
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
|
return $this->LedgerEntry->debitCreditFields
|
|
($sum, $balance, $entry_name, $account_name);
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: ledgerEntries
|
|
* - Returns an array of ledger entries that belong to a given
|
|
* ledger. There is extra work done to establish debit/credit
|
|
*/
|
|
function ledgerEntries($ids, $query = null) {
|
|
if (empty($ids))
|
|
return null;
|
|
|
|
$entries = $this->LedgerEntry->find
|
|
('all', array
|
|
('link' => array('Ledger' => array('Account')),
|
|
'fields' => array_merge(array("LedgerEntry.*"),
|
|
$this->LedgerEntry->debitCreditFields()),
|
|
'conditions' => array('LedgerEntry.ledger_id' => $ids),
|
|
));
|
|
|
|
//pr(compact('entries'));
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: stats
|
|
* - Returns summary data from the requested ledger.
|
|
*/
|
|
function stats($id, $query = null) {
|
|
if (!$id)
|
|
return null;
|
|
|
|
$this->queryInit($query);
|
|
|
|
if (!isset($query['link']['Account']))
|
|
$query['link']['Account'] = array();
|
|
if (!isset($query['link']['Account']['fields']))
|
|
$query['link']['Account']['fields'] = array();
|
|
if (!isset($query['fields']))
|
|
$query['fields'] = array();
|
|
|
|
$query['fields'] = array_merge($query['fields'],
|
|
$this->debitCreditFields(true));
|
|
|
|
$query['conditions'][] = array('LedgerEntry.ledger_id' => $id);
|
|
$query['group'][] = 'LedgerEntry.ledger_id';
|
|
|
|
$stats = $this->LedgerEntry->find('first', $query);
|
|
|
|
// The fields are all tucked into the [0] index,
|
|
// and the rest of the array is useless (empty).
|
|
$stats = $stats[0];
|
|
|
|
// Make sure we have a member for debit/credit
|
|
foreach(array('debits', 'credits') AS $crdr)
|
|
if (!isset($stats[$crdr]))
|
|
$stats[$crdr] = null;
|
|
|
|
// Make sure we have a non-null balance
|
|
if (!isset($stats['balance']))
|
|
$stats['balance'] = 0;
|
|
|
|
return $stats;
|
|
}
|
|
|
|
}
|
|
?>
|