From bc6348486df456c9f88fad5bee75a20bfc6bb739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 26 Aug 2025 12:16:05 +0200 Subject: [PATCH] back to begin :) --- modules/EcmPaymentStates/summaryNew.php | 1047 ++++++++++++++--------- 1 file changed, 666 insertions(+), 381 deletions(-) diff --git a/modules/EcmPaymentStates/summaryNew.php b/modules/EcmPaymentStates/summaryNew.php index 3bffd347..9916bff5 100644 --- a/modules/EcmPaymentStates/summaryNew.php +++ b/modules/EcmPaymentStates/summaryNew.php @@ -1,454 +1,739 @@ query('RESET QUERY CACHE;'); $db->query('FLUSH QUERY CACHE'); - -// Initialize variables with defaults -$account_type = ''; -$saldo_type = ''; -$data = array(); -$sum = array(); -$idToPdf = ''; - -if (!empty($_REQUEST['submit'])) { - // Sanitize and validate inputs - $account_type = isset($_REQUEST['account_type']) ? trim($_REQUEST['account_type']) : ''; - if ($account_type == 'a') { +if($_REQUEST['submit']){ + if ($_REQUEST['account_type']) + $account_type = $_REQUEST['account_type']; + else + $account_type = ''; + if($_REQUEST['account_type']=='a'){ $account_type = ''; } + if ($_REQUEST['saldo_type']) + $saldo_type = $_REQUEST['saldo_type']; + else + $saldo_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 ($_REQUEST['type2']) + $and = " and account_type2='".$_REQUEST['type2']."'"; + if($saldo_type!=''){ + $type="t.settled!='1' and"; + } else { + $type=''; } - if (!empty($type2)) { - $conditions[] = "a.account_type2='" . $type2 . "'"; + if ($_REQUEST['user_id']!=''){ + $and.= " and a.assigned_user_id='".$_REQUEST['user_id']."'"; + } else { + $type=''; } - if (!empty($user_id)) { - $conditions[] = "a.assigned_user_id='" . $user_id . "'"; +// 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" ); + 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 + " ); +// arrays to handle data for SMARTY + $data = array (); + $sum = array (); + $idToPdf=''; + + while ( $c = $db->fetchByAssoc ( $clients ) ) { + + $row = array (); + $row['id'] = $c['parent_id']; + + $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; + + + 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; } - // 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, - a.index_dbf, - a.name as account_name, - a.currency_id, - a.id as account_id - FROM ecmtransactions as t - INNER JOIN accounts AS a ON t.parent_id = a.id - WHERE " . implode(' AND ', $conditions) . $account_condition . " - ORDER BY a.index_dbf"; +// sort + $sort = array (); - $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'])) { - $client_ids[] = $c['parent_id']; - $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])) { - $currency_obj = new Currency(); - $currency_obj->retrieve($currency_id); - $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']; - $row['not_overdue'] = $fin_data['not_overdue']; - $row['overdue'] = $fin_data['overdue']; - $row['2'] = $fin_data['2']; - $row['3'] = $fin_data['3']; - $row['4'] = $fin_data['4']; - $row['5'] = $fin_data['5']; - $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( - 'unsettled' => 0, - 'not_overdue' => 0, - 'overdue' => 0, - '2' => 0, - '3' => 0, - '4' => 0, - '5' => 0, - '6' => 0, - 'saldo' => 0, - 'today_saldo' => 0, - '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']; - $sum[$currency_id]['2'] += $row['2']; - $sum[$currency_id]['3'] += $row['3']; - $sum[$currency_id]['4'] += $row['4']; - $sum[$currency_id]['5'] += $row['5']; - $sum[$currency_id]['6'] += $row['6']; - $sum[$currency_id]['saldo'] += $row['saldo']; - $sum[$currency_id]['today_saldo'] += $row['today_saldo']; - - $data[] = $row; - } } +$user_list=array(); -// Get user list with single optimized query -$user_list = array(); -$users_query = "SELECT id, CONCAT(COALESCE(first_name, ''), ' ', COALESCE(last_name, '')) as full_name - FROM users - WHERE deleted=0 - ORDER BY first_name, last_name"; -$users_result = $db->query($users_query); -while ($user_data = $db->fetchByAssoc($users_result)) { - $user_list[$user_data['id']] = trim($user_data['full_name']); +$z=$db->query("select id,first_name,last_name from users where deleted=0"); +while($dane=$db->fetchByAssoc($z)){ + $user_list[$dane['id']]=$dane['first_name'].' '.$dane['last_name']; } - -// Initialize Smarty template -$ss = new Sugar_Smarty(); -global $mod_strings, $app_list_strings; - -$ss->assign("MOD", $mod_strings); -$ss->assign("DATA", $data); -$ss->assign("SUM", $sum); -$ss->assign("SORT", array()); +// create & execute smarty +$ss = new Sugar_Smarty (); +global $mod_strings,$app_list_strings; +$ss->assign ( "MOD", $mod_strings ); +$ss->assign ( "DATA", $data ); +$ss->assign ( "SUM", $sum ); +$ss->assign ( "SORT", $sort ); $ss->assign("account_type", $account_type); - -$app_list_strings['account_type_dom']['a'] = 'Wszystkie'; +$app_list_strings['account_type_dom']['a']='Wszystkie'; $ss->assign("account_type_list", $app_list_strings['account_type_dom']); -$ss->assign("saldo_type", $saldo_type); -$ss->assign("saldo_date_val", isset($_REQUEST['saldo_date_val']) ? $_REQUEST['saldo_date_val'] : ''); -$ss->assign("idToPdf", rtrim($idToPdf, ',')); -$ss->assign("type", isset($_REQUEST['type2']) ? $_REQUEST['type2'] : ''); -$ss->assign("type2", $app_list_strings['account_type2_dom']); -$ss->assign("user_id", isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : ''); -$ss->assign("users", $user_list); +$ss->assign("saldo_type",$saldo_type); +$ss->assign("saldo_date_val",$_REQUEST['saldo_date_val']); +$ss->assign("idToPdf",$idToPdf); +$ss->assign("type",$_REQUEST['type2']); +$ss->assign("type2",$app_list_strings['account_type2_dom']); +$ss->assign("user_id",$_REQUEST['user_id']); +$ss->assign("users",$user_list); -echo $ss->display('modules/EcmPaymentStates/tpls/summary1.tpl'); +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; +// helper functions +function getData2($id, $expired,$dates = null) { + $db = $GLOBALS ['db']; - if ($global_cache !== null && !empty($global_cache)) { - return $global_cache; + $saldo = false; + $settled = " t.settled!='1' AND "; + switch ($expired) { + case 1 : + $date = new DateTime ( date ( "Y-m-d" ) ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date <= '$d'"; + break; + case 2 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-30 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date >= '$d_f' AND payment_date < '$d_t'"; + break; + case 3 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-31 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-60 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date >= '$d_f' AND payment_date <= '$d_t'"; + break; + case 4 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-61 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-90 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date >= '$d_f' AND payment_date <= '$d_t'"; + break; + case 5 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-91 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-180 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date >= '$d_f' AND payment_date <= '$d_t'"; + break; + case 6 : + $date = new DateTime ( date ( "Y-m-d" ) ); + $date->modify ( '-181 days' ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date <= '$d'"; + break; + case 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' ); + } + $date = new DateTime ( date ( "Y-m-d" ) ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date <= '$d'"; + $settled = " "; + $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']; } - if (empty($client_ids)) { - return array(); + $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'])); + } + $suma2+=$r['value']; } - $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 - t.parent_id, - t.type, - t.settled, - t.value, - t.payment_date, - COALESCE(settled_data.settled_amount, 0) as settled_amount - FROM ecmtransactions t - LEFT JOIN ( - SELECT - parent_trans.id as transaction_id, - SUM( - CASE - WHEN parent_trans.type = '0' AND rel_trans.type = '0' AND parent_trans.value < 0 AND r.value < 0 - THEN ABS(r.value) - WHEN parent_trans.type = '0' - THEN r.value - ELSE ABS(r.value) - END - ) as settled_amount - FROM ecmtransactions parent_trans - LEFT JOIN ecmtransactions_rel r ON (r.ecmtransaction_a_id = parent_trans.id OR r.ecmtransaction_b_id = parent_trans.id) - LEFT JOIN ecmtransactions rel_trans ON ( - rel_trans.id = CASE - WHEN r.ecmtransaction_a_id = parent_trans.id THEN r.ecmtransaction_b_id - ELSE r.ecmtransaction_a_id - END - AND rel_trans.deleted = '0' - ) - WHERE parent_trans.parent_id IN ($ids_string) - AND parent_trans.deleted = '0' - GROUP BY parent_trans.id - ) settled_data ON settled_data.transaction_id = t.id - WHERE t.parent_id IN ($ids_string) - AND t.deleted = '0' - ORDER BY t.parent_id, t.payment_date - "; + return $total_settled-$suma+$suma2-$total_settled2; - $result = $db->query($comprehensive_query); - $financial_data = array(); + return $total_settled-$suma; - while ($row = $db->fetchByAssoc($result)) { - $parent_id = $row['parent_id']; - $type = $row['type']; - $settled = $row['settled']; - $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, - 'not_overdue' => 0, - 'overdue' => 0, - '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0, - '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; - if ($days_diff < 0) { // Future date - $financial_data[$parent_id]['not_overdue'] += $effective_value; - } - if ($settled != '1') { - $financial_data[$parent_id]['unsettled'] += $effective_value; - } - } else { // WINIEN (payable) - $financial_data[$parent_id]['saldo'] -= $effective_value; - if ($days_diff >= 0 && $settled != '1') { // Past due and not settled - $financial_data[$parent_id]['overdue'] += $effective_value; - } - if ($settled != '1') { - $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) { - $financial_data[$parent_id]['2'] += $effective_value; - } elseif ($days_diff >= 31 && $days_diff <= 60) { - $financial_data[$parent_id]['3'] += $effective_value; - } elseif ($days_diff >= 61 && $days_diff <= 90) { - $financial_data[$parent_id]['4'] += $effective_value; - } elseif ($days_diff >= 91 && $days_diff <= 180) { - $financial_data[$parent_id]['5'] += $effective_value; - } elseif ($days_diff > 180) { - $financial_data[$parent_id]['6'] += $effective_value; - } - } - } - - $global_cache = $financial_data; - return $financial_data; } -// Legacy function wrappers for backward compatibility (now use cached data) +function getSaldo($id){ + $db = $GLOBALS ['db']; + + $query_w=" + SELECT * FROM ecmtransactions + WHERE deleted='0' AND + parent_id='".$id."' + AND type='0' + ORDER BY payment_date desc"; + $query_ma=" + SELECT * FROM ecmtransactions + WHERE deleted='0' AND + parent_id='".$id."' + AND type='1' + ORDER BY payment_date desc"; + + $res = $db->query($query_w); + $total_winien = 0; + + $winien = array(); + + while ($r = $db->fetchByAssoc($res)) { + + $tmp = array(); + $tmp['settled_with'] = array(); + $total_settled = 0; + $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(); + $tmp2['name'] = $t['name']; + $tmp2['trans_id'] = $t['id']; + $tmp2['value']=$rr['value']; + if($t['type']==0 && $rel->num_rows==1){ + + if(abs($rr['value'])==abs($r['value'])){ + if($r['value']>0){ + $rr['value']=abs($rr['value']); + } else { + $v = -1 * abs( $rr['value']); + $rr['value']=$v; + } + } + } else { + + if(abs($rr['value'])==abs($r['value'])){ + if($r['value']>0){ + $rr['value']=abs($rr['value']); + } else { + $v = -1 * abs( $rr['value']); + $rr['value']=$v; + } + } else { + if($r['value']>0){ + $rr['value']=abs($rr['value']); + } + } + } + $tmp['settled_with'][] = $tmp2; + $total_settled+=floatval($rr['value']); + } + + //date comparsion + if ($r['settled'] == '1') { + $d1 = new DateTime($r['register_date']); + $d2 = new DateTime($change_date); + if ($d1 < $d2) + $total_settled = $r['value']; + } else $r['settled'] = '0'; //prevent null + + + + $tmp['document_no'] = ''.$r['name'].''; + $tmp['total'] = format_number($r['value']); + $tmp['settled'] = format_number($total_settled); + if($r['settled']==1){ + $tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']); + $total_winien+=floatval(unsettledValue($total_settled,$r['value'])); + }else{ + $tmp['unsettled'] = format_number($r['value']-$total_settled); + $total_winien+=floatval($r['value']-$total_settled); + } + $tmp['id'] = $r['id']; + $tmp['note']= $r['note']; + $tmp['note_id']= $r['note_id']; + $tmp['is_settled'] = $r['settled']; + + + + } + + $res = $db->query($query_ma); + $ma = array(); + $total_ma = 0; + while ($r = $db->fetchByAssoc($res)) { + $tmp = array(); + $tmp['settled_with'] = array(); + $total_settled = 0; + $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(); + $tmp2['name'] = $t['name']; + $tmp2['trans_id'] = $t['id']; + $tmp2['value']=$rr['value']; + if($t['type']==0 && $rel->num_rows==1){ + + if(abs($rr['value'])==abs($r['value'])){ + if($r['value']>0){ + $rr['value']=abs($rr['value']); + } else { + $v = -1 * abs( $rr['value']); + $rr['value']=$v; + } + } + } + $tmp['settled_with'][] = $tmp2; + $total_settled+=floatval($rr['value']); + } + + + //date comparsion + if ($r['settled'] == '1') { + $d1 = new DateTime($r['register_date']); + $d2 = new DateTime($change_date); + if ($d1 < $d2) + $total_settled = $r['value']; + } else $r['settled'] = '0'; //prevent null + + + $tmp['name'] = ''.$r['name'].''; + $tmp['total'] = format_number($r['value']); + $tmp['settled'] = format_number($total_settled); + if($r['settled']==1){ + $tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']); + $total_winien+=floatval(unsettledValue($total_settled,$r['value'])); + //$tmp['unsettled'] = format_number(abs($r['value'])-$total_settled); echo 'rozliczone'; + }else{ + $tmp['unsettled'] = format_number($r['value']-$total_settled); + $total_winien+=floatval($r['value']-$total_settled); + } + $tmp['is_settled'] = $r['settled']; + $tmp['id'] = $r['id']; + $ma[] = $tmp; + + //$total_ma+=floatval($r['value']); + } + return $total_ma - $total_winien; +} +function unsettledFormatValue($settled,$val){ + if($settled<0 && $val>0){ + return format_number($val+$settled); + } + if($settled<0 && $val<0){ + return format_number($val+abs($settled)); + } + if($settled>0 && $val>0){ + return format_number($val-abs($settled)); + } + if($settled>0 && $val<0){ + return format_number($val+$settled); + } +} + +function unsettledValue($settled,$val){ + if($settled<0 && $val>0){ + return ($val+$settled); + } + if($settled<0 && $val<0){ + return ($val+abs($settled)); + } + if($settled>0 && $val>0){ + return ($val-abs($settled)); + } + if($settled>0 && $val<0){ + return ($val+$settled); + } +} function getData($id, $expired) { - static $batch_data = null; + $db = $GLOBALS ['db']; - if ($batch_data === null) { - $batch_data = getAllFinancialData(array($id)); - } - - if (!isset($batch_data[$id])) { - return 0; - } - - $data = $batch_data[$id]; + $saldo = false; + $settled = " t.settled!='1' AND "; 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 9: return $data['saldo']; // Saldo - default: return getData2($id, $expired); - } -} - -function getData2($id, $expired, $dates = null) { - static $batch_data = null; - - if ($batch_data === null) { - $batch_data = getAllFinancialData(array($id)); + case 0 : + $payment_date = "1=1"; + break; + case 1 : + $date = new DateTime ( date ( "Y-m-d" ) ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date <= '$d'"; + $saldo=true; + break; + case 2 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-30 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date > '$d_f' AND payment_date < '$d_t'"; + break; + case 3 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-31 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-60 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date > '$d_f' AND payment_date < '$d_t'"; + break; + case 4 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-61 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-90 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date > '$d_f' AND payment_date < '$d_t'"; + break; + case 5 : + $date_to = new DateTime ( date ( "Y-m-d" ) ); + $date_to->modify ( '-91 days' ); + $d_t = $date_to->format ( 'Y-m-d' ); + $date_from = new DateTime ( date ( "Y-m-d" ) ); + $date_from->modify ( '-180 days' ); + $d_f = $date_from->format ( 'Y-m-d' ); + $payment_date = " payment_date > '$d_f' AND payment_date < '$d_t'"; + break; + case 6 : + $date = new DateTime ( date ( "Y-m-d" ) ); + $date->modify ( '-181 days' ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date < '$d'"; + break; + case 7 : + $date = new DateTime ( date ( "Y-m-d" ) ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date >= '$d'"; + break; + case 8 : + $payment_date = " 1=1 "; + $saldo = true; + $settled = " 1=1 and"; + break; + case 9: + $date = new DateTime ( date ( "Y-m-d" ) ); + $d = $date->format ( 'Y-m-d' ); + $payment_date = " payment_date <= '$d'"; + $settled = " 1=1 AND "; + $saldo = true; + break; } - if (!isset($batch_data[$id])) { + + //start WINIEN + $r = $db->fetchByAssoc ( $db->query ( " + SELECT + sum( + t.value + ) AS sum + FROM ecmtransactions AS t + WHERE + t.parent_id = '$id' AND + t.deleted='0' AND + " .$settled. " + t.type='1' AND + " . $payment_date . " + " ) ); + + //get part settled transactions + /* + if (!$saldo){ + $s = $db->fetchByAssoc($db->query(" + SELECT + sum( + CASE WHEN t.currency_id='PLN' THEN t.value + ELSE t.value*t.currency_value + END + ) AS sum + FROM ecmtransactions AS t + WHERE + t.parent_id = '$id' AND + t.deleted='0' AND + " .$settled. " + t.type='0' AND + t.register_date > '2011-12-31' AND + " . $payment_date . " + + ")); + + } + */ + if (!$saldo){ + return $r['sum']; + } + if (! is_numeric ( $r ['sum'] )) + $r ['sum'] = 0; + + if (! is_numeric ( $s ['settled'] )) + $s ['settled'] = 0; + + if (floatval($s['settled']) > 0) + $r['sum'] = floatval($r['sum']) - floatval($s['settled']); + + $sum = $r['sum']; + //END WINIEN + + $r['sum'] = 0; + if ($saldo) { + $r = $db->fetchByAssoc ( $db->query ( " + SELECT + sum( + t.value + ) AS sum + FROM ecmtransactions AS t + WHERE + t.parent_id = '$id' AND + t.deleted='0' AND + " .$settled. " + t.type='0' AND + " . $payment_date . " + " ) ); + } + $res=$sum - $r ['sum']; + if($expired==1 && $res>0){ 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 4: return $data['4']; // 61-90 days - case 5: return $data['5']; // 91-180 days - case 6: return $data['6']; // 180+ days - case 9: return $data['saldo']; // Saldo with date - default: return 0; - } + //END MA + return $res; } - -// Comparison functions (PHP 5.6 compatible) +// sort comparing functions function cmpUnsettled($a, $b) { - if ($a['unsettled'] == $b['unsettled']) return 0; - return ($a['unsettled'] < $b['unsettled']) ? -1 : 1; + if ($a ['unsettled'] == $b ['unsettled']) { + return 0; + } + return ($a ['unsettled'] < $b ['unsettled']) ? - 1 : 1; } - function cmpUnsettledDesc($a, $b) { - if ($a['unsettled'] == $b['unsettled']) return 0; - return ($a['unsettled'] < $b['unsettled']) ? 1 : -1; + if ($a ['unsettled'] == $b ['unsettled']) { + return 0; + } + return ($a ['unsettled'] < $b ['unsettled']) ? 1 : - 1; } - function cmpNotOverdue($a, $b) { - if ($a['not_overdue'] == $b['not_overdue']) return 0; - return ($a['not_overdue'] < $b['not_overdue']) ? -1 : 1; + if ($a ['not_overdue'] == $b ['not_overdue']) { + return 0; + } + return ($a ['not_overdue'] < $b ['not_overdue']) ? - 1 : 1; } - function cmpNotOverdueDesc($a, $b) { - if ($a['not_overdue'] == $b['not_overdue']) return 0; - return ($a['not_overdue'] < $b['not_overdue']) ? 1 : -1; + if ($a ['not_overdue'] == $b ['not_overdue']) { + return 0; + } + return ($a ['not_overdue'] < $b ['not_overdue']) ? 1 : - 1; } - function cmpOverdue($a, $b) { - if ($a['overdue'] == $b['overdue']) return 0; - return ($a['overdue'] < $b['overdue']) ? -1 : 1; + if ($a ['overdue'] == $b ['overdue']) { + return 0; + } + return ($a ['overdue'] < $b ['overdue']) ? - 1 : 1; } - function cmpOverdueDesc($a, $b) { - if ($a['overdue'] == $b['overdue']) return 0; - return ($a['overdue'] < $b['overdue']) ? 1 : -1; + if ($a ['overdue'] == $b ['overdue']) { + return 0; + } + return ($a ['overdue'] < $b ['overdue']) ? 1 : - 1; } - function cmpAccount($a, $b) { - return strnatcmp($a['name'], $b['name']); + return strnatcmp ( $a ['name'], $b ['name'] ); } - function cmpAccountDesc($a, $b) { - return -strnatcmp($a['name'], $b['name']); + return - 1 * strnatcmp ( $a ['name'], $b ['name'] ); } function cmpSaldo($a, $b) { - if ($a['saldo'] == $b['saldo']) return 0; - return ($a['saldo'] < $b['saldo']) ? -1 : 1; + if ($a ['saldo'] == $b ['saldo']) { + return 0; + } + return ($a ['saldo'] < $b ['saldo']) ? - 1 : 1; } function cmpSaldoDesc($a, $b) { - if ($a['saldo'] == $b['saldo']) return 0; - return ($a['saldo'] < $b['saldo']) ? 1 : -1; + if ($a ['saldo'] == $b ['saldo']) { + return 0; + } + return ($a ['saldo'] < $b ['saldo']) ? 1 : -1; } function cmpTodaySaldo($a, $b) { - if ($a['today_saldo'] == $b['today_saldo']) return 0; - return ($a['today_saldo'] < $b['today_saldo']) ? -1 : 1; + if ($a ['today_saldo'] == $b ['today_saldo']) { + return 0; + } + return ($a ['today_saldo'] < $b ['today_saldo']) ? - 1 : 1; } function cmpTodaySaldoDesc($a, $b) { - if ($a['today_saldo'] == $b['today_saldo']) return 0; - return ($a['today_saldo'] < $b['today_saldo']) ? 1 : -1; + if ($a ['today_saldo'] == $b ['today_saldo']) { + return 0; + } + return ($a ['today_saldo'] < $b ['today_saldo']) ? 1 : -1; } -// 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: - $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"; - } -} \ No newline at end of file +?> \ No newline at end of file