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

999 lines
28 KiB
PHP

<?php
//ini_set('display_errors', 1);
// ini_set('error_reporting', E_ALL);
// ini_set('display_startup_errors', 1);
global $mod_strings, $app_list_strings, $db;
$db = $GLOBALS['db'];
// Filters for query
// Array with months
$array_months = [
1 => 'Styczeń',
2 => 'Luty',
3 => 'Marzec',
4 => 'Kwiecień',
5 => 'Maj',
6 => 'Czerwiec',
7 => 'Lipec',
8 => 'Sierpień',
9 => 'Wrzesień',
10 => 'Październik',
11 => 'Listopad',
12 => 'Grudzizeń'
];
$whereFilter = "";
$whereFilterBranch2 = "";
$otherGroupFilter = "";
$relationsQuery = " FROM `ecminvoiceouts`
INNER JOIN `accounts` ON
(accounts.id = ecminvoiceouts.parent_id)
INNER JOIN `ecminvoiceoutitems` ON
(ecminvoiceoutitems.ecminvoiceout_id = ecminvoiceouts.id)
INNER JOIN `ecmproducts` ON
(ecmproducts.id = ecminvoiceoutitems.ecmproduct_id)
" /*. $relationsQueryReceipts*/ . "
";
$relationsQueryRec = " FROM `ecmreceipts`
INNER JOIN `accounts` ON
(accounts.id = ecmreceipts.parent_id)
INNER JOIN `ecmreceiptitems` ON
(ecmreceiptitems.ecmreceipt_id = ecmreceipts.id)
INNER JOIN `ecmproducts` ON
(ecmproducts.id = ecmreceiptitems.ecmproduct_id)
" /*. $relationsQueryReceipts*/ . "
";
$dataInvoice = EcmInvoiceOut::getInvoiceList();
$dataRecipe = EcmReceipt::getReceiptsList();
print_r($dataInvoice);
function get_items_fk( $request,$db,$whereFilter,$whereFilterBranch2,$otherGroupFilter,$relationsQuery){
// Years and months
$years_group = isset($request['years_group']) ? $request['years_group'] : [0];
$months_group = isset($request['months_group']) ?$request['months_group'] : [0];
$document_type=isset($request['document_sales_type']) ? $request['document_sales_type'] : ['invoice','invoice_correct','recipe','recipe_correct'];
if((count($years_group) > 0) AND (count($months_group) > 0)){
$_months_group = strlen($months_group[0]) == 1 ? '0'.$months_group[0] : $months_group[0];
$value = $years_group[0] . '-' . $_months_group;
$whereFilter .= ' AND ecminvoiceouts.register_date >= "' . $value . '" ';
}
//print_r($document_type);
// Types
$types_group = isset($request['types_group']) ? $request['types_group'] : [0];
if(count($types_group) > 0){
if(in_array("",$types_group)){
$index = array_search("",$types_group);
unset($types_group[$index]);
}
if(count($types_group) > 0){
$value = array_values ( $types_group );
//$whereFilter .= ' AND accounts.account_type IN ("' . implode('","',$value) . '") ';
}
}
// Group_ks/*
$product_group = isset($request['product_group']) ?$request['product_group'] : [0];
if(count($product_group) > 0){
if(in_array("",$product_group)){
$index = array_search("",$product_group);
unset($product_group[$index]);
}
if(count($product_group) > 0){
$value = array_values ( $product_group );
$whereFilterBranch2 = $whereFilter;
$otherGroupFilter .= $whereFilter;
//$whereFilter .= ' AND ecmproducts.ks_group IN ("' . implode('","',$value) . '") ';
//$whereFilterBranch2 .= ' AND ecmproducts.ks_group NOT IN ("' . implode('","',$value) . '")';
$otherGroupFilter .= $whereFilterBranch2;
}
}
// Other_group_ks
$other_group_ks_filter = isset($request['product_other_group']) ? $request['product_other_group'] : [0];
if(count($other_group_ks_filter) > 0){
if(in_array("",$other_group_ks_filter)){
$index = array_search("",$other_group_ks_filter);
unset($other_group_ks_filter[$index]);
}
if(count($other_group_ks_filter) > 0){
$value = array_values ( $other_group_ks_filter );
$whereFilterBranch2 .= ' AND ecmproducts.ks_group IN ("' . implode('","',$value) . '") ';
}
}
// Shop
$stocks = isset($request['stocks']) ? $request['stocks'] : [0];
if(count($stocks)>0){
if(in_array("",$stocks)){
$indeks = array_search("",$stocks);
unset($stocks[$indeks]);
}
if(count($stocks)>0){
$stocksList = array();
$value = array_values ( $stocks );
$whereFilter .= ' AND ecminvoiceouts.stock_id IN ("' . implode('","',$value) . '")';
$whereFilterBranch2 .= $whereFilter;
$otherGroupFilter .= $whereFilter;
}
}
// Added to query check deleted and canceled fields
if(in_array('invoice',$document_type)){
$document_types[]='normal';
}
if(in_array('invoice_correct',$document_type)){
$document_types[]='correct';
}
$whereFilter .= "
AND ecminvoiceouts.deleted=0
AND ecminvoiceouts.canceled=0
AND ecminvoiceouts.type in ('".implode("','",$document_types)."')
";
$relationsQueryReceipts = "
";
$total_netto_request = "
ROUND(SUM(
CASE
WHEN ecminvoiceouts.type = 'normal' THEN
ecminvoiceoutitems.total_netto *
CASE WHEN ecminvoiceouts.currency_value_nbp != '' THEN
ecminvoiceouts.currency_value_nbp
ELSE 1
END
ELSE
ecminvoiceoutitems.total_netto_corrected *
CASE WHEN ecminvoiceouts.currency_value_nbp != '' THEN
ecminvoiceouts.currency_value_nbp
ELSE 1
END
END
),2)
";
$pre_table_request = "
SELECT
COUNT(DISTINCT accounts.id) AS `accounts_count2`,
COUNT(ecminvoiceouts.id) AS `ecminvoiceouts_count2`,
MONTH(ecminvoiceouts.register_date) AS `ecminvoiceouts_month2`,
YEAR(ecminvoiceouts.register_date) AS `ecminvoiceouts_year2`,
accounts.account_type AS `accounts_type2`
" . $relationsQuery . "
" . $whereFilter . "
GROUP BY `ecminvoiceouts_year2`, `ecminvoiceouts_month2`, `accounts_type2`
-- ORDER BY `ecminvoiceouts_year2`, `ecminvoiceouts_month2`, `accounts_type2`
";
$pre_table_request_other_groups = "
SELECT
accounts.account_type AS `accounts_type3`,
ecmproducts.ks_group AS `ecmproducts_group_ks3`,
MONTH(ecminvoiceouts.register_date) AS `ecminvoiceouts_month3`,
YEAR(ecminvoiceouts.register_date) AS `ecminvoiceouts_year3`,
" . $total_netto_request . " AS `ecminvoiceouts_total_brutto3`
" . $relationsQuery . "
" . $whereFilterBranch2 . "
";
$pre_table_result_other_groups = $db->query($pre_table_request_other_groups);
while($row = $db->fetchByAssoc($pre_table_result_other_groups)) { $pre_table_result_other_groups_array[] = $row; }
//print_r($pre_table_result_other_groups_array);
// Generate table data
$table_request = "
SELECT
counter.`accounts_count2`,
counter.`ecminvoiceouts_count2`,
accounts.account_type AS `accounts_type`,
ecmproducts.ks_group AS `ecmproducts_group_ks`,
MONTH(ecminvoiceouts.register_date) AS `ecminvoiceouts_month`,
YEAR(ecminvoiceouts.register_date) AS `ecminvoiceouts_year`,
" . $total_netto_request . " AS `ecminvoiceouts_total_brutto`
" . $relationsQuery . "
" . $whereFilter . "
INNER JOIN (" . $pre_table_request . ") AS counter
ON (counter.ecminvoiceouts_year2 = YEAR(ecminvoiceouts.register_date))
AND (counter.ecminvoiceouts_month2 = MONTH(ecminvoiceouts.register_date))
AND (counter.accounts_type2 = accounts.account_type)
GROUP BY `ecminvoiceouts_year`, `ecminvoiceouts_month`, `accounts_type`, `ecmproducts_group_ks`
-- ORDER BY `ecminvoiceouts_year`, `ecminvoiceouts_month`, `accounts_type`, `ecmproducts_group_ks`
";
//print_r('<pre>');
//print_r($table_request);
$table_result = $db->query($table_request);
while($row = $db->fetchByAssoc($table_result)) { $table_result_array[] = $row; }
// Initial variable for generate array
// If table_result is not empty
if(!empty($table_result_array)):
// Genarate main array
foreach ($table_result_array as $table_key => $table) {
if(empty($pre_table_result_other_groups_array)) { $pre_table_result_other_groups_array = []; }
foreach ($pre_table_result_other_groups_array as $pre_table_key => $pre_table) {
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['accounts_count'] = $table['accounts_count2'];
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['documents_count'] = $table['ecminvoiceouts_count2'];
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['accounts_type']
[$table['ecmproducts_group_ks']] = $table['ecminvoiceouts_total_brutto'];
if(!empty($pre_table_result_other_groups_array)) {
if(
$table['ecminvoiceouts_year'] == $pre_table['ecminvoiceouts_year3'] &&
$table['ecminvoiceouts_month'] == $pre_table['ecminvoiceouts_month3'] &&
$table['accounts_type'] == $pre_table['accounts_type3']
) {
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['together'] = $pre_table['ecminvoiceouts_total_brutto3'];
}
}
}
}
endif;
if (isset($generate_table_array) && count($generate_table_array)>0){
return $generate_table_array;
}else{
return [];
}
}
//koniec fk
// poczatek paragony
function get_items_rec( $request,$db,$whereFilter,$whereFilterBranch2,$otherGroupFilter,$relationsQuery){
// Years and months
$years_group = isset($request['years_group']) ? $request['years_group'] : [0];
$months_group = isset($request['months_group']) ?$request['months_group'] : [0];
$document_type=isset($request['document_sales_type']) ? $request['document_sales_type'] : [0];
if((count($years_group) > 0) AND (count($months_group) > 0)){
$_months_group = strlen($months_group[0]) == 1 ? '0'.$months_group[0] : $months_group[0];
$value = $years_group[0] . '-' . $_months_group;
$whereFilter .= ' AND ecmreceipts.register_date >= "' . $value . '" ';
}
//print_r($document_type);
// Types
$types_group = isset($request['types_group']) ? $request['types_group'] : [0];
if(count($types_group) > 0){
if(in_array("",$types_group)){
$index = array_search("",$types_group);
unset($types_group[$index]);
}
if(count($types_group) > 0){
$value = array_values ( $types_group );
//$whereFilter .= ' AND accounts.account_type IN ("' . implode('","',$value) . '") ';
}
}
// Group_ks
$product_group = isset($request['product_group']) ?$request['product_group'] : [0];
if(count($product_group) > 0){
if(in_array("",$product_group)){
$index = array_search("",$product_group);
unset($product_group[$index]);
}
if(count($product_group) > 0){
$value = array_values ( $product_group );
$whereFilterBranch2 = $whereFilter;
$otherGroupFilter .= $whereFilter;
//$whereFilter .= ' AND ecmproducts.ks_group IN ("' . implode('","',$value) . '") ';
//$whereFilterBranch2 .= ' AND ecmproducts.ks_group NOT IN ("' . implode('","',$value) . '")';
$otherGroupFilter .= $whereFilterBranch2;
}
}
// Other_group_ks
$other_group_ks_filter = isset($request['product_other_group']) ? $request['product_other_group'] : [0];
if(count($other_group_ks_filter) > 0){
if(in_array("",$other_group_ks_filter)){
$index = array_search("",$other_group_ks_filter);
unset($other_group_ks_filter[$index]);
}
if(count($other_group_ks_filter) > 0){
$value = array_values ( $other_group_ks_filter );
//$whereFilterBranch2 .= ' AND ecmproducts.ks_group IN ("' . implode('","',$value) . '") ';
}
}
// Shop
$stocks = isset($request['stocks']) ? $request['stocks'] : [0];
if(count($stocks)>0){
if(in_array("",$stocks)){
$indeks = array_search("",$stocks);
unset($stocks[$indeks]);
}
if(count($stocks)>0){
$stocksList = array();
$value = array_values ( $stocks );
$whereFilter .= ' AND ecmreceipts.stock_id IN ("' . implode('","',$value) . '")';
$whereFilterBranch2 .= $whereFilter;
$otherGroupFilter .= $whereFilter;
}
}
// Added to query check deleted and canceled fields
if(in_array('recipe',$document_type)){
$document_types[]='normal';
}
if(in_array('recipe_correct',$document_type)){
$document_types[]='correct';
}
$whereFilter .= "
AND ecmreceipts.deleted=0
AND ecmreceipts.canceled=0
AND ecmreceipts.type in ('".implode("','",$document_types)."')
";
$relationsQueryReceipts = "
";
$total_netto_request = "
ROUND(SUM(
CASE
WHEN ecmreceipts.type = 'normal' THEN
ecmreceiptitems.total_netto *
CASE WHEN 0 THEN
1
ELSE 1
END
ELSE
ecmreceiptitems.total_netto_corrected *
CASE WHEN 0 THEN
1
ELSE 1
END
END
),2)
";
$pre_table_request = "
SELECT
COUNT(DISTINCT accounts.id) AS `accounts_count2`,
COUNT(ecmreceipts.id) AS `ecminvoiceouts_count2`,
MONTH(ecmreceipts.register_date) AS `ecminvoiceouts_month2`,
YEAR(ecmreceipts.register_date) AS `ecminvoiceouts_year2`,
accounts.account_type AS `accounts_type2`
" . $relationsQuery . "
" . $whereFilter . "
GROUP BY `ecminvoiceouts_year2`, `ecminvoiceouts_month2`, `accounts_type2`
-- ORDER BY `ecminvoiceouts_year2`, `ecminvoiceouts_month2`, `accounts_type2`
";
$pre_table_request_other_groups = "
SELECT
accounts.account_type AS `accounts_type3`,
ecmproducts.ks_group AS `ecmproducts_group_ks3`,
MONTH(ecmreceipts.register_date) AS `ecminvoiceouts_month3`,
YEAR(ecmreceipts.register_date) AS `ecminvoiceouts_year3`,
" . $total_netto_request . " AS `ecminvoiceouts_total_brutto3`
" . $relationsQuery . "
" . $whereFilterBranch2 . "
";
$pre_table_result_other_groups = $db->query($pre_table_request_other_groups);
while($row = $db->fetchByAssoc($pre_table_result_other_groups)) { $pre_table_result_other_groups_array[] = $row; }
//print_r($pre_table_result_other_groups_array);
// Generate table data
$table_request = "
SELECT
counter.`accounts_count2`,
counter.`ecminvoiceouts_count2`,
accounts.account_type AS `accounts_type`,
ecmproducts.ks_group AS `ecmproducts_group_ks`,
MONTH(ecmreceipts.register_date) AS `ecminvoiceouts_month`,
YEAR(ecmreceipts.register_date) AS `ecminvoiceouts_year`,
" . $total_netto_request . " AS `ecminvoiceouts_total_brutto`
" . $relationsQuery . "
" . $whereFilter . "
INNER JOIN (" . $pre_table_request . ") AS counter
ON (counter.ecminvoiceouts_year2 = YEAR(ecmreceipts.register_date))
AND (counter.ecminvoiceouts_month2 = MONTH(ecmreceipts.register_date))
AND (counter.accounts_type2 = accounts.account_type)
GROUP BY `ecminvoiceouts_year`, `ecminvoiceouts_month`, `accounts_type`, `ecmproducts_group_ks`
-- ORDER BY `ecminvoiceouts_year`, `ecminvoiceouts_month`, `accounts_type`, `ecmproducts_group_ks`
";
$table_result = $db->query($table_request);
while($row = $db->fetchByAssoc($table_result)) { $table_result_array[] = $row; }
// Initial variable for generate array
// If table_result is not empty
if(!empty($table_result_array)):
// Genarate main array
foreach ($table_result_array as $table_key => $table) {
if(empty($pre_table_result_other_groups_array)) { $pre_table_result_other_groups_array = []; }
foreach ($pre_table_result_other_groups_array as $pre_table_key => $pre_table) {
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['accounts_count'] = $table['accounts_count2'];
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['documents_count'] = $table['ecminvoiceouts_count2'];
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['accounts_type']
[$table['ecmproducts_group_ks']] = $table['ecminvoiceouts_total_brutto'];
if(!empty($pre_table_result_other_groups_array)) {
if(
$table['ecminvoiceouts_year'] == $pre_table['ecminvoiceouts_year3'] &&
$table['ecminvoiceouts_month'] == $pre_table['ecminvoiceouts_month3'] &&
$table['accounts_type'] == $pre_table['accounts_type3']
) {
$generate_table_array
[$table['ecminvoiceouts_year']]
[$table['ecminvoiceouts_month']]
[$table['accounts_type']]
['together'] = $pre_table['ecminvoiceouts_total_brutto3'];
}
}
}
}
endif;
if (isset($generate_table_array) && count($generate_table_array)>0){
return $generate_table_array;
}else{
return [];
}
}
//koniec paragony
// Get all groups from ecmproducts table (with filter)
$group_ks = [];
$group_ks_result = $db->query("SELECT DISTINCT `ks_group` " . $relationsQuery . " " . $whereFilter . " ORDER BY `ks_group`");
while($row = $db->fetchByAssoc($group_ks_result)) { $group_ks[$row['ks_group']] = $app_list_strings['ecmproducts_group_ks_dom'][ $row['ks_group'] ]; }
// Get all groups from ecmproducts table (without filter)
$group_ks_without_filter = [];
$group_ks_result_without_filter = $db->query("SELECT DISTINCT `ks_group` " . $relationsQuery . " ORDER BY `ks_group`");
while($row = $db->fetchByAssoc($group_ks_result_without_filter)) { $group_ks_without_filter[$row['ks_group']] = $app_list_strings['ecmproducts_group_ks_dom'][ $row['ks_group'] ]; }
// Get groups that is not selected from ecmproducts table
$other_group_ks = [];
$other_group_ks_result = $db->query("SELECT DISTINCT `ks_group` " . $relationsQuery . " " . $otherGroupFilter . " ORDER BY `ks_group`");
while($row = $db->fetchByAssoc($other_group_ks_result)) { $other_group_ks[$row['ks_group']] = $app_list_strings['ecmproducts_group_ks_dom'][ $row['ks_group'] ]; }
// Get all types from accounts table (filter)
$types = [];
$types_result = $db->query("SELECT DISTINCT accounts.account_type " . $relationsQuery . " ORDER BY `account_type`");
while($row = $db->fetchByAssoc($types_result)) { $types[$row['account_type']] = $app_list_strings['account_type_dom'][ $row['account_type'] ]; }
//Get Stock List
$stock_array = array(""=>"");
$stock_request = $db->query("SELECT id, name FROM ecmstocks WHERE deleted=0");
while($row = $db->fetchByAssoc($stock_request)){ $stock_array[$row['id']] = $row['name']; }
// Get years from ecminvoiceouts table
$years = [];
$years_result = $db->query("SELECT DISTINCT YEAR(ecminvoiceouts.register_date) AS `years` " . $relationsQuery . " ORDER BY `years`");
while($row = $db->fetchByAssoc($years_result)) { $years[] = $row['years']; }
// Get months from ecminvoiceouts table
$months = [];
$months_result = $db->query("SELECT DISTINCT MONTH(ecminvoiceouts.register_date) AS `months` " . $relationsQuery . " ORDER BY `months`");
while($row = $db->fetchByAssoc($months_result)) { $months[$row['months']] = $row['months']; }
// Generate file pdf
function pdf($smarty) {
include_once ("include/MPDF60/mpdf60/mpdf.php");
include_once ("modules/EcmSysInfos/EcmSysInfo.php");
$EcmSysInfos = new EcmSysInfo();
if(!$_REQUEST['date_from']) {
$date_from = date("01.m.Y");
} else {
$date_from = $_REQUEST["date_from"];
}
if(!$_REQUEST['date_to']) {
$date_to = date("d.m.Y");
} else {
$date_to = $_REQUEST["date_to"];
}
$content = $smarty->fetch('modules/EcmReports/tpls/ReportSalesByGroupPDF.tpl');
$mPDF = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 10, 10, 5, 5 );
$mPDF->mirrorMargins = 1;
//print_r($content);
$mPDF->WriteHTML($content);
$dir = 'upload/' . $EcmSysInfos->getDatabaseName() . '/pdf/EcmReports/';
if(!is_dir($dir)){
mkdir($dir, '755', true);
}
$file_name = str_replace(".","",$date_from) . "_" . str_replace(".","",$date_to) ."_Raport_Sprzedazy_Po_Gropach.pdf";
$mPDF->Output( $file_name, "F");
print $file_name;
}
// Generate JSON-array for Chart
function chart_json($generate_table_array, $group_ks, $types,$req) {
$chart_column = !empty($_REQUEST['chart_column'][0]) ? $_REQUEST['chart_column'] : ["1efd09c99174463e7f3a81d80c4d6668"];
$group_ks_column = $chart_column[0];
if(!empty($_REQUEST['types_group'][0])) {
// Translate all index in text
foreach ($_REQUEST['types_group'] as $types_request_key => $type_request) {
$types_request[$types_request_key] = $types[$type_request];
}
} elseif(isset($types)) {
$types_request = $types;
} else {
$types_request[] = '';
};
$head[] = 'Data';
foreach ($types_request as $type_request) { $head[] = $type_request; }
$head[]='total';
// Add header to main array
// Generate array
$count=1;
$arr[0] = $head;
foreach ($generate_table_array as $year_key => $year) {
foreach ($year as $month_key => $month) {
$ccc=0;
$i=0;
foreach ($month as $type_key => $types_val) {
//print_r($type_key);print_r('<br><br>');
//print_r($types_val);print_r('<br><br>');
if($ccc == 0) {
$arr[$count][] = $year_key."-".$month_key; // date
$ccc++;
}
if($group_ks_column == 'accounts_count' ||$group_ks_column == 'documents_count') {
$arr[$count][] = (int)$types_val[$group_ks_column];
}
elseif($group_ks_column=='suma'){
if(in_array($type_key,$req)){
$arr[$count][]=$types_val['accounts_type']['total'];
}
//print_r($types_val);
}
else {
foreach ($types_val['accounts_type'] as $groups_key => $groups_val) {
$name=$types[$type_key];
$kx=array_search($name,$head);
if($type_key=='suma' || $type_key=='total'){
$kx=count($head)-1;
}
//var_dump($kx);
//print_r('<br><br>');
//var_dump($groups_val);print_r('<br><br>');
if($kx!=0){
if($groups_key == $group_ks_column ) {
$arr[$count][$kx] = (int)$groups_val;
}
}
}
}
}
$count++;
}
}
$header_count=0;
foreach($arr as $a_key => $a) {
$max=count($head);
for($i=0;$i<$max;$i++){
if(!isset($arr[$a_key][$i])){
$arr[$a_key][$i]=0;
}
}
//$arr[$a_key]
}
echo json_encode($arr);
//echo "<pre>";
//echo json_encode($arr);
}
// Genearte file xls
function xsl($group_ks, $array_months, $types, $generate_table_array) {
$filename='modules/Home/Files/raport_sprzedazy_w_groupach_'.date('d_m-Y').'.csv';
$fp = fopen($filename,'w');
$header = generate_header($group_ks);
$line = generate_line($group_ks, $array_months, $types, $generate_table_array);
fwrite($fp, implode(";", $header).PHP_EOL);
foreach ($line as $l) {
$l = coding($l);
fwrite($fp, implode(";", $l).PHP_EOL);
}
print $filename;
}
// Generate header for xls file
function generate_header($group_ks) {
$header = [];
$header[] = 'ROK';
$header[] = 'MIESIĄC';
$header[] = 'PROFIL GŁÓWNY';
$header[] = 'ILU ODBIORCÓW W LICZBACH';
$header[] = 'ILE DOKUMENTÓW';
foreach ($group_ks as $list_group_k) { $header[] = $list_group_k; }
return $header;
}
// Genearte body for xls file
function generate_line($group_ks, $array_months, $types, $generate_table_array) {
$line = [];
foreach ($generate_table_array as $year_key => $year) {
foreach ($year as $month_key => $month) {
foreach ($month as $type_key => $type) {
$line[$year_key."_".$month_key."_".$type_key]['ecminvoiceouts_year'] = $year_key;
$line[$year_key."_".$month_key."_".$type_key]['ecminvoiceouts_month'] = $array_months[$month_key];
$line[$year_key."_".$month_key."_".$type_key]['accounts_type'] = $types[$type_key];
$line[$year_key."_".$month_key."_".$type_key]['accounts_count'] = $type['accounts_count'];
$line[$year_key."_".$month_key."_".$type_key]['documents_count'] = $type['documents_count'];
foreach ($group_ks as $groups_key => $groups) {
$count=0;
foreach ($type['accounts_type'] as $group_key => $group) {
if($groups_key == $group_key) {
$count=1;
$line[$year_key."_".$month_key."_".$type_key][$group_key] = $group;
}
}
if($count == 0) {
$line[$year_key."_".$month_key."_".$type_key][$groups_key] = '';
}
}
}
}
}
return $line;
}
function coding_pl($variable) {
return preg_replace("/[^A-ZłŁąĄęĘżŻ,:.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/", ' ', preg_replace("/\r\n|\r|\n/", ' ', html_entity_decode($variable)));
}
// UTF-8 -> Windows-1250
function coding($data) {
foreach ($data as $k=>$v){
$data[$k]=iconv('UTF-8', 'windows-1250', $data[$k]);;
}
return $data;
}
$generate_table_array=[];
$document_type=isset($_REQUEST['document_sales_type']) ? $_REQUEST['document_sales_type'] : ['invoice','invoice_correct','recipe','recipe_correct'];
if(count($_REQUEST)>2){
if(in_array('invoice',$document_type) ||in_array('invoice_correct',$document_type) ){
$generate_table_array_fk=get_items_fk($_REQUEST,$db,$whereFilter,$whereFilterBranch2,$otherGroupFilter,$relationsQuery);
}
if(in_array('recipe',$document_type) ||in_array('recipe_correct',$document_type) ){
$generate_table_array_rec=get_items_rec($_REQUEST,$db,$whereFilter,$whereFilterBranch2,$otherGroupFilter,$relationsQueryRec);
}
//print_r($generate_table_array_fk);
if(!isset($generate_table_array_fk) || count($generate_table_array_fk)==0){
$generate_table_array=$generate_table_array_rec;
}elseif(!isset($generate_table_array_rec) || count($generate_table_array_rec)==0){
$generate_table_array=$generate_table_array_fk;
}else{
$generate_table_array=$generate_table_array_fk;
foreach ($generate_table_array_rec as $key =>$value){
if(isset($generate_table_array[$key])){
foreach($value as $miech =>$reszta){
if(isset($generate_table_array[$key][$miech])){
foreach($reszta as $kont =>$data){
if(isset($generate_table_array[$key][$miech][$kont])){
$generate_table_array[$key][$miech][$kont]['accounts_count']+=$data['accounts_count'];
$generate_table_array[$key][$miech][$kont]['documents_count']+=$data['documents_count'];
foreach($data['accounts_type'] as $grupa =>$ilosc){
if(isset($generate_table_array[$key][$miech][$kont]['accounts_type'][$grupa])){
$generate_table_array[$key][$miech][$kont]['accounts_type'][$grupa]+=$ilosc;
}
}
}
}
}
}
}
}//koniec foreacha glownego
}// koniec elsa
}
//dodawanie sumy na koncu
//print_r($_REQUEST['types_group_sum']);
foreach($generate_table_array as $rok=>$data_rok){
foreach($data_rok as $miech=>$data_miech ){
foreach($data_miech as $typ_gs => $data_type_gs){
$total_sum_group=0;
//print_r($data_type_gs);print_r('<br><br>');
if(!in_array($typ_gs,$_REQUEST['types_group'])){
if(!isset($generate_table_array[$rok][$miech]['suma'])){
$generate_table_array[$rok][$miech][suma] = array();
}
$generate_table_array[$rok][$miech]['suma']['accounts_count']+=$data_type_gs['accounts_count'];
$generate_table_array[$rok][$miech]['suma']['documents_count']+=$data_type_gs['documents_count'];
}
foreach($data_type_gs['accounts_type'] as $sum_k => $sum_data){
// sumowanie w kolumnach
if(!in_array($typ_gs,$_REQUEST['types_group'])){
$generate_table_array[$rok][$miech]['suma']['accounts_type'][$sum_k]+=$sum_data;
}
}
}
}
}//koniec glownego foreacha
foreach($generate_table_array as $rok=>$data_rok){
//sumowanie w wierszach
foreach($data_rok as $miech=>$data_miech ){
foreach($data_miech as $typ_gs => $data_type_gs){
foreach($data_type_gs['accounts_type'] as $sum_k => $sum_data){
if(!in_array($sum_k,$_REQUEST['product_group'])){
if(!isset($generate_table_array[$rok][$miech][$typ_gs]['accounts_type']['total'])){
$generate_table_array[$rok][$miech][$typ_gs]['accounts_type']['total'] = 0;
}
$generate_table_array[$rok][$miech][$typ_gs]['accounts_type']['total']+=$sum_data;
}
}
}
}
}//koniec glownego foreacha
$smarty = new Sugar_Smarty ();
// Variables for Smarty (tpl)
$smarty->assign("MOD", $mod_strings);
$smarty->assign("APP_LIST_STRINGS", $app_list_strings);
$smarty->assign("table_array", $generate_table_array);
//print_r($generate_table_array);
$req=$_REQUEST['types_group'];
$req[]='suma';
$req_prod=$_REQUEST['product_group'];
$req_prod[]='total';
$smarty->assign("list_types", $types);
$smarty->assign("list_group_ks", $group_ks);
$smarty->assign("list_group_ks_without_filter", $group_ks_without_filter);
$smarty->assign("list_other_group_ks", $other_group_ks);
$smarty->assign('types_group_req',$req);
$smarty->assign('types_prod_req',$req_prod);
$smarty->assign("list_years", $years);
$smarty->assign("list_months", $months);
$smarty->assign("months", $array_months);
$smarty->assign("stock_array", $stock_array);
$_REQUEST['to_xls'] = isset($_REQUEST['to_xls']) ? $_REQUEST['to_xls'] : null;
$_REQUEST['to_json'] = isset($_REQUEST['to_json']) ? $_REQUEST['to_json'] : null;
$_REQUEST['to_pdf_gen'] = isset($_REQUEST['to_pdf_gen']) ? $_REQUEST['to_pdf_gen'] : null;
$_REQUEST['to_chart'] = isset($_REQUEST['to_chart']) ? $_REQUEST['to_chart'] : null;
// All pages
if($_REQUEST['to_xls'] == '1') { xsl($group_ks, $array_months, $types, $generate_table_array); }
elseif($_REQUEST['to_pdf_gen'] == '1') { pdf($smarty); }
elseif($_REQUEST['to_json'] == '1') {
//print_r($generate_table_array);
chart_json($generate_table_array, $group_ks, $types,$req); }
elseif($_REQUEST['to_chart'] == '1') { echo $smarty->display('modules/EcmReports/tpls/ReportSalesByGroupChart.tpl'); }
else { echo $smarty->display('modules/EcmReports/tpls/ReportSalesByGroup.tpl'); }