Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
graphhandler.php
1<?php
2
4
14
16{
17 protected const COLORS = [
18 'SOLD' => '#64b1e2',
19 'PROFIT' => '#fda505',
20 ];
21 private const GROUP_MONTH = 'month';
22 private const GROUP_DAY = 'day';
23 private const GROUP_WEEK_DAY = 'weekday';
24
25 public function getMultipleGroupedData()
26 {
27 return $this->getCalculatedData();
28 }
29
31 {
32 return [];
33 }
34
35 public function prepare()
36 {
37 return $this->getGraphsData();
38 }
39
40 private function getDateGrouping(): string
41 {
42 $filter = $this->getFilterParameters();
43
44 $periodDefinition = $filter[StoreSaleFilter::REPORT_INTERVAL_FIELD_NAME]['datasel'] ?? DateType::CURRENT_MONTH;
45
46 switch ($periodDefinition)
47 {
48 case DateType::YEAR:
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;
56 }
57
58 return self::GROUP_DAY;
59 }
60
61 private function getGraphsData(): array
62 {
63 $filterParams = $this->getFormattedFilter();
64
66
67 if (empty($basketItems))
68 {
69 $basketItems = $this->getEmptyBasketItemsData();
70 }
71
72 $combinedData = [];
73 $dateGrouping = $this->getDateGrouping();
74 foreach ($basketItems as $item)
75 {
76 $item['BASKET_QUANTITY'] *= -1;
77 $dateDeducted = $item['DATE_DEDUCTED'];
78 if (!($dateDeducted instanceof DateTime))
79 {
80 continue;
81 }
82 $dateDeducted->setTime(0,0);
83 if ($dateGrouping === self::GROUP_MONTH)
84 {
85 $year = $dateDeducted->format('Y');
86 $month = $dateDeducted->format('m');
87 $dateDeducted->setDate($year, $month, 1);
88 }
89
90 $priceFields = [
91 'TOTAL_SOLD' => $item['BASKET_PRICE'] * $item['BASKET_QUANTITY'],
92 'COST_PRICE' => $item['COST_PRICE'] * $item['BASKET_QUANTITY'],
93 ];
94 if ($item['CURRENCY'])
95 {
96 $priceFields = $this->preparePriceFields($priceFields, $item['CURRENCY']);
97 }
98
99 $combinedData[$dateDeducted->toString()] ??= [
100 'TOTAL_SOLD' => 0.0,
101 'COST_PRICE' => 0.0,
102 'PROFIT' => 0.0,
103 'PROFITABILITY' => null,
104 'DATE_DEDUCTED' => $dateDeducted,
105 ];
106
107 $combinedData[$dateDeducted->toString()]['COST_PRICE'] += $priceFields['COST_PRICE'];
108 $combinedData[$dateDeducted->toString()]['TOTAL_SOLD'] += $priceFields['TOTAL_SOLD'];
109 }
110
111 $totalProfit = 0;
112 $totalSold = 0;
113 foreach ($combinedData as $dateKey => $data)
114 {
115 $profit = $data['TOTAL_SOLD'] - $data['COST_PRICE'];
116 $combinedData[$dateKey]['PROFIT'] = $profit;
117 if ($data['COST_PRICE'] > 0)
118 {
119 $combinedData[$dateKey]['PROFITABILITY'] = $this->calculateProfitability($data['COST_PRICE'], $profit);
120 }
121 $totalProfit += $profit;
122 $totalSold += $data['TOTAL_SOLD'];
123 }
124
125 $labels = [];
126 $soldGraphItems = [];
127 $profitGraphItems = [];
128
129 foreach ($combinedData as $date => $value)
130 {
131 $groupByValue = $value['DATE_DEDUCTED']->getTimestamp();
132 $label = $this->formatDateForLabel($value['DATE_DEDUCTED']);
133 $item = [
134 "groupBy" => $groupByValue,
135 "label" => $label,
136 "balloon" => [
137 'title' => $label,
138 'items' => [
139 [
140 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_SOLD'),
141 'htmlValue' => $this->formatAmountByCurrency((float)$value['TOTAL_SOLD']),
142 ],
143 [
144 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_PROFIT'),
145 'htmlValue' => $this->formatAmountByCurrency((float)$value['PROFIT']),
146 ],
147 [
148 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_PROFITABILITY'),
149 'value' => $value['PROFITABILITY'] !== null ? "{$value['PROFITABILITY']}%" : '-',
150 ],
151 ]
152 ],
153 ];
154
155 $soldGraphItems[] = $item + ['value' => (float)$value['TOTAL_SOLD']];
156 $profitGraphItems[] = $item + ['value' => (float)$value['PROFIT']];
157 $labels[$groupByValue] = $label;
158 }
159
160 return [
161 [
162 "items" => $soldGraphItems,
163 "config" => $this->getConfigByCode('SOLD', $labels, $totalSold),
164 ],
165 [
166 "items" => $profitGraphItems,
167 "config" => $this->getConfigByCode('PROFIT', $labels, $totalProfit)
168 ],
169 ];
170 }
171 private function getEmptyBasketItemsData(): array
172 {
173 return [
174 [
175 'BASKET_PRICE' => 0,
176 'COST_PRICE' => 0,
177 'DATE_DEDUCTED' => new DateTime(),
178 'BASKET_QUANTITY' => 0,
179 ]
180 ];
181 }
182 private function getConfigByCode(string $code, array $labels, float $total): array
183 {
184 return [
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()
191 ];
192 }
193
194 private function formatAmountByCurrency(float $amount): string
195 {
196 $totalAmountFormatted = \CCurrencyLang::CurrencyFormat($amount, CurrencyManager::getBaseCurrency());
197
198 return str_replace("&nbsp;", " ", $totalAmountFormatted);
199 }
200
201 private function formatDateForLabel(Date $date)
202 {
203 return FormatDate($this->getDateFormatForLabel(), $date);
204 }
205
206 private function getDateFormatForLabel(): string
207 {
208 switch ($this->getDateGrouping())
209 {
210 case self::GROUP_DAY:
211 return Context::getCurrent()->getCulture()->getDayMonthFormat();
212
213 case self::GROUP_WEEK_DAY:
214 return "l";
215
216 case self::GROUP_MONTH:
217 return "f";
218 }
219
220 return Context::getCurrent()->getCulture()->getLongDateFormat();
221 }
222}
static getCurrent()
Definition context.php:241
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29