PaymentStates fix

This commit is contained in:
Michał Zieliński
2025-08-26 12:03:57 +02:00
parent f63dd0c7e2
commit d9a932acd9

View File

@@ -28,32 +28,32 @@ if (!empty($_REQUEST['submit'])) {
if ($account_type == 'a') {
$account_type = '';
}
$saldo_type = isset($_REQUEST['saldo_type']) ? trim($_REQUEST['saldo_type']) : '';
$type2 = isset($_REQUEST['type2']) ? $db->quote(trim($_REQUEST['type2'])) : '';
$user_id = isset($_REQUEST['user_id']) ? $db->quote(trim($_REQUEST['user_id'])) : '';
// Build WHERE conditions safely
$conditions = array("t.deleted='0'");
if ($saldo_type != '') {
$conditions[] = "t.settled!='1'";
}
if (!empty($type2)) {
$conditions[] = "a.account_type2='" . $type2 . "'";
}
if (!empty($user_id)) {
$conditions[] = "a.assigned_user_id='" . $user_id . "'";
}
// Build account type condition
$account_condition = '';
if ($account_type != '') {
$account_condition = " AND (a.account_type='rs' OR a.account_type = '" . $db->quote($account_type) . "')";
}
// Optimized query - get all needed data in one go
$query = "SELECT DISTINCT
t.parent_id,
@@ -65,11 +65,11 @@ if (!empty($_REQUEST['submit'])) {
INNER JOIN accounts AS a ON t.parent_id = a.id
WHERE " . implode(' AND ', $conditions) . $account_condition . "
ORDER BY a.index_dbf";
$clients = $db->query($query);
$client_ids = array();
$clients_data = array();
// First pass - collect all client IDs and data
while ($c = $db->fetchByAssoc($clients)) {
if (!empty($c['parent_id'])) {
@@ -77,26 +77,26 @@ if (!empty($_REQUEST['submit'])) {
$clients_data[] = $c;
}
}
// Get all financial data in one batch
$financial_batch = getAllFinancialData($client_ids);
// Cache currency objects to avoid repeated database calls
$currency_cache = array();
foreach ($clients_data as $c) {
$parent_id = $c['parent_id'];
// Skip if no financial data
if (!isset($financial_batch[$parent_id])) {
continue;
}
$row = array();
$row['id'] = $parent_id;
$row['name'] = $c['account_name'];
$row['index'] = $c['index_dbf'];
// Use cached currency data
$currency_id = !empty($c['currency_id']) ? $c['currency_id'] : 'PLN';
if (!isset($currency_cache[$currency_id])) {
@@ -105,7 +105,7 @@ if (!empty($_REQUEST['submit'])) {
$currency_cache[$currency_id] = $currency_obj->name;
}
$row['currency_id'] = $currency_cache[$currency_id];
// Use pre-calculated financial data
$fin_data = $financial_batch[$parent_id];
$row['unsettled'] = $fin_data['unsettled'];
@@ -118,15 +118,15 @@ if (!empty($_REQUEST['submit'])) {
$row['6'] = $fin_data['6'];
$row['saldo'] = $fin_data['saldo'];
$row['today_saldo'] = 0;
// Apply saldo 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'] . ',';
// Accumulate sums by currency
if (!isset($sum[$currency_id])) {
$sum[$currency_id] = array(
@@ -143,7 +143,7 @@ if (!empty($_REQUEST['submit'])) {
'currency_id' => $currency_cache[$currency_id]
);
}
$sum[$currency_id]['unsettled'] += $row['unsettled'];
$sum[$currency_id]['not_overdue'] += $row['not_overdue'];
$sum[$currency_id]['overdue'] += $row['overdue'];
@@ -154,7 +154,7 @@ if (!empty($_REQUEST['submit'])) {
$sum[$currency_id]['6'] += $row['6'];
$sum[$currency_id]['saldo'] += $row['saldo'];
$sum[$currency_id]['today_saldo'] += $row['today_saldo'];
$data[] = $row;
}
}
@@ -195,19 +195,19 @@ echo $ss->display('modules/EcmPaymentStates/tpls/summary1.tpl');
// OPTIMIZED CORE FUNCTION - Gets all financial data in one query
function getAllFinancialData($client_ids) {
static $global_cache = null;
if ($global_cache !== null && !empty($global_cache)) {
return $global_cache;
}
if (empty($client_ids)) {
return array();
}
$db = $GLOBALS['db'];
$today = date('Y-m-d');
$ids_string = "'" . implode("','", array_unique($client_ids)) . "'";
// Single comprehensive query for all financial calculations
$comprehensive_query = "
SELECT
@@ -247,10 +247,10 @@ function getAllFinancialData($client_ids) {
AND t.deleted = '0'
ORDER BY t.parent_id, t.payment_date
";
$result = $db->query($comprehensive_query);
$financial_data = array();
while ($row = $db->fetchByAssoc($result)) {
$parent_id = $row['parent_id'];
$type = $row['type'];
@@ -258,7 +258,7 @@ function getAllFinancialData($client_ids) {
$value = floatval($row['value']);
$settled_amount = floatval($row['settled_amount']);
$payment_date = $row['payment_date'];
if (!isset($financial_data[$parent_id])) {
$financial_data[$parent_id] = array(
'unsettled' => 0,
@@ -268,13 +268,13 @@ function getAllFinancialData($client_ids) {
'saldo' => 0
);
}
// Calculate effective value based on settlement
$effective_value = ($settled == '1') ? $value : ($value - $settled_amount);
// Determine date category
$days_diff = (strtotime($today) - strtotime($payment_date)) / (60 * 60 * 24);
// Apply business logic based on type and date
if ($type == '1') { // MA (receivable)
$financial_data[$parent_id]['saldo'] += $effective_value;
@@ -293,7 +293,7 @@ function getAllFinancialData($client_ids) {
$financial_data[$parent_id]['unsettled'] += $effective_value;
}
}
// Categorize by aging periods (only for non-settled)
if ($settled != '1') {
if ($days_diff >= 1 && $days_diff <= 30) {
@@ -309,7 +309,7 @@ function getAllFinancialData($client_ids) {
}
}
}
$global_cache = $financial_data;
return $financial_data;
}
@@ -317,22 +317,22 @@ function getAllFinancialData($client_ids) {
// Legacy function wrappers for backward compatibility (now use cached data)
function getData($id, $expired) {
static $batch_data = null;
if ($batch_data === null) {
$batch_data = getAllFinancialData(array($id));
}
if (!isset($batch_data[$id])) {
return 0;
}
$data = $batch_data[$id];
switch ($expired) {
case 0: return $data['unsettled']; // All unsettled
case 1: return $data['overdue']; // Overdue
case 7: return $data['not_overdue']; // Not overdue
case 8:
case 7: return $data['not_overdue']; // Not overdue
case 8:
case 9: return $data['saldo']; // Saldo
default: return getData2($id, $expired);
}
@@ -340,20 +340,20 @@ function getData($id, $expired) {
function getData2($id, $expired, $dates = null) {
static $batch_data = null;
if ($batch_data === null) {
$batch_data = getAllFinancialData(array($id));
}
if (!isset($batch_data[$id])) {
return 0;
}
$data = $batch_data[$id];
switch ($expired) {
case 2: return $data['2']; // 1-30 days
case 3: return $data['3']; // 31-60 days
case 3: return $data['3']; // 31-60 days
case 4: return $data['4']; // 61-90 days
case 5: return $data['5']; // 91-180 days
case 6: return $data['6']; // 180+ days
@@ -363,60 +363,60 @@ function getData2($id, $expired, $dates = null) {
}
// Comparison functions (PHP 5.6 compatible)
function cmpUnsettled($a, $b) {
function cmpUnsettled($a, $b) {
if ($a['unsettled'] == $b['unsettled']) return 0;
return ($a['unsettled'] < $b['unsettled']) ? -1 : 1;
}
function cmpUnsettledDesc($a, $b) {
function cmpUnsettledDesc($a, $b) {
if ($a['unsettled'] == $b['unsettled']) return 0;
return ($a['unsettled'] < $b['unsettled']) ? 1 : -1;
}
function cmpNotOverdue($a, $b) {
function cmpNotOverdue($a, $b) {
if ($a['not_overdue'] == $b['not_overdue']) return 0;
return ($a['not_overdue'] < $b['not_overdue']) ? -1 : 1;
}
function cmpNotOverdueDesc($a, $b) {
function cmpNotOverdueDesc($a, $b) {
if ($a['not_overdue'] == $b['not_overdue']) return 0;
return ($a['not_overdue'] < $b['not_overdue']) ? 1 : -1;
}
function cmpOverdue($a, $b) {
function cmpOverdue($a, $b) {
if ($a['overdue'] == $b['overdue']) return 0;
return ($a['overdue'] < $b['overdue']) ? -1 : 1;
}
function cmpOverdueDesc($a, $b) {
function cmpOverdueDesc($a, $b) {
if ($a['overdue'] == $b['overdue']) return 0;
return ($a['overdue'] < $b['overdue']) ? 1 : -1;
}
function cmpAccount($a, $b) {
return strnatcmp($a['name'], $b['name']);
function cmpAccount($a, $b) {
return strnatcmp($a['name'], $b['name']);
}
function cmpAccountDesc($a, $b) {
return -strnatcmp($a['name'], $b['name']);
function cmpAccountDesc($a, $b) {
return -strnatcmp($a['name'], $b['name']);
}
function cmpSaldo($a, $b) {
function cmpSaldo($a, $b) {
if ($a['saldo'] == $b['saldo']) return 0;
return ($a['saldo'] < $b['saldo']) ? -1 : 1;
}
function cmpSaldoDesc($a, $b) {
function cmpSaldoDesc($a, $b) {
if ($a['saldo'] == $b['saldo']) return 0;
return ($a['saldo'] < $b['saldo']) ? 1 : -1;
}
function cmpTodaySaldo($a, $b) {
function cmpTodaySaldo($a, $b) {
if ($a['today_saldo'] == $b['today_saldo']) return 0;
return ($a['today_saldo'] < $b['today_saldo']) ? -1 : 1;
}
function cmpTodaySaldoDesc($a, $b) {
function cmpTodaySaldoDesc($a, $b) {
if ($a['today_saldo'] == $b['today_saldo']) return 0;
return ($a['today_saldo'] < $b['today_saldo']) ? 1 : -1;
}
@@ -424,7 +424,7 @@ function cmpTodaySaldoDesc($a, $b) {
// Helper function for date conditions (now unused but kept for compatibility)
function getDateConditions($expired, $dates = null) {
$today = date('Y-m-d');
switch ($expired) {
case 1: return "AND t.payment_date <= '$today'";
case 2: