749 lines
26 KiB
PHP
749 lines
26 KiB
PHP
<?php
|
|
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
|
|
die ( 'Not A Valid Entry Point' );
|
|
|
|
|
|
ini_set('memory_limit','-1');
|
|
ini_set('max_execution_time','-1');
|
|
// prepare data
|
|
$db = $GLOBALS ['db'];
|
|
|
|
$db->query('RESET QUERY CACHE;');
|
|
$db->query('FLUSH QUERY CACHE');
|
|
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 = '';
|
|
|
|
if ($_REQUEST['type2'])
|
|
$and = " and account_type2='".$_REQUEST['type2']."'";
|
|
if($saldo_type!=''){
|
|
$type="t.settled!='1' and";
|
|
} else {
|
|
$type='';
|
|
}
|
|
|
|
if ($_REQUEST['user_id']!=''){
|
|
$and.= " and a.assigned_user_id='".$_REQUEST['user_id']."'";
|
|
} else {
|
|
$type='';
|
|
}
|
|
|
|
// get list of accounts
|
|
if ($account_type == '')
|
|
$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,
|
|
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 ();
|
|
$idToPdf='';
|
|
|
|
while ( $c = $db->fetchByAssoc ( $clients ) ) {
|
|
|
|
$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'];
|
|
|
|
// 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;
|
|
|
|
// 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;
|
|
}
|
|
|
|
|
|
// sort
|
|
$sort = array ();
|
|
|
|
|
|
}
|
|
$user_list=array();
|
|
|
|
$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'];
|
|
}
|
|
// 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';
|
|
$ss->assign("account_type_list", $app_list_strings['account_type_dom']);
|
|
$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' );
|
|
|
|
// helper functions
|
|
function getData2($id, $expired,$dates = null) {
|
|
$db = $GLOBALS ['db'];
|
|
|
|
$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;
|
|
}
|
|
|
|
// 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']);
|
|
}
|
|
|
|
$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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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']);
|
|
}
|
|
|
|
$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){
|
|
$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'] = '<a href="index.php?module=EcmTransactions&action=DetailView&record='.$r['id'].'" traget="new">'.$r['name'].'</a>';
|
|
$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'] = '<a href="index.php?module=EcmTransactions&action=DetailView&record='.$r['id'].'" traget="new">'.$r['name'].'</a>';
|
|
$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) {
|
|
$db = $GLOBALS ['db'];
|
|
|
|
$saldo = false;
|
|
$settled = " t.settled!='1' AND ";
|
|
|
|
switch ($expired) {
|
|
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;
|
|
}
|
|
|
|
|
|
//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;
|
|
}
|
|
//END MA
|
|
return $res;
|
|
}
|
|
// sort comparing functions
|
|
function cmpUnsettled($a, $b) {
|
|
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;
|
|
}
|
|
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) {
|
|
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;
|
|
}
|
|
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 cmpAccountDesc($a, $b) {
|
|
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;
|
|
}
|
|
|
|
function cmpSaldoDesc($a, $b) {
|
|
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;
|
|
}
|
|
|
|
function cmpTodaySaldoDesc($a, $b) {
|
|
if ($a ['today_saldo'] == $b ['today_saldo']) {
|
|
return 0;
|
|
}
|
|
return ($a ['today_saldo'] < $b ['today_saldo']) ? 1 : -1;
|
|
}
|
|
|
|
?>
|