Another snapshot. I think I'll be taking the CREDIT/DEBIT reconcile functionality out.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@353 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-20 02:17:54 +00:00
parent 3d262fd4db
commit 7304a0e889
6 changed files with 266 additions and 244 deletions

View File

@@ -373,19 +373,95 @@ OPTION 2
/**************************************************************************
**************************************************************************
**************************************************************************
* function: reconcilingEntries
* function: reconciledSet
* - Returns the set of entries satisfying the given conditions,
* along with any entries that they reconcile
*/
function reconciledSet($set, $cond = null, $link = null, $unrec = false) {
if (!isset($cond))
$cond = array();
if (!isset($link))
$link = array();
if ($set == 'CHARGE' || $set == 'PAYMENT')
$cond[] = array('Entry.type' => $set);
elseif ($set == 'DEBIT' || $set == 'CREDIT')
$cond[] = array('Entry.crdr' => $set);
else
die("INVALID RECONCILE SET");
$link['DoubleEntry'] += array();
/* if ($set == 'CHARGE') */
/* $link['Payment'] = array('fields' => array("SUM(ChargesPayment.amount) AS applied")); */
/* if ($set == 'PAYMENT') */
/* $link['Charge'] = array('fields' => array("SUM(ChargesPayment.amount) AS applied")); */
/* if ($set == 'DEBIT') */
/* $link['Credit'] = array('fields' => array("SUM(Reconciliation.amount) AS applied")); */
/* if ($set == 'CREDIT') */
/* $link['Debit'] = array('fields' => array("SUM(Reconciliation.amount) AS applied")); */
if ($set == 'CHARGE')
$link['Payment'] = array('fields' => array("SUM(AppliedPayment.amount) AS applied"));
if ($set == 'PAYMENT')
$link['Charge'] = array('fields' => array("SUM(AppliedCharge.amount) AS applied"));
if ($set == 'DEBIT')
$link['Credit'] = array('fields' => array("SUM(AppliedCredit.amount) AS applied"));
if ($set == 'CREDIT')
$link['Debit'] = array('fields' => array("SUM(AppliedDebit.amount) AS applied"));
$result = $this->find
('all', array
(
'link' => $link,
'conditions' => $cond,
'group' => 'Entry.id',
));
pr(array('reconciledSet', compact('set', 'cond', 'link', 'result')));
$resultset = array();
foreach ($result AS $i => $entry) {
$entry['DoubleEntry'] += $entry[0];
unset($entry[0]);
$entry['DoubleEntry']['balance'] =
$entry['DoubleEntry']['amount'] - $entry['DoubleEntry']['applied'];
// Since HAVING isn't a builtin feature of CakePHP,
// we'll have to post-process to get the desired entries
if ($entry['DoubleEntry']['balance'] == 0) {
if (!$unrec)
$resultset[] = $entry;
}
else {
if ($unrec)
$resultset[] = $entry;
}
}
//$result['stats'] = $this->stats(null, $cond);
pr($this->stats(null, $cond, $link));
return $resultset;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: reconciledEntries
* - Returns a list of entries that reconcile against the given entry.
* (such as payments towards a charge).
*/
function reconciledEntries($id, $cond = null) {
function reconciledEntries($id, $cond = null, $link = null) {
if (!isset($cond))
$cond = array();
if (!isset($link))
$link = array();
$cond[] = array('Entry.id' => $id);
$entry = $this->find('first', array('recursive' => -1));
$entry = $this->find('first', array('conditions' => array('Entry.id' => $id),
'recursive' => -1));
$entry = $entry['Entry'];
$contain = array();
@@ -471,42 +547,49 @@ OPTION 2
* function: stats
* - Returns summary data from the requested ledger entry
*/
function stats($id, $cond = null) {
function stats($id = null, $cond = null, $link = null) {
if (!isset($cond))
$cond = array();
if (!isset($link))
$link = array();
$cond[] = array('Entry.id' => $id);
if (isset($id))
$cond[] = array('Entry.id' => $id);
$entry = $this->find('first', array('contain' => array('DoubleEntry.amount'),
'fields' => array('Entry.type', 'Entry.crdr')));
$entry['Entry'] += $entry['DoubleEntry'];
$entry = $entry['Entry'];
/* $entry = $this->find('first', array('contain' => array('DoubleEntry.amount'), */
/* 'fields' => array('Entry.type', 'Entry.crdr'), */
/* 'conditions' => array('Entry.id' => $id))); */
/* pr(compact('entry')); */
/* $entry['Entry'] += $entry['DoubleEntry']; */
/* $entry = $entry['Entry']; */
$stats = array();
foreach(array('charge', 'payment', 'debit', 'credit') AS $rtype) {
$Rtype = ucfirst($rtype);
if (($rtype == 'charge' && $entry['type'] == 'PAYMENT') ||
$rlink = $link;
$rlink[$Rtype] = array('fields' => array("SUM(Applied{$Rtype}.amount) AS applied"));
pr(array('stats()', compact('id', 'cond', 'link', 'rlink')));
if (1 || ($rtype == 'charge' && $entry['type'] == 'PAYMENT') ||
($rtype == 'payment' && $entry['type'] == 'CHARGE') ||
($rtype == 'debit' && $entry['crdr'] == 'CREDIT') ||
($rtype == 'credit' && $entry['crdr'] == 'DEBIT')) {
$result = $this->find
('first', array
('link' =>
array($Rtype =>
array('fields' => array("SUM(Applied{$Rtype}.amount) AS applied"))),
'fields' => array(),
('link' => $rlink,
'fields' => array("SUM(DoubleEntry.amount) AS total",
"SUM(DoubleEntry.amount) - SUM(Applied{$Rtype}.amount) AS unapplied"),
'conditions' => $cond,
'group' => 'Entry.id',
//'group' => 'Entry.id',
));
//pr(compact('Rtype', 'result'));
pr(compact('Rtype', 'result'));
$sumfld = $Rtype;
$stats[$sumfld] = $result[0];
if (!isset($stats[$sumfld]['applied']))
$stats[$sumfld]['applied'] = 0;
$stats[$sumfld]['unapplied'] = $entry['amount'] - $stats[$sumfld]['applied'];
/* if (!isset($stats[$sumfld]['applied'])) */
/* $stats[$sumfld]['applied'] = 0; */
}
}