Further tweaked the code to add new transactions, primarily by creating separate verification and addition functions in each underlying model. There are logic changes as well, such as adding in the other half of the missing double entry (as well as the double entry itself). This checkin also introduces the creation of CREDIT statement entries, when the customer has overpayed. It's working fairly well, although undoubtedly will need more tweaking.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@394 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-28 01:16:42 +00:00
parent 4adf0dd2fc
commit 24ccb56e50
6 changed files with 447 additions and 374 deletions

View File

@@ -10,4 +10,86 @@ class DoubleEntry extends AppModel {
),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* 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'))) {
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);
}
}