668 lines
27 KiB
PHP
668 lines
27 KiB
PHP
|
|
<?php
|
||
|
|
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
|
||
|
|
die ( 'Not A Valid Entry Point' );
|
||
|
|
|
||
|
|
/*****************************************************/
|
||
|
|
/*********************** PREPARE *********************/
|
||
|
|
/*****************************************************/
|
||
|
|
$db = $GLOBALS ['db'];
|
||
|
|
|
||
|
|
if($_GET['selectUser']!="")
|
||
|
|
{
|
||
|
|
$selectUser=$_GET['selectUser'];
|
||
|
|
}else{
|
||
|
|
$selectUser="";
|
||
|
|
}
|
||
|
|
|
||
|
|
if($_GET['selectPdfType']!="")
|
||
|
|
{
|
||
|
|
$selectPdfType=$_GET['selectPdfType'];
|
||
|
|
}else{
|
||
|
|
$selectPdfType="";
|
||
|
|
}
|
||
|
|
|
||
|
|
$source = $_GET["source"];
|
||
|
|
|
||
|
|
$group_media_saturn_holding = $_GET["group_media_saturn_holding"];
|
||
|
|
$data = array();
|
||
|
|
$categoryArray = array();
|
||
|
|
|
||
|
|
$sum = array();
|
||
|
|
$sumContracor = array();
|
||
|
|
$sumCategory = array();
|
||
|
|
$sumSubCategory = array();
|
||
|
|
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"];
|
||
|
|
|
||
|
|
$searchByType = $_GET['type'];
|
||
|
|
|
||
|
|
$date_from_to_query = new DateTime($date_from);
|
||
|
|
$date_to_to_query = new DateTime($date_to);
|
||
|
|
|
||
|
|
$datausers = array();
|
||
|
|
$users = array();
|
||
|
|
$queryUsers="SELECT
|
||
|
|
first_name as 'first',
|
||
|
|
last_name as 'last',
|
||
|
|
id
|
||
|
|
FROM users
|
||
|
|
;";
|
||
|
|
// Usuniete z powyzszego zapytania na polecenie Michała
|
||
|
|
//where deleted= 0
|
||
|
|
//and status = 'Active'
|
||
|
|
|
||
|
|
$rowsUsers= $db->query ($queryUsers);
|
||
|
|
while($rowUser = $db->fetchByAssoc ( $rowsUsers ))
|
||
|
|
{
|
||
|
|
$users["first"] = $rowUser["first"];
|
||
|
|
$users["last"] = $rowUser["last"];
|
||
|
|
$users["id"] = $rowUser["id"];
|
||
|
|
$datausers [] = $users;
|
||
|
|
}
|
||
|
|
|
||
|
|
$query = "SELECT
|
||
|
|
faktura.parent_name as 'parent',
|
||
|
|
acco.parent_id as 'parent_id',
|
||
|
|
faktura.parent_id as 'cotructor_id',
|
||
|
|
pozycja.name as 'name',
|
||
|
|
pozycja.ecmproduct_id as 'id',
|
||
|
|
pozycja.code,
|
||
|
|
faktura.type as 'type',
|
||
|
|
sale.order_source as 'source',
|
||
|
|
sum(
|
||
|
|
CASE WHEN faktura.type!='correct'
|
||
|
|
THEN
|
||
|
|
CASE WHEN faktura.currency_value_nbp is null or faktura.currency_value_nbp='' or faktura.currency_value_nbp=0
|
||
|
|
THEN
|
||
|
|
pozycja.total_netto
|
||
|
|
ELSE
|
||
|
|
pozycja.total_netto*faktura.currency_value_nbp
|
||
|
|
END
|
||
|
|
ELSE
|
||
|
|
|
||
|
|
CASE WHEN faktura.currency_value_nbp is null or faktura.currency_value_nbp='' or faktura.currency_value_nbp=0
|
||
|
|
THEN
|
||
|
|
pozycja.total_netto_corrected
|
||
|
|
ELSE
|
||
|
|
pozycja.total_netto_corrected*faktura.currency_value_nbp
|
||
|
|
END
|
||
|
|
|
||
|
|
END
|
||
|
|
) as netto,
|
||
|
|
sum(
|
||
|
|
CASE WHEN faktura.type!='correct'
|
||
|
|
THEN
|
||
|
|
pozycja.quantity
|
||
|
|
ELSE
|
||
|
|
pozycja.quantity_corrected
|
||
|
|
END
|
||
|
|
) as ilosc, ";
|
||
|
|
if($reportSales===1){
|
||
|
|
|
||
|
|
$query.="
|
||
|
|
sum( CASE WHEN faktura.type!='correct'
|
||
|
|
THEN
|
||
|
|
produkt.cena_produkcyjna*pozycja.quantity
|
||
|
|
ELSE
|
||
|
|
produkt.cena_produkcyjna*pozycja.quantity_corrected
|
||
|
|
END
|
||
|
|
) as koszt";
|
||
|
|
} else {
|
||
|
|
|
||
|
|
$query.="
|
||
|
|
sum(
|
||
|
|
CASE WHEN faktura.type!='correct'
|
||
|
|
THEN
|
||
|
|
pozycja.price_purchase*pozycja.quantity
|
||
|
|
ELSE
|
||
|
|
pozycja.price_purchase*pozycja.quantity_corrected
|
||
|
|
END
|
||
|
|
) as koszt";
|
||
|
|
}
|
||
|
|
$query.="
|
||
|
|
FROM
|
||
|
|
ecminvoiceoutitems pozycja
|
||
|
|
JOIN
|
||
|
|
ecminvoiceouts faktura ON pozycja.ecminvoiceout_id = faktura.id
|
||
|
|
LEFT JOIN
|
||
|
|
ecmproducts produkt ON pozycja.ecmproduct_id = produkt.id
|
||
|
|
JOIN
|
||
|
|
accounts acco ON acco.id = faktura.parent_id
|
||
|
|
LEFT JOIN
|
||
|
|
ecmsales sale ON sale.id = faktura.so_id
|
||
|
|
WHERE
|
||
|
|
faktura.register_date BETWEEN
|
||
|
|
'".$date_from_to_query->format('Y-m-d')."' AND
|
||
|
|
'".$date_to_to_query->format('Y-m-d')."'
|
||
|
|
and faktura.type like '".$searchByType."'
|
||
|
|
and faktura.canceled = 0
|
||
|
|
and faktura.deleted= 0";
|
||
|
|
if ($selectPdfType!="")
|
||
|
|
$query.= " and faktura.pdf_type='$selectPdfType'";
|
||
|
|
if ($source=='allegro') {
|
||
|
|
$query .= " and sale.order_source = 'allegro' ";
|
||
|
|
} else if ($source=='no-allegro') {
|
||
|
|
$query .= " and sale.order_source IS NULL ";
|
||
|
|
}
|
||
|
|
|
||
|
|
$query .= " and pozycja.deleted= 0
|
||
|
|
GROUP BY pozycja.id
|
||
|
|
ORDER BY faktura.parent_name
|
||
|
|
COLLATE utf8_polish_ci;";
|
||
|
|
|
||
|
|
/*****************************************************/
|
||
|
|
/*************** GET DATA FROM DB*********************/
|
||
|
|
/*****************************************************/
|
||
|
|
$rows = $db->query ($query);
|
||
|
|
|
||
|
|
// prepare data for Smarty
|
||
|
|
while($r = $db->fetchByAssoc ( $rows ))
|
||
|
|
{
|
||
|
|
$row = prepareRow($r, $selectUser, $group_media_saturn_holding);
|
||
|
|
if($row) {
|
||
|
|
$data [] = $row;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// E-Commerce data
|
||
|
|
$ecommercerows = getEcommerceData($date_from_to_query, $date_to_to_query, 'allegro', $searchByType);
|
||
|
|
foreach($ecommercerows as $r) {
|
||
|
|
$row = prepareRow($r, $selectUser, $group_media_saturn_holding);
|
||
|
|
if($row) {
|
||
|
|
$data [] = $row;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$ecommercerows = getEcommerceData($date_from_to_query, $date_to_to_query, 'shop', $searchByType);
|
||
|
|
foreach($ecommercerows as $r) {
|
||
|
|
$row = prepareRow($r, $selectUser, $group_media_saturn_holding);
|
||
|
|
if($row) {
|
||
|
|
$data [] = $row;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if($searchByType!="")
|
||
|
|
{
|
||
|
|
|
||
|
|
////////////////// SUM /////////////////////////////////
|
||
|
|
foreach( $data as $key=>&$element )
|
||
|
|
{
|
||
|
|
if($element["netto"]!=0)
|
||
|
|
{
|
||
|
|
$categoryArray[$element["parent"] == "" ? "Nieznany" : $element["parent"]][$element["kategoria"] == "" ? "Inne" : $element["kategoria"]][$element["podkategoria"] == "" ? "Reszta" : $element["podkategoria"]][$key] = $element;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach( $categoryArray as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category=>&$elementCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $subcategory=>&$elementSubCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementSubCategory as $product=>&$elementProduct )
|
||
|
|
{
|
||
|
|
$sumContracor["IloscSum"][$parent] += $elementProduct["ilosc"];
|
||
|
|
$sumContracor["SoldSum"][$parent] += $elementProduct["netto"];
|
||
|
|
//if($elementProduct["type"]=="normal")
|
||
|
|
//{
|
||
|
|
$sumContracor["SoldSumNormal"][$parent] += $elementProduct["netto"];
|
||
|
|
$sumContracor["IloscNormal"][$parent] += $elementProduct["ilosc"];
|
||
|
|
// }else{
|
||
|
|
// $sumContracor["SoldSumNormal"][$parent] += 0;
|
||
|
|
// $sumContracor["IloscNormal"][$parent] += 0;
|
||
|
|
//}
|
||
|
|
$sumContracor["KosztSum"][$parent] += $elementProduct["koszt"];
|
||
|
|
|
||
|
|
$sumCategory["IloscSum"][$parent][$category] += $elementProduct["ilosc"];
|
||
|
|
$sumCategory["SoldSum"][$parent][$category] += $elementProduct["netto"];
|
||
|
|
//if($elementProduct["type"]=="normal")
|
||
|
|
//{
|
||
|
|
$sumCategory["SoldSumNormal"][$parent][$category] += $elementProduct["netto"];
|
||
|
|
$sumCategory["IloscNormal"][$parent][$category] += $elementProduct["ilosc"];
|
||
|
|
//}else{
|
||
|
|
// $sumCategory["SoldSumNormal"][$parent][$category] += 0;
|
||
|
|
// $sumCategory["IloscNormal"][$parent][$category] += 0;
|
||
|
|
//}
|
||
|
|
$sumCategory["KosztSum"][$parent][$category] += $elementProduct["koszt"];
|
||
|
|
|
||
|
|
$sumSubCategory["IloscSum"][$parent][$category][$subcategory] += $elementProduct["ilosc"];
|
||
|
|
$sumSubCategory["SoldSum"][$parent][$category][$subcategory] += $elementProduct["netto"];
|
||
|
|
//if($elementProduct["type"]=="normal")
|
||
|
|
//{
|
||
|
|
$sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory] += $elementProduct["netto"];
|
||
|
|
$sumSubCategory["IloscNormal"][$parent][$category][$subcategory] += $elementProduct["ilosc"];
|
||
|
|
//}else{
|
||
|
|
// $sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory] += 0;
|
||
|
|
// $sumSubCategory["IloscNormal"][$parent][$category][$subcategory] += 0;
|
||
|
|
//}
|
||
|
|
$sumSubCategory["KosztSum"][$parent][$category][$subcategory] += $elementProduct["koszt"];
|
||
|
|
}
|
||
|
|
$sumSubCategory["MarzaSum"][$parent][$category][$subcategory] = ($sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory]-$sumSubCategory["KosztSum"][$parent][$category][$subcategory])/$sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory]*100;
|
||
|
|
$sumSubCategory["SredniaSum"][$parent][$category][$subcategory] = $sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory]/$sumSubCategory["IloscNormal"][$parent][$category][$subcategory];
|
||
|
|
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["IloscSum"] = $sumSubCategory["IloscSum"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["SoldSum"] = $sumSubCategory["SoldSum"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["SoldSumNormal"] = $sumSubCategory["SoldSumNormal"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["IloscNormal"] = $sumSubCategory["IloscNormal"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["SredniaSum"] = $sumSubCategory["SredniaSum"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["KosztSum"] = $sumSubCategory["KosztSum"][$parent][$category][$subcategory];
|
||
|
|
$categoryArray[$parent][$category][$subcategory]["MarzaSum"] = $sumSubCategory["MarzaSum"][$parent][$category][$subcategory];
|
||
|
|
}
|
||
|
|
$sumCategory["MarzaSum"][$parent][$category] = ($sumCategory["SoldSumNormal"][$parent][$category]-$sumCategory["KosztSum"][$parent][$category])/$sumCategory["SoldSumNormal"][$parent][$category]*100;
|
||
|
|
$sumCategory["SredniaSum"][$parent][$category] = $sumCategory["SoldSumNormal"][$parent][$category]/$sumCategory["IloscNormal"][$parent][$category];
|
||
|
|
|
||
|
|
$categoryArray[$parent][$category]["IloscSum"] = $sumCategory["IloscSum"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["SoldSum"] = $sumCategory["SoldSum"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["SoldSumNormal"] = $sumCategory["SoldSumNormal"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["IloscNormal"] = $sumCategory["IloscNormal"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["SredniaSum"] = $sumCategory["SredniaSum"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["KosztSum"] = $sumCategory["KosztSum"][$parent][$category];
|
||
|
|
$categoryArray[$parent][$category]["MarzaSum"] = $sumCategory["MarzaSum"][$parent][$category];
|
||
|
|
}
|
||
|
|
$sumContracor["MarzaSum"][$parent] = ($sumContracor["SoldSumNormal"][$parent]-$sumContracor["KosztSum"][$parent])/$sumContracor["SoldSumNormal"][$parent]*100;
|
||
|
|
$sumContracor["SredniaSum"][$parent] = $sumContracor["SoldSumNormal"][$parent]/$sumContracor["IloscNormal"][$parent];
|
||
|
|
|
||
|
|
$categoryArray[$parent]["IloscSum"] = $sumContracor["IloscSum"][$parent];
|
||
|
|
$categoryArray[$parent]["SoldSum"] = $sumContracor["SoldSum"][$parent];
|
||
|
|
$categoryArray[$parent]["SoldSumNormal"] = $sumContracor["SoldSumNormal"][$parent];
|
||
|
|
$categoryArray[$parent]["IloscNormal"] = $sumContracor["IloscNormal"][$parent];
|
||
|
|
$categoryArray[$parent]["SredniaSum"] = $sumContracor["SredniaSum"][$parent];
|
||
|
|
$categoryArray[$parent]["KosztSum"] = $sumContracor["KosztSum"][$parent];
|
||
|
|
$categoryArray[$parent]["MarzaSum"] = $sumContracor["MarzaSum"][$parent];
|
||
|
|
|
||
|
|
}
|
||
|
|
foreach( $categoryArray as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
$sum["IloscSumSum"] += $sumContracor["IloscSum"][$parent];
|
||
|
|
$sum["SoldSumSum"] += $sumContracor["SoldSum"][$parent];
|
||
|
|
$sum["SoldSumNormal"] += $sumContracor["SoldSumNormal"][$parent];
|
||
|
|
$sum["IloscNormal"] += $sumContracor["IloscNormal"][$parent];
|
||
|
|
$sum["KosztSumSum"] += $sumContracor["KosztSum"][$parent];
|
||
|
|
}
|
||
|
|
$sum["MarzaSumSum"] = ($sum["SoldSumNormal"]-$sum["KosztSumSum"])/$sum["SoldSumNormal"]*100;
|
||
|
|
$sum["SredniaSumSum"] = $sum["SoldSumNormal"]/$sum["IloscNormal"];
|
||
|
|
|
||
|
|
//mz: dotąd jest OK
|
||
|
|
|
||
|
|
function cmp($a, $b)
|
||
|
|
{
|
||
|
|
if ($a["id"] == $b["id"]) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return ($a["id"] < $b["id"]) ? -1 : 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// grupowanie po kontrahentach
|
||
|
|
foreach($categoryArray as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
$tmp2[$parent]["IloscSum"] = $categoryArray[$parent]["IloscSum"];
|
||
|
|
$tmp2[$parent]["SoldSum"] = $categoryArray[$parent]["SoldSum"];
|
||
|
|
$tmp2[$parent]["SoldSumNormal"] = $categoryArray[$parent]["SoldSumNormal"];
|
||
|
|
$tmp2[$parent]["IloscNormal"] = $categoryArray[$parent]["IloscNormal"];
|
||
|
|
$tmp2[$parent]["KosztSum"] = $categoryArray[$parent]["KosztSum"];
|
||
|
|
$tmp2[$parent]["SredniaSum"] = 0;
|
||
|
|
$tmp2[$parent]["SredniaSum"] = $categoryArray[$parent]["SredniaSum"];
|
||
|
|
$tmp2[$parent]["MarzaSum"] = $categoryArray[$parent]["MarzaSum"];
|
||
|
|
foreach($elementParent as $category=>&$elementCategory)
|
||
|
|
{
|
||
|
|
$tmp2[$parent][$category]["IloscSum"] = $categoryArray[$parent][$category]["IloscSum"];
|
||
|
|
$tmp2[$parent][$category]["SoldSum"] = $categoryArray[$parent][$category]["SoldSum"];
|
||
|
|
$tmp2[$parent][$category]["SoldSumNormal"] = $categoryArray[$parent][$category]["SoldSumNormal"];
|
||
|
|
$tmp2[$parent][$category]["IloscNormal"] = $categoryArray[$parent][$category]["IloscNormal"];
|
||
|
|
$tmp2[$parent][$category]["KosztSum"] = $categoryArray[$parent][$category]["KosztSum"];
|
||
|
|
$tmp2[$parent][$category]["SredniaSum"] = $categoryArray[$parent][$category]["SredniaSum"];
|
||
|
|
$tmp2[$parent][$category]["MarzaSum"] = $categoryArray[$parent][$category]["MarzaSum"];
|
||
|
|
foreach( $elementCategory as $subcategory=>&$elementSubCategory )
|
||
|
|
{
|
||
|
|
|
||
|
|
$tmp2[$parent][$category][$subcategory]["IloscSum"] = $categoryArray[$parent][$category][$subcategory]["IloscSum"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["SoldSum"] = $categoryArray[$parent][$category][$subcategory]["SoldSum"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["SoldSumNormal"] = $categoryArray[$parent][$category][$subcategory]["SoldSumNormal"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["IloscNormal"] = $categoryArray[$parent][$category][$subcategory]["IloscNormal"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["KosztSum"] = $categoryArray[$parent][$category][$subcategory]["KosztSum"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["SredniaSum"] = $categoryArray[$parent][$category][$subcategory]["SredniaSum"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["MarzaSum"] = $categoryArray[$parent][$category][$subcategory]["MarzaSum"];
|
||
|
|
|
||
|
|
|
||
|
|
usort($elementSubCategory, "cmp");
|
||
|
|
$i=0;
|
||
|
|
$tmp2[$parent][$category][$subcategory][0]= array ('is' => null);
|
||
|
|
foreach($elementSubCategory as $count=>&$element3)
|
||
|
|
{
|
||
|
|
if($element3["id"]!=$tmp2[$parent][$category][$subcategory][$i]["id"])
|
||
|
|
{
|
||
|
|
if($tmp2[$parent][$category][$subcategory][$i]["id"]!=null)
|
||
|
|
{
|
||
|
|
$i++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["id"]=$element3["id"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["name"]=$element3["name"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["code"]=$element3["code"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["parent_id"]=$element3["parent_id"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["stan"] = $element3["stan"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["wartosc"] = $element3["wartosc"];
|
||
|
|
|
||
|
|
$tmp2[$parent]["StanSum"] += $element3["stan"];
|
||
|
|
$tmp2[$parent]["WartoscSum"] += $element3["wartosc"];
|
||
|
|
|
||
|
|
$tmp2[$parent][$category]["StanSum"] += $element3["stan"];
|
||
|
|
$tmp2[$parent][$category]["WartoscSum"] += $element3["wartosc"];
|
||
|
|
|
||
|
|
$tmp2[$parent][$category][$subcategory]["StanSum"] += $element3["stan"];
|
||
|
|
$tmp2[$parent][$category][$subcategory]["WartoscSum"] += $element3["wartosc"];
|
||
|
|
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["nettoNormal"] += $element3["netto"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["IloscNormal"] += $element3["ilosc"];
|
||
|
|
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["ilosc"] += $element3["ilosc"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["netto"] += $element3["netto"];
|
||
|
|
$tmp2[$parent][$category][$subcategory][$i]["koszt"] += $element3["koszt"];
|
||
|
|
|
||
|
|
}
|
||
|
|
if(is_array($tmp2[$parent][$category][$subcategory]["SredniaSum"]) || $tmp2[$parent][$category][$subcategory]["SredniaSum"]=="")
|
||
|
|
{
|
||
|
|
$tmp2[$parent][$category][$subcategory]["SredniaSum"] = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(is_array($tmp2[$parent][$category]["SredniaSum"]))
|
||
|
|
{
|
||
|
|
$tmp2[$parent][$category]["SredniaSum"] = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(is_array($tmp2[$parent]["SredniaSum"]))
|
||
|
|
{
|
||
|
|
$tmp2[$parent]["SredniaSum"] = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
$categoryArray1=$tmp2;
|
||
|
|
}
|
||
|
|
////////////////// SORT sumSubCategory /////////////////////////////////
|
||
|
|
foreach( $categoryArray1 as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category=>&$elementCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $SubCategory1=>&$elementSubCategory1)
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $SubCategory2=>&$elementSubCategory2 )
|
||
|
|
{
|
||
|
|
if($sumSubCategory["SoldSum"][$parent][$category][$SubCategory1]>$sumSubCategory["SoldSum"][$parent][$category][$SubCategory2])
|
||
|
|
{
|
||
|
|
$tmp=$sumSubCategory["SoldSum"][$parent][$category][$SubCategory1];
|
||
|
|
$sumSubCategory["SoldSum"][$parent][$category][$SubCategory1]=$sumSubCategory["SoldSum"][$parent][$category][$SubCategory2];
|
||
|
|
$sumSubCategory["SoldSum"][$parent][$category][$SubCategory2]=$tmp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
////////////////// SORT sumCategory /////////////////////////////////
|
||
|
|
foreach( $categoryArray1 as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category1=>&$elementCategory1 )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category2=>&$elementCategory2 )
|
||
|
|
{
|
||
|
|
if($sumCategory["SoldSum"][$parent][$category1]>$sumCategory["SoldSum"][$parent][$category2])
|
||
|
|
{
|
||
|
|
$tmp=$sumCategory["SoldSum"][$parent][$category1];
|
||
|
|
$sumCategory["SoldSum"][$parent][$category1]=$sumCategory["SoldSum"][$parent][$category2];
|
||
|
|
$sumCategory["SoldSum"][$parent][$category2]=$tmp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
////////////////// SORT $sumContracor /////////////////////////////////
|
||
|
|
foreach( $categoryArray1 as $parent1=>&$elementParent1 )
|
||
|
|
{
|
||
|
|
foreach( $categoryArray1 as $parent2=>&$elementParent2 )
|
||
|
|
{
|
||
|
|
if($sumContracor["SoldSum"][$parent1]>$sumContracor["SoldSum"][$parent2])
|
||
|
|
{
|
||
|
|
$tmp=$sumContracor["SoldSum"][$parent1];
|
||
|
|
$sumContracor["SoldSum"][$parent1]=$sumContracor["SoldSum"][$parent2];
|
||
|
|
$sumContracor["SoldSum"][$parent2]=$tmp;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
////////////////// SORT array at sumContracor/////////////////////////////////
|
||
|
|
$newArray2;
|
||
|
|
foreach( $categoryArray1 as $parent1=>&$elementParent1 )
|
||
|
|
{
|
||
|
|
foreach( $categoryArray1 as $parent2=>&$elementParent2 )
|
||
|
|
{
|
||
|
|
if($sumContracor["SoldSum"][$parent1]==$categoryArray1[$parent2]["SoldSum"])
|
||
|
|
{
|
||
|
|
$newArray2[$parent2]=$categoryArray1[$parent2];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
////////////////// SORT array at sumCategory/////////////////////////////////
|
||
|
|
$newArray1;
|
||
|
|
foreach( $newArray2 as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category1=>&$elementCategory1 )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category2=>&$elementCategory2 )
|
||
|
|
{
|
||
|
|
if($sumCategory["SoldSum"][$parent][$category1]==$newArray2[$parent][$category2]["SoldSum"])
|
||
|
|
{
|
||
|
|
$newArray1[$parent][$category2]=$newArray2[$parent][$category2];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
////////////////// SORT array at sumSubCategory/////////////////////////////////
|
||
|
|
$newArray;
|
||
|
|
foreach( $newArray1 as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category=>&$elementCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $subcategory1=>&$elementSubCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $subcategory2=>&$elementSubCategory )
|
||
|
|
{
|
||
|
|
if($sumSubCategory["SoldSum"][$parent][$category][$subcategory1]==$newArray1[$parent][$category][$subcategory2]["SoldSum2"])
|
||
|
|
{
|
||
|
|
$newArray[$parent][$category][$subcategory2]=$newArray1[$parent][$category][$subcategory2];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$newArray[$parent]["IloscSum"]=$newArray1[$parent]["IloscSum"];
|
||
|
|
$newArray[$parent]["SoldSum"]=$newArray1[$parent]["SoldSum"];
|
||
|
|
$newArray[$parent]["SredniaSum"]=$newArray1[$parent]["SredniaSum"];
|
||
|
|
$newArray[$parent]["KosztSum"]=$newArray1[$parent]["KosztSum"];
|
||
|
|
$newArray[$parent]["MarzaSum"]=$newArray1[$parent]["MarzaSum"];
|
||
|
|
}
|
||
|
|
|
||
|
|
//wyliczanie dokładne marży i średniej dla produktów
|
||
|
|
foreach( $newArray as $parent=>&$elementParent )
|
||
|
|
{
|
||
|
|
foreach( $elementParent as $category=>&$elementCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementCategory as $subcategory=>&$elementSubCategory )
|
||
|
|
{
|
||
|
|
foreach( $elementSubCategory as $key=>&$element )
|
||
|
|
{
|
||
|
|
$newArray[$parent][$category][$subcategory][$key]["marza"] = ($newArray[$parent][$category][$subcategory][$key]["nettoNormal"]-$newArray[$parent][$category][$subcategory][$key]["koszt"])/$newArray[$parent][$category][$subcategory][$key]["nettoNormal"]*100;
|
||
|
|
$newArray[$parent][$category][$subcategory][$key]["srednia"] = $newArray[$parent][$category][$subcategory][$key]["nettoNormal"]/$newArray[$parent][$category][$subcategory][$key]["IloscNormal"];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function prepareRow($r, $selectUser, $group_media_saturn_holding) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
|
||
|
|
$row = array();
|
||
|
|
$row["id"] = $r["id"];
|
||
|
|
$row["name"] = $r["name"];
|
||
|
|
$row["code"] = $r["code"];
|
||
|
|
$row["type"] = $r["type"];
|
||
|
|
|
||
|
|
$userBool=1;
|
||
|
|
if($selectUser!="")
|
||
|
|
{
|
||
|
|
$userBool=0;
|
||
|
|
$querySelectUsers="SELECT user.id,
|
||
|
|
first_name as 'first',
|
||
|
|
last_name as 'last'
|
||
|
|
FROM
|
||
|
|
accounts acc
|
||
|
|
JOIN
|
||
|
|
users user ON acc.assigned_user_id=user.id
|
||
|
|
WHERE acc.id='".$r["cotructor_id"]."';";
|
||
|
|
$rowsSelectUsers = $db->query ($querySelectUsers);
|
||
|
|
while($rowSelectUser = $db->fetchByAssoc ( $rowsSelectUsers ))
|
||
|
|
{
|
||
|
|
if($rowSelectUser["id"]==$selectUser)
|
||
|
|
{
|
||
|
|
$userBool=1;
|
||
|
|
}else{
|
||
|
|
$userBool=0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if($userBool==1)
|
||
|
|
{
|
||
|
|
|
||
|
|
if($r["parent_id"]==1249 && $group_media_saturn_holding == "enabled")
|
||
|
|
{
|
||
|
|
$row["parent"] = "Media Saturn Holding";
|
||
|
|
}else{
|
||
|
|
$row["parent"] = $r["parent"];
|
||
|
|
}
|
||
|
|
|
||
|
|
$querySubCategory="SELECT category.name as 'podkategoria'
|
||
|
|
FROM
|
||
|
|
ecmproductcategories_bean bean
|
||
|
|
JOIN
|
||
|
|
ecmproductcategories category ON bean.ecmproductcategory_id = category.id
|
||
|
|
WHERE bean.bean_id='".$row["id"]."'
|
||
|
|
and category.deleted=0
|
||
|
|
and bean.deleted = '0'
|
||
|
|
and bean.position = 1;";
|
||
|
|
$rowsSubCategory = $db->query ($querySubCategory);
|
||
|
|
while($rowSubCategory = $db->fetchByAssoc ( $rowsSubCategory ))
|
||
|
|
{
|
||
|
|
$row["podkategoria"] = $rowSubCategory["podkategoria"];
|
||
|
|
}
|
||
|
|
|
||
|
|
$queryCategory="SELECT category.name as 'kategoria'
|
||
|
|
FROM
|
||
|
|
ecmproductcategories_bean bean
|
||
|
|
JOIN
|
||
|
|
ecmproductcategories category ON bean.ecmproductcategory_id = category.id
|
||
|
|
WHERE bean.bean_id='".$row["id"]."'
|
||
|
|
and category.deleted=0
|
||
|
|
and bean.deleted = '0'
|
||
|
|
and bean.position = 0;";
|
||
|
|
|
||
|
|
$rowscategory = $db->query ($queryCategory);
|
||
|
|
while($rowcategory = $db->fetchByAssoc ( $rowscategory ))
|
||
|
|
{
|
||
|
|
//echo "assadad";
|
||
|
|
$row["kategoria"] = $rowcategory["kategoria"];
|
||
|
|
}
|
||
|
|
|
||
|
|
$row["ilosc"] = $r["ilosc"];
|
||
|
|
$row["netto"] = $r["netto"];
|
||
|
|
$row["srednia"] = $row["netto"]/$row["ilosc"];
|
||
|
|
|
||
|
|
$row["srednia"] = 5;
|
||
|
|
|
||
|
|
$row["koszt"] = $r["koszt"];
|
||
|
|
|
||
|
|
//if($row["netto"]>$row["koszt"])
|
||
|
|
//{
|
||
|
|
$row["marza"] = ($row["netto"]-$row["koszt"])/($row["netto"])*100;
|
||
|
|
//}else{
|
||
|
|
// $row["marza"] = 5;
|
||
|
|
//$row["marza"] = ($row["netto"]-$row["koszt"])/($row["netto"])*100;
|
||
|
|
//}
|
||
|
|
return $row;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getEcommerceData($date_from_to_query, $date_to_to_query, $type, $inv_type) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$data = [];
|
||
|
|
if ($inv_type == 'correct') {
|
||
|
|
$inv_type = 'correcting';
|
||
|
|
}
|
||
|
|
$query = "
|
||
|
|
SELECT
|
||
|
|
ip.ecmproduct_id AS id,
|
||
|
|
p.name,
|
||
|
|
p.code,
|
||
|
|
SUM(CASE WHEN i.type ='normal'
|
||
|
|
THEN
|
||
|
|
ip.price_netto * ip.quantity
|
||
|
|
ELSE
|
||
|
|
(ip.price_netto * ip.quantity) - (ipc.price_netto * ipc.quantity)
|
||
|
|
END) AS netto,
|
||
|
|
SUM(CASE WHEN i.type ='normal'
|
||
|
|
THEN
|
||
|
|
ip.quantity
|
||
|
|
ELSE
|
||
|
|
ip.quantity - ipc.quantity
|
||
|
|
END) AS ilosc
|
||
|
|
FROM
|
||
|
|
ecommerce_invoices_products AS ip
|
||
|
|
INNER JOIN
|
||
|
|
ecmproducts AS p ON p.id = ip.ecmproduct_id
|
||
|
|
INNER JOIN
|
||
|
|
ecommerce_invoices AS i ON i.id = ip.invoice_id
|
||
|
|
LEFT JOIN
|
||
|
|
ecommerce_invoices_products AS ipc ON ipc.invoice_id = i.corrected_invoice_id AND ipc.ecmproduct_id = ip.ecmproduct_id
|
||
|
|
WHERE
|
||
|
|
i.register_date BETWEEN
|
||
|
|
'".$date_from_to_query->format('Y-m-d')."' AND
|
||
|
|
'".$date_to_to_query->format('Y-m-d')."' AND
|
||
|
|
i.origin = '$type' AND
|
||
|
|
i.type LIKE '$inv_type'
|
||
|
|
GROUP BY ip.ecmproduct_id";
|
||
|
|
|
||
|
|
$rows = $db->query($query);
|
||
|
|
while($r = $db->fetchByAssoc($rows)) {
|
||
|
|
if ($type == 'allegro') {
|
||
|
|
$r['parent'] = "Allegro";
|
||
|
|
$r['parent_id'] = "allegro";
|
||
|
|
$r['contructor_id'] = "allegro";
|
||
|
|
} else if ($type == 'shop') {
|
||
|
|
$r['parent'] = "ABC-Czystosci.pl";
|
||
|
|
$r['parent_id'] = "abc-czystosci.pl";
|
||
|
|
$r['contructor_id'] = "abc-czystosci.pl";
|
||
|
|
}
|
||
|
|
$q = "
|
||
|
|
SELECT AVG(i.price) AS sell_price
|
||
|
|
FROM ecmstockdocoutitems AS i
|
||
|
|
INNER JOIN ecmstockdocouts AS o ON i.ecmstockdocout_id = o.id
|
||
|
|
WHERE i.ecmproduct_id = '{$r['id']}' AND o.delivery_date BETWEEN '{$date_from_to_query->format('Y-m-d')}' AND '{$date_to_to_query->format('Y-m-d')}'
|
||
|
|
AND o.parent_id = 'b5612f7f-85e5-f930-293e-62cead14b424'
|
||
|
|
";
|
||
|
|
$price = $db->query($q);
|
||
|
|
$price = $db->fetchByAssoc($price);
|
||
|
|
$r['koszt'] = $price['sell_price'] * $r['ilosc'];
|
||
|
|
$data[] = $r;
|
||
|
|
}
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
?>
|