summaryNew
This commit is contained in:
@@ -78,7 +78,22 @@ if($_REQUEST['submit']){
|
|||||||
$row['name'] = $c['account_name'];
|
$row['name'] = $c['account_name'];
|
||||||
$row['index'] = $c['index_dbf'];
|
$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['unsettled'] = getData($c['parent_id'], 0);
|
||||||
$row['not_overdue'] = getData($c['parent_id'], 7);
|
$row['not_overdue'] = getData($c['parent_id'], 7);
|
||||||
$row['overdue'] = getData($c['parent_id'], 1);
|
$row['overdue'] = getData($c['parent_id'], 1);
|
||||||
@@ -87,19 +102,6 @@ if($_REQUEST['submit']){
|
|||||||
$row['4'] = getData2($c['parent_id'], 4); // 61..90
|
$row['4'] = getData2($c['parent_id'], 4); // 61..90
|
||||||
$row['5'] = getData2($c['parent_id'], 5); // 91..180
|
$row['5'] = getData2($c['parent_id'], 5); // 91..180
|
||||||
$row['6'] = getData2($c['parent_id'], 6); // 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'].',';
|
$idToPdf .= $row['id'].',';
|
||||||
|
|
||||||
@@ -220,115 +222,91 @@ function getData2($id, $expired,$dates = null) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TYPE 0 transactions
|
// Combined fetch for type 0 and type 1 to reduce queries
|
||||||
$ids0 = array();
|
$ids0 = array();
|
||||||
|
$ids1 = array();
|
||||||
$r_values0 = array();
|
$r_values0 = array();
|
||||||
|
$r_values1 = array();
|
||||||
$suma0 = 0.0;
|
$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);
|
$suma1 = 0.0;
|
||||||
while ($r = $db->fetchByAssoc($q0)) {
|
|
||||||
$ids0[] = $r['id'];
|
$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);
|
||||||
$r_values0[$r['id']] = floatval($r['value']);
|
while ($r = $db->fetchByAssoc($q)) {
|
||||||
$suma0 += floatval($r['value']);
|
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;
|
$total_settled0 = 0.0;
|
||||||
if (!empty($ids0)) {
|
foreach ($ids0 as $pid) {
|
||||||
// Prefetch relations for all ids0
|
if (empty($relsById[$pid])) continue;
|
||||||
$idList0 = "'" . implode("','", $ids0) . "'";
|
$r_val = $r_values0[$pid];
|
||||||
$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)");
|
foreach ($relsById[$pid] as $rr) {
|
||||||
$relsById0 = array();
|
$rel_id = ($rr['ecmtransaction_a_id'] == $pid) ? $rr['ecmtransaction_b_id'] : $rr['ecmtransaction_a_id'];
|
||||||
$relatedIds0 = array();
|
$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."'"));
|
||||||
while ($rr = $db->fetchByAssoc($relQ0)) {
|
if (!$t || $t['deleted'] == '1') continue;
|
||||||
$a = $rr['ecmtransaction_a_id'];
|
if ($expired == 9) {
|
||||||
$b = $rr['ecmtransaction_b_id'];
|
$date2 = new DateTime($t['payment_date']);
|
||||||
$relsById0[$a][] = $rr;
|
$cmpDate = new DateTime($d);
|
||||||
$relsById0[$b][] = $rr;
|
if ($date2 > $cmpDate) continue;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
$rrVal = floatval($rr['value']);
|
||||||
foreach ($ids0 as $pid) {
|
if ($t['type'] == '0') {
|
||||||
if (empty($relsById0[$pid])) continue;
|
if ($r_val < 0 && $rrVal < 0) {
|
||||||
$r_val = $r_values0[$pid];
|
|
||||||
foreach ($relsById0[$pid] as $rr) {
|
|
||||||
// Determine the other side of the relation
|
|
||||||
$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."'"));
|
|
||||||
if (!$t || $t['deleted'] == '1') continue;
|
|
||||||
if ($expired == 9) {
|
|
||||||
$date2 = new DateTime($t['payment_date']);
|
|
||||||
$cmpDate = new DateTime($d);
|
|
||||||
if ($date2 > $cmpDate) continue;
|
|
||||||
}
|
|
||||||
$rrVal = floatval($rr['value']);
|
|
||||||
if ($t['type'] == '0') {
|
|
||||||
if ($r_val < 0 && $rrVal < 0) {
|
|
||||||
$total_settled0 += abs($rrVal);
|
|
||||||
} else {
|
|
||||||
$total_settled0 += $rrVal;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$total_settled0 += abs($rrVal);
|
$total_settled0 += abs($rrVal);
|
||||||
|
} else {
|
||||||
|
$total_settled0 += $rrVal;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$total_settled0 += abs($rrVal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
$total_settled1 = 0.0;
|
||||||
if (!empty($ids1)) {
|
foreach ($ids1 as $pid) {
|
||||||
$idList1 = "'" . implode("','", $ids1) . "'";
|
if (empty($relsById[$pid])) continue;
|
||||||
$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)");
|
foreach ($relsById[$pid] as $rr) {
|
||||||
$relsById1 = array();
|
$rel_id = ($rr['ecmtransaction_a_id'] == $pid) ? $rr['ecmtransaction_b_id'] : $rr['ecmtransaction_a_id'];
|
||||||
$relatedIds1 = array();
|
$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."'"));
|
||||||
while ($rr = $db->fetchByAssoc($relQ1)) {
|
if (!$t || $t['deleted'] == '1') continue;
|
||||||
$a = $rr['ecmtransaction_a_id'];
|
if ($expired == 9) {
|
||||||
$b = $rr['ecmtransaction_b_id'];
|
$date2 = new DateTime($t['payment_date']);
|
||||||
$relsById1[$a][] = $rr;
|
$cmpDate = new DateTime($d);
|
||||||
$relsById1[$b][] = $rr;
|
if ($date2 > $cmpDate) continue;
|
||||||
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) {
|
|
||||||
$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."'"));
|
|
||||||
if (!$t || $t['deleted'] == '1') continue;
|
|
||||||
if ($expired == 9) {
|
|
||||||
$date2 = new DateTime($t['payment_date']);
|
|
||||||
$cmpDate = new DateTime($d);
|
|
||||||
if ($date2 > $cmpDate) continue;
|
|
||||||
}
|
|
||||||
$total_settled1 += abs(floatval($rr['value']));
|
|
||||||
}
|
}
|
||||||
|
$total_settled1 += abs(floatval($rr['value']));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user