Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PriceRepository.php
1<?php
2
4
12
22{
24 protected $factory;
25
27 {
28 $this->factory = $factory;
29 }
30
31 public function getEntityById(int $id): ?BaseEntity
32 {
33 if ($id <= 0)
34 {
35 throw new \OutOfRangeException($id);
36 }
37
38 $entities = $this->getEntitiesBy([
39 'filter' => [
40 '=ID' => $id,
41 ],
42 ]);
43
44 return reset($entities) ?: null;
45 }
46
47 public function getEntitiesBy($params): array
48 {
49 $entities = [];
50
51 foreach ($this->getList((array)$params) as $item)
52 {
53 $entities[] = $this->createEntity($item);
54 }
55
56 return $entities;
57 }
58
59 public function getProductId(BaseEntity $entity): ?int
60 {
61 $id = null;
62
63 $parent = $entity->getParent();
64
65 if ($parent && !$parent->isNew())
66 {
67 $id = $parent->getId();
68 }
69
70 return $id;
71 }
72
73 public function save(BaseEntity ...$entities): Result
74 {
75 $result = new Result();
76
78 foreach ($entities as $entity)
79 {
80 if (!$entity->hasPrice())
81 {
82 if (!$entity->isNew())
83 {
84 $res = $this->deleteInternal($entity->getId());
85
86 if ($res->isSuccess())
87 {
88 $entity->setField('ID', null);
89 }
90 else
91 {
92 $result->addErrors($res->getErrors());
93 }
94 }
95
96 continue;
97 }
98
99 if (!$entity->getProductId())
100 {
101 $productId = $this->getProductId($entity);
102
103 if ($productId)
104 {
105 $entity->setProductId($productId);
106 }
107 else
108 {
109 $result->addError(new Error('Wrong product id'));
110 continue;
111 }
112 }
113
114 if ($entityId = $entity->getId())
115 {
116 $res = $this->updateInternal($entityId, $entity->getChangedFields());
117
118 if (!$res->isSuccess())
119 {
120 $result->addErrors($res->getErrors());
121 }
122 }
123 else
124 {
125 $res = $this->addInternal($entity->getFields());
126
127 if ($res->isSuccess())
128 {
129 $entity->setId($res->getData()['ID']);
130 }
131 else
132 {
133 $result->addErrors($res->getErrors());
134 }
135 }
136 }
137
138 return $result;
139 }
140
141 public function delete(BaseEntity ...$entities): Result
142 {
143 $result = new Result();
144
146 foreach ($entities as $entity)
147 {
148 if ($entityId = $entity->getId())
149 {
150 $res = $this->deleteInternal($entityId);
151
152 if (!$res->isSuccess())
153 {
154 $result->addErrors($res->getErrors());
155 }
156 }
157 }
158
159 return $result;
160 }
161
163 {
164 if ($sku->isNew())
165 {
166 return $this->createCollection();
167 }
168
169 $result = $this->getByProductId($sku->getId());
170
171 return $this->createCollection($result);
172 }
173
174 protected function getByProductId(int $skuId): array
175 {
176 return $this->getList([
177 'filter' => [
178 '=PRODUCT_ID' => $skuId,
179 ],
180 ]);
181 }
182
183 protected function getList(array $params): array
184 {
185 $prices = PriceTable::getList($params)->fetchAll();
186
187 return array_column($prices, null, 'CATALOG_GROUP_ID');
188 }
189
190 protected function createEntity(array $fields = []): BasePrice
191 {
192 $entity = $this->factory->createEntity();
193
194 $entity->initFields($fields);
195
196 return $entity;
197 }
198
199 protected function createCollection(array $entityFields = []): PriceCollection
200 {
201 $collection = $this->factory->createCollection();
202
203 foreach ($this->getPriceSettings() as $settings)
204 {
205 $fields = $entityFields[$settings['ID']]
206 ?? [
207 'CATALOG_GROUP_ID' => $settings['ID'],
208 ];
209 $price = $this->createEntity($fields);
210 $price->setSettings($settings);
211 $collection->add($price);
212 }
213
214 return $collection;
215 }
216
217 protected function addInternal(array $fields): Result
218 {
219 $result = new Result();
220
221 // ToDo external_fields and actions?
222 $res = Price::add([
223 'fields' => $fields,
224 // 'external_fields' => [
225 // 'IBLOCK_ID' => $destinationPrice['ELEMENT_IBLOCK_ID']
226 // ],
227 // 'actions' => [
228 // 'RECOUNT_PRICES' => true
229 // ],
230 ]);
231
232 if ($res->isSuccess())
233 {
234 $result->setData(['ID' => $res->getId()]);
235 }
236 else
237 {
238 $result->addErrors($res->getErrors());
239 }
240
241 return $result;
242 }
243
244 protected function updateInternal(int $id, array $fields): Result
245 {
246 $result = new Result();
247
248 // ToDo external_fields and actions?
249 $res = Price::update($id, [
250 'fields' => $fields,
251 // 'external_fields' => [
252 // 'IBLOCK_ID' => $destinationPrice['ELEMENT_IBLOCK_ID']
253 // ],
254 // 'actions' => [
255 // 'RECOUNT_PRICES' => true
256 // ],
257 ]);
258
259 if (!$res->isSuccess())
260 {
261 $result->addErrors($res->getErrors());
262 }
263
264 return $result;
265 }
266
267 protected function deleteInternal(int $id): Result
268 {
269 $result = new Result();
270
271 $res = Price::delete($id);
272
273 if (!$res->isSuccess())
274 {
275 $result->addErrors($res->getErrors());
276 }
277
278 return $result;
279 }
280
281 private function getPriceSettings(): array
282 {
283 static $priceSettings = null;
284
285 if ($priceSettings === null)
286 {
287 $priceSettings = GroupTable::getList()->fetchAll();
288 }
289
290 return $priceSettings;
291 }
292}
static getList(array $parameters=array())
static update($primary, array $data)
delete(BaseEntity ... $entities)
save(BaseEntity ... $entities)