Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
storestockquantity.php
1<?php
2
4
10
13{
14 protected const DEFAULT_DATE_INTERVAL = '-30D';
15
22 public static function getOutgoingQuantityForStores(array $userFilter = []): array
23 {
24 $receivedQuantity = self::getIncomingOutgoingQuantitiesFromDocuments($userFilter);
25
26 $reduceCallback = static function($result, $current)
27 {
28 $measureId = (int)$current['MEASURE_ID'] ?: \CCatalogMeasure::getDefaultMeasure(true)['ID'];
29 if (!array_key_exists($measureId, $result))
30 {
31 $result[$measureId] = 0.0;
32 }
33
34 $result[$measureId] += $current['AMOUNT']['OUTGOING'];
35 return $result;
36 };
37
38 foreach ($receivedQuantity as $storeId => $productEntry)
39 {
40 $receivedQuantity[$storeId] = array_reduce($productEntry, $reduceCallback, []);
41 }
42
43 return $receivedQuantity;
44 }
45
53 public static function getOutgoingQuantityForProductsOnStore(int $storeId, array $userFilter = []): array
54 {
55 $userFilter['STORES'] = $storeId;
56
57 $outgoingQuantity = self::getIncomingOutgoingQuantitiesFromDocuments($userFilter)[$storeId] ?? [];
58
59 $mapCallback = static function ($entry)
60 {
61 return $entry['AMOUNT']['OUTGOING'];
62 };
63
64 return array_map($mapCallback, $outgoingQuantity);
65 }
66
73 public static function getReceivedQuantityForStores(array $userFilter = []): array
74 {
75 $receivedQuantity = self::getIncomingOutgoingQuantitiesFromDocuments($userFilter);
76
77 $reduceCallback = static function($result, $current)
78 {
79 $measureId = (int)$current['MEASURE_ID'] ?: \CCatalogMeasure::getDefaultMeasure(true)['ID'];
80 if (!array_key_exists($measureId, $result))
81 {
82 $result[$measureId] = 0.0;
83 }
84
85 $result[$measureId] += $current['AMOUNT']['INCOMING'];
86 return $result;
87 };
88
89 foreach ($receivedQuantity as $storeId => $productEntry)
90 {
91 $receivedQuantity[$storeId] = array_reduce($productEntry, $reduceCallback, []);
92 }
93
94 return $receivedQuantity;
95 }
96
105 public static function getReceivedQuantityForProductsOnStore(int $storeId, array $userFilter = []): array
106 {
107 $userFilter['STORES'] = $storeId;
108
109 $receivedQuantity = self::getIncomingOutgoingQuantitiesFromDocuments($userFilter)[$storeId] ?? [];
110
111 $mapCallback = static function ($entry)
112 {
113 return $entry['AMOUNT']['INCOMING'];
114 };
115
116 return array_map($mapCallback, $receivedQuantity);
117 }
118
128 private static function getIncomingOutgoingQuantitiesFromDocuments(array $userFilter = []): array
129 {
130 $userFilter = self::prepareFilter($userFilter);
131
132 $productsDataList = self::getDocumentProductsDataList($userFilter);
133
134 $result = [];
135
136 while ($entry = $productsDataList->fetch())
137 {
138 $storeFromId = (int)$entry['STORE_FROM'];
139 $storeToId = (int)$entry['STORE_TO'];
140
141 if (!isset($result[$storeFromId]))
142 {
143 $result[$storeFromId] = [];
144 }
145 if (!isset($result[$storeToId]))
146 {
147 $result[$storeToId] = [];
148 }
149
150 $productId = $entry['ELEMENT_ID'];
151 if (!array_key_exists($productId, $result[$storeToId]))
152 {
153 $result[$storeToId][$productId] = [
154 'MEASURE_ID' => (int)$entry['MEASURE_ID'] ?: \CCatalogMeasure::getDefaultMeasure(true)['ID'],
155 'AMOUNT' => [
156 'INCOMING' => 0.0,
157 'OUTGOING' => 0.0,
158 ],
159 ];
160 }
161
162 if ($storeFromId > 0)
163 {
164 $result[$storeFromId][$productId]['AMOUNT']['OUTGOING'] += (float)$entry['AMOUNT_SUM'];
165 }
166
167 if ($storeToId > 0)
168 {
169 $result[$storeToId][$productId]['AMOUNT']['INCOMING'] += (float)$entry['AMOUNT_SUM'];
170 }
171 }
172
173 return $result;
174 }
175
180 private static function getDocumentProductsDataList(array $userFilter = []): Result
181 {
182 $receivedQuantityQuery = new Query(StoreDocumentElementTable::getEntity());
183 $receivedQuantityQuery->setSelect([
184 'DOCUMENT.DOC_TYPE',
185 'STORE_FROM',
186 'STORE_TO',
187 'ELEMENT_ID',
188 'AMOUNT_SUM',
189 'MEASURE_ID' => 'PRODUCT.MEASURE'
190 ]);
191
192 $filter = [
193 '=DOCUMENT.STATUS' => 'Y',
194 ];
195
196 if (isset($userFilter['PRODUCTS']))
197 {
198 $filter['=ELEMENT_ID'] = $userFilter['PRODUCTS'];
199 }
200
201 if (isset($userFilter['STORES']))
202 {
203 $filter[] = [
204 'LOGIC' => 'OR',
205 '=STORE_TO' => $userFilter['STORES'],
206 '=STORE_FROM' => $userFilter['STORES'],
207 ];
208 }
209
210 if (isset($userFilter['REPORT_INTERVAL']))
211 {
212 $filter['>=DOCUMENT.DATE_STATUS'] = new DateTime($userFilter['REPORT_INTERVAL']['FROM']);
213 $filter['<=DOCUMENT.DATE_STATUS'] = new DateTime($userFilter['REPORT_INTERVAL']['TO']);
214 }
215
216 $receivedQuantityQuery->setFilter($filter);
217 $receivedQuantityQuery->setGroup(['STORE_FROM', 'STORE_TO', 'ELEMENT_ID']);
218 $receivedQuantityQuery->registerRuntimeField(
219 new ExpressionField('AMOUNT_SUM', 'SUM(de.AMOUNT)')
220 );
221 $receivedQuantityQuery->setCustomBaseTableAlias('de');
222
223 return $receivedQuantityQuery->exec();
224 }
225
229 private static function getDefaultReportInterval(): array
230 {
231 $currentDate = new DateTime();
232 $intervalStartDate = new DateTime();
233 $intervalStartDate->add(self::DEFAULT_DATE_INTERVAL);
234
235 return [
236 'FROM' => $intervalStartDate->toString(),
237 'TO' => $currentDate->toString(),
238 ];
239 }
240
245 private static function prepareFilter(array $filter): array
246 {
247 if (!isset($filter['REPORT_INTERVAL']['FROM'], $filter['REPORT_INTERVAL']['TO']))
248 {
249 $filter['REPORT_INTERVAL'] = self::getDefaultReportInterval();
250 }
251
252 return $filter;
253 }
254}
static getReceivedQuantityForProductsOnStore(int $storeId, array $userFilter=[])
static getOutgoingQuantityForProductsOnStore(int $storeId, array $userFilter=[])