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
if ($account_type == '')
$clients = $db->query ( "SELECT distinct t.parent_id,a.index_dbf as index_dbf FROM ecmtransactions as t
INNER JOIN accounts AS a
ON t.parent_id = a.id
WHERE ".$type." t.deleted='0' ".$and." ORDER BY a.index_dbf" );
$clients = $db->query ( "SELECT DISTINCT
t.parent_id,
a.index_dbf AS 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
$clients = $db->query ( "
SELECT distinct t.parent_id, a.index_dbf as index_dbf FROM ecmtransactions AS t
INNER JOIN accounts AS a
ON t.parent_id = a.id
WHERE
".$type."
t.deleted='0' AND (a.account_type='rs' OR
a.account_type = '$account_type') ".$and."
ORDER BY a.index_dbf
" );
SELECT DISTINCT
t.parent_id,
a.index_dbf AS 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 (a.account_type='rs' OR a.account_type = '$account_type') " . $and . "
ORDER BY a.index_dbf
" );
// arrays to handle data for SMARTY
$data = array ();
$sum = array ();
@@ -63,47 +74,49 @@ ORDER BY a.index_dbf
$row = array ();
$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'] . "'" ) );
$row ['name'] =$tt['name'];
$a=New Account();
$a->retrieve($c['parent_id']);
$a->currency_id;
if($a->id=='')continue;
$row ['unsettled'] = getData ( $c ['parent_id'], 0 );
$row ['not_overdue'] = getData ( $c ['parent_id'], 7 );
$row ['overdue'] = getData ( $c ['parent_id'], 1 );
$row ['2'] =getData2 ( $c ['parent_id'], 2 ); // 1..30
$row ['3'] = getData2 ( $c ['parent_id'], 3 ); // 31..60
$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;
// Compute financial aggregates
$row['unsettled'] = getData($c['parent_id'], 0);
$row['not_overdue'] = getData($c['parent_id'], 7);
$row['overdue'] = getData($c['parent_id'], 1);
$row['2'] = getData2($c['parent_id'], 2); // 1..30
$row['3'] = getData2($c['parent_id'], 3); // 31..60
$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;
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'].',';
$sum [$a->currency_id]['unsettled'] += $row ['unsettled'];
$sum [$a->currency_id]['not_overdue'] += $row ['not_overdue'];
$sum [$a->currency_id]['overdue'] += $row ['overdue'];
$sum [$a->currency_id]['2'] += $row ['2']; // 1..30
$sum [$a->currency_id]['3'] += $row ['3']; // 31..60
$sum [$a->currency_id]['4'] += $row ['4']; // 61..90
$sum [$a->currency_id]['5'] += $row ['5']; // 91..180
$sum [$a->currency_id]['6'] += $row ['6']; // 180..
$sum [$a->currency_id]['saldo'] += $row ['saldo'];
$sum [$a->currency_id]['today_saldo'] += $row ['today_saldo'];
$sum [$a->currency_id]['currency_id']=$c->name;
$data [] = $row;
// 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'].',';
// Summaries per currency
$sum[$currency_id]['unsettled'] += $row['unsettled'];
$sum[$currency_id]['not_overdue'] += $row['not_overdue'];
$sum[$currency_id]['overdue'] += $row['overdue'];
$sum[$currency_id]['2'] += $row['2']; // 1..30
$sum[$currency_id]['3'] += $row['3']; // 31..60
$sum[$currency_id]['4'] += $row['4']; // 61..90
$sum[$currency_id]['5'] += $row['5']; // 91..180
$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;
break;
}
//start WINIEN
if($_REQUEST['account_type']=='rec')$typ=0;else $typ=1;
$zap= $db->query ( "
SELECT
(
t.value
) AS value,t.id
FROM ecmtransactions AS t
left JOIN ecmtransactions_rel AS rel
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
WHERE
t.parent_id = '$id' AND
t.deleted='0' AND
" .$settled. "
t.type='0' AND
" . $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'];
// TYPE 0 transactions
$ids0 = array();
$r_values0 = 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)) {
$ids0[] = $r['id'];
$r_values0[$r['id']] = floatval($r['value']);
$suma0 += floatval($r['value']);
}
$zap= $db->query ( "
SELECT
(
t.value
) AS value,t.id
FROM ecmtransactions AS t
left JOIN ecmtransactions_rel AS rel
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
WHERE
t.parent_id = '$id' AND
t.deleted='0' AND
" .$settled. "
t.type='1' AND
" . $payment_date . " group by t.id
" );
$suma2=0;
$total_settled2=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'"));
if($t['deleted']==1)continue;
$tmp2 = array();
$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;
}
$total_settled2+=abs(floatval($rr['value']));
$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;
$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);
}
}
}
$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;
return $total_settled-$suma;
$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) {
$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){