Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderarchiveitem.php
1<?php
3
10
11Loc::loadMessages(__FILE__);
12
17{
18 private $order = null;
19 private $orderDataFields = [];
20 private $basketDataFields = [];
21
27 public function __construct(Sale\Order $order)
28 {
29 $this->order = $order;
30 $this->orderDataFields['ORDER'] = $order->getFieldValues();
31 }
32
36 public function getId()
37 {
38 return $this->order->getId();
39 }
40
45 public function addOrderDataField($name, array $value)
46 {
47 if ($name !== 'ORDER')
48 {
49 $this->orderDataFields[$name] = $value;
50 }
51 }
52
56 public function addBasketDataFields(array $value)
57 {
58 $this->basketDataFields = $value;
59 }
60
64 public function archive()
65 {
66 $r = $this->callEventOnBeforeOrderArchived($this->order);
67 if (!$r->isSuccess())
68 {
69 return $r;
70 }
71
72 $result = $this->saveOrderArchive();
73 if ($result->isSuccess())
74 {
75 $archivedOrderId = $result->getId();
76 $basketItemIdList = array();
77 foreach ($this->basketDataFields as $basketItem)
78 {
79 $additionBasketResult = $this->saveBasketItemArchive($basketItem, $archivedOrderId);
80 if ($additionBasketResult->isSuccess())
81 {
82 $basketItemIdList[] = $additionBasketResult->getId();
83 }
84 else
85 {
86 $result->addErrors($additionBasketResult->getErrors());
87 break;
88 }
89 }
90 }
91
92 if ($result->isSuccess())
93 {
94 $this->tryUnreserveShipments();
95
96 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
98 $orderClass = $registry->getOrderClassName();
99
100 $orderClass::deleteNoDemand($this->getId());
101 }
102 else
103 {
104 if (!empty($archivedOrderId))
105 {
106 Internals\OrderArchiveTable::delete($archivedOrderId);
107 }
108
109 if (!empty($basketItemIdList) && is_array($basketItemIdList))
110 {
111 foreach ($basketItemIdList as $archivedBasketItemId)
112 {
113 Internals\BasketArchiveTable::delete($archivedBasketItemId);
114 }
115 }
116 }
117
118 return $result;
119 }
120
126 private function callEventOnBeforeOrderArchived(Sale\Order $order)
127 {
128 $result = new Main\Result();
129
130 $eventManager = Main\EventManager::getInstance();
131 if ($eventsList = $eventManager->findEventHandlers('sale', Sale\EventActions::EVENT_ON_ORDER_BEFORE_ARCHIVED))
132 {
134 $event = new Main\Event('sale', Sale\EventActions::EVENT_ON_ORDER_BEFORE_ARCHIVED, array(
135 'ENTITY' => $order
136 ));
137 $event->send();
138
139 if ($event->getResults())
140 {
142 foreach($event->getResults() as $eventResult)
143 {
144 if($eventResult->getType() == Main\EventResult::ERROR)
145 {
146 $errorMsg = new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR'), 'SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR');
147 if ($eventResultData = $eventResult->getParameters())
148 {
149 if (isset($eventResultData) && $eventResultData instanceof Sale\ResultError)
150 {
152 $errorMsg = $eventResultData;
153 }
154 }
155
156 $result->addError($errorMsg);
157 }
158 }
159 }
160 }
161
162 return $result;
163 }
164
168 private function tryUnreserveShipments()
169 {
170 $shipmentData = $this->getOrderDataField('SHIPMENT');
171 foreach ($shipmentData as $shipment)
172 {
173 if ($shipment["RESERVED"] == "Y" && $shipment["DEDUCTED"] == "N")
174 {
175 if ($shipmentCollection = $this->order->getShipmentCollection())
176 {
177 foreach ($shipmentCollection as $shipmentItem)
178 {
179 $shipmentItem->tryUnreserve();
180 }
181
182 $this->order->save();
183 }
184
185 break;
186 }
187 }
188 }
189
193 private function saveOrderArchive()
194 {
195 $preparedOrderData = array_intersect_key($this->getOrderDataField('ORDER'), array_flip(Manager::getOrderFieldNames()));
196 $preparedOrderData['ORDER_ID'] = $this->getId();
197 $preparedOrderData['DATE_ARCHIVED'] = new Type\DateTime();
198 $preparedOrderData['VERSION'] = Manager::SALE_ARCHIVE_VERSION;
199 $preparedFields = $this->prepareEncodeFields($this->orderDataFields);
200 $preparedOrderData['ORDER_DATA'] = Main\Web\Json::encode($preparedFields);
201 return Internals\OrderArchiveTable::add($preparedOrderData);
202 }
203
204 private function prepareEncodeFields(array $fields)
205 {
206 foreach ($fields as &$field)
207 {
208 if (is_array($field))
209 {
210 $field = $this->prepareEncodeFields($field);
211 }
212 elseif ($field instanceof Type\Date)
213 {
214 \CTimeZone::Disable();
215 $field = $field->toString();
216 \CTimeZone::Enable();
217 }
218 }
219
220 return $fields;
221 }
222
229 private function saveBasketItemArchive(array $item, $archivedOrderId)
230 {
231 $preparedBasketItems = array_intersect_key($item, array_flip(Manager::getBasketFieldNames()));
232 $preparedBasketItems['ARCHIVE_ID'] = (int)$archivedOrderId;
233 $preparedFields = $this->prepareEncodeFields($item);
234 $preparedBasketItems['BASKET_DATA'] = Main\Web\Json::encode($preparedFields);
235
236 if (empty($preparedBasketItems['DATE_INSERT']))
237 {
238 $zeroDate = new \DateTime();
239 $zeroDate->setDate(0,0,0);
240 $zeroDate->setTime(0,0,0);
241 $preparedBasketItems['DATE_INSERT'] = Type\DateTime::createFromPhp($zeroDate);
242 }
243
244 return Internals\BasketArchiveTable::add($preparedBasketItems);
245 }
246
247 private function getOrderDataField($name)
248 {
249 return $this->orderDataFields[$name];
250 }
251
252}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29