Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tradebindingentity.php
1<?php
2
3namespace Bitrix\Sale;
4
7
13{
14 private $tradePlatform = null;
15
19 public static function getRegistryEntity()
20 {
22 }
23
27 public static function getRegistryType()
28 {
30 }
31
35 public static function getAvailableFields()
36 {
37 return [
38 'ORDER_ID', 'EXTERNAL_ORDER_ID',
39 'TRADING_PLATFORM_ID', 'PARAMS', 'XML_ID'
40 ];
41 }
42
46 protected static function getMeaningfulFields()
47 {
48 return array();
49 }
50
54 protected static function getFieldsMap()
55 {
56 return TradingPlatform\OrderTable::getMap();
57 }
58
66 public static function create(TradeBindingCollection $collection, TradingPlatform\Platform $platform = null)
67 {
69 $entity = static::createEntityObject();
70
71 $entity->setCollection($collection);
72
73 if ($platform !== null)
74 {
75 $entity->setFieldNoDemand('TRADING_PLATFORM_ID', $platform->getId());
76 $entity->tradePlatform = $platform;
77 }
78
79 $entity->setFieldNoDemand('XML_ID', static::generateXmlId());
80
81 return $entity;
82 }
83
87 protected static function generateXmlId()
88 {
89 return uniqid('bx_');
90 }
91
98 private static function createEntityObject(array $fields = array())
99 {
100 $registry = Registry::getInstance(static::getRegistryType());
101 $entityClassName = $registry->get(Registry::ENTITY_TRADE_BINDING_ENTITY);
102
103 return new $entityClassName($fields);
104 }
105
114 public static function loadForOrder($id)
115 {
116 if (intval($id) <= 0)
117 {
118 throw new Main\ArgumentNullException("id");
119 }
120
121 $registry = Registry::getInstance(static::getRegistryType());
122
124 $tradeBindingCollection = $registry->get(Registry::ENTITY_TRADE_BINDING_COLLECTION);
125 $dbRes = $tradeBindingCollection::getList([
126 'filter' => ['ORDER_ID' => $id]
127 ]);
128
129 $entityList = [];
130 while ($data = $dbRes->fetch())
131 {
132 $entityList[] = static::createEntityObject($data);
133 }
134
135 return $entityList;
136 }
137
142 public function save()
143 {
144 $result = new Result();
145
146 if (!$this->isChanged())
147 {
148 return $result;
149 }
150
151 $id = $this->getId();
152
153 if ($id > 0)
154 {
155 $fields = $this->getFields()->getChangedValues();
156 $r = $this->updateInternal($id, $fields);
157 $result->setId($r->getId());
158 }
159 else
160 {
162 $collection = $this->getCollection();
163
165 $order = $collection->getOrder();
166
167 $this->setFieldNoDemand('ORDER_ID', $order->getId());
168
169 if ((int)$this->getField('EXTERNAL_ORDER_ID') <= 0)
170 {
171 $this->setFieldNoDemand('EXTERNAL_ORDER_ID', $order->getId());
172 }
173
174 $fields = $this->getFields()->getValues();
175 $r = $this->addInternal($fields);
176 if ($r->isSuccess())
177 {
178 $id = $r->getId();
179 $this->setFieldNoDemand('ID', $id);
180 }
181 }
182
183 if (!$r->isSuccess())
184 {
185 $result->addErrors($r->getErrors());
186 return $result;
187 }
188
189 $result->setId($id);
190
191 return $result;
192 }
193
201 public function getTradePlatform()
202 {
203 if ($this->tradePlatform === null)
204 {
205 if ($this->getField('TRADING_PLATFORM_ID') > 0)
206 {
207 $dbRes = TradingPlatformTable::getList([
208 'select' => ['CODE'],
209 'filter' => [
210 '=ID' => $this->getField('TRADING_PLATFORM_ID')
211 ]
212 ]);
213
214 if ($item = $dbRes->fetch())
215 {
216 $this->tradePlatform = TradingPlatform\Landing\Landing::getInstanceByCode($item['CODE']);
217 }
218 }
219 }
220
221 return $this->tradePlatform;
222 }
223
230 protected function updateInternal($primary, array $data)
231 {
232 return TradingPlatform\OrderTable::update($primary, $data);
233 }
234
240 protected function addInternal(array $data)
241 {
242 return TradingPlatform\OrderTable::add($data);
243 }
244
250 public static function getEntityEventName()
251 {
252 return 'SaleTradeBindingEntity';
253 }
254
261 public function setFieldNoDemand($name, $value)
262 {
263 parent::setFieldNoDemand($name, $value);
264
265 if ($name === 'TRADING_PLATFORM_ID')
266 {
267 $this->tradePlatform = null;
268 }
269 }
270
271 protected function onFieldModify($name, $oldValue, $value)
272 {
273 $result = parent::onFieldModify($name, $oldValue, $value);
274 if (!$result->isSuccess())
275 {
276 return $result;
277 }
278
279 if ($name === 'TRADING_PLATFORM_ID')
280 {
281 $this->tradePlatform = null;
282 }
283
284 return $result;
285 }
286}
const ENTITY_TRADE_BINDING_COLLECTION
Definition registry.php:55
static getInstance($type)
Definition registry.php:183
const ENTITY_TRADE_BINDING_ENTITY
Definition registry.php:56
onFieldModify($name, $oldValue, $value)
updateInternal($primary, array $data)