Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
editactionsitem.php
1<?php
2
4
12
13Loader::requireModule('iblock');
14
16{
17 protected function saveElement(int $id, array $fields): Result
18 {
19 $result = new Result();
20
21 [$elementFields, $productFields, $priceFields] = $this->splitProductFields($fields);
22
23 if (!empty($elementFields))
24 {
25 $result = parent::saveElement($id, $elementFields);
26 }
27
28 if ($result->isSuccess())
29 {
33 $product = ServiceContainer::getProductRepository($this->getIblockId())->getEntityById($id);
34 if ($product)
35 {
36 $product->setFields($productFields);
37
38 $sku = $product->getSkuCollection()->getFirst();
39 if ($sku)
40 {
41 $sku->getPriceCollection()->setValues($priceFields);
42 }
43
44 $result = $product->save();
45 }
46 else
47 {
51 $sku = ServiceContainer::getSkuRepository($this->getIblockId())->getEntityById($id);
52 if ($sku)
53 {
54 $sku->setFields($productFields);
55 $sku->getPriceCollection()->setValues($priceFields);
56
57 $result = $sku->save();
58 }
59 }
60 }
61
62 return $result;
63 }
64
70 private function splitProductFields(array $fields): array
71 {
72 $elementFields = [];
73 $productFields = [];
74 $priceFields = [];
75
76 $priceColumns = $this->getPriceColumns();
77 $productColumns = $this->getProductColumns();
78
79 foreach ($fields as $name => $value)
80 {
81 if (isset($productColumns[$name]))
82 {
83 if ($name === 'PURCHASING_PRICE')
84 {
85 if (is_array($value))
86 {
87 if (isset($value['PRICE']['VALUE']))
88 {
89 $productFields['PURCHASING_PRICE'] =
90 $value['PRICE']['VALUE'] === ''
91 ? null
92 : $value['PRICE']['VALUE']
93 ;
94 $productFields['PURCHASING_CURRENCY'] = $value['CURRENCY']['VALUE'] ?? null;
95 }
96 }
97 elseif ($value !== '')
98 {
99 $productFields[$name] = (float)$value;
100 }
101 }
102 else
103 {
104 $productFields[$name] = $value;
105 }
106 }
107 elseif (isset($priceColumns[$name]))
108 {
109 $priceTypeId = PriceProvider::parsePriceTypeId($name);
110 if (isset($priceTypeId))
111 {
112 $priceFields[$priceTypeId] = [
113 'PRICE' => $value['PRICE']['VALUE'] ?? null,
114 'CURRENCY' => $value['CURRENCY']['VALUE'] ?? null,
115 ];
116 }
117 }
118 else
119 {
120 $elementFields[$name] = $value;
121 }
122 }
123
124 return [$elementFields, $productFields, $priceFields];
125 }
126
130 private function getPriceColumns(): array
131 {
132 $result = [];
133
134 $availableColumnsIds = [];
135 foreach (GroupTable::getTypeList() as $type)
136 {
137 $id = PriceProvider::getPriceTypeColumnId($type['ID']);
138 $availableColumnsIds[$id] = true;
139 }
140
141 foreach ($this->getColumns() as $column)
142 {
143 $id = $column->getId();
144 if (isset($availableColumnsIds[$id]))
145 {
146 $result[$id] = true;
147 }
148 }
149
150 return $result;
151 }
152
156 private function getProductColumns(): array
157 {
158 $result = [];
159
160 $availableColumnsIds = array_fill_keys([
161 'VAT_ID',
162 'VAT_INCLUDED',
163 'MEASURE_RATIO',
164 'MEASURE',
165 'PURCHASING_PRICE',
166 'PURCHASING_CURRENCY',
167 'QUANTITY_TRACE',
168 'QUANTITY',
169 'QUANTITY_RESERVED',
170 'WIDTH',
171 'LENGTH',
172 'WEIGHT',
173 'HEIGHT',
174 'CAN_BUY_ZERO',
175 'BARCODE',
176 ], true);
177
178 foreach ($this->getColumns() as $column)
179 {
180 $id = $column->getId();
181 if (isset($availableColumnsIds[$id]))
182 {
183 $result[$id] = true;
184 }
185 }
186
187 return $result;
188 }
189}