PaymentStates fix

This commit is contained in:
Michał Zieliński
2025-08-26 10:42:45 +02:00
parent 3548ed7b26
commit 7e33e40fcc

View File

@@ -174,112 +174,6 @@ $ss->assign("users", $user_list);
echo $ss->display('modules/EcmPaymentStates/tpls/summary1.tpl');
// OPTIMIZED HELPER FUNCTIONS
function getData2($id, $expired, $dates = null) {
static $cache = array();
$cache_key = $id . '_' . $expired . '_' . $dates;
if (isset($cache[$cache_key])) {
return $cache[$cache_key];
}
$db = $GLOBALS['db'];
$today = date('Y-m-d');
// Prepare date conditions based on expired type
$date_conditions = getDateConditions($expired, $dates);
if ($date_conditions === false) {
return 0;
}
$settled_condition = ($expired == 9) ? "" : "t.settled!='1' AND";
// Single optimized query for both transaction types
$query = "
SELECT
t.type,
SUM(t.value) as total_value,
SUM(COALESCE(settled_amounts.settled_amount, 0)) as total_settled
FROM ecmtransactions AS t
LEFT JOIN (
SELECT
CASE
WHEN rel.ecmtransaction_a_id = ? THEN rel.ecmtransaction_b_id
ELSE rel.ecmtransaction_a_id
END as related_id,
SUM(ABS(rel.value)) as settled_amount
FROM ecmtransactions_rel rel
INNER JOIN ecmtransactions t2 ON (
t2.id = CASE
WHEN rel.ecmtransaction_a_id = ? THEN rel.ecmtransaction_b_id
ELSE rel.ecmtransaction_a_id
END
)
WHERE (rel.ecmtransaction_a_id = ? OR rel.ecmtransaction_b_id = ?)
AND t2.deleted = '0'
" . ($expired == 9 && $dates ? "AND t2.payment_date <= '" . date('Y-m-d', strtotime($dates)) . "'" : "") . "
GROUP BY related_id
) settled_amounts ON settled_amounts.related_id = t.id
WHERE t.parent_id = ?
AND t.deleted = '0'
AND " . $settled_condition . "
" . $date_conditions . "
GROUP BY t.type
";
$result = $db->query($query, array($id, $id, $id, $id, $id));
$type0_total = 0; $type0_settled = 0;
$type1_total = 0; $type1_settled = 0;
while ($row = $db->fetchByAssoc($result)) {
if ($row['type'] == '0') {
$type0_total = $row['total_value'];
$type0_settled = $row['total_settled'];
} else {
$type1_total = $row['total_value'];
$type1_settled = $row['total_settled'];
}
}
$result_value = $type1_settled - $type0_total + $type1_total - $type0_settled;
$cache[$cache_key] = $result_value;
return $result_value;
}
function getDateConditions($expired, $dates = null) {
$today = date('Y-m-d');
switch ($expired) {
case 1:
return "AND t.payment_date <= '$today'";
case 2:
$date_from = date('Y-m-d', strtotime('-30 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date < '$today'";
case 3:
$date_to = date('Y-m-d', strtotime('-31 days'));
$date_from = date('Y-m-d', strtotime('-60 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 4:
$date_to = date('Y-m-d', strtotime('-61 days'));
$date_from = date('Y-m-d', strtotime('-90 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 5:
$date_to = date('Y-m-d', strtotime('-91 days'));
$date_from = date('Y-m-d', strtotime('-180 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 6:
$date = date('Y-m-d', strtotime('-181 days'));
return "AND t.payment_date <= '$date'";
case 9:
$target_date = $dates ? date('Y-m-d', strtotime($dates)) : $today;
return "AND t.payment_date <= '$target_date'";
default:
return "AND 1=1";
}
}
function getData($id, $expired) {
static $cache = array();
$cache_key = $id . '_' . $expired;
@@ -320,14 +214,14 @@ function getData($id, $expired) {
$query_ma = "
SELECT SUM(t.value) AS sum
FROM ecmtransactions AS t
WHERE t.parent_id = ?
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='1'
AND $date_condition
";
$result_ma = $db->query($query_ma, array($id));
$result_ma = $db->query($query_ma);
$ma_sum = 0;
if ($row = $db->fetchByAssoc($result_ma)) {
$ma_sum = floatval($row['sum']);
@@ -342,14 +236,14 @@ function getData($id, $expired) {
$query_winien = "
SELECT SUM(t.value) AS sum
FROM ecmtransactions AS t
WHERE t.parent_id = ?
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='0'
AND $date_condition
";
$result_winien = $db->query($query_winien, array($id));
$result_winien = $db->query($query_winien);
$winien_sum = 0;
if ($row = $db->fetchByAssoc($result_winien)) {
$winien_sum = floatval($row['sum']);
@@ -366,6 +260,157 @@ function getData($id, $expired) {
return $result;
}
function getData2($id, $expired, $dates = null) {
static $cache = array();
$cache_key = $id . '_' . $expired . '_' . $dates;
if (isset($cache[$cache_key])) {
return $cache[$cache_key];
}
$db = $GLOBALS['db'];
// Prepare date conditions based on expired type
$date_conditions = getDateConditions($expired, $dates);
if ($date_conditions === false) {
return 0;
}
$settled_condition = ($expired == 9) ? "" : "t.settled!='1' AND";
// Get type 0 transactions (WINIEN)
$query_winien = "
SELECT SUM(t.value) AS sum
FROM ecmtransactions AS t
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='0'
$date_conditions
";
$result_winien = $db->query($query_winien);
$winien_sum = 0;
if ($row = $db->fetchByAssoc($result_winien)) {
$winien_sum = floatval($row['sum']);
}
// Calculate settled amounts for type 0
$settled_winien = 0;
$rel_query_winien = "
SELECT r.value
FROM ecmtransactions AS t
LEFT JOIN ecmtransactions_rel AS r ON (r.ecmtransaction_a_id=t.id OR r.ecmtransaction_b_id=t.id)
LEFT JOIN ecmtransactions AS t2 ON (
t2.id = CASE
WHEN r.ecmtransaction_a_id = t.id THEN r.ecmtransaction_b_id
ELSE r.ecmtransaction_a_id
END
)
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='0'
$date_conditions
AND t2.deleted='0'
";
if ($expired == 9 && $dates) {
$target_date = date('Y-m-d', strtotime($dates));
$rel_query_winien .= " AND t2.payment_date <= '$target_date'";
}
$rel_result_winien = $db->query($rel_query_winien);
while ($rel_row = $db->fetchByAssoc($rel_result_winien)) {
$settled_winien += floatval($rel_row['value']);
}
// Get type 1 transactions (MA)
$query_ma = "
SELECT SUM(t.value) AS sum
FROM ecmtransactions AS t
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='1'
$date_conditions
";
$result_ma = $db->query($query_ma);
$ma_sum = 0;
if ($row = $db->fetchByAssoc($result_ma)) {
$ma_sum = floatval($row['sum']);
}
// Calculate settled amounts for type 1
$settled_ma = 0;
$rel_query_ma = "
SELECT r.value
FROM ecmtransactions AS t
LEFT JOIN ecmtransactions_rel AS r ON (r.ecmtransaction_a_id=t.id OR r.ecmtransaction_b_id=t.id)
LEFT JOIN ecmtransactions AS t2 ON (
t2.id = CASE
WHEN r.ecmtransaction_a_id = t.id THEN r.ecmtransaction_b_id
ELSE r.ecmtransaction_a_id
END
)
WHERE t.parent_id = '$id'
AND t.deleted='0'
AND $settled_condition
t.type='1'
$date_conditions
AND t2.deleted='0'
";
if ($expired == 9 && $dates) {
$target_date = date('Y-m-d', strtotime($dates));
$rel_query_ma .= " AND t2.payment_date <= '$target_date'";
}
$rel_result_ma = $db->query($rel_query_ma);
while ($rel_row = $db->fetchByAssoc($rel_result_ma)) {
$settled_ma += abs(floatval($rel_row['value']));
}
$result_value = $settled_ma - $winien_sum + $ma_sum - $settled_winien;
$cache[$cache_key] = $result_value;
return $result_value;
}
function getDateConditions($expired, $dates = null) {
$today = date('Y-m-d');
switch ($expired) {
case 1:
return "AND t.payment_date <= '$today'";
case 2:
$date_from = date('Y-m-d', strtotime('-30 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date < '$today'";
case 3:
$date_to = date('Y-m-d', strtotime('-31 days'));
$date_from = date('Y-m-d', strtotime('-60 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 4:
$date_to = date('Y-m-d', strtotime('-61 days'));
$date_from = date('Y-m-d', strtotime('-90 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 5:
$date_to = date('Y-m-d', strtotime('-91 days'));
$date_from = date('Y-m-d', strtotime('-180 days'));
return "AND t.payment_date >= '$date_from' AND t.payment_date <= '$date_to'";
case 6:
$date = date('Y-m-d', strtotime('-181 days'));
return "AND t.payment_date <= '$date'";
case 9:
$target_date = $dates ? date('Y-m-d', strtotime($dates)) : $today;
return "AND t.payment_date <= '$target_date'";
default:
return "AND 1=1";
}
}
// Comparison functions (compatible with PHP 5.6)
function cmpUnsettled($a, $b) {
if ($a['unsettled'] == $b['unsettled']) return 0;