summaryNew

This commit is contained in:
Michał Zieliński
2025-08-26 15:26:23 +02:00
parent bc6348486d
commit 7ec831b114

View File

@@ -39,21 +39,32 @@ if($_REQUEST['submit']){
// get list of accounts // get list of accounts
if ($account_type == '') if ($account_type == '')
$clients = $db->query ( "SELECT distinct t.parent_id,a.index_dbf as index_dbf FROM ecmtransactions as t $clients = $db->query ( "SELECT DISTINCT
INNER JOIN accounts AS a t.parent_id,
ON t.parent_id = a.id a.index_dbf AS index_dbf,
WHERE ".$type." t.deleted='0' ".$and." ORDER BY a.index_dbf" ); a.name AS account_name,
a.currency_id AS currency_id,
cur.name AS currency_name
FROM ecmtransactions AS t
INNER JOIN accounts AS a ON t.parent_id = a.id
LEFT JOIN currencies AS cur ON cur.id = a.currency_id
WHERE " . $type . " t.deleted='0' " . $and . "
ORDER BY a.index_dbf" );
else else
$clients = $db->query ( " $clients = $db->query ( "
SELECT distinct t.parent_id, a.index_dbf as index_dbf FROM ecmtransactions AS t SELECT DISTINCT
INNER JOIN accounts AS a t.parent_id,
ON t.parent_id = a.id a.index_dbf AS index_dbf,
WHERE a.name AS account_name,
".$type." a.currency_id AS currency_id,
t.deleted='0' AND (a.account_type='rs' OR cur.name AS currency_name
a.account_type = '$account_type') ".$and." FROM ecmtransactions AS t
ORDER BY a.index_dbf INNER JOIN accounts AS a ON t.parent_id = a.id
" ); LEFT JOIN currencies AS cur ON cur.id = a.currency_id
WHERE " . $type . "
t.deleted='0' AND (a.account_type='rs' OR a.account_type = '$account_type') " . $and . "
ORDER BY a.index_dbf
" );
// arrays to handle data for SMARTY // arrays to handle data for SMARTY
$data = array (); $data = array ();
$sum = array (); $sum = array ();
@@ -63,47 +74,49 @@ ORDER BY a.index_dbf
$row = array (); $row = array ();
$row['id'] = $c['parent_id']; $row['id'] = $c['parent_id'];
// Use data from joined accounts/currencies to avoid per-row queries
$row['name'] = $c['account_name'];
$row['index'] = $c['index_dbf'];
$tt= $db->fetchByAssoc ( $db->query ( "SELECT name FROM accounts WHERE id='" . $c ['parent_id'] . "'" ) ); // Compute financial aggregates
$row ['name'] =$tt['name']; $row['unsettled'] = getData($c['parent_id'], 0);
$a=New Account(); $row['not_overdue'] = getData($c['parent_id'], 7);
$a->retrieve($c['parent_id']); $row['overdue'] = getData($c['parent_id'], 1);
$a->currency_id; $row['2'] = getData2($c['parent_id'], 2); // 1..30
if($a->id=='')continue; $row['3'] = getData2($c['parent_id'], 3); // 31..60
$row ['unsettled'] = getData ( $c ['parent_id'], 0 ); $row['4'] = getData2($c['parent_id'], 4); // 61..90
$row ['not_overdue'] = getData ( $c ['parent_id'], 7 ); $row['5'] = getData2($c['parent_id'], 5); // 91..180
$row ['overdue'] = getData ( $c ['parent_id'], 1 ); $row['6'] = getData2($c['parent_id'], 6); // 180..
$row ['2'] =getData2 ( $c ['parent_id'], 2 ); // 1..30 $row['saldo'] = getData($c['parent_id'], 8);
$row ['3'] = getData2 ( $c ['parent_id'], 3 ); // 31..60 $row['today_saldo'] = 0; // getData2 ( $c ['parent_id'],9,$_REQUEST['saldo_date_val'] );
$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['index']=$c['index_dbf'];
$row ['saldo'] = getData ( $c ['parent_id'], 8 );
$row ['today_saldo'] = 0; // getData2 ( $c ['parent_id'],9,$_REQUEST['saldo_date_val'] );
$c= new Currency();
if($a->currency_id=='')$a->currency_id='PLN';
$c->retrieve($a->currency_id);
$row ['currency_id']=$c->name;
// Currency handling
$currency_id = $c['currency_id'] ?: 'PLN';
$currency_name = $c['currency_name'] ?: $currency_id;
$row['currency_id'] = $currency_name;
if($saldo_type=='plus' && !($row ['saldo']>0))continue; // Filters
if($saldo_type=='minus' && $row ['saldo']>=0 )continue; if ($saldo_type=='plus' && !($row['saldo']>0)) continue;
if($saldo_type=='zero' && !$row ['saldo']==0 )continue; if ($saldo_type=='minus' && ($row['saldo']>=0)) continue;
if($saldo_type=='' && $row ['saldo']==0 )continue; if ($saldo_type=='zero' && !($row['saldo']==0)) continue;
$idToPdf.=$row['id'].','; if ($saldo_type=='' && $row['saldo']==0) continue;
$sum [$a->currency_id]['unsettled'] += $row ['unsettled'];
$sum [$a->currency_id]['not_overdue'] += $row ['not_overdue']; $idToPdf .= $row['id'].',';
$sum [$a->currency_id]['overdue'] += $row ['overdue'];
$sum [$a->currency_id]['2'] += $row ['2']; // 1..30 // Summaries per currency
$sum [$a->currency_id]['3'] += $row ['3']; // 31..60 $sum[$currency_id]['unsettled'] += $row['unsettled'];
$sum [$a->currency_id]['4'] += $row ['4']; // 61..90 $sum[$currency_id]['not_overdue'] += $row['not_overdue'];
$sum [$a->currency_id]['5'] += $row ['5']; // 91..180 $sum[$currency_id]['overdue'] += $row['overdue'];
$sum [$a->currency_id]['6'] += $row ['6']; // 180.. $sum[$currency_id]['2'] += $row['2']; // 1..30
$sum [$a->currency_id]['saldo'] += $row ['saldo']; $sum[$currency_id]['3'] += $row['3']; // 31..60
$sum [$a->currency_id]['today_saldo'] += $row ['today_saldo']; $sum[$currency_id]['4'] += $row['4']; // 61..90
$sum [$a->currency_id]['currency_id']=$c->name; $sum[$currency_id]['5'] += $row['5']; // 91..180
$data [] = $row; $sum[$currency_id]['6'] += $row['6']; // 180..
$sum[$currency_id]['saldo'] += $row['saldo'];
$sum[$currency_id]['today_saldo'] += $row['today_saldo'];
$sum[$currency_id]['currency_id'] = $currency_name;
$data[] = $row;
} }
@@ -206,123 +219,120 @@ function getData2($id, $expired,$dates = null) {
$saldo = true; $saldo = true;
break; break;
} }
//start WINIEN
if($_REQUEST['account_type']=='rec')$typ=0;else $typ=1;
$zap= $db->query ( "
SELECT
(
t.value
) AS value,t.id // TYPE 0 transactions
FROM ecmtransactions AS t $ids0 = array();
left JOIN ecmtransactions_rel AS rel $r_values0 = array();
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id) $suma0 = 0.0;
WHERE $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);
t.parent_id = '$id' AND while ($r = $db->fetchByAssoc($q0)) {
t.deleted='0' AND $ids0[] = $r['id'];
" .$settled. " $r_values0[$r['id']] = floatval($r['value']);
t.type='0' AND $suma0 += floatval($r['value']);
" . $payment_date . " group by t.id
" );
$suma=0;
$total_settled=0;
while($r = $db->fetchByAssoc ($zap)){
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$r['id']."' OR ecmtransaction_b_id='".$r['id']."'");
while ($rr = $db->fetchByAssoc($rel)) {
if ($rr['ecmtransaction_a_id'] == $r['id'])
$rel_id = $rr['ecmtransaction_b_id'];
else
$rel_id = $rr['ecmtransaction_a_id'];
$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'"));
$tmp2 = array();
if($t['deleted']==1)continue;
$tmp2['name'] = $t['name'];
$tmp2['trans_id'] = $t['id'];
$tmp['settled_with'][] = $tmp2;
if($expired==9){
if($dates==null){
$date = new DateTime ( date ( "Y-m-d" ) );
$d = $date->format ( 'Y-m-d' );
} else {
$date = new DateTime ( date ( "Y-m-d",strtotime($dates) ) );
$d = $date->format ( 'Y-m-d' );
}
$date2 = new DateTime ( $t['payment_date'] );
if($date2>$date)continue;
}
if($t['type']==0){
if($r['value']<0 && $rr['value']<0){
$total_settled+=abs(floatval($rr['value']));
} else {
$total_settled+=floatval($rr['value']);
}
} else {
$total_settled+=abs(floatval($rr['value']));
}
}
$suma+=$r['value'];
} }
$zap= $db->query ( " $total_settled0 = 0.0;
SELECT if (!empty($ids0)) {
( // Prefetch relations for all ids0
t.value $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)");
) AS value,t.id $relsById0 = array();
FROM ecmtransactions AS t $relatedIds0 = array();
left JOIN ecmtransactions_rel AS rel while ($rr = $db->fetchByAssoc($relQ0)) {
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id) $a = $rr['ecmtransaction_a_id'];
WHERE $b = $rr['ecmtransaction_b_id'];
t.parent_id = '$id' AND $relsById0[$a][] = $rr;
t.deleted='0' AND $relsById0[$b][] = $rr;
" .$settled. " if (!isset($r_values0[$a])) $relatedIds0[$a] = true;
t.type='1' AND if (!isset($r_values0[$b])) $relatedIds0[$b] = true;
}
" . $payment_date . " group by t.id $relatedIds0 = array_keys($relatedIds0);
" ); $txMap0 = array();
$suma2=0; if (!empty($relatedIds0)) {
$total_settled2=0; $relIdList0 = "'" . implode("','", $relatedIds0) . "'";
while($r = $db->fetchByAssoc ($zap)){ $tq = $db->query("SELECT id, type, payment_date, deleted, name FROM ecmtransactions WHERE id IN ($relIdList0)");
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$r['id']."' OR ecmtransaction_b_id='".$r['id']."'"); while ($t = $db->fetchByAssoc($tq)) {
while ($rr = $db->fetchByAssoc($rel)) { $txMap0[$t['id']] = $t;
if ($rr['ecmtransaction_a_id'] == $r['id']) }
$rel_id = $rr['ecmtransaction_b_id']; }
else foreach ($ids0 as $pid) {
$rel_id = $rr['ecmtransaction_a_id']; if (empty($relsById0[$pid])) continue;
$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'")); $r_val = $r_values0[$pid];
if($t['deleted']==1)continue; foreach ($relsById0[$pid] as $rr) {
$tmp2 = array(); // Determine the other side of the relation
$tmp2['name'] = $t['name']; $rel_id = ($rr['ecmtransaction_a_id'] == $pid) ? $rr['ecmtransaction_b_id'] : $rr['ecmtransaction_a_id'];
$tmp2['trans_id'] = $t['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."'"));
$tmp['settled_with'][] = $tmp2; if (!$t || $t['deleted'] == '1') continue;
if($expired==9){ if ($expired == 9) {
if($dates==null){ $date2 = new DateTime($t['payment_date']);
$date = new DateTime ( date ( "Y-m-d" ) ); $cmpDate = new DateTime($d);
$d = $date->format ( 'Y-m-d' ); if ($date2 > $cmpDate) continue;
} else { }
$date = new DateTime ( date ( "Y-m-d",strtotime($dates) ) ); $rrVal = floatval($rr['value']);
$d = $date->format ( 'Y-m-d' ); if ($t['type'] == '0') {
} if ($r_val < 0 && $rrVal < 0) {
$date2 = new DateTime ( $t['payment_date'] ); $total_settled0 += abs($rrVal);
} else {
if($date2>$date)continue; $total_settled0 += $rrVal;
} }
$total_settled2+=abs(floatval($rr['value'])); } else {
$total_settled0 += abs($rrVal);
}
}
} }
$suma2+=$r['value'];
} }
// 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']);
}
return $total_settled-$suma+$suma2-$total_settled2; $total_settled1 = 0.0;
if (!empty($ids1)) {
return $total_settled-$suma; $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) {
$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']));
}
}
}
return $total_settled0 - $suma0 + $suma1 - $total_settled1;
} }
function getSaldo($id){ function getSaldo($id){