Files
crm.twinpol.com/modules/EcmReports/ReportBuyesByVat.php
2025-05-12 15:44:39 +00:00

306 lines
11 KiB
PHP

<?php
require_once ('include/utils.php');
$data = array();
$date_from = date("Y-M-D");
$date_to = date("Y-m-d");
$tmp3 = array();
$vat_id;
// Ustawienia daty
if (!$_GET ['date_from']) {
$date_from = date("Y-M-01");
} else {
$date_from = $_GET ['date_from'];
}
if (!$_GET ['date_to']) {
$date_to = date("Y-m-d");
} else {
$date_to = $_GET ['date_to'];
}
// formatowanie daty
$Calendar_daFormat = str_replace("d", "%d", str_replace("m", "%m", str_replace("Y", "%Y", $GLOBALS ['timedate']->get_date_format())));
$date_from = $GLOBALS ['timedate']->to_display_date($date_from);
$date_to = $GLOBALS ['timedate']->to_display_date($date_to);
$date = new DateTime ();
$current_date_formatted = $date->format('d.m.Y');
$category_list = $GLOBALS['app_list_strings'] ['document_category_dom'];
$category= array();
if (isset ( $_GET ['category'] )) {
foreach ( $_GET ['category'] as $selectedOption ) {
array_push($category, $selectedOption);
}
}
global $app_list_strings;
global $mod_strings;
$db = $GLOBALS ['db'];
$subquery = "SELECT id
FROM documents
WHERE deleted= '0'
AND document_date >= STR_TO_DATE('" . $date_from . "','%d.%m.%Y')
AND document_date <= STR_TO_DATE('" . $date_to . "','%d.%m.%Y') ";
$query = " SELECT
acc.id accid,
dcn.id document_id,
dcn.parent_id,
dcn.document_name as document_number,
dcn.document_date,
dcn.category_id,
dcn.active_date,
dcn.document_date as register_date,
dcn.currency_value,
dcn.currency_id,
acc.name,
acc.register_address_city,
acc.register_address_postalcode,
acc.register_address_street,
acc.to_vatid
FROM
documents_accounts da
LEFT JOIN
documents dcn ON da.document_id = dcn.id
LEFT JOIN
accounts acc ON da.parent_id = acc.id
WHERE
da.parent_type = 'Account'
AND da.deleted = '0' ";
$ilscZaznaczonychKategorii = count($category);
if($ilscZaznaczonychKategorii>=1){
$query.= " AND dcn.category_id IN (";
foreach ($category as $key => $val){
$query.= "'" . $val . "',";
}
$query = rtrim($query,',');
$query.=") ";
}
$query .= " AND dcn.id IN (SELECT
id
FROM
documents
WHERE
deleted = '0'
AND dcn.document_date IS NOT NULL
AND dcn.id IN (" . $subquery . "))
ORDER BY dcn.document_name";
$sumabrutto = 0;
$rows = $db->query($query);
while ($r = $db->fetchByAssoc($rows)) {
$row = array();
$queryvat = 'SELECT
id,
document_id,
vat_value,
vat_id,
netto,
vat
FROM
documents_vat where document_id = "' . $r ['document_id'] . '"
AND deleted = "0" AND netto !="0"
ORDER BY position';
$rowsv = $db->query($queryvat);
$row ['vaty'] = array();
$row ['vatsumapozycja'] = 0;
while ($rr = $db->fetchByAssoc($rowsv)) {
$tmp2 = array();
$tmp2 ['vat_value'] = $rr ['vat_value'];
$tmp2 ['vat_id'] = $rr ['vat_id'];
$tmp2 ['netto'] =($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$tmp2 ['vat'] = ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
$tmp2 ['sumabrutto'] += ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat'])+ ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$row ['vatsumapozycja'] += ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
$row ['value'] += ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto'])+ ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
$row ['vaty'] [] = $tmp2;
if (isset($tmp3 [$rr ['vat_value']])) {
$tmp3 [$rr ['vat_value']] ['sumanetto'] += ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$tmp3 [$rr ['vat_value']] ['sumavat'] += ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
$sumanetto += ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$sumavat += ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
// $sumabrutto += $sumanetto + $sumavat;
} else {
$tmp3 [$rr ['vat_value']] = array();
$tmp3 [$rr ['vat_value']] ['sumanetto'] = ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$tmp3 [$rr ['vat_value']] ['sumavat'] = ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
$sumanetto += ($r['currency_id']!="PLN" ? $rr ['netto']* $r['currency_value'] : $rr ['netto']);
$sumavat += ($r['currency_id']!="PLN" ? $rr ['vat']* $r['currency_value'] : $rr ['vat']);
//$sumabrutto += $sumanetto + $sumavat;
}
$porownanie = ($tmp3[$rr['vat_value']]);
$tablica_z_podzielonymi_wartosciami_procentow = str_split($rr['vat_value']);
$suma_czarow=0;
foreach($tablica_z_podzielonymi_wartosciami_procentow as $key=>$t)
{ $suma_czarow += ord($t);}
$tab=(ord($tablica_z_podzielonymi_wartosciami_procentow));
}
$row ['document_id'] = $r ['document_id'];
$row ['id'] = $r ['accid'];
$row ['document_number'] = $r ['document_number'];
$row ['name'] = $r ['name'];
$row ['register_address_postalcode'] = $r ['register_address_postalcode'];
$row ['register_address_city'] = $r ['register_address_city'];
$row ['register_address_street'] = $r ['register_address_street'];
$row ['document_date'] = $r ['document_date'];
$row ['category_id'] = $r ['category_id'];
$row ['active_date'] = $r ['active_date'];
$row ['to_vatid'] = $r ['to_vatid'];
$row ['register_date'] = $r ['register_date'];
$sumabrutto=$sumabrutto+$row ['value'];
$data [] = $row;
$sumvat += $row ['vatsumapozycja'];
}
foreach ($tmp3 as $key => $value) {
if ($value ['sumavat'] == 0) {
$tmp3 [$key] ['sumavat'] = NULL;
}
}
$smarty = new Sugar_Smarty ();
$smarty->assign('MOD', $mod_strings);
$smarty->assign('date_from', $date_from);
$smarty->assign('date_to', $date_to);
$smarty->assign('dateFormat', $Calendar_daFormat);
$smarty->assign('netto', $dvnetto);
$smarty->assign('wartosc', $wartosc);
$smarty->assign('vat', $dvvat);
$smarty->assign('to_vatid', $to_vatid);
$smarty->assign('document_number', $document_number);
$smarty->assign('name', $name);
$smarty->assign('SumaNetto', $sumanetto);
$smarty->assign('SumaBrutto', $sumabrutto);
$smarty->assign('SumaVat', $sumvat);
$smarty->assign('vat', $vat);
$smarty->assign('vat_value', $tmp3);
$smarty->assign('value', $value);
$smarty->assign('category', $category_list);
$smarty->assign('category_selected', $category);
$smarty->assign('current_date_formatted', $current_date_formatted);
if ($_GET ['to_pdf'] == '1') {
$dataPart = NULL;
$dataPart = array_chunk($data, 25);
$pageVatSum = array();
$pageBruttoVat = array();
$partialVatSum = array();
$partialBruttoVat = array();
$tmpVatSum = array();
$tmpBruttoVat = array();
foreach ($tmp3 as $vatkey => $costampotrzebnego) {
$tmpVatSum [$vatkey] = array();
$tmpVatSum [$vatkey] ['netto'] = 0;
if ($costampotrzebnego['sumavat'] == null) {
$tmpVatSum [$vatkey] ['vat'] = 'null';
} else {
$tmpVatSum [$vatkey] ['vat'] = 0;
}
}
$tmpBruttoVat ['brutto'] = 0;
$tmpBruttoVat ['vat'] = 0;
foreach ($dataPart as $key => $value123) {
$pageVatSum [$key] = array();
$pageBruttoVat [$key] = array();
foreach ($tmp3 as $vatkey => $costampotrzebnego) {
$pageVatSum [$key] [$vatkey] = array();
$partialVatSum [$key] [$vatkey] = array();
$pageVatSum [$key] [$vatkey] ['netto'] = 0;
$partialVatSum [$key] [$vatkey] ['netto'] = 0;
if ($costampotrzebnego['sumavat'] == null) {
$pageVatSum [$key] [$vatkey] ['vat'] = 'null';
$partialVatSum [$key] [$vatkey] ['vat'] = 'null';
} else {
$pageVatSum [$key] [$vatkey] ['vat'] = 0;
$partialVatSum[$key] [$vatkey] ['vat'] = 0;
}
}
$pageBruttoVat [$key]['brutto'] = 0;
$pageBruttoVat [$key]['vat'] = 0;
foreach ($value123 as $key2 => $val123) {
foreach ($val123 ['vaty'] as $k => $vat123) {
$pageVatSum [$key] [$vat123 ['vat_value']] ['netto'] += $vat123 ['netto'];
$tmpVatSum [$vat123 ['vat_value']] ['netto'] += $vat123 ['netto'];
$kolejka = array_unshift ($kolejka, $vat123['netto']);
$kolejka1= array_unshift ($kolejka1, $vat123['vat']);
if ($vat123 ['vat'] != 0) {
$pageVatSum [$key] [$vat123 ['vat_value']] ['vat'] += $vat123 ['vat'];
$tmpVatSum [$vat123 ['vat_value']] ['vat'] += $vat123 ['vat'];
}
$pageBruttoVat [$key]['brutto'] += $vat123 ['netto'] + $vat123 ['vat'];
$pageBruttoVat [$key]['vat'] += $vat123 ['vat'];
$tmpBruttoVat ['brutto'] += $vat123 ['netto'] + $vat123 ['vat'];
$tmpBruttoVat ['vat'] += $vat123 ['vat'];
}
}
foreach ($tmpVatSum as $vatKey => $vatValue) {
$partialVatSum [$key] [$vatKey] ['netto'] += $vatValue ['netto'];
if ($vatValue['vat'] != 'null') {
$partialVatSum [$key] [$vatKey] ['vat'] += $vatValue ['vat'];
}
}
$partialBruttoVat [$key] ['brutto'] = $tmpBruttoVat ['brutto'];
$partialBruttoVat [$key] ['vat'] = $tmpBruttoVat ['vat'];
}
if(count($category)==0 || count($category) == count($category_list)){
$listaKategorii = 'WSZYSTKIE KATEGORIE';
}else{
$listaKategorii ='';
foreach($category as $key=>$value){
$listaKategorii .= $category_list[$value] . ', ';
}
$listaKategorii = rtrim(trim($listaKategorii),',');
}
$smarty->assign('listaKategorii', $listaKategorii);
$smarty->assign('pageBruttoVat', $pageBruttoVat);
$smarty->assign('pageVatSum', $pageVatSum);
$smarty->assign('partialBruttoVat', $partialBruttoVat);
$smarty->assign('partialVatSum', $partialVatSum);
$smarty->assign('dataPart', $dataPart);
$smarty -> assign('category_id', $app_list_strings['document_category_dom']);
$EcmSysInfo = new EcmSysInfo();
$smarty->assign("EcmSysInfo", $EcmSysInfo);
$smarty->assign('company_name',$EcmSysInfo);
$output = $smarty->fetch ( 'modules/EcmReports/tpls/PDF/ReportBuyesByVat.tpl' );
include_once ("include/MPDF57/mpdf.php");
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 35, 10, 5, 5 );
$p->setTitle ( $mod_strings ["LBL_REPORT_RECEIVE_REGISTER"] );
$p->setFooter ( '{PAGENO}' );
$p->writeHTML ( $output );
$p->Output ( 'RaportRejestrPrzyjecia.pdf', 'I' );
} else {
$smarty->assign('data', $data);
echo $smarty->display('modules/EcmReports/tpls/ReportBuyesByVat.tpl');
}
?>