555 lines
18 KiB
PHP
Executable File
555 lines
18 KiB
PHP
Executable File
<?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
|
|
$umowa_id=$_REQUEST['agreement'];
|
|
|
|
$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;
|
|
|
|
$query_w='';
|
|
$query_ma='';
|
|
$label='Wszystkie';
|
|
if($_REQUEST['switch_show']=='')$_REQUEST['switch_show']=3;
|
|
switch ($_REQUEST['switch_show']){
|
|
case 1:
|
|
$query_w="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='0'
|
|
ORDER BY payment_date desc";
|
|
$query_ma="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='1'
|
|
ORDER BY payment_date desc";
|
|
$label='Wszystkie';
|
|
break;
|
|
case 2:
|
|
$query_w="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='0' AND (SELECT count(*) FROM ecmtransactions_rel WHERE ecmtransaction_a_id=ecmtransactions.id OR ecmtransaction_b_id=ecmtransactions.id)>0
|
|
ORDER BY payment_date desc";
|
|
$query_ma="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='1' AND (SELECT count(*) FROM ecmtransactions_rel WHERE ecmtransaction_a_id=ecmtransactions.id OR ecmtransaction_b_id=ecmtransactions.id)>0
|
|
ORDER BY payment_date desc";
|
|
$label='Rozliczone';
|
|
break;
|
|
case 3:
|
|
$query_w="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='0' AND (settled='0' or settled=null)
|
|
ORDER BY payment_date desc";
|
|
$query_ma="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='1' AND (settled='0' or settled=null)
|
|
ORDER BY payment_date desc";
|
|
$label='Nierozliczone';
|
|
break;
|
|
case 4:
|
|
$query_w="
|
|
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.settled=1
|
|
and t.payment_date < t2.payment_date
|
|
ORDER BY t.payment_date desc";
|
|
$query_ma="
|
|
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.settled=1
|
|
and t.payment_date>t2.payment_date group by t.id
|
|
ORDER BY t.payment_date desc";
|
|
$label='Rozliczone po terminie';
|
|
break;
|
|
case 5:
|
|
$query_w="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='0'
|
|
AND (settled='0' OR settled IS NULL) and payment_date < '".date("Y-m-d")."'
|
|
ORDER BY payment_date desc";
|
|
$query_ma="
|
|
SELECT * FROM ecmtransactions
|
|
WHERE deleted='0' AND
|
|
parent_id='".$a->id."'
|
|
AND type='1'
|
|
AND (settled='0' OR settled IS NULL) and payment_date < '".date("Y-m-d")."'
|
|
ORDER BY payment_date desc";
|
|
$label='Nierozliczone po terminie';
|
|
break;
|
|
}
|
|
|
|
$res = $db->query($query_w);
|
|
$total_winien = 0;
|
|
|
|
$winien = array();
|
|
$totals_w['wartosc']=0;
|
|
$totals_w['roz']=0;
|
|
$totals_w['nie']=0;
|
|
while ($r = $db->fetchByAssoc($res)) {
|
|
|
|
$tmp = array();
|
|
$tmp['settled_with'] = array();
|
|
$total_settled = 0;
|
|
if($umowa_id!=''){
|
|
if($r['record_id']!=$umowa_id){
|
|
if($r['record_type']=='EcmInvoiceOuts'){
|
|
$m= new EcmInvoiceOut();
|
|
$m->retrieve($r['record_id']);
|
|
if($m->ecmagreement_id!=$umowa_id){
|
|
continue;
|
|
}
|
|
} else {
|
|
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$r['id']."' OR ecmtransaction_b_id='".$r['id']."'");
|
|
$skip=true;
|
|
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['record_type']=='EcmInvoiceOuts'){
|
|
$m= new EcmInvoiceOut();
|
|
$m->retrieve($t['record_id']);
|
|
if($m->ecmagreement_id==$umowa_id){
|
|
$skip==false;
|
|
}
|
|
} else {
|
|
|
|
if($t['record_id']==$umowa_id){
|
|
$skip=false;
|
|
}
|
|
}
|
|
}
|
|
if($skip==true){
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$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 {
|
|
$rr['value']=abs($rr['value']);
|
|
}
|
|
} 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') {
|
|
if($total_settled==0){
|
|
$total_settled=$r['value'];
|
|
}
|
|
} else $r['settled'] = '0'; //prevent null
|
|
|
|
|
|
$tmp['register_date'] = $timedate->to_display_date($r['payment_date']);
|
|
if($r['record_type']!=''){
|
|
$module=substr($r['record_type'],0,strlen($r['record_type'])-1);
|
|
$m = new $module();
|
|
|
|
$m->retrieve($r['record_id']);
|
|
|
|
$add=' z dnia '.date('d.m.Y',strtotime($m->register_date));
|
|
} else {
|
|
$add='';
|
|
}
|
|
$tmp['document_no'] = '<a href="index.php?module=EcmTransactions&action=DetailView&record='.$r['id'].'" traget="new">'.$r['name'].' '.$add.'</a>';
|
|
$tmp['total'] = format_number($r['value']);
|
|
$tmp['settled'] = format_number($total_settled);
|
|
$totals_w['wartosc']+=$r['value'];
|
|
$totals_w['roz']+=$total_settled;
|
|
$totals_w['nie']+=floatval(unsettledValue($total_settled,$r['value']));
|
|
if($r['settled']==1){
|
|
if($_REQUEST['switch_show']==1){
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_winien+=floatval($r['value']);
|
|
|
|
} else {
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_winien+=floatval(unsettledValue($total_settled,$r['value']));
|
|
}
|
|
}else{
|
|
if($_REQUEST['switch_show']==1){
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_winien+=floatval($r['value']);
|
|
|
|
} else {
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_winien+=floatval(unsettledValue($total_settled,$r['value']));
|
|
}
|
|
}
|
|
|
|
$tmp['id'] = $r['id'];
|
|
$tmp['sort_date']=date('Ymd',strtotime($r['payment_date']));
|
|
$tmp['note']= $r['note'];
|
|
$tmp['type']='0';
|
|
$tmp['note_id']= $r['note_id'];
|
|
$tmp['is_settled'] = $r['settled'];
|
|
$winien[] = $tmp;
|
|
|
|
|
|
}
|
|
$totals_m['wartosc']=0;
|
|
$totals_m['roz']=0;
|
|
$totals_m['nie']=0;
|
|
$ss -> assign('WINIEN', $winien);
|
|
$res = $db->query($query_ma);
|
|
$ma = array();
|
|
$total_ma = 0;
|
|
while ($r = $db->fetchByAssoc($res)) {
|
|
$tmp = array();
|
|
$tmp['settled_with'] = array();
|
|
$total_settled = 0;
|
|
if($umowa_id!=''){
|
|
if($r['record_id']!=$umowa_id){
|
|
if($r['record_type']=='EcmInvoiceOuts'){
|
|
$m= new EcmInvoiceOut();
|
|
$m->retrieve($r['record_id']);
|
|
if($m->ecmagreement_id!=$umowa_id){
|
|
continue;
|
|
}
|
|
} else {
|
|
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$r['id']."' OR ecmtransaction_b_id='".$r['id']."'");
|
|
$skip=true;
|
|
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['record_type']=='EcmInvoiceOuts'){
|
|
$m= new EcmInvoiceOut();
|
|
$m->retrieve($t['record_id']);
|
|
if($m->ecmagreement_id==$umowa_id){
|
|
$skip==false;
|
|
}
|
|
} else {
|
|
|
|
if($t['record_id']==$umowa_id){
|
|
$skip=false;
|
|
}
|
|
}
|
|
}
|
|
if($skip==true){
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$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 {
|
|
$rr['value']=abs($rr['value']);
|
|
}
|
|
$tmp['settled_with'][] = $tmp2;
|
|
$total_settled+=floatval($rr['value']);
|
|
}
|
|
|
|
|
|
//date comparsion
|
|
if ($r['settled'] == '1') {
|
|
if($total_settled==0){
|
|
$total_settled=$r['value'];
|
|
}
|
|
} else $r['settled'] = '0'; //prevent null
|
|
|
|
$tmp['register_date'] = $timedate->to_display_date($r['payment_date']);
|
|
if($r['record_type']!=''){
|
|
$module=substr($r['record_type'],0,strlen($r['record_type'])-1);
|
|
$m = new $module();
|
|
|
|
$m->retrieve($r['record_id']);
|
|
if($m->register_date!=""){
|
|
$add=' z dnia '.date('d.m.Y',strtotime($m->register_date));
|
|
} else {
|
|
if($m->document_date!=""){
|
|
$add=' z dnia '.date('d.m.Y',strtotime($m->document_date));
|
|
}
|
|
}
|
|
|
|
} else {
|
|
$add='';
|
|
}
|
|
$tmp['sort_date']=date('Ymd',strtotime($r['payment_date']));
|
|
$tmp['name'] = '<a href="index.php?module=EcmTransactions&action=DetailView&record='.$r['id'].'" traget="new">'.$r['name'].' '.$add.'</a>';
|
|
$tmp['total'] = format_number($r['value']);
|
|
$tmp['settled'] = format_number($total_settled);
|
|
$totals_m['wartosc']+=$r['value'];
|
|
$totals_m['roz']+=$total_settled;
|
|
$totals_m['nie']+=floatval(unsettledValue($total_settled,$r['value']));
|
|
if($r['settled']==1){
|
|
if($_REQUEST['switch_show']==1){
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
|
|
$total_ma+=floatval($r['value']);
|
|
} else {
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_ma+=floatval(unsettledValue($total_settled,$r['value']));
|
|
}
|
|
//$tmp['unsettled'] = format_number(abs($r['value'])-$total_settled); echo 'rozliczone';
|
|
}else{
|
|
if($_REQUEST['switch_show']==1){
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
|
|
$total_ma+=floatval($r['value']);
|
|
} else {
|
|
$tmp['unsettled'] = unsettledFormatValue($total_settled,$r['value']);
|
|
$total_ma+=floatval(unsettledValue($total_settled,$r['value']));
|
|
}
|
|
}
|
|
$tmp['is_settled'] = $r['settled'];
|
|
$tmp['type']='1';
|
|
$tmp['id'] = $r['id'];
|
|
$ma[] = $tmp;
|
|
|
|
//$total_ma+=floatval($r['value']);
|
|
}
|
|
$sea = new SugarEmailAddress;
|
|
// Grab the array of addresses
|
|
|
|
|
|
$emails = $sea->getAddressesByGUID( $account_id, 'Accounts');
|
|
$ss -> assign('MA', $ma);
|
|
|
|
$sum = array();
|
|
$sum['WINIEN'] = format_number($total_winien);
|
|
$sum['MA'] = format_number($total_ma);
|
|
$sum['SALDO'] = format_number($total_ma-$total_winien);
|
|
$ss->assign('SUM', $sum);
|
|
//$ss->assign('EMAILLIST',getEmails($account_id));
|
|
$ss->assign('umowy',getAgreements($account_id));
|
|
$ss->assign('umowa_sel',$umowa_id);
|
|
$ss->assign('SWITCH_SHOW', $_REQUEST['switch_show']);
|
|
$ss->assign('PROCESS', '1');
|
|
$c = new EcmSysInfo();
|
|
$ss->assign("number",$a->phone_office);
|
|
$ss->assign('CURRENT_COMPANY', $c->getShortName());
|
|
$ss->assign('ACCOUNT', array('ID'=>$account_id, 'NAME'=>$a->name));
|
|
$ss->assign('EMAILLIST',$emails);
|
|
$ss->assign("system_name",$c->getShortName());
|
|
|
|
if( $_REQUEST['toPDF'] == '1' ) {
|
|
$info = new EcmSysInfo();
|
|
$all_transactions=array_merge($ma,$winien);
|
|
$all_transactions=array_sort_by_key($all_transactions,'sort_date',SORT_ASC);
|
|
$ss->assign('transactions',$all_transactions);
|
|
$ss->assign('totals_w',$totals_w);
|
|
$ss->assign('totals_m',$totals_m);
|
|
$ss->assign('saldo',$totals_m['wartosc']-$totals_w['wartosc']);
|
|
|
|
|
|
$output = $ss->fetch( 'modules/EcmPaymentStates/tpls/PDF/AccountPaymentStatespdf.tpl' );
|
|
include_once ("include/MPDF60/mpdf60/mpdf.php");
|
|
unset($smarty);
|
|
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 25, 10, 5, 5 );
|
|
$p->setFooter('Strona {PAGENO} z {nbpg}');
|
|
|
|
$p->SetHTMLHeader('<p style="text-align:left;font-size: 10px;">'.$info->getName().'<br>
|
|
Kontrahent: '.$a->name.'<br>Rozrachunki: '.$label.'<br>Data wydruku: '.date("d.m.Y").'<br>Strona {PAGENO} z {nbpg}</p>');
|
|
//$p->setTitle($mod_strings["LBL_REPORT_STOCKS_DOCS"]);
|
|
//echo $output;
|
|
$p->writeHTML( $output );
|
|
|
|
$p->Output ();
|
|
|
|
} else {
|
|
echo $ss->display ( 'modules/EcmPaymentStates/tpls/AccountPaymentStates.tpl' );
|
|
}
|
|
unset($a);
|
|
function array_sort_by_key($array, $on, $order=SORT_ASC)
|
|
{
|
|
$new_array = array();
|
|
$sortable_array = array();
|
|
|
|
if (count($array) > 0) {
|
|
foreach ($array as $k => $v) {
|
|
if (is_array($v)) {
|
|
foreach ($v as $k2 => $v2) {
|
|
if ($k2 == $on) {
|
|
$sortable_array[$k] = $v2;
|
|
}
|
|
}
|
|
} else {
|
|
$sortable_array[$k] = $v;
|
|
}
|
|
}
|
|
|
|
switch ($order) {
|
|
case SORT_ASC:
|
|
asort($sortable_array);
|
|
break;
|
|
case SORT_DESC:
|
|
arsort($sortable_array);
|
|
break;
|
|
}
|
|
|
|
foreach ($sortable_array as $k => $v) {
|
|
$new_array[$k] = $array[$k];
|
|
}
|
|
}
|
|
|
|
return $new_array;
|
|
}
|
|
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);
|
|
}
|
|
if($settled==0 && $val<0){
|
|
return format_number($val);
|
|
}
|
|
if($settled==0 && $val>0){
|
|
return format_number($val);
|
|
}
|
|
}
|
|
|
|
function getAgreements($id){
|
|
$db=$GLOBALS['db'];
|
|
$zap="select id,document_no from ecmagreements where parent_id='".$id."' and deleted=0";
|
|
$res=$db->query($zap);
|
|
|
|
$as=array();
|
|
while($dane=$db->fetchByAssoc($res)){
|
|
|
|
$as[]=$dane;
|
|
}
|
|
return $as;
|
|
}
|
|
|
|
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);
|
|
}
|
|
return $val;
|
|
}
|
|
function getEmails ($id)
|
|
{
|
|
$sea = new SugarEmailAddress();
|
|
$addresses = $sea->getAddressesByGUID($id, 'Accounts');
|
|
$return = array();
|
|
foreach ($addresses as $address) {
|
|
if ($address['email_address'] != '' && $address['opt_out'] == 1) {
|
|
$return[] = $address['email_address'];
|
|
}
|
|
}
|
|
return $return;
|
|
} |