git-svn-id: file:///svn-source/pmgr/branches/reconcile_entry_to_receipt_20090629@187 97e9348a-65ac-dc4b-aefc-98561f571b83
241 lines
8.2 KiB
PHP
241 lines
8.2 KiB
PHP
<?php
|
|
class LedgerEntry extends AppModel {
|
|
|
|
var $name = 'LedgerEntry';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'transaction_id' => array('numeric'),
|
|
'amount' => array('money')
|
|
);
|
|
|
|
var $belongsTo = array(
|
|
'MonetarySource',
|
|
'Transaction',
|
|
'Customer',
|
|
'Lease',
|
|
|
|
'DebitLedger' => array(
|
|
'className' => 'Ledger',
|
|
'foreignKey' => 'debit_ledger_id',
|
|
),
|
|
'CreditLedger' => array(
|
|
'className' => 'Ledger',
|
|
'foreignKey' => 'credit_ledger_id',
|
|
),
|
|
);
|
|
|
|
var $hasAndBelongsToMany = array(
|
|
'ReconcilingTransaction' => array(
|
|
'className' => 'Transaction',
|
|
'joinTable' => 'reconciliations',
|
|
'foreignKey' => 'transaction_id',
|
|
'associationForeignKey' => 'ledger_entry_id',
|
|
),
|
|
);
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: conditionEntryAsCreditOrDebit
|
|
* - returns the condition necessary to match a set of
|
|
* Ledgers to all related LedgerEntries
|
|
*/
|
|
function conditionEntryAsCreditOrDebit($ledger_ids) {
|
|
return array('OR' =>
|
|
array(array('debit_ledger_id' => $ledger_ids),
|
|
array('credit_ledger_id' => $ledger_ids)));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: ledgerContext query helpers
|
|
* - Returns parameters necessary to generate a query which
|
|
* puts ledger entries into the context of a ledger. Since
|
|
* debit/credit depends on the account type, it is required
|
|
* as an argument for each function to avoid having to
|
|
* query the ledger/account to find it out.
|
|
*/
|
|
function ledgerContextFields($ledger_id = null, $account_type = null) {
|
|
$fields = array('id', 'name', 'comment', 'amount');
|
|
|
|
if (isset($ledger_id)) {
|
|
$fields[] = ("IF(LedgerEntry.debit_ledger_id = $ledger_id," .
|
|
" LedgerEntry.amount, NULL) AS debit");
|
|
$fields[] = ("IF(LedgerEntry.credit_ledger_id = $ledger_id," .
|
|
" LedgerEntry.amount, NULL) AS credit");
|
|
|
|
if (isset($account_type)) {
|
|
if (in_array($account_type, array('ASSET', 'EXPENSE')))
|
|
$ledger_type = 'debit';
|
|
else
|
|
$ledger_type = 'credit';
|
|
|
|
$fields[] = ("(IF(LedgerEntry.{$ledger_type}_ledger_id = $ledger_id," .
|
|
" 1, -1) * LedgerEntry.amount) AS balance");
|
|
}
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
function ledgerContextFields2($ledger_id = null, $account_id = null, $account_type = null) {
|
|
$fields = array('id', 'name', 'comment', 'amount');
|
|
|
|
if (isset($ledger_id)) {
|
|
$fields[] = ("IF(LedgerEntry.debit_ledger_id = $ledger_id," .
|
|
" SUM(LedgerEntry.amount), NULL) AS debit");
|
|
$fields[] = ("IF(LedgerEntry.credit_ledger_id = $ledger_id," .
|
|
" SUM(LedgerEntry.amount), NULL) AS credit");
|
|
|
|
if (isset($account_id) || isset($account_type)) {
|
|
$Account = new Account();
|
|
$account_ftype = $Account->fundamentalType($account_id ? $account_id : $account_type);
|
|
$fields[] = ("(IF(LedgerEntry.{$account_ftype}_ledger_id = $ledger_id," .
|
|
" 1, -1) * SUM(LedgerEntry.amount)) AS balance");
|
|
}
|
|
}
|
|
elseif (isset($account_id)) {
|
|
$fields[] = ("IF(DebitLedger.account_id = $account_id," .
|
|
" SUM(LedgerEntry.amount), NULL) AS debit");
|
|
$fields[] = ("IF(CreditLedger.account_id = $account_id," .
|
|
" SUM(LedgerEntry.amount), NULL) AS credit");
|
|
|
|
$Account = new Account();
|
|
$account_ftype = ucfirst($Account->fundamentalType($account_id));
|
|
$fields[] = ("(IF({$account_ftype}Ledger.account_id = $account_id," .
|
|
" 1, -1) * SUM(LedgerEntry.amount)) AS balance");
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
|
|
function ledgerContextConditions($ledger_id, $account_type) {
|
|
if (isset($ledger_id)) {
|
|
return array
|
|
('OR' =>
|
|
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
|
|
array('LedgerEntry.credit_ledger_id' => $ledger_id)),
|
|
);
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findInLedgerContext
|
|
* - Returns an array of ledger entries that belong to a given ledger.
|
|
* There is extra logic to also figure out whether the ledger_entry
|
|
* amount is either a credit, or a debit, depending on how it was
|
|
* written into the ledger, as well as whether the amount increases or
|
|
* decreases the balance depending on the particular account type of
|
|
* the ledger.
|
|
*/
|
|
function findInLedgerContext($ledger_id, $account_type, $cond = null, $link = null) {
|
|
if (!isset($link))
|
|
$link = array('Transaction');
|
|
|
|
if (!isset($cond))
|
|
$cond = array();
|
|
|
|
$fields = $this->ledgerContextFields($ledger_id, $account_type);
|
|
$cond[] = $this->ledgerContextConditions($ledger_id, $account_type);
|
|
$order = array('Transaction.stamp');
|
|
|
|
$entries = $this->find
|
|
('all',
|
|
array('link' => $link,
|
|
'fields' => $fields,
|
|
'conditions' => $cond,
|
|
'order' => $order,
|
|
));
|
|
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findReconcilingTransactions
|
|
* - Returns transactions that reconcile the given entry
|
|
* (such as receipts that reconcile a charge).
|
|
*/
|
|
|
|
function findReconcilingTransactions($id = null, $fundamental_type = null) {
|
|
foreach (($fundamental_type
|
|
? array($fundamental_type)
|
|
: array('debit', 'credit')) AS $fund) {
|
|
$reconciled[$fund]['LedgerEntry'] = $this->find
|
|
('all', array
|
|
('link' => array
|
|
('ReconcilingTransaction' => array
|
|
('fields' => array
|
|
('id',
|
|
"COALESCE(SUM(Reconciliation.amount),0) AS 'reconciled'",
|
|
//"ReconcilingTransaction.amount - COALESCE(SUM(Reconciliation.amount),0) AS 'balance'",
|
|
"COALESCE(7) AS 'balance'",
|
|
),
|
|
'conditions' => array('Reconciliation.type' => $fund),
|
|
),
|
|
),
|
|
'group' => ('ReconcilingTransaction.id'),
|
|
'conditions' => array('LedgerEntry.id' => $id),
|
|
'fields' => array(),
|
|
));
|
|
//pr($reconciled);
|
|
$balance = 0;
|
|
foreach ($reconciled[$fund]['LedgerEntry'] AS &$entry) {
|
|
$entry = array_merge($entry["ReconcilingTransaction"], $entry[0]);
|
|
$balance += $entry['balance'];
|
|
}
|
|
$reconciled[$fund]['balance'] = $balance;
|
|
}
|
|
|
|
return $reconciled;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: stats
|
|
* - Returns summary data from the requested ledger entry
|
|
*/
|
|
function stats($id, $cond = null) {
|
|
|
|
if (!isset($cond))
|
|
$cond = array();
|
|
|
|
$cond[] = array('LedgerEntry.id' => $id);
|
|
|
|
$query = array
|
|
(
|
|
'link' => array('ReconcilingTransaction'),
|
|
'fields' => array("SUM(Reconciliation.amount) AS 'reconciled'"),
|
|
'group' => 'LedgerEntry.id',
|
|
);
|
|
|
|
// Get the applied amounts on the debit side
|
|
$qcond = $cond;
|
|
$qcond[] = array('Reconciliation.type' => 'DEBIT');
|
|
$query['conditions'] = $qcond;
|
|
$tmpstats = $this->find('first', $query);
|
|
$stats['debit_amount_reconciled'] = $tmpstats[0]['reconciled'];
|
|
|
|
// Get the applied amounts on the credit side
|
|
$qcond = $cond;
|
|
$qcond[] = array('Reconciliation.type' => 'CREDIT');
|
|
$query['conditions'] = $qcond;
|
|
$tmpstats = $this->find('first', $query);
|
|
$stats['credit_amount_reconciled'] = $tmpstats[0]['reconciled'];
|
|
|
|
return $stats;
|
|
}
|
|
|
|
}
|