git-svn-id: file:///svn-source/pmgr/branches/reconcile_entry_to_receipt_20090629@187 97e9348a-65ac-dc4b-aefc-98561f571b83
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
class Transaction extends AppModel {
|
|
|
|
var $name = 'Transaction';
|
|
/* var $validate = array( */
|
|
/* 'stamp' => array('date') */
|
|
/* ); */
|
|
|
|
var $belongsTo = array(
|
|
'Customer',
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'LedgerEntry',
|
|
);
|
|
|
|
var $hasAndBelongsToMany = array(
|
|
'ReconciledLedgerEntry' => array(
|
|
'className' => 'LedgerEntry',
|
|
'joinTable' => 'reconciliations',
|
|
'foreignKey' => 'ledger_entry_id',
|
|
'associationForeignKey' => 'transaction_id',
|
|
),
|
|
);
|
|
|
|
function beforeSave() {
|
|
|
|
if(!empty($this->data['Transaction']['stamp'])) {
|
|
$this->data['Transaction']['stamp'] =
|
|
$this->dateFormatBeforeSave($this->data['Transaction']['stamp']);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findReconciledLedgerEntries
|
|
* - Returns ledger entries that are reconciled by the given
|
|
* transaction (such as charges reconciled by a receipt)
|
|
*/
|
|
|
|
function findReconciledLedgerEntries($id = null) {
|
|
$reconciled['LedgerEntry'] = $this->find
|
|
('all', array
|
|
('link' => array
|
|
('ReconciledLedgerEntry' => array
|
|
('fields' => array
|
|
('id',
|
|
"COALESCE(SUM(Reconciliation.amount),0) AS 'reconciled'",
|
|
"ReconciledLedgerEntry.amount - COALESCE(SUM(Reconciliation.amount),0) AS 'balance'",
|
|
),
|
|
),
|
|
),
|
|
'group' => ('ReconciledLedgerEntry.id'),
|
|
'conditions' => array('Transaction.id' => $id),
|
|
'fields' => array(),
|
|
));
|
|
//pr($reconciled);
|
|
$balance = 0;
|
|
foreach ($reconciled['LedgerEntry'] AS &$entry) {
|
|
$entry = array_merge($entry["ReconciledLedgerEntry"], $entry[0]);
|
|
$balance += $entry['balance'];
|
|
}
|
|
$reconciled['balance'] = $balance;
|
|
|
|
return $reconciled;
|
|
}
|
|
|
|
}
|
|
?>
|