Finally resolved the StatementEntries::stats() query issue. It has been lingering in an incomplete state because it wasn't clear what it was supposed to really do. I think it is now performing the task expected of it.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@374 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -263,6 +263,12 @@ OPTION 2
|
|||||||
$query['link']['ChargeEntry'] = array('fields' => array("SUM(ChargeEntry.amount) AS reconciled"));
|
$query['link']['ChargeEntry'] = array('fields' => array("SUM(ChargeEntry.amount) AS reconciled"));
|
||||||
|
|
||||||
$query['group'] = 'StatementEntry.id';
|
$query['group'] = 'StatementEntry.id';
|
||||||
|
|
||||||
|
// REVISIT: TESTING
|
||||||
|
//$query['link']['PaymentEntry'] = array('fields' => array("(`PaymentEntry.amount`+0) AS reconciled"));
|
||||||
|
//$query['group'] = null;
|
||||||
|
// END REVISIT
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,13 +276,12 @@ OPTION 2
|
|||||||
$lquery = $this->reconciledSetQuery($set, $query);
|
$lquery = $this->reconciledSetQuery($set, $query);
|
||||||
$result = $this->find('all', $lquery);
|
$result = $this->find('all', $lquery);
|
||||||
|
|
||||||
//pr(compact('lquery', 'result'));
|
//pr(array('StatementEntry::reconciledSet' => compact('set', 'unrec', 'lquery', 'result')));
|
||||||
|
|
||||||
//pr(array('reconciledSet', compact('set', 'lquery', 'result')));
|
|
||||||
|
|
||||||
$resultset = array();
|
$resultset = array();
|
||||||
foreach ($result AS $i => $entry) {
|
foreach ($result AS $i => $entry) {
|
||||||
//pr(compact('entry'));
|
//pr(compact('entry'));
|
||||||
|
if (!empty($entry[0]))
|
||||||
$entry['StatementEntry'] = $entry[0] + $entry['StatementEntry'];
|
$entry['StatementEntry'] = $entry[0] + $entry['StatementEntry'];
|
||||||
unset($entry[0]);
|
unset($entry[0]);
|
||||||
|
|
||||||
@@ -295,9 +300,7 @@ OPTION 2
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//pr(compact('resultset'));
|
//pr(array('StatementEntry::reconciledSet' => compact('resultset')));
|
||||||
|
|
||||||
//pr(array('StatementEntry::reconciledSet()' => compact('resultset')));
|
|
||||||
|
|
||||||
//$resultset['stats'] = $this->stats(null, $query);
|
//$resultset['stats'] = $this->stats(null, $query);
|
||||||
//pr($this->stats(null, $query));
|
//pr($this->stats(null, $query));
|
||||||
@@ -354,128 +357,56 @@ OPTION 2
|
|||||||
$this->queryInit($query);
|
$this->queryInit($query);
|
||||||
unset($query['group']);
|
unset($query['group']);
|
||||||
|
|
||||||
|
/* pr(array('StatementEntry::stats' => compact('id', 'query'))); */
|
||||||
if (isset($id)) {
|
|
||||||
$this->id = $id;
|
|
||||||
$this->recursive = -1;
|
|
||||||
$this->read();
|
|
||||||
|
|
||||||
if ($this->data['StatementEntry']['type'] == 'PAYMENT') {
|
|
||||||
// Payment has only a _single_ charge, and by definition
|
|
||||||
// 100% of the payment is used to reconcile that charge.
|
|
||||||
return array('total' => $this->data['StatementEntry']['amount'],
|
|
||||||
'reconciled' => $this->data['StatementEntry']['amount'],
|
|
||||||
'balance' => 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
$query['link']['PaymentEntry'] =
|
|
||||||
array('fields' => array("SUM(COALESCE(PaymentEntry.amount,0)) AS reconciled"),
|
|
||||||
//'type' => 'INNER',
|
|
||||||
);
|
|
||||||
|
|
||||||
$query['fields'] = array('StatementEntry.amount');
|
|
||||||
$query['conditions'][] = array('StatementEntry.id' => $id);
|
|
||||||
$query['group'] = 'StatementEntry.id';
|
|
||||||
|
|
||||||
$result = $this->find('first', $query);
|
|
||||||
$result[0]['balance'] = $result['StatementEntry']['amount'] - $result[0]['reconciled'];
|
|
||||||
//pr(compact('query', 'result'));
|
|
||||||
|
|
||||||
return $result['StatementEntry'] + $result[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
$stats = array();
|
$stats = array();
|
||||||
|
if (isset($id))
|
||||||
$rquery = $query;
|
|
||||||
$rquery['link']['PaymentEntry'] =
|
|
||||||
array('fields' => array("SUM(COALESCE(PaymentEntry.amount,0)) AS reconciled"),
|
|
||||||
//'type' => 'INNER',
|
|
||||||
);
|
|
||||||
|
|
||||||
$rquery['fields'] = array();
|
|
||||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
|
||||||
|
|
||||||
$rquery['conditions'][] = array("PaymentEntry.id !=" => null);
|
|
||||||
|
|
||||||
$result = $this->find('first', $rquery);
|
|
||||||
$result[0]['balance'] = $result[0]['total'] - $result[0]['reconciled'];
|
|
||||||
$stats['Payment'] = $result[0];
|
|
||||||
//pr(array('stats Payment', compact('rquery', 'stats')));
|
|
||||||
|
|
||||||
$rquery = $query;
|
|
||||||
$rquery['fields'] = array();
|
|
||||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
|
||||||
$rquery['fields'][] = "SUM(StatementEntry.amount) AS reconciled";
|
|
||||||
|
|
||||||
$result = $this->find('first', $rquery);
|
|
||||||
$result[0]['balance'] = $result[0]['total'] - $result[0]['reconciled'];
|
|
||||||
$stats['Charge'] = $result[0];
|
|
||||||
//pr(array('stats Charge', compact('rquery', 'stats')));
|
|
||||||
|
|
||||||
|
|
||||||
return $stats;
|
|
||||||
|
|
||||||
|
|
||||||
$this->id = $id;
|
|
||||||
$this->recursive = -1;
|
|
||||||
$this->read();
|
|
||||||
|
|
||||||
//pr(array('stats()', compact('id', 'query', 'set')));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* if ($this->data['StatementEntry']['type'] == 'CHARGE') */
|
|
||||||
/* $set = 'PAYMENT'; */
|
|
||||||
/* else */
|
|
||||||
/* $set = 'CHARGE'; */
|
|
||||||
|
|
||||||
/* $Set = ucfirst(strtolower($set)); */
|
|
||||||
|
|
||||||
/* $query['link'][$Set.'Entry'] = */
|
|
||||||
/* array('fields' => array("SUM(COALESCE({$Set}Entry.amount,0)) AS reconciled"), */
|
|
||||||
/* //'type' => 'INNER', */
|
|
||||||
/* ); */
|
|
||||||
|
|
||||||
/* $query['fields'] = array(); */
|
|
||||||
/* $query['fields'][] = "StatementEntry.amount AS total"; */
|
|
||||||
/* $query['fields'][] = "StatementEntry.amount - SUM(COALESCE({$Set}Entry.amount,0)) AS balance"; */
|
|
||||||
/* //$query['conditions'][] = array("{$Set}Entry.id !=" => null); */
|
|
||||||
|
|
||||||
/* $query['conditions'][] = array('StatementEntry.id' => $id); */
|
|
||||||
|
|
||||||
/* $query['group'] = 'StatementEntry.id'; */
|
|
||||||
|
|
||||||
/* $result = $this->find('first', $query); */
|
|
||||||
/* pr(compact('Rtype', 'query', 'result')); */
|
|
||||||
|
|
||||||
/* return $result['StatementEntry'] + $result[0]; */
|
|
||||||
|
|
||||||
if ($this->data['StatementEntry']['type'] == 'PAYMENT') {
|
|
||||||
// Payment has only a _single_ charge, and by definition
|
|
||||||
// 100% of the payment is used to reconcile that charge.
|
|
||||||
return array('total' => $this->data['StatementEntry']['amount'],
|
|
||||||
'reconciled' => $this->data['StatementEntry']['amount'],
|
|
||||||
'balance' => 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
$query['link']['PaymentEntry'] =
|
|
||||||
array('fields' => array("SUM(COALESCE(PaymentEntry.amount,0)) AS reconciled"),
|
|
||||||
//'type' => 'INNER',
|
|
||||||
);
|
|
||||||
|
|
||||||
$query['fields'] = array();
|
|
||||||
$query['fields'][] = "StatementEntry.amount AS total";
|
|
||||||
$query['fields'][] = "StatementEntry.amount - SUM(COALESCE(PaymentEntry.amount,0)) AS balance";
|
|
||||||
//$query['conditions'][] = array("{$Set}Entry.id !=" => null);
|
|
||||||
|
|
||||||
$query['conditions'][] = array('StatementEntry.id' => $id);
|
$query['conditions'][] = array('StatementEntry.id' => $id);
|
||||||
|
|
||||||
$query['group'] = 'StatementEntry.id';
|
$rquery = $query;
|
||||||
|
unset($rquery['link']['ChargeEntry']);
|
||||||
|
$rquery['link']['PaymentEntry'] = array('fields' => array());
|
||||||
|
|
||||||
$result = $this->find('first', $query);
|
$rquery['fields'] = array();
|
||||||
//pr(compact('Rtype', 'query', 'result'));
|
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||||
|
$rquery['fields'][] = "SUM(PaymentEntry.amount) AS reconciled";
|
||||||
|
$rquery['fields'][] = "SUM(StatementEntry.amount - COALESCE(PaymentEntry.amount,0)) AS balance";
|
||||||
|
|
||||||
return $result['StatementEntry'] + $result[0];
|
$rquery['conditions'][] = array('StatementEntry.type' => 'CHARGE');
|
||||||
|
$result = $this->find('first', $rquery);
|
||||||
|
if (!isset($result[0]['balance']))
|
||||||
|
$result[0]['balance'] = 0;
|
||||||
|
$stats['Charge'] = $result[0];
|
||||||
|
|
||||||
|
/* pr(array('StatementEntry::stats' => */
|
||||||
|
/* array('checkpoint' => 'Charges') */
|
||||||
|
/* + compact('query', 'result'))); */
|
||||||
|
|
||||||
|
$rquery = $query;
|
||||||
|
unset($rquery['link']['PaymentEntry']);
|
||||||
|
$rquery['link']['ChargeEntry'] = array('fields' => array());
|
||||||
|
|
||||||
|
$rquery['fields'] = array();
|
||||||
|
$rquery['fields'][] = "SUM(StatementEntry.amount) AS total";
|
||||||
|
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, 0, StatementEntry.amount)) AS reconciled";
|
||||||
|
$rquery['fields'][] = "SUM(IF(ChargeEntry.id IS NULL, StatementEntry.amount, 0)) AS balance";
|
||||||
|
|
||||||
|
$rquery['conditions'][] = array('StatementEntry.type' => 'PAYMENT');
|
||||||
|
$result = $this->find('first', $rquery);
|
||||||
|
if (!isset($result[0]['balance']))
|
||||||
|
$result[0]['balance'] = 0;
|
||||||
|
$stats['Payment'] = $result[0];
|
||||||
|
|
||||||
|
/* pr(array('StatementEntry::stats' => */
|
||||||
|
/* array('checkpoint' => 'Payments') */
|
||||||
|
/* + compact('rquery', 'result'))); */
|
||||||
|
|
||||||
|
|
||||||
|
/* pr(array('StatementEntry::stats' => */
|
||||||
|
/* array('checkpoint' => 'return') */
|
||||||
|
/* + compact('stats'))); */
|
||||||
|
|
||||||
|
return $stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user