Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
profithandler.php
1<?php
2
4
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 getBaseCurrency(): ?string
37 {
38 if (!Loader::includeModule('currency'))
39 {
40 return null;
41 }
42
43 return CurrencyManager::getBaseCurrency();
44 }
45
46 protected function getStoreTotals(): array
47 {
48 $storeTotals = parent::getStoreTotals();
49 if (empty($storeTotals))
50 {
51 return $storeTotals;
52 }
53
54 $priceAmounts = $this->getPriceAmounts();
55
56 $formattedTotalsByPrices = $storeTotals;
57 foreach ($storeTotals as $storeId => $storeTotal)
58 {
59 $total = [
60 'TOTAL_SOLD' => 0,
61 'TOTAL_COST_PRICE' => 0
62 ];
63 $storeTotal['TOTALS'] ??= [];
64 foreach ($storeTotal['TOTALS'] as $measureId => $fields)
65 {
66 foreach ($fields as $fieldId => $value)
67 {
68 $total[$fieldId][$measureId] = $value;
69 }
70 }
71
72 $priceAmounts[$storeId] ??= [];
73 foreach ($priceAmounts[$storeId] as $currency => $fields)
74 {
75 $fields = $this->preparePriceFields($fields, $currency);
76 $total['TOTAL_SOLD'] ??= 0;
77 $total['TOTAL_SOLD'] += $fields['TOTAL_SOLD'];
78 $total['TOTAL_COST_PRICE'] ??= 0;
79 $total['TOTAL_COST_PRICE'] += $fields['COST_PRICE'];
80 }
81
82 $total['PROFIT'] = $total['TOTAL_SOLD'] - $total['TOTAL_COST_PRICE'];
83 $total['PROFITABILITY'] = $this->calculateProfitability((float)$total['TOTAL_COST_PRICE'], (float)$total['PROFIT']);
84 $formattedTotalsByPrices[$storeId]['TOTALS'] = $total;
85 }
86
87 return $formattedTotalsByPrices;
88 }
89
90 protected function prepareOverallTotals(array $storeTotals): array
91 {
92 $overallTotals = [
93 'STARTING_QUANTITY' => [],
94 'RECEIVED_QUANTITY' => [],
95 'AMOUNT_SUM' => [],
96 'QUANTITY_RESERVED_SUM' => [],
97 'QUANTITY' => [],
98 'AMOUNT_SOLD' => [],
99 'TOTAL_SOLD' => 0,
100 'TOTAL_COST_PRICE' => 0,
101 'PROFIT' => 0,
102 'PROFITABILITY' => 0,
103 ];
104
105 foreach ($storeTotals as $storeTotalEntry)
106 {
107 foreach ($storeTotalEntry['TOTALS'] as $fieldName => $value)
108 {
109 if ($value && is_array($value))
110 {
111 $overallTotals[$fieldName] ??= [];
112 foreach ($value as $measureId => $measureItemValue)
113 {
114 $overallTotals[$fieldName][$measureId] ??= 0.0;
115 $overallTotals[$fieldName][$measureId] += $measureItemValue;
116 }
117 }
118 else
119 {
120 $overallTotals[$fieldName] ??= 0.0;
121 $overallTotals[$fieldName] += (float)$value;
122 }
123 }
124 }
125
126 $overallTotals['PROFITABILITY'] = $this->calculateProfitability($overallTotals['TOTAL_COST_PRICE'], $overallTotals['PROFIT']);
127
128 return $overallTotals;
129 }
130
131 protected function calculateProfitability(float $costPrice, float $profit): ?float
132 {
133 if ($costPrice <= 0 && $profit <= 0)
134 {
135 return null;
136 }
137
138 $profitability =
139 $costPrice > 0
140 ? round($profit / $costPrice, 4)
141 : 0
142 ;
143
144 return $profitability * 100;
145 }
146
147 protected function getProductFilter(array $productFilter): array
148 {
149 return ['=PRODUCT_ID' => StoreStockFilter::prepareProductFilter($productFilter)];
150 }
151}