19 'PROFIT' =>
'#fda505',
21 private const GROUP_MONTH =
'month';
22 private const GROUP_DAY =
'day';
23 private const GROUP_WEEK_DAY =
'weekday';
37 return $this->getGraphsData();
40 private function getDateGrouping(): string
44 $periodDefinition = $filter[StoreSaleFilter::REPORT_INTERVAL_FIELD_NAME][
'datasel'] ?? DateType::CURRENT_MONTH;
46 switch ($periodDefinition)
49 case DateType::QUARTER:
50 case DateType::CURRENT_QUARTER:
51 return self::GROUP_MONTH;
52 case DateType::LAST_WEEK:
53 case DateType::CURRENT_WEEK:
54 case DateType::NEXT_WEEK:
55 return self::GROUP_WEEK_DAY;
58 return self::GROUP_DAY;
61 private function getGraphsData(): array
67 if (empty($basketItems))
69 $basketItems = $this->getEmptyBasketItemsData();
73 $dateGrouping = $this->getDateGrouping();
74 foreach ($basketItems as $item)
76 $item[
'BASKET_QUANTITY'] *= -1;
77 $dateDeducted = $item[
'DATE_DEDUCTED'];
78 if (!($dateDeducted instanceof
DateTime))
82 $dateDeducted->setTime(0,0);
83 if ($dateGrouping === self::GROUP_MONTH)
85 $year = $dateDeducted->format(
'Y');
86 $month = $dateDeducted->format(
'm');
87 $dateDeducted->setDate($year, $month, 1);
91 'TOTAL_SOLD' => $item[
'BASKET_PRICE'] * $item[
'BASKET_QUANTITY'],
92 'COST_PRICE' => $item[
'COST_PRICE'] * $item[
'BASKET_QUANTITY'],
94 if ($item[
'CURRENCY'])
99 $combinedData[$dateDeducted->toString()] ??= [
103 'PROFITABILITY' =>
null,
104 'DATE_DEDUCTED' => $dateDeducted,
107 $combinedData[$dateDeducted->toString()][
'COST_PRICE'] += $priceFields[
'COST_PRICE'];
108 $combinedData[$dateDeducted->toString()][
'TOTAL_SOLD'] += $priceFields[
'TOTAL_SOLD'];
113 foreach ($combinedData as $dateKey => $data)
115 $profit = $data[
'TOTAL_SOLD'] - $data[
'COST_PRICE'];
116 $combinedData[$dateKey][
'PROFIT'] = $profit;
117 if ($data[
'COST_PRICE'] > 0)
121 $totalProfit += $profit;
122 $totalSold += $data[
'TOTAL_SOLD'];
126 $soldGraphItems = [];
127 $profitGraphItems = [];
129 foreach ($combinedData as $date => $value)
131 $groupByValue = $value[
'DATE_DEDUCTED']->getTimestamp();
132 $label = $this->formatDateForLabel($value[
'DATE_DEDUCTED']);
134 "groupBy" => $groupByValue,
141 'htmlValue' => $this->formatAmountByCurrency((
float)$value[
'TOTAL_SOLD']),
145 'htmlValue' => $this->formatAmountByCurrency((
float)$value[
'PROFIT']),
148 'title' =>
Loc::getMessage(
'GRAPH_HANDLER_BALLOON_SUBTITLE_PROFITABILITY'),
149 'value' => $value[
'PROFITABILITY'] !==
null ?
"{$value['PROFITABILITY']}%" :
'-',
155 $soldGraphItems[] = $item + [
'value' => (float)$value[
'TOTAL_SOLD']];
156 $profitGraphItems[] = $item + [
'value' => (float)$value[
'PROFIT']];
157 $labels[$groupByValue] = $label;
162 "items" => $soldGraphItems,
163 "config" => $this->getConfigByCode(
'SOLD', $labels, $totalSold),
166 "items" => $profitGraphItems,
167 "config" => $this->getConfigByCode(
'PROFIT', $labels, $totalProfit)
171 private function getEmptyBasketItemsData(): array
177 'DATE_DEDUCTED' =>
new DateTime(),
178 'BASKET_QUANTITY' => 0,
182 private function getConfigByCode(
string $code, array $labels,
float $total): array
185 "groupsLabelMap" => $labels,
186 "reportTitle" =>
Loc::getMessage(
'GRAPH_HANDLER_BALLOON_SUBTITLE_' . $code),
187 "reportColor" => self::COLORS[$code],
188 "amount" => $this->formatAmountByCurrency($total),
189 "dateFormatForLabel" => $this->getDateFormatForLabel(),
190 "dateGrouping" => $this->getDateGrouping()
194 private function formatAmountByCurrency(
float $amount): string
196 $totalAmountFormatted = \CCurrencyLang::CurrencyFormat($amount, CurrencyManager::getBaseCurrency());
198 return str_replace(
" ",
" ", $totalAmountFormatted);
201 private function formatDateForLabel(Date $date)
203 return FormatDate($this->getDateFormatForLabel(), $date);
206 private function getDateFormatForLabel(): string
208 switch ($this->getDateGrouping())
210 case self::GROUP_DAY:
213 case self::GROUP_WEEK_DAY:
216 case self::GROUP_MONTH:
static getMessage($code, $replace=null, $language=null)