18 private $order =
null;
19 private $orderDataFields = [];
20 private $basketDataFields = [];
29 $this->order = $order;
30 $this->orderDataFields[
'ORDER'] = $order->getFieldValues();
38 return $this->order->getId();
47 if ($name !==
'ORDER')
49 $this->orderDataFields[$name] = $value;
58 $this->basketDataFields = $value;
64 public function archive()
66 $r = $this->callEventOnBeforeOrderArchived($this->order);
72 $result = $this->saveOrderArchive();
73 if ($result->isSuccess())
75 $archivedOrderId = $result->getId();
76 $basketItemIdList = array();
77 foreach ($this->basketDataFields as $basketItem)
79 $additionBasketResult = $this->saveBasketItemArchive($basketItem, $archivedOrderId);
80 if ($additionBasketResult->isSuccess())
82 $basketItemIdList[] = $additionBasketResult->getId();
86 $result->addErrors($additionBasketResult->getErrors());
92 if ($result->isSuccess())
94 $this->tryUnreserveShipments();
98 $orderClass = $registry->getOrderClassName();
100 $orderClass::deleteNoDemand($this->
getId());
104 if (!empty($archivedOrderId))
106 Internals\OrderArchiveTable::delete($archivedOrderId);
109 if (!empty($basketItemIdList) && is_array($basketItemIdList))
111 foreach ($basketItemIdList as $archivedBasketItemId)
113 Internals\BasketArchiveTable::delete($archivedBasketItemId);
126 private function callEventOnBeforeOrderArchived(
Sale\Order $order)
128 $result =
new Main\Result();
130 $eventManager = Main\EventManager::getInstance();
139 if ($event->getResults())
142 foreach($event->getResults() as $eventResult)
144 if($eventResult->getType() == Main\EventResult::ERROR)
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())
149 if (isset($eventResultData) && $eventResultData instanceof
Sale\ResultError)
152 $errorMsg = $eventResultData;
156 $result->addError($errorMsg);
168 private function tryUnreserveShipments()
170 $shipmentData = $this->getOrderDataField(
'SHIPMENT');
171 foreach ($shipmentData as $shipment)
173 if ($shipment[
"RESERVED"] ==
"Y" && $shipment[
"DEDUCTED"] ==
"N")
175 if ($shipmentCollection = $this->order->getShipmentCollection())
177 foreach ($shipmentCollection as $shipmentItem)
179 $shipmentItem->tryUnreserve();
182 $this->order->save();
193 private function saveOrderArchive()
196 $preparedOrderData[
'ORDER_ID'] = $this->
getId();
199 $preparedFields = $this->prepareEncodeFields($this->orderDataFields);
200 $preparedOrderData[
'ORDER_DATA'] = Main\Web\Json::encode($preparedFields);
201 return Internals\OrderArchiveTable::add($preparedOrderData);
204 private function prepareEncodeFields(array $fields)
206 foreach ($fields as &$field)
208 if (is_array($field))
210 $field = $this->prepareEncodeFields($field);
212 elseif ($field instanceof
Type\Date)
214 \CTimeZone::Disable();
215 $field = $field->toString();
216 \CTimeZone::Enable();
229 private function saveBasketItemArchive(array $item, $archivedOrderId)
232 $preparedBasketItems[
'ARCHIVE_ID'] = (int)$archivedOrderId;
233 $preparedFields = $this->prepareEncodeFields($item);
234 $preparedBasketItems[
'BASKET_DATA'] = Main\Web\Json::encode($preparedFields);
236 if (empty($preparedBasketItems[
'DATE_INSERT']))
238 $zeroDate = new \DateTime();
239 $zeroDate->setDate(0,0,0);
240 $zeroDate->setTime(0,0,0);
241 $preparedBasketItems[
'DATE_INSERT'] = Type\DateTime::createFromPhp($zeroDate);
244 return Internals\BasketArchiveTable::add($preparedBasketItems);
247 private function getOrderDataField($name)
249 return $this->orderDataFields[$name];
static getMessage($code, $replace=null, $language=null)