Files
crm.e5.pl/modules/EcmPaymentStates/AccountPaymentStates.php

258 lines
7.5 KiB
PHP
Raw Normal View History

2024-04-27 09:23:34 +02:00
<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
die ( 'Not A Valid Entry Point' );
$db = $GLOBALS['db'];
if($_REQUEST['account_id']=='')$_REQUEST['account_id']=$_REQUEST['record'];
$account_id = $_REQUEST['account_id'];
//if (isset($_REQUEST['rid']))
$change_date = '2014-09-03'; //data zmiany systemu rozliczania
$ss = new Sugar_Smarty ();
global $mod_strings;
$ss->assign ( "MOD", $mod_strings );
global $app_strings;
$ss->assign ( "APP", $app_strings );
//var_dump($app_strings);
//brak kontrahenta, tylko wyszukiwanie
if (!$account_id || $account_id=='') {
echo $ss->display ( 'modules/EcmPaymentStates/tpls/AccountPaymentStates.tpl' );
return;
}
$a = new Account();
$a->retrieve($account_id);
global $timedate;
if($_REQUEST['date_to']!=''){
$date_to=$_REQUEST['date_to'];
} else {
$date_to=date('d.m.Y');
}
if ($_REQUEST['show_settled']!='on')
$show_settled = " AND (settled='0' OR settled IS NULL) ";
else
$ss->assign("SHOW_SETTLED", "checked");
if ($_REQUEST['show_after_d']=='on'){
$ss->assign("SHOW_AFTER_D", "checked");
$show_settled = "";
}else
//$ss->assign("SHOW_AFTER_D", "");
if ($_REQUEST['show_after_t']=='on'){ $show_settled = " AND (settled='0' OR settled IS NULL) and payment_date < '".date("Y-m-d")."' ";$ss->assign("SHOW_AFTER_T", "checked");
}else
$ss->assign("SHOW_AFTER_T", "");
if ($_REQUEST['show_after_p']!='on'){
$res = $db->query("
SELECT * FROM ecmtransactions
WHERE deleted='0' AND
parent_id='".$a->id."'
AND type='0'
AND register_date > '2011-12-31'
".$show_settled."
ORDER BY register_date desc");
} else {
$ss->assign("SHOW_AFTER_P", "checked");
$res = $db->query("
SELECT t.* FROM ecmtransactions t
inner join ecmtransactions_rel r on r.ecmtransaction_a_id=t.id
inner join ecmtransactions t2 on
t2.id=r.ecmtransaction_b_id
WHERE
t.parent_id='".$a->id."'
AND t.type='0'
AND t.register_date > '2011-12-31'
and t.settled=1
and t.payment_date<t2.payment_date
ORDER BY t.register_date desc");
}
$total_winien = 0;
$total_winien_settled=0;
$winien = array();
while ($r = $db->fetchByAssoc($res)) {
$tmp = array();
$tmp['settled_with'] = array();
$total_settled = 0;
$found=false;
$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' and deleted=0"));
if($t['id']!=""){
$tmp2 = array();
$tmp2['name'] = $t['name'];
$tmp2['trans_id'] = $t['id'];
$tmp['settled_with'][] = $tmp2;
$total_settled+=floatval($rr['value']);
$dateone= new DateTime(substr($rr['date_entered'],0,10));
$datetwo= new DateTime($date_to);
if($dateone==$datetwo){
$found=true;
}
}
}
if($_REQUEST['show_after_d']=='on' && $found==false){
continue;
}
//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['register_date'] = $timedate->to_display_date($r['payment_date']);
$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);
$tmp['unsettled'] = format_number($r['value'] - $total_settled);
$tmp['id'] = $r['id'];
$tmp['note']= $r['note'];
$tmp['note_id']= $r['note_id'];
$tmp['is_settled'] = $r['settled'];
$winien[] = $tmp;
//$total_winien+=floatval($r['value']);
$total_winien+=$r['value'] - $total_settled;
$total_winien_settled+=$total_settled;
}
$ss -> assign('WINIEN', $winien);
if ($_REQUEST['show_after_p']!='on'){
$res = $db->query("
SELECT * FROM ecmtransactions
WHERE deleted='0' AND
parent_id='".$a->id."'
AND type='1'
AND register_date > '2011-12-31'
".$show_settled."
ORDER BY register_date desc");
} else {
$ss->assign("SHOW_AFTER_P", "checked");
$res = $db->query("
SELECT t.* FROM ecmtransactions t
inner join ecmtransactions_rel r on r.ecmtransaction_b_id=t.id
inner join ecmtransactions t2 on
t2.id=r.ecmtransaction_a_id
WHERE
t.parent_id='".$a->id."'
AND t.type='1'
AND t.register_date > '2011-12-31'
and t.settled=1
and t.payment_date>t2.payment_date
ORDER BY t.register_date desc");
}
$ma = array();
$total_ma = 0;
$total_ma_settled = 0;
while ($r = $db->fetchByAssoc($res)) {
$tmp = array();
$tmp['settled_with'] = array();
$total_settled = 0;
$found=false;
$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' and deleted=0"));
if($t['id']!=""){
$tmp2 = array();
$tmp2['name'] = $t['name'];
$tmp2['trans_id'] = $t['id'];
$tmp['settled_with'][] = $tmp2;
$total_settled+=floatval($rr['value']);
$dateone= new DateTime(substr($rr['date_entered'],0,10));
$datetwo= new DateTime($date_to);
if($dateone==$datetwo){
$found=true;
}
}
}
if($_REQUEST['show_after_d']=='on' && $found==false){
continue;
}
$tmp = array();
//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['register_date'] = $timedate->to_display_date($r['payment_date']);
$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);
$tmp['unsettled'] = format_number($r['value']-$total_settled);
$tmp['is_settled'] = $r['settled'];
$tmp['id'] = $r['id'];
$ma[] = $tmp;
// $total_ma+=floatval($r['value']);
$total_ma+=$r['value']-$total_settled;
$total_ma_settled+=$total_settled;
}
$ss -> assign('MA', $ma);
// winien
$res = $db->query("
SELECT sum(value) as total FROM ecmtransactions
WHERE deleted='0' AND
parent_id='".$a->id."'
AND type='0'
AND register_date > '2011-12-31'
ORDER BY register_date desc");
$all_winien=$db->fetchByAssoc($res);
// ma
$res = $db->query("
SELECT sum(value) as total FROM ecmtransactions
WHERE deleted='0' AND
parent_id='".$a->id."'
AND type='1'
AND register_date > '2011-12-31'
ORDER BY register_date desc");
$all_ma=$db->fetchByAssoc($res);
$sum = array();
$sum['WINIEN'] = format_number($total_winien);
$sum['MA'] = format_number($total_ma);
$sum['MA_SETTLED'] = format_number($total_ma_settled);
$sum['WINIEN_SETTLED'] = format_number($total_winien_settled);
$sum['SALDO'] = format_number($all_ma['total']-$all_winien['total']);
$ss->assign('SUM', $sum);
$ss->assign('PROCESS', '1');
$ss->assign('date_to',$date_to);
$ss->assign('ACCOUNT', array('ID'=>$account_id, 'NAME'=>$a->name));
unset($a);
echo $ss->display ( 'modules/EcmPaymentStates/tpls/AccountPaymentStates.tpl' );