Files
crm.e5.pl/modules/EcmReports/ReportSales.inc.php

519 lines
21 KiB
PHP
Raw Normal View History

2024-04-27 09:23:34 +02:00
<?php
2025-04-02 15:24:40 +00:00
if (! defined('sugarEntry') || ! sugarEntry)
die('Not A Valid Entry Point');
2024-04-27 09:23:34 +02:00
2025-04-02 15:24:40 +00:00
/*****************************************************/
/*********************** PREPARE *********************/
/*****************************************************/
$db = $GLOBALS['db'];
global $app_list_strings;
$all_groups_ks = $app_list_strings["ecmproducts_group_ks_dom"];
if ($_GET['selectUser'] != "") {
$selectUser = $_GET['selectUser'];
} else {
$selectUser = "";
}
if ($_GET['selectPdfType'] != "") {
$selectPdfType = $_GET['selectPdfType'];
} else {
$selectPdfType = "";
}
$data = array();
$categoryArray = array();
$sum = array();
$sumSub = array();
$sumSub2 = array();
$searchBy = $_GET['groupByProducentsOrProducts'];
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"];
$contractorName = $_GET['account_name'];
$contractorId = $_GET['account_id'];
$searchByType = $_GET['type'];
$group_ks = $_GET["group_ks"];
if ($group_ks == '')
$group_ks = "%";
$date_from_to_query = new DateTime($date_from);
$date_to_to_query = new DateTime($date_to);
$addTwinpol = (isset($_REQUEST['twinpol']) ? 1 : 0);
$datausers = array();
$users = array();
$queryUsers = "SELECT
2024-04-27 09:23:34 +02:00
first_name as 'first',
last_name as 'last',
id
FROM users
where deleted= 0
and status = 'Active';";
2025-04-02 15:24:40 +00:00
$rowsUsers = $db->query($queryUsers);
while ($rowUser = $db->fetchByAssoc($rowsUsers)) {
$users["first"] = $rowUser["first"];
$users["last"] = $rowUser["last"];
$users["id"] = $rowUser["id"];
$datausers[] = $users;
}
if ($searchBy == "contractor") {
$groupBy = "name";
} else {
$groupBy = "id";
}
if ($searchBy == "product") {
$query = "SELECT
2024-04-27 09:23:34 +02:00
pozycja.name,
pozycja.ecmproduct_id as 'id',
faktura.parent_id as 'parent_id',
produkt.code as 'code',
faktura.type as 'type',
sum(
CASE WHEN faktura.type!='correct'
THEN
CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
THEN
pozycja.total_netto
ELSE
pozycja.total_netto*faktura.currency_value
END
ELSE
CASE WHEN pozycja.old_ecminvoiceoutitem_id IS null OR pozycja.old_ecminvoiceoutitem_id='' THEN
0
ELSE
CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
THEN
pozycja.total_netto-pozycja.old_total_netto
ELSE
(pozycja.total_netto-pozycja.old_total_netto)*faktura.currency_value
END
END
END
) as netto,
sum(
CASE WHEN faktura.type!='correct'
THEN
pozycja.quantity
ELSE
pozycja.quantity_corrected
END
) as ilosc,
sum(
CASE WHEN faktura.type!='correct'
THEN
pozycja.price_purchase*pozycja.quantity
ELSE
pozycja.price_purchase*pozycja.quantity_corrected
END
2025-02-06 10:38:41 +00:00
) as koszt,
2025-04-02 15:24:40 +00:00
(SELECT SUM(s.quantity) FROM ecmstockstates s WHERE s.product_id = pozycja.ecmproduct_id) as state,
(SELECT SUM(si.quantity)
FROM ecmsaleitems si
JOIN ecmsales sa ON si.ecmsale_id = sa.id
WHERE si.ecmproduct_id = pozycja.ecmproduct_id
AND sa.status = 's30'
AND sa.type = 'sales_order'
AND sa.send_date >= NOW()) AS ordered_quantity
2024-04-27 09:23:34 +02:00
FROM
ecminvoiceoutitems pozycja
JOIN
ecminvoiceouts faktura ON pozycja.ecminvoiceout_id = faktura.id
LEFT JOIN
ecmproducts produkt ON pozycja.ecmproduct_id = produkt.id
WHERE
faktura.register_date BETWEEN
2025-04-02 15:24:40 +00:00
'" . $date_from_to_query->format('Y-m-d') . "' AND
'" . $date_to_to_query->format('Y-m-d') . "'
and faktura.type like '" . $searchByType . "'
2024-04-27 09:23:34 +02:00
and faktura.canceled = 0
and faktura.deleted= 0";
2025-04-02 15:24:40 +00:00
if ($selectPdfType != "")
$query .= " and faktura.pdf_type='$selectPdfType'";
$query .= " and pozycja.deleted= 0 ";
if ($addTwinpol === 0) {
$query .= " and faktura.parent_id <> '1b9643ca-5b1a-8f9b-b809-586b5619b068' ";
}
if ($group_ks != '%') {
$query .= "AND produkt.group_ks LIKE '$group_ks'";
}
$query .= "GROUP BY pozycja.id
2024-04-27 09:23:34 +02:00
COLLATE utf8_polish_ci;";
2025-04-02 15:24:40 +00:00
} else if ($searchBy == "contractor") {
$query = "SELECT
2024-04-27 09:23:34 +02:00
faktura.parent_name as 'name',
faktura.parent_id as 'parent_id',
pozycja.name as 'product_name',
pozycja.ecmproduct_id as 'id',
faktura.type as 'type',
acco.parent_id as 'parent2',
faktura.parent_name as 'parent',
sum(
CASE WHEN faktura.type!='correct'
THEN
CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
THEN
pozycja.total_netto
ELSE
pozycja.total_netto*faktura.currency_value
END
ELSE
CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
THEN
pozycja.total_netto_corrected
ELSE
pozycja.total_netto_corrected * faktura.currency_value
END
END
) as netto,
sum(
CASE WHEN faktura.type!='correct'
THEN
pozycja.quantity
ELSE
pozycja.quantity_corrected
END
) as ilosc,
sum(
CASE WHEN faktura.type!='correct'
THEN
pozycja.price_purchase*pozycja.quantity
ELSE
0
END
) as koszt
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
WHERE
faktura.register_date BETWEEN
2025-04-02 15:24:40 +00:00
'" . $date_from_to_query->format('Y-m-d') . "' AND
'" . $date_to_to_query->format('Y-m-d') . "'
and faktura.type like '" . $searchByType . "'
2024-04-27 09:23:34 +02:00
and faktura.canceled = 0
and faktura.deleted= 0";
2025-04-02 15:24:40 +00:00
if ($selectPdfType != "")
$query .= " and faktura.pdf_type='$selectPdfType'";
$query .= " and pozycja.deleted= 0
2024-04-27 09:23:34 +02:00
and produkt.group_ks LIKE '$group_ks'";
2025-04-02 15:24:40 +00:00
if ($addTwinpol === 0) {
$query .= " and faktura.parent_id <> '1b9643ca-5b1a-8f9b-b809-586b5619b068' ";
}
$query .= "GROUP BY pozycja.id
2024-04-27 09:23:34 +02:00
COLLATE utf8_polish_ci;";
2025-04-02 15:24:40 +00:00
}
/*****************************************************/
/*************** GET DATA FROM DB*********************/
/*****************************************************/
$rows = $db->query($query);
// prepare data for Smarty
while ($r = $db->fetchByAssoc($rows)) {
$row = array();
$row["id"] = $r["id"];
if ($r["parent2"] == 1249)
$row["name"] = "Media Saturn Holding";
else
$row["name"] = $r["name"];
$row["code"] = $r["code"];
$row["state"] = $r["state"];
$row["ordered_quantity"] = $r["ordered_quantity"];
$row["parent_id"] = $r["parent_id"];
$row["type"] = $r["type"];
$userBool = 1;
if ($selectUser != "") {
$userBool = 0;
$querySelectUsers = "SELECT user.id,
2024-04-27 09:23:34 +02:00
first_name as 'first',
last_name as 'last'
FROM
accounts acc
JOIN
users user ON acc.assigned_user_id=user.id
2025-04-02 15:24:40 +00:00
WHERE acc.id='" . $r["parent_id"] . "';";
$rowsSelectUsers = $db->query($querySelectUsers);
while ($rowSelectUser = $db->fetchByAssoc($rowsSelectUsers)) {
if ($rowSelectUser["id"] == $selectUser) {
$userBool = 1;
} else {
$userBool = 0;
}
}
}
if ($userBool == 1) {
if ($searchBy == "contractor") {
$row["product_name"] = $r["product_name"];
} else {
$row["product_name"] = $r["name"];
}
$querySubCategory = "SELECT category.name as 'podkategoria'
2024-04-27 09:23:34 +02:00
FROM
ecmproductcategories_bean bean
JOIN
ecmproductcategories category ON bean.ecmproductcategory_id = category.id
2025-04-02 15:24:40 +00:00
WHERE bean.bean_id='" . $row["id"] . "'
2024-04-27 09:23:34 +02:00
and bean.position = '1'
and bean.deleted = '0'
and category.deleted = '0';";
2025-04-02 15:24:40 +00:00
$rowsSubCategory = $db->query($querySubCategory);
while ($rowSubCategory = $db->fetchByAssoc($rowsSubCategory)) {
$row["podkategoria"] = $rowSubCategory["podkategoria"];
}
$queryCategory = "SELECT category.name as 'kategoria'
2024-04-27 09:23:34 +02:00
FROM
ecmproductcategories_bean bean
JOIN
ecmproductcategories category ON bean.ecmproductcategory_id = category.id
2025-04-02 15:24:40 +00:00
WHERE bean.bean_id='" . $row["id"] . "'
2024-04-27 09:23:34 +02:00
and bean.position = '0'
and bean.deleted = '0'
and category.deleted = '0';";
2025-04-02 15:24:40 +00:00
$rowscategory = $db->query($queryCategory);
while ($rowcategory = $db->fetchByAssoc($rowscategory)) {
$row["kategoria"] = $rowcategory["kategoria"];
}
$row["ilosc"] = $r["ilosc"];
$row["netto"] = $r["netto"];
$row["srednia"] = $row["netto"] / $row["ilosc"];
$row["koszt"] = $r["koszt"];
//if($row["netto"]>$row["koszt"])
//{
$row["marza"] = ($row["netto"] - $row["koszt"]) / ($row["netto"]) * 100;
//}else{
//$row["marza"] = 0;
//}
$data[] = $row;
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
}
if ($searchBy != "") {
////////////////// SUM /////////////////////////////////
foreach ($data as $key => &$element) {
if ($element["netto"] != 0) {
$categoryArray[$element["kategoria"] == "" ? "Inne" : $element["kategoria"]][$element["podkategoria"] == "" ? "Reszta" : $element["podkategoria"]][$key] = $element;
} else {
//echo "Nie przechodzi: <br>";
//print_r( $element );
}
}
foreach ($categoryArray as $key => &$element) {
foreach ($element as $keyy => &$elementt) {
foreach ($elementt as $keyyy => &$elementtt) {
$sumSub["IloscSum"][$key] += $elementtt["ilosc"];
$sumSub["SoldSum"][$key] += $elementtt["netto"];
//if($elementtt["type"]=="normal")
//{
$sumSub["SoldSumNormal"][$key] += $elementtt["netto"];
$sumSub["IloscNormal"][$key] += $elementtt["ilosc"];
//}else{
// $sumSub["SoldSumNormal"][$key] += 0;
//$sumSub["IloscNormal"][$key] += 0;
// }
$sumSub["KosztSum"][$key] += $elementtt["koszt"];
$sumSub2["IloscSum"][$key][$keyy] += $elementtt["ilosc"];
$sumSub2["SoldSum"][$key][$keyy] += $elementtt["netto"];
//if($elementtt["type"]=="normal")
//{
$sumSub2["SoldSumNormal"][$key][$keyy] += $elementtt["netto"];
$sumSub2["IloscNormal"][$key][$keyy] += $elementtt["ilosc"];
//}else{
// $sumSub2["SoldSumNormal"][$key][$keyy] += 0;
// $sumSub2["IloscNormal"][$key][$keyy] += 0;
//}
$sumSub2["KosztSum"][$key][$keyy] += $elementtt["koszt"];
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
$categoryArray[$key][$keyy]["IloscSum2"] = $sumSub2["IloscSum"][$key][$keyy];
$categoryArray[$key][$keyy]["SoldSum2"] = $sumSub2["SoldSum"][$key][$keyy];
$categoryArray[$key][$keyy]["SoldSumNormal2"] = $sumSub2["SoldSumNormal"][$key][$keyy];
$categoryArray[$key][$keyy]["IloscNormal2"] = $sumSub2["IloscNormal"][$key][$keyy];
$categoryArray[$key][$keyy]["KosztSum2"] = $sumSub2["KosztSum"][$key][$keyy];
}
$categoryArray[$key]["IloscSum"] = $sumSub["IloscSum"][$key];
$categoryArray[$key]["SoldSum"] = $sumSub["SoldSum"][$key];
$categoryArray[$key]["SoldSumNormal"] = $sumSub["SoldSumNormal"][$key];
$categoryArray[$key]["IloscNormal"] = $sumSub["IloscNormal"][$key];
$categoryArray[$key]["KosztSum"] = $sumSub["KosztSum"][$key];
}
foreach ($categoryArray as $key => &$element) {
$sum["IloscSumSum"] += $sumSub["IloscSum"][$key];
$sum["SoldSumSum"] += $sumSub["SoldSum"][$key];
$sum["SoldSumSumNormal"] += $sumSub["SoldSumNormal"][$key];
$sum["IloscNormal"] += $sumSub["IloscNormal"][$key];
$sum["KosztSumSum"] += $sumSub["KosztSum"][$key];
}
$sum["SredniaSumSum"] = $sum["SoldSumSumNormal"] / $sum["IloscNormal"];
$sum["MarzaSumSum"] = ($sum["SoldSumSumNormal"] - $sum["KosztSumSum"]) / $sum["SoldSumSumNormal"] * 100;
function cmp($a, $b)
{
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
}
function cmp2($a, $b)
{
if ($a['id'] == $b['id']) {
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
// grupowanie po kontrahentach
foreach ($categoryArray as $key1 => &$element1) {
foreach ($element1 as $key2 => &$element2) {
$tmp2[$key1][$key2]["IloscSum2"] = $categoryArray[$key1][$key2]["IloscSum2"];
$tmp2[$key1][$key2]["SoldSum2"] = $categoryArray[$key1][$key2]["SoldSum2"];
$tmp2[$key1][$key2]["SoldSumNormal2"] = $categoryArray[$key1][$key2]["SoldSumNormal2"];
$tmp2[$key1][$key2]["IloscNormal2"] = $categoryArray[$key1][$key2]["IloscNormal2"];
$tmp2[$key1][$key2]["KosztSum2"] = $categoryArray[$key1][$key2]["KosztSum2"];
if ($searchBy == "contractor") {
usort($element2, "cmp");
} else {
usort($element2, "cmp2");
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
$i = 0;
$tmp2[$key1][$key2][0] = array('name' => null);
foreach ($element2 as $count => &$element3) {
if ($element3[$groupBy] != $tmp2[$key1][$key2][$i][$groupBy]) {
if ($tmp2[$key1][$key2][$i][$groupBy] != null) {
$i++;
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
$tmp2[$key1][$key2][$i]["id"] = $element3["id"];
$tmp2[$key1][$key2][$i]["name"] = $element3["name"];
$tmp2[$key1][$key2][$i]["code"] = $element3["code"];
$tmp2[$key1][$key2][$i]["state"] = $element3["state"];
$tmp2[$key1][$key2][$i]["ordered_quantity"] = $element3["ordered_quantity"];
$tmp2[$key1][$key2][$i]["parent_id"] = $element3["parent_id"];
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
//if($element3["type"]=="normal")
//{
$tmp2[$key1][$key2][$i]["nettoNormal"] += $element3["netto"];
$tmp2[$key1][$key2][$i]["IloscNormal"] += $element3["ilosc"];
//}
$tmp2[$key1][$key2][$i]["ilosc"] += $element3["ilosc"];
$tmp2[$key1][$key2][$i]["netto"] += $element3["netto"];
$tmp2[$key1][$key2][$i]["koszt"] += $element3["koszt"];
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
$tmp2[$key1]["IloscSum"] = $categoryArray[$key1]["IloscSum"];
$tmp2[$key1]["SoldSum"] = $categoryArray[$key1]["SoldSum"];
$tmp2[$key1]["SoldSumNormal"] = $categoryArray[$key1]["SoldSumNormal"];
$tmp2[$key1]["IloscNormal"] = $categoryArray[$key1]["IloscNormal"];
$tmp2[$key1]["KosztSum"] = $categoryArray[$key1]["KosztSum"];
2024-04-27 09:23:34 +02:00
2025-04-02 15:24:40 +00:00
$sumSub2["MarzaSum"][$key1][$key2] = ($tmp2[$key1][$key2]["SoldSumNormal2"] - $tmp2[$key1][$key2]["KosztSum2"]) / $tmp2[$key1][$key2]["SoldSumNormal2"] * 100;
$sumSub2["SredniaSum"][$key1][$key2] = $tmp2[$key1][$key2]["SoldSumNormal2"] / $tmp2[$key1][$key2]["IloscNormal2"];
if ($key2 == "Reszta") {
if (is_array($sumSub2["SredniaSum"][$key1][$key2]) || $sumSub2["SredniaSum"][$key1][$key2] == '') {
$sumSub2["SredniaSum"][$key1][$key2] = 0;
2024-04-27 09:23:34 +02:00
}
}
2025-04-02 15:24:40 +00:00
$count1++;
$tmp2[$key1][$key2]["SredniaSum2"] = $sumSub2["SredniaSum"][$key1][$key2];
$tmp2[$key1][$key2]["MarzaSum2"] = $sumSub2["MarzaSum"][$key1][$key2];
}
$sumSub["MarzaSum"][$key1] = ($tmp2[$key1]["SoldSumNormal"] - $tmp2[$key1]["KosztSum"]) / $tmp2[$key1]["SoldSumNormal"] * 100;
$sumSub["SredniaSum"][$key1] = $tmp2[$key1]["SoldSumNormal"] / $tmp2[$key1]["IloscNormal"];
$tmp2[$key1]["SredniaSum"] = $sumSub["SredniaSum"][$key1];
$tmp2[$key1]["MarzaSum"] = $sumSub["MarzaSum"][$key1];
$categoryArray1 = $tmp2;
}
////////////////// SORT sumSub /////////////////////////////////
foreach ($categoryArray1 as $key1 => &$element) {
foreach ($element as $key2 => &$elements) {
foreach ($element as $key3 => &$element2) {
if ($sumSub2["SoldSum"][$key1][$key2] > $sumSub2["SoldSum"][$key1][$key3]) {
$tmp = $sumSub2["SoldSum"][$key1][$key2];
$sumSub2["SoldSum"][$key1][$key2] = $sumSub2["SoldSum"][$key1][$key3];
$sumSub2["SoldSum"][$key1][$key3] = $tmp;
2024-04-27 09:23:34 +02:00
}
}
2025-04-02 15:24:40 +00:00
}
}
foreach ($sumSub["SoldSum"] as $key1 => &$element) {
foreach ($sumSub["SoldSum"] as $key2 => &$element2) {
if ($sumSub["SoldSum"][$key1] > $sumSub["SoldSum"][$key2]) {
$tmp = $sumSub["SoldSum"][$key1];
$sumSub["SoldSum"][$key1] = $sumSub["SoldSum"][$key2];
$sumSub["SoldSum"][$key2] = $tmp;
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
}
}
////////////////// SORT array /////////////////////////////////
$newArray2;
foreach ($sumSub["SoldSum"] as $key1 => &$element1) {
foreach ($categoryArray1 as $key2 => &$element2) {
if ($sumSub["SoldSum"][$key1] == $categoryArray1[$key2]["SoldSum"]) {
$newArray2[$key2] = $categoryArray1[$key2];
2024-04-27 09:23:34 +02:00
}
2025-04-02 15:24:40 +00:00
}
}
$newArray1;
foreach ($newArray2 as $key1 => &$element1) {
foreach ($element1 as $key2 => &$element2) {
foreach ($element1 as $key3 => &$element3) {
if ($sumSub2["SoldSum"][$key1][$key2] == $newArray2[$key1][$key3]["SoldSum2"]) {
$newArray1[$key1][$key3] = $newArray2[$key1][$key3];
2024-04-27 09:23:34 +02:00
}
}
}
2025-04-02 15:24:40 +00:00
}
//wyliczanie dokładne marży i średniej dla produktów
foreach ($newArray1 as $key1 => &$element1) {
foreach ($element1 as $key2 => &$element2) {
foreach ($element2 as $key3 => &$element3) {
$newArray1[$key1][$key2][$key3]["marza"] = ($newArray1[$key1][$key2][$key3]["nettoNormal"] - $newArray1[$key1][$key2][$key3]["koszt"]) / $newArray1[$key1][$key2][$key3]["nettoNormal"] * 100;
$newArray1[$key1][$key2][$key3]["srednia"] = $newArray1[$key1][$key2][$key3]["nettoNormal"] / $newArray1[$key1][$key2][$key3]["IloscNormal"];
}
}
}
}