git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@414 97e9348a-65ac-dc4b-aefc-98561f571b83
97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
class DoubleEntry extends AppModel {
|
|
|
|
var $belongsTo = array(
|
|
'DebitEntry' => array(
|
|
'className' => 'LedgerEntry',
|
|
),
|
|
'CreditEntry' => array(
|
|
'className' => 'LedgerEntry',
|
|
),
|
|
);
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: verifyDoubleEntry
|
|
* - Verifies consistenty of new double entry data
|
|
* (not in a pre-existing double entry)
|
|
*/
|
|
function verifyDoubleEntry($entry1, $entry2, $entry1_tender = null) {
|
|
/* pr(array("DoubleEntry::verifyDoubleEntry()" */
|
|
/* => compact('entry1', 'entry2', 'entry1_tender'))); */
|
|
|
|
$LE = new LedgerEntry();
|
|
if (!$LE->verifyLedgerEntry($entry1, $entry1_tender)) {
|
|
/* pr(array("DoubleEntry::verifyDoubleEntry()" */
|
|
/* => "Entry1 verification failed")); */
|
|
return false;
|
|
}
|
|
if (!$LE->verifyLedgerEntry($entry2)) {
|
|
/* pr(array("DoubleEntry::verifyDoubleEntry()" */
|
|
/* => "Entry2 verification failed")); */
|
|
return false;
|
|
}
|
|
|
|
if (!(($entry1['crdr'] === 'DEBIT' && $entry2['crdr'] === 'CREDIT') ||
|
|
($entry1['crdr'] === 'CREDIT' && $entry2['crdr'] === 'DEBIT')) ||
|
|
($entry1['amount'] != $entry2['amount'])) {
|
|
/* pr(array("DoubleEntry::verifyDoubleEntry()" */
|
|
/* => "Double Entry verification failed")); */
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: addDoubleEntry
|
|
* - Inserts new Double Entry into the database
|
|
*/
|
|
|
|
function addDoubleEntry($entry1, $entry2, $entry1_tender = null) {
|
|
/* pr(array('DoubleEntry::addDoubleEntry' => */
|
|
/* compact('entry1', 'entry2', 'entry1_tender'))); */
|
|
|
|
$ret = array();
|
|
if (!$this->verifyDoubleEntry($entry1, $entry2, $entry1_tender))
|
|
return array('error' => true) + $ret;
|
|
|
|
// Since this model only relates to DebitEntry and CreditEntry...
|
|
$LE = new LedgerEntry();
|
|
|
|
// Add the first ledger entry to the database
|
|
$result = $LE->addLedgerEntry($entry1, $entry1_tender);
|
|
$ret['Entry1'] = $result;
|
|
if ($result['error'])
|
|
return array('error' => true) + $ret;
|
|
|
|
// Add the second ledger entry to the database
|
|
$result = $LE->addLedgerEntry($entry2);
|
|
$ret['Entry2'] = $result;
|
|
if ($result['error'])
|
|
return array('error' => true) + $ret;
|
|
|
|
// Now link them as a double entry
|
|
$double_entry = array();
|
|
$double_entry['debit_entry_id'] =
|
|
($entry1['crdr'] === 'DEBIT') ? $ret['Entry1']['ledger_entry_id'] : $ret['Entry2']['ledger_entry_id'];
|
|
$double_entry['credit_entry_id'] =
|
|
($entry1['crdr'] === 'CREDIT') ? $ret['Entry1']['ledger_entry_id'] : $ret['Entry2']['ledger_entry_id'];
|
|
|
|
/* pr(array('DoubleEntry::addDoubleEntry' => */
|
|
/* array('checkpoint' => 'Pre-Save') */
|
|
/* + compact('double_entry'))); */
|
|
|
|
$this->create();
|
|
if (!$this->save($double_entry))
|
|
return array('error' => true) + $ret;
|
|
|
|
$ret['double_entry_id'] = $this->id;
|
|
return $ret + array('error' => false);
|
|
}
|
|
}
|