1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
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 {
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['BATCH_CURRENCY'])
95 {
96 $priceFields['COST_PRICE'] = $this->preparePrice($priceFields['COST_PRICE'], $item['BATCH_CURRENCY']);
97 }
98 if ($item['BASKET_CURRENCY'])
99 {
100 $priceFields['TOTAL_SOLD'] = $this->preparePrice($priceFields['TOTAL_SOLD'], $item['BASKET_CURRENCY']);
101 }
102
103 $combinedData[$dateDeducted->toString()] ??= [
104 'TOTAL_SOLD' => 0.0,
105 'COST_PRICE' => 0.0,
106 'PROFIT' => 0.0,
107 'PROFITABILITY' => null,
108 'DATE_DEDUCTED' => $dateDeducted,
109 ];
110
111 $combinedData[$dateDeducted->toString()]['COST_PRICE'] += $priceFields['COST_PRICE'];
112 $combinedData[$dateDeducted->toString()]['TOTAL_SOLD'] += $priceFields['TOTAL_SOLD'];
113 }
114
115 $totalProfit = 0;
116 $totalSold = 0;
117 foreach ($combinedData as $dateKey => $data)
118 {
119 $profit = $data['TOTAL_SOLD'] - $data['COST_PRICE'];
120 $combinedData[$dateKey]['PROFIT'] = $profit;
121 if ($data['COST_PRICE'] > 0)
122 {
123 $combinedData[$dateKey]['PROFITABILITY'] = $this->calculateProfitability($data['COST_PRICE'], $profit);
124 }
125 $totalProfit += $profit;
126 $totalSold += $data['TOTAL_SOLD'];
127 }
128
129 $labels = [];
130 $soldGraphItems = [];
131 $profitGraphItems = [];
132
133 foreach ($combinedData as $date => $value)
134 {
135 $groupByValue = $value['DATE_DEDUCTED']->getTimestamp();
136 $label = $this->formatDateForLabel($value['DATE_DEDUCTED']);
137 $item = [
138 "groupBy" => $groupByValue,
139 "label" => $label,
140 "balloon" => [
141 'title' => $label,
142 'items' => [
143 [
144 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_SOLD'),
145 'htmlValue' => $this->formatAmountByCurrency((float)$value['TOTAL_SOLD']),
146 ],
147 [
148 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_PROFIT'),
149 'htmlValue' => $this->formatAmountByCurrency((float)$value['PROFIT']),
150 ],
151 [
152 'title' => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_PROFITABILITY'),
153 'value' => $value['PROFITABILITY'] !== null ? "{$value['PROFITABILITY']}%" : '-',
154 ],
155 ]
156 ],
157 ];
158
159 $soldGraphItems[] = $item + ['value' => (float)$value['TOTAL_SOLD']];
160 $profitGraphItems[] = $item + ['value' => (float)$value['PROFIT']];
161 $labels[$groupByValue] = $label;
162 }
163
164 return [
165 [
166 "items" => $soldGraphItems,
167 "config" => $this->getConfigByCode('SOLD', $labels, $totalSold),
168 ],
169 [
170 "items" => $profitGraphItems,
171 "config" => $this->getConfigByCode('PROFIT', $labels, $totalProfit)
172 ],
173 ];
174 }
175 private function getEmptyBasketItemsData(): array
176 {
177 return [
178 [
179 'BASKET_PRICE' => 0,
180 'COST_PRICE' => 0,
181 'DATE_DEDUCTED' => new DateTime(),
182 'BASKET_QUANTITY' => 0,
183 ]
184 ];
185 }
186 private function getConfigByCode(string $code, array $labels, float $total): array
187 {
188 return [
189 "groupsLabelMap" => $labels,
190 "reportTitle" => Loc::getMessage('GRAPH_HANDLER_BALLOON_SUBTITLE_' . $code),
191 "reportColor" => self::COLORS[$code],
192 "amount" => $this->formatAmountByCurrency($total),
193 "dateFormatForLabel" => $this->getDateFormatForLabel(),
194 "dateGrouping" => $this->getDateGrouping()
195 ];
196 }
197
198 private function formatAmountByCurrency(float $amount): string
199 {
200 $totalAmountFormatted = \CCurrencyLang::CurrencyFormat($amount, CurrencyManager::getBaseCurrency());
201
202 return str_replace("&nbsp;", " ", $totalAmountFormatted);
203 }
204
205 private function formatDateForLabel(Date $date)
206 {
207 return FormatDate($this->getDateFormatForLabel(), $date);
208 }
209
210 private function getDateFormatForLabel(): string
211 {
212 switch ($this->getDateGrouping())
213 {
214 case self::GROUP_DAY:
215 return Context::getCurrent()->getCulture()->getDayMonthFormat();
216
217 case self::GROUP_WEEK_DAY:
218 return "l";
219
220 case self::GROUP_MONTH:
221 return "f";
222 }
223
224 return Context::getCurrent()->getCulture()->getLongDateFormat();
225 }
226}
if($strVal !='') $priceFields
Определения options.php:1817
preparePrice(float $value, string $currency)
Определения profithandler.php:36
calculateProfitability(float $costPrice, float $profit)
Определения profithandler.php:142
static getProductsSoldPricesForDeductedPeriod(array $filter=[])
Определения storestocksale.php:153
Определения date.php:9
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$filter
Определения iblock_catalog_list.php:54
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
$value
Определения Param.php:39
Определения culture.php:9
$year
Определения payment.php:9
if(!function_exists("bx_hmac")) $amount
Определения payment.php:30