1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
profithandler.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Integration\Report\Handler\StoreProfit;
4
5use Bitrix\Catalog\Integration\Report\Filter\StoreStockFilter;
6use Bitrix\Catalog\Integration\Report\Handler\BaseHandler;
7use Bitrix\Catalog\Integration\Report\StoreStock\StoreStockSale;
8use Bitrix\Currency\CurrencyManager;
9use Bitrix\Main\Loader;
10
11abstract class ProfitHandler extends BaseHandler
12{
13 private function getPriceAmounts(): array
14 {
16 $this->getFormattedFilter()
17 );
18 }
19
20 protected function preparePriceFields(array $fields, string $currency): array
21 {
22 $reportCurrency = $this->getBaseCurrency();
23 if (!$reportCurrency)
24 {
25 return $fields;
26 }
27
28 foreach ($fields as $key => $value)
29 {
30 $fields[$key] = \CCurrencyRates::convertCurrency($value, $currency, $reportCurrency);
31 }
32
33 return $fields;
34 }
35
36 protected function preparePrice(float $value, string $currency): float
37 {
38 $reportCurrency = $this->getBaseCurrency();
39 if (!$reportCurrency)
40 {
41 return $value;
42 }
43
44 return \CCurrencyRates::convertCurrency($value, $currency, $reportCurrency);
45 }
46
47 protected function getBaseCurrency(): ?string
48 {
49 if (!Loader::includeModule('currency'))
50 {
51 return null;
52 }
53
54 return CurrencyManager::getBaseCurrency();
55 }
56
57 protected function getStoreTotals(): array
58 {
59 $storeTotals = parent::getStoreTotals();
60 if (empty($storeTotals))
61 {
62 return $storeTotals;
63 }
64
65 $priceAmounts = $this->getPriceAmounts();
66
67 $formattedTotalsByPrices = $storeTotals;
68 foreach ($storeTotals as $storeId => $storeTotal)
69 {
70 $total = [
71 'TOTAL_SOLD' => 0,
72 'TOTAL_COST_PRICE' => 0
73 ];
74 $storeTotal['TOTALS'] ??= [];
75 foreach ($storeTotal['TOTALS'] as $measureId => $fields)
76 {
77 foreach ($fields as $fieldId => $value)
78 {
79 $total[$fieldId][$measureId] = $value;
80 }
81 }
82
83 $priceAmounts[$storeId] ??= [];
84 foreach ($priceAmounts[$storeId] as $currency => $fields)
85 {
87 $total['TOTAL_SOLD'] ??= 0;
88 $total['TOTAL_SOLD'] += $fields['TOTAL_SOLD'];
89 $total['TOTAL_COST_PRICE'] ??= 0;
90 $total['TOTAL_COST_PRICE'] += $fields['COST_PRICE'];
91 }
92
93 $total['PROFIT'] = $total['TOTAL_SOLD'] - $total['TOTAL_COST_PRICE'];
94 $total['PROFITABILITY'] = $this->calculateProfitability((float)$total['TOTAL_COST_PRICE'], (float)$total['PROFIT']);
95 $formattedTotalsByPrices[$storeId]['TOTALS'] = $total;
96 }
97
98 return $formattedTotalsByPrices;
99 }
100
101 protected function prepareOverallTotals(array $storeTotals): array
102 {
103 $overallTotals = [
104 'STARTING_QUANTITY' => [],
105 'RECEIVED_QUANTITY' => [],
106 'AMOUNT_SUM' => [],
107 'QUANTITY_RESERVED_SUM' => [],
108 'QUANTITY' => [],
109 'AMOUNT_SOLD' => [],
110 'TOTAL_SOLD' => 0,
111 'TOTAL_COST_PRICE' => 0,
112 'PROFIT' => 0,
113 'PROFITABILITY' => 0,
114 ];
115
116 foreach ($storeTotals as $storeTotalEntry)
117 {
118 foreach ($storeTotalEntry['TOTALS'] as $fieldName => $value)
119 {
120 if ($value && is_array($value))
121 {
122 $overallTotals[$fieldName] ??= [];
123 foreach ($value as $measureId => $measureItemValue)
124 {
125 $overallTotals[$fieldName][$measureId] ??= 0.0;
126 $overallTotals[$fieldName][$measureId] += $measureItemValue;
127 }
128 }
129 else
130 {
131 $overallTotals[$fieldName] ??= 0.0;
132 $overallTotals[$fieldName] += (float)$value;
133 }
134 }
135 }
136
137 $overallTotals['PROFITABILITY'] = $this->calculateProfitability($overallTotals['TOTAL_COST_PRICE'], $overallTotals['PROFIT']);
138
139 return $overallTotals;
140 }
141
142 protected function calculateProfitability(float $costPrice, float $profit): ?float
143 {
144 if ($costPrice <= 0 && $profit <= 0)
145 {
146 return null;
147 }
148
149 $profitability =
150 $costPrice > 0
151 ? round($profit / $costPrice, 4)
152 : 0
153 ;
154
155 return $profitability * 100;
156 }
157
158 protected function getProductFilter(array $productFilter): array
159 {
160 return ['=PRODUCT_ID' => StoreStockFilter::prepareProductFilter($productFilter)];
161 }
162}
preparePrice(float $value, string $currency)
Определения profithandler.php:36
preparePriceFields(array $fields, string $currency)
Определения profithandler.php:20
calculateProfitability(float $costPrice, float $profit)
Определения profithandler.php:142
static getProductsSoldPricesForStores(array $filter=[])
Определения storestocksale.php:78
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$currency
Определения template.php:266
$fields
Определения yandex_run.php:501