summaryNew
This commit is contained in:
@@ -78,7 +78,22 @@ if($_REQUEST['submit']){
|
||||
$row['name'] = $c['account_name'];
|
||||
$row['index'] = $c['index_dbf'];
|
||||
|
||||
// Compute financial aggregates
|
||||
// Currency handling
|
||||
$currency_id = $c['currency_id'] ?: 'PLN';
|
||||
$currency_name = $c['currency_name'] ?: $currency_id;
|
||||
$row['currency_id'] = $currency_name;
|
||||
|
||||
// Compute saldo first (used for filter)
|
||||
$row['saldo'] = getData($c['parent_id'], 8);
|
||||
$row['today_saldo'] = 0; // getData2 ( $c ['parent_id'],9,$_REQUEST['saldo_date_val'] );
|
||||
|
||||
// Filters: if filtered out, skip expensive calculations
|
||||
if ($saldo_type=='plus' && !($row['saldo']>0)) continue;
|
||||
if ($saldo_type=='minus' && ($row['saldo']>=0)) continue;
|
||||
if ($saldo_type=='zero' && !($row['saldo']==0)) continue;
|
||||
if ($saldo_type=='' && $row['saldo']==0) continue;
|
||||
|
||||
// Compute remaining financial aggregates only when needed
|
||||
$row['unsettled'] = getData($c['parent_id'], 0);
|
||||
$row['not_overdue'] = getData($c['parent_id'], 7);
|
||||
$row['overdue'] = getData($c['parent_id'], 1);
|
||||
@@ -87,19 +102,6 @@ if($_REQUEST['submit']){
|
||||
$row['4'] = getData2($c['parent_id'], 4); // 61..90
|
||||
$row['5'] = getData2($c['parent_id'], 5); // 91..180
|
||||
$row['6'] = getData2($c['parent_id'], 6); // 180..
|
||||
$row['saldo'] = getData($c['parent_id'], 8);
|
||||
$row['today_saldo'] = 0; // getData2 ( $c ['parent_id'],9,$_REQUEST['saldo_date_val'] );
|
||||
|
||||
// Currency handling
|
||||
$currency_id = $c['currency_id'] ?: 'PLN';
|
||||
$currency_name = $c['currency_name'] ?: $currency_id;
|
||||
$row['currency_id'] = $currency_name;
|
||||
|
||||
// Filters
|
||||
if ($saldo_type=='plus' && !($row['saldo']>0)) continue;
|
||||
if ($saldo_type=='minus' && ($row['saldo']>=0)) continue;
|
||||
if ($saldo_type=='zero' && !($row['saldo']==0)) continue;
|
||||
if ($saldo_type=='' && $row['saldo']==0) continue;
|
||||
|
||||
$idToPdf .= $row['id'].',';
|
||||
|
||||
@@ -220,48 +222,59 @@ function getData2($id, $expired,$dates = null) {
|
||||
break;
|
||||
}
|
||||
|
||||
// TYPE 0 transactions
|
||||
// Combined fetch for type 0 and type 1 to reduce queries
|
||||
$ids0 = array();
|
||||
$ids1 = array();
|
||||
$r_values0 = array();
|
||||
$r_values1 = array();
|
||||
$suma0 = 0.0;
|
||||
$q0 = $db->query("SELECT t.id, t.value FROM ecmtransactions AS t WHERE t.parent_id='$id' AND t.deleted='0' AND " . $settled . " t.type='0' AND " . $payment_date);
|
||||
while ($r = $db->fetchByAssoc($q0)) {
|
||||
$suma1 = 0.0;
|
||||
|
||||
$q = $db->query("SELECT t.id, t.value, t.type FROM ecmtransactions AS t WHERE t.parent_id='$id' AND t.deleted='0' AND " . $settled . " t.type IN ('0','1') AND " . $payment_date);
|
||||
while ($r = $db->fetchByAssoc($q)) {
|
||||
if ($r['type'] === '0') {
|
||||
$ids0[] = $r['id'];
|
||||
$r_values0[$r['id']] = floatval($r['value']);
|
||||
$suma0 += floatval($r['value']);
|
||||
} else {
|
||||
$ids1[] = $r['id'];
|
||||
$r_values1[$r['id']] = floatval($r['value']);
|
||||
$suma1 += floatval($r['value']);
|
||||
}
|
||||
}
|
||||
|
||||
$allIds = array_merge($ids0, $ids1);
|
||||
$relsById = array();
|
||||
$relatedIds = array();
|
||||
if (!empty($allIds)) {
|
||||
$idList = "'" . implode("','", $allIds) . "'";
|
||||
$relQ = $db->query("SELECT ecmtransaction_a_id, ecmtransaction_b_id, value FROM ecmtransactions_rel WHERE ecmtransaction_a_id IN ($idList) OR ecmtransaction_b_id IN ($idList)");
|
||||
while ($rr = $db->fetchByAssoc($relQ)) {
|
||||
$a = $rr['ecmtransaction_a_id'];
|
||||
$b = $rr['ecmtransaction_b_id'];
|
||||
$relsById[$a][] = $rr;
|
||||
$relsById[$b][] = $rr;
|
||||
if (!isset($r_values0[$a]) && !isset($r_values1[$a])) $relatedIds[$a] = true;
|
||||
if (!isset($r_values0[$b]) && !isset($r_values1[$b])) $relatedIds[$b] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$txMap = array();
|
||||
if (!empty($relatedIds)) {
|
||||
$relIdList = "'" . implode("','", array_keys($relatedIds)) . "'";
|
||||
$tq = $db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id IN ($relIdList)");
|
||||
while ($t = $db->fetchByAssoc($tq)) {
|
||||
$txMap[$t['id']] = $t;
|
||||
}
|
||||
}
|
||||
|
||||
$total_settled0 = 0.0;
|
||||
if (!empty($ids0)) {
|
||||
// Prefetch relations for all ids0
|
||||
$idList0 = "'" . implode("','", $ids0) . "'";
|
||||
$relQ0 = $db->query("SELECT ecmtransaction_a_id, ecmtransaction_b_id, value FROM ecmtransactions_rel WHERE ecmtransaction_a_id IN ($idList0) OR ecmtransaction_b_id IN ($idList0)");
|
||||
$relsById0 = array();
|
||||
$relatedIds0 = array();
|
||||
while ($rr = $db->fetchByAssoc($relQ0)) {
|
||||
$a = $rr['ecmtransaction_a_id'];
|
||||
$b = $rr['ecmtransaction_b_id'];
|
||||
$relsById0[$a][] = $rr;
|
||||
$relsById0[$b][] = $rr;
|
||||
if (!isset($r_values0[$a])) $relatedIds0[$a] = true;
|
||||
if (!isset($r_values0[$b])) $relatedIds0[$b] = true;
|
||||
}
|
||||
$relatedIds0 = array_keys($relatedIds0);
|
||||
$txMap0 = array();
|
||||
if (!empty($relatedIds0)) {
|
||||
$relIdList0 = "'" . implode("','", $relatedIds0) . "'";
|
||||
$tq = $db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id IN ($relIdList0)");
|
||||
while ($t = $db->fetchByAssoc($tq)) {
|
||||
$txMap0[$t['id']] = $t;
|
||||
}
|
||||
}
|
||||
foreach ($ids0 as $pid) {
|
||||
if (empty($relsById0[$pid])) continue;
|
||||
if (empty($relsById[$pid])) continue;
|
||||
$r_val = $r_values0[$pid];
|
||||
foreach ($relsById0[$pid] as $rr) {
|
||||
// Determine the other side of the relation
|
||||
foreach ($relsById[$pid] as $rr) {
|
||||
$rel_id = ($rr['ecmtransaction_a_id'] == $pid) ? $rr['ecmtransaction_b_id'] : $rr['ecmtransaction_a_id'];
|
||||
$t = isset($txMap0[$rel_id]) ? $txMap0[$rel_id] : $db->fetchByAssoc($db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id='".$rel_id."'"));
|
||||
$t = isset($txMap[$rel_id]) ? $txMap[$rel_id] : $db->fetchByAssoc($db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id='".$rel_id."'"));
|
||||
if (!$t || $t['deleted'] == '1') continue;
|
||||
if ($expired == 9) {
|
||||
$date2 = new DateTime($t['payment_date']);
|
||||
@@ -280,47 +293,13 @@ function getData2($id, $expired,$dates = null) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TYPE 1 transactions
|
||||
$ids1 = array();
|
||||
$r_values1 = array();
|
||||
$suma1 = 0.0;
|
||||
$q1 = $db->query("SELECT t.id, t.value FROM ecmtransactions AS t WHERE t.parent_id='$id' AND t.deleted='0' AND " . $settled . " t.type='1' AND " . $payment_date);
|
||||
while ($r = $db->fetchByAssoc($q1)) {
|
||||
$ids1[] = $r['id'];
|
||||
$r_values1[$r['id']] = floatval($r['value']);
|
||||
$suma1 += floatval($r['value']);
|
||||
}
|
||||
|
||||
$total_settled1 = 0.0;
|
||||
if (!empty($ids1)) {
|
||||
$idList1 = "'" . implode("','", $ids1) . "'";
|
||||
$relQ1 = $db->query("SELECT ecmtransaction_a_id, ecmtransaction_b_id, value FROM ecmtransactions_rel WHERE ecmtransaction_a_id IN ($idList1) OR ecmtransaction_b_id IN ($idList1)");
|
||||
$relsById1 = array();
|
||||
$relatedIds1 = array();
|
||||
while ($rr = $db->fetchByAssoc($relQ1)) {
|
||||
$a = $rr['ecmtransaction_a_id'];
|
||||
$b = $rr['ecmtransaction_b_id'];
|
||||
$relsById1[$a][] = $rr;
|
||||
$relsById1[$b][] = $rr;
|
||||
if (!isset($r_values1[$a])) $relatedIds1[$a] = true;
|
||||
if (!isset($r_values1[$b])) $relatedIds1[$b] = true;
|
||||
}
|
||||
$relatedIds1 = array_keys($relatedIds1);
|
||||
$txMap1 = array();
|
||||
if (!empty($relatedIds1)) {
|
||||
$relIdList1 = "'" . implode("','", $relatedIds1) . "'";
|
||||
$tq = $db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id IN ($relIdList1)");
|
||||
while ($t = $db->fetchByAssoc($tq)) {
|
||||
$txMap1[$t['id']] = $t;
|
||||
}
|
||||
}
|
||||
foreach ($ids1 as $pid) {
|
||||
if (empty($relsById1[$pid])) continue;
|
||||
foreach ($relsById1[$pid] as $rr) {
|
||||
if (empty($relsById[$pid])) continue;
|
||||
foreach ($relsById[$pid] as $rr) {
|
||||
$rel_id = ($rr['ecmtransaction_a_id'] == $pid) ? $rr['ecmtransaction_b_id'] : $rr['ecmtransaction_a_id'];
|
||||
$t = isset($txMap1[$rel_id]) ? $txMap1[$rel_id] : $db->fetchByAssoc($db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id='".$rel_id."'"));
|
||||
$t = isset($txMap[$rel_id]) ? $txMap[$rel_id] : $db->fetchByAssoc($db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id='".$rel_id."'"));
|
||||
if (!$t || $t['deleted'] == '1') continue;
|
||||
if ($expired == 9) {
|
||||
$date2 = new DateTime($t['payment_date']);
|
||||
@@ -330,7 +309,6 @@ function getData2($id, $expired,$dates = null) {
|
||||
$total_settled1 += abs(floatval($rr['value']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $total_settled0 - $suma0 + $suma1 - $total_settled1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user