76 parent::__construct($fields);
78 $this->
isNew = (empty($fields[
'ID']));
89 if ($this->internalId == 0)
92 $this->internalId = $idPool;
106 "LID",
"PERSON_TYPE_ID",
"CANCELED",
"DATE_CANCELED",
107 "EMP_CANCELED_ID",
"REASON_CANCELED",
"STATUS_ID",
"DATE_STATUS",
"EMP_STATUS_ID",
"DEDUCTED",
108 "MARKED",
"DATE_MARKED",
"EMP_MARKED_ID",
"REASON_MARKED",
109 "PRICE",
"CURRENCY",
"DISCOUNT_VALUE",
"USER_ID",
110 "DATE_INSERT",
"DATE_UPDATE",
"USER_DESCRIPTION",
"ADDITIONAL_INFO",
"COMMENTS",
"TAX_VALUE",
111 "STAT_GID",
"RECURRING_ID",
"LOCKED_BY",
"IS_RECURRING",
112 "DATE_LOCK",
"RECOUNT_FLAG",
"AFFILIATE_ID",
"DELIVERY_DOC_NUM",
"DELIVERY_DOC_DATE",
"UPDATED_1C",
113 "STORE_ID",
"ORDER_TOPIC",
"RESPONSIBLE_ID",
"DATE_BILL",
"DATE_PAY_BEFORE",
"ACCOUNT_NUMBER",
114 "XML_ID",
"ID_1C",
"VERSION_1C",
"VERSION",
"EXTERNAL_ORDER",
"COMPANY_ID",
"IS_SYNC_B24"
117 return array_merge($result, static::getCalculatedFields());
128 'PRICE_WITHOUT_DISCOUNT',
131 'BASE_PRICE_DELIVERY',
134 'DELIVERY_LOCATION_ZIP',
159 return array(
'PERSON_TYPE_ID',
'PRICE');
168 private static function createOrderObject(array $fields = array())
171 $orderClassName = $registry->getOrderClassName();
173 return new $orderClassName($fields);
195 public static function create($siteId, $userId =
null, $currency =
null)
200 'ALLOW_DELIVERY' =>
'N',
204 $order = static::createOrderObject($fields);
205 $order->setFieldNoDemand(
'LID', $siteId);
206 if (intval($userId) > 0)
208 $order->setFieldNoDemand(
'USER_ID', $userId);
211 if ($currency ==
null)
213 $currency = Internals\SiteCurrencyTable::getSiteCurrency($siteId);
216 if ($currency ==
null)
218 $currency = Currency\CurrencyManager::getBaseCurrency();
221 $order->setFieldNoDemand(
'CURRENCY', $currency);
222 $order->setField(
'STATUS_ID', static::getInitialStatus());
223 $order->setFieldNoDemand(
'XML_ID', static::generateXmlId());
225 $order->calculateType = static::SALE_ORDER_CALC_TYPE_NEW;
235 return uniqid(
'bx_');
245 public static function load($id)
254 'filter' => [
'ID' => $id],
258 $list = static::loadByFilter($filter);
259 if (!empty($list) && is_array($list))
275 public static function loadByFilter(array $parameters)
279 $parameters = static::prepareParams($parameters);
282 $res = static::loadFromDb($parameters);
283 while($orderData = $res->fetch())
285 $order = static::createOrderObject($orderData);
287 $order->calculateType = static::SALE_ORDER_CALC_TYPE_CHANGE;
298 private static function prepareParams($parameters)
301 'select' => array(
'*')
304 if (isset($parameters[
'filter']))
306 $result[
'filter'] = $parameters[
'filter'];
308 if (isset($parameters[
'limit']))
310 $result[
'limit'] = $parameters[
'limit'];
312 if (isset($parameters[
'order']))
314 $result[
'order'] = $parameters[
'order'];
316 if (isset($parameters[
'offset']))
318 $result[
'offset'] = $parameters[
'offset'];
320 if (isset($parameters[
'runtime']))
322 $result[
'runtime'] = $parameters[
'runtime'];
339 if (trim($value) ==
'')
345 'filter' => [
'=ACCOUNT_NUMBER' => $value],
349 $list = static::loadByFilter($parameters);
361 return static::getList($parameters);
378 $r = $basket->
refreshData([
'PRICE',
'QUANTITY',
'COUPONS']);
379 if (!$r->isSuccess())
381 $result->addErrors($r->getErrors());
405 $this->basketCollection = $basket;
417 if (!isset($this->basketCollection) || empty($this->basketCollection))
419 $this->basketCollection = $this->loadBasket();
432 protected function loadBasket()
434 if ($this->getId() > 0)
438 $basketClassName = $registry->getBasketClassName();
440 return $basketClassName::loadItemsForOrder($this);
460 $this->calculatedFields->set($name, $value);
464 return parent::setField($name, $value);
475 $result = parent::checkValueBeforeSet($name, $value);
477 if ($name ===
'ACCOUNT_NUMBER')
479 $dbRes = static::getList([
481 'filter' => [
'=ACCOUNT_NUMBER' => $value]
504 return parent::normalizeValue($name, $value);
521 $this->calculatedFields->set($name, $value);
525 if (!$this->fields->isChanged(
"UPDATED_1C") && $name !=
'UPDATED_1C')
530 if ($this->isSaveExecuting ===
false)
534 $this->
isNew =
false;
538 parent::setFieldNoDemand($name, $value);
551 return $this->calculatedFields->get($name);
554 return parent::getField($name);
571 $this->calculatedFields->set($name, $value);
576 $this->
isNew =
false;
579 parent::initField($name, $value);
591 if(empty($this->propertyCollection))
593 $this->propertyCollection = $this->loadPropertyCollection();
606 public function loadPropertyCollection()
610 $propertyCollectionClassName = $registry->getPropertyValueCollectionClassName();
612 return $propertyCollectionClassName::load($this);
639 public function refreshData()
645 $this->calculateType = ($this->getId() > 0 ? static::SALE_ORDER_CALC_TYPE_REFRESH : static::SALE_ORDER_CALC_TYPE_NEW);
649 $this->refreshInternal();
656 $r = $this->doFinalAction($hasMeaningfulFields);
657 if (!$r->isSuccess())
659 $result->addErrors($r->getErrors());
673 protected function refreshInternal()
675 $result =
new Result();
685 $r = $this->
setField(
'PRICE', $basket->getPrice());
686 if (!$r->isSuccess())
688 $result->addErrors($r->getErrors());
702 return $this->
getField(
'PERSON_TYPE_ID');
714 return $this->
setField(
'PERSON_TYPE_ID', intval($personTypeId));
724 return floatval($this->
getField(
'PRICE'));
737 return $basket->getBasePrice() + $taxPrice;
747 return floatval($this->
getField(
'SUM_PAID'));
755 return floatval($this->
getField(
'PRICE_DELIVERY'));
763 return $this->
getField(
'DELIVERY_LOCATION');
771 return floatval($this->
getField(
'TAX_PRICE'));
779 return floatval($this->
getField(
'TAX_VALUE'));
789 return floatval($this->
getField(
'DISCOUNT_PRICE'));
811 public function changeCurrency(
string $currency): Main\
Result
819 elseif (empty($currency))
821 throw new ArgumentNullException(
'currency');
826 foreach ($this->
getBasket() as $basketItem)
833 $basketItem->changeCurrency($currency)->getErrors()
867 $useVat = $this->
getField(
'USE_VAT');
868 if ($useVat ===
null)
873 return $this->
getField(
'USE_VAT') ===
'Y';
883 $vatRate = $this->
getField(
'VAT_RATE');
884 if ($vatRate ===
null && $this->getId() > 0)
899 $vatSum = $this->
getField(
'VAT_SUM');
900 if ($vatSum ===
null && $this->getId() > 0)
914 return $this->
getField(
'MARKED') ===
"Y";
921 || $name ===
'PRICE_DELIVERY'
922 || $name ===
'SUM_PAID'
923 || $name ===
'PRICE_PAYMENT'
924 || $name ===
'DISCOUNT_VALUE'
951 if ($vatInfo && $vatInfo[
'VAT_RATE'] > 0)
953 return $this->applyCalculatedVat($vatInfo);
971 $result[
'VAT_RATE'] = $basket->getVatRate();
972 $result[
'VAT_SUM'] = $basket->getVatSum();
982 private function applyCalculatedVat(array $vatInfo)
987 $r = $this->
setField(
'USE_VAT',
'Y');
988 if (!$r->isSuccess())
990 $result->addErrors($r->getErrors());
994 $r = $this->
setField(
'VAT_RATE', $vatInfo[
'VAT_RATE']);
995 if (!$r->isSuccess())
997 $result->addErrors($r->getErrors());
1001 $r = $this->
setField(
'VAT_SUM', $vatInfo[
'VAT_SUM']);
1002 if (!$r->isSuccess())
1004 $result->addErrors($r->getErrors());
1017 return $this->
getField(
'DEDUCTED') ===
'Y';
1027 return $this->
getField(
'EXTERNAL_ORDER') ==
"Y";
1036 if ($this->calculatedFields ==
null )
1041 return (in_array($field, static::getCalculatedFields()));
1049 protected static function getInitialStatus()
1054 $orderStatus = $registry->getOrderStatusClassName();
1055 return $orderStatus::getInitialStatus();
1063 protected static function getFinalStatus()
1068 $orderStatus = $registry->getOrderStatusClassName();
1069 return $orderStatus::getFinalStatus();
1081 public function save()
1083 if ($this->isSaveExecuting)
1085 trigger_error(
"Order saving in recursion", E_USER_WARNING);
1088 $this->isSaveExecuting =
true;
1090 $result =
new Result();
1092 $id = $this->getId();
1093 $this->
isNew = ($id == 0);
1096 $r = $this->callEventOnBeforeOrderSaved();
1097 if (!$r->isSuccess())
1099 $this->isSaveExecuting =
false;
1103 $r = $this->verify();
1104 if (!$r->isSuccess())
1106 $result->addWarnings($r->getErrors());
1110 if (!$r->isSuccess())
1112 $this->isSaveExecuting =
false;
1115 elseif ($r->hasWarnings())
1117 $result->addWarnings($r->getWarnings());
1127 if ($r->getId() > 0)
1133 if ($r->hasWarnings())
1135 $result->addWarnings($r->getWarnings());
1138 if (!$r->isSuccess())
1140 $this->isSaveExecuting =
false;
1146 $result->setId($id);
1149 $this->callEventOnSaleOrderEntitySaved();
1152 if (!$r->isSuccess())
1154 $result->addWarnings($r->getErrors());
1157 if ($r->hasWarnings())
1159 $result->addWarnings($r->getWarnings());
1167 if (!$r->isSuccess())
1169 $result->addWarnings($r->getErrors());
1173 if (!$r->isSuccess())
1175 $result->addWarnings($r->getErrors());
1183 $this->
isNew =
false;
1184 $this->isSaveExecuting =
false;
1206 $notifyClassName = $registry->getNotifyClassName();
1208 $eventList = Internals\EventsPool::getEvents($this->
getInternalId());
1211 foreach ($eventList as $eventName => $eventData)
1213 $event =
new Main\Event(
'sale', $eventName, $eventData);
1216 $notifyClassName::callNotify($this, $eventName);
1236 $updateFields = [
'RUNNING' =>
'N'];
1238 $changedFields = $this->fields->getChangedValues();
1242 && !array_key_exists(
'DATE_UPDATE', $changedFields)
1246 $updateFields[
'DATE_UPDATE'] = $currentDateTime;
1249 if ($needUpdateDateInsert)
1251 $updateFields[
'DATE_INSERT'] = $currentDateTime;
1254 $this->setFieldsNoDemand($updateFields);
1255 $r = static::updateInternal($this->getId(), $updateFields);
1256 if (!$r->isSuccess())
1258 $result->addErrors($r->getErrors());
1280 if (!$this->
getField(
'DATE_UPDATE'))
1282 $this->
setField(
'DATE_UPDATE', $currentDateTime);
1285 if (!$this->
getField(
'DATE_INSERT'))
1287 $this->
setField(
'DATE_INSERT', $currentDateTime);
1290 $fields = $this->fields->getValues();
1292 if (is_object($USER) && $USER->isAuthorized())
1294 $fields[
'CREATED_BY'] = $USER->getID();
1298 if (array_key_exists(
'REASON_MARKED', $fields) && mb_strlen($fields[
'REASON_MARKED']) > 255)
1300 $fields[
'REASON_MARKED'] = mb_substr($fields[
'REASON_MARKED'], 0, 255);
1303 $fields[
'RUNNING'] =
'Y';
1306 if (!$r->isSuccess())
1308 return $result->addErrors($r->getErrors());
1311 if ($resultData = $r->getData())
1313 $result->setData($resultData);
1318 $result->setId($id);
1334 $fields = $this->fields->getChangedValues();
1336 if ($this->isChanged())
1338 if (array_key_exists(
'DATE_UPDATE', $fields) && $fields[
'DATE_UPDATE'] ===
null)
1340 unset($fields[
'DATE_UPDATE']);
1343 $fields[
'VERSION'] = intval($this->
getField(
'VERSION')) + 1;
1346 if (array_key_exists(
'REASON_MARKED', $fields) && mb_strlen($fields[
'REASON_MARKED']) > 255)
1348 $fields[
'REASON_MARKED'] = mb_substr($fields[
'REASON_MARKED'], 0, 255);
1351 $r = static::updateInternal($this->getId(), $fields);
1353 if (!$r->isSuccess())
1355 return $result->addErrors($r->getErrors());
1358 if ($resultData = $r->getData())
1360 $result->setData($resultData);
1370 protected function callEventOnSaleOrderEntitySaved()
1372 $oldEntityValues = $this->fields->getOriginalValues();
1374 if (!empty($oldEntityValues))
1376 $eventManager = Main\EventManager::getInstance();
1377 if ($eventsList = $eventManager->findEventHandlers(
'sale',
'OnSaleOrderEntitySaved'))
1380 $event =
new Main\Event(
'sale',
'OnSaleOrderEntitySaved', array(
1382 'VALUES' => $oldEntityValues,
1394 $eventManager = Main\EventManager::getInstance();
1399 'IS_NEW' => $this->
isNew,
1400 'IS_CHANGED' => $this->isChanged(),
1401 'VALUES' => $this->fields->getOriginalValues(),
1410 protected function callEventOnBeforeOrderSaved()
1415 $oldEntityValues = $this->fields->getOriginalValues();
1417 $eventManager = Main\EventManager::getInstance();
1423 'VALUES' => $oldEntityValues
1427 if ($event->getResults())
1430 foreach($event->getResults() as $eventResult)
1432 if($eventResult->getType() == Main\EventResult::ERROR)
1434 $errorMsg =
new ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR'),
'SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR');
1435 if ($eventResultData = $eventResult->getParameters())
1437 if (isset($eventResultData) && $eventResultData instanceof
ResultError)
1440 $errorMsg = $eventResultData;
1444 $result->addError($errorMsg);
1467 if (!$r->isSuccess())
1469 $result->addWarnings($r->getErrors());
1472 $r = $this->
getTax()->save();
1473 if (!$r->isSuccess())
1475 $result->addWarnings($r->getErrors());
1479 if (!$r->isSuccess())
1481 $result->addWarnings($r->getErrors());
1497 $accountNumber = Internals\AccountNumberGenerator::generateForOrder($this);
1498 if ($accountNumber !==
false)
1500 $this->
setField(
'ACCOUNT_NUMBER', $accountNumber);
1502 static::updateInternal($this->getId(), [
'ACCOUNT_NUMBER' => $accountNumber]);
1513 $this->
setField(
'VAT_SUM', $price);
1523 $this->
setField(
'VAT_DELIVERY', $price);
1533 return $this->
getField(
'DATE_INSERT');
1557 protected function onFieldModify($name, $oldValue, $value)
1563 if ($name !==
'UPDATED_1C' && !$this->getFields()->isChanged(
'UPDATED_1C'))
1565 $this->
setField(
"UPDATED_1C",
"N");
1568 if ($name ==
"PRICE")
1572 if (!$r->isSuccess())
1574 $result->addErrors($r->getErrors());
1578 elseif ($name ==
"CURRENCY")
1580 throw new Main\NotImplementedException(
'field CURRENCY');
1582 elseif ($name ==
"CANCELED")
1589 if ($event->getResults())
1592 foreach($event->getResults() as $eventResult)
1594 if($eventResult->getType() == Main\EventResult::ERROR)
1596 $errorMsg =
new ResultError(
1597 Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_ORDER_CANCELED_ERROR'),
1598 'SALE_EVENT_ON_BEFORE_ORDER_CANCELED_ERROR'
1600 if ($eventResultData = $eventResult->getParameters())
1602 if (isset($eventResultData) && $eventResultData instanceof ResultError)
1605 $errorMsg = $eventResultData;
1609 $result->addError($errorMsg);
1614 if (!$result->isSuccess())
1620 if (!$r->isSuccess())
1622 $result->addErrors($r->getErrors());
1628 if (is_object($USER) && $USER->isAuthorized())
1630 $this->
setField(
'EMP_CANCELED_ID', $USER->getID());
1633 Internals\EventsPool::addEvent(
1636 array(
'ENTITY' => $this)
1639 Internals\EventsPool::addEvent(
1642 array(
'ENTITY' => $this)
1645 elseif ($name ==
"USER_ID")
1647 throw new Main\NotImplementedException(
'field USER_ID');
1649 elseif($name ==
"MARKED")
1651 if ($oldValue !=
"Y")
1655 if (is_object($USER) && $USER->isAuthorized())
1657 $this->
setField(
'EMP_MARKED_ID', $USER->getID());
1660 elseif ($value ==
"N")
1662 $this->
setField(
'REASON_MARKED',
'');
1665 elseif ($name ==
"STATUS_ID")
1670 'OLD_VALUE' => $oldValue,
1674 if ($event->getResults())
1677 foreach($event->getResults() as $eventResult)
1679 if($eventResult->getType() == Main\EventResult::ERROR)
1681 $errorMsg =
new ResultError(
1682 Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_ORDER_STATUS_CHANGE_ERROR'),
1683 'SALE_EVENT_ON_BEFORE_ORDER_STATUS_CHANGE_ERROR'
1685 if ($eventResultData = $eventResult->getParameters())
1687 if (isset($eventResultData) && $eventResultData instanceof ResultError)
1690 $errorMsg = $eventResultData;
1694 $result->addError($errorMsg);
1699 if (!$result->isSuccess())
1706 if (is_object($USER) && $USER->isAuthorized())
1708 $this->
setField(
'EMP_STATUS_ID', $USER->GetID());
1714 'OLD_VALUE' => $oldValue,
1720 'OLD_VALUE' => $oldValue,
1723 if ($this->isStatusChangedOnPay($value, $oldValue))
1728 'OLD_VALUE' => $oldValue,
1760 public function onBasketModify($action,
BasketItemBase $basketItem, $name =
null, $oldValue =
null, $value =
null)
1768 if (!$r->isSuccess())
1770 $result->addErrors($r->getErrors());
1776 $tax->resetTaxList();
1780 $r = $this->refreshOrderPrice();
1781 if (!$r->isSuccess())
1783 $result->addErrors($r->getErrors());
1793 if ($name ==
"QUANTITY" || $name ==
"PRICE")
1796 $r = $this->refreshOrderPrice();
1797 if (!$r->isSuccess())
1799 $result->addErrors($r->getErrors());
1802 elseif ($name ==
"CURRENCY")
1804 if ($value != $this->
getField(
"CURRENCY"))
1806 throw new Main\NotSupportedException(
"CURRENCY");
1838 if ($this->tax ===
null)
1840 $this->tax = $this->loadTax();
1873 public function isChanged()
1875 if (parent::isChanged())
1890 if ($basket->isChanged())
1909 parent::clearChanged();
1918 $property->clearChanged();
1940 return $this->
getField(
'PAYED') ===
'Y';
1950 return $this->
getField(
'ALLOW_DELIVERY') ===
'Y';
1960 return $this->
getField(
'DEDUCTED') ===
'Y';
1970 return $this->
getField(
'CANCELED') ===
'Y';
1978 public function getHash()
1981 $dateInsert = $this->
getDateInsert()->setTimeZone(
new \DateTimeZone(
"Europe/Moscow"));
1982 $timestamp = $dateInsert->getTimestamp();
1987 $this->getField(
'ACCOUNT_NUMBER')
1996 public function verify()
1998 $result =
new Result();
2004 if (!$r->isSuccess())
2006 $result->addErrors($r->getErrors());
2014 if (!$r->isSuccess())
2016 $result->addErrors($r->getErrors());
2023 $r = $discounts->verify();
2024 if (!$r->isSuccess())
2026 $result->addErrors($r->getErrors());
2042 public static function getList(array $parameters = array())
2054 if ((
string)$this->
getField(
'TAX_LOCATION') ===
"")
2060 $this->
setField(
'TAX_LOCATION', $property->getValue());
2065 return $this->
getField(
'TAX_LOCATION');
2094 $this->isMeaningfulField =
false;
2114 $this->isMeaningfulField =
true;
2129 $this->isOnlyMathAction = $value;
2141 public static function deleteNoDemand($id)
2145 if (!static::isExists($id))
2152 $deleteResult = static::deleteEntitiesNoDemand($id);
2153 if (!$deleteResult->isSuccess())
2155 $result->addErrors($deleteResult->getErrors());
2159 $r = static::deleteInternal($id);
2160 if (!$r->isSuccess())
2161 $result->addErrors($r->getErrors());
2163 static::deleteExternalEntities($id);
2175 public static function delete($id)
2177 $result =
new Result();
2181 $orderClassName = $registry->getOrderClassName();
2183 if (!$order = $orderClassName::load($id))
2185 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_ORDER_ENTITY_NOT_FOUND'),
'SALE_ORDER_ENTITY_NOT_FOUND'));
2190 $notifyClassName = $registry->getNotifyClassName();
2191 $notifyClassName::setNotifyDisable(
true);
2194 $r = $order->setField(
'CANCELED',
'Y');
2195 if (!$r->isSuccess())
2197 $result->addErrors($r->getErrors());
2201 $r = $order->save();
2202 if (!$r->isSuccess())
2204 $result->addErrors($r->getErrors());
2208 static::deleteEntities($order);
2210 $event =
new Main\Event(
2213 array(
'ENTITY' => $order)
2217 foreach ($event->getResults() as $eventResult)
2220 if ($eventResult->getType() == Main\EventResult::ERROR)
2222 if ($eventResultData = $eventResult->getParameters())
2224 if (isset($eventResultData) && $eventResultData instanceof ResultError)
2227 $errorMsg = $eventResultData;
2231 if (!isset($errorMsg))
2232 $errorMsg =
new ResultError(
'EVENT_ORDER_DELETE_ERROR');
2234 $result->addError($errorMsg);
2240 $r = $order->save();
2241 if ($r->isSuccess())
2244 $r = static::deleteInternal($id);
2245 if ($r->isSuccess())
2246 static::deleteExternalEntities($id);
2250 $result->addErrors($r->getErrors());
2253 $notifyClassName::setNotifyDisable(
false);
2255 $event =
new Main\Event(
2258 array(
'ENTITY' => $order,
'VALUE' => $r->isSuccess())
2262 $result->addData(array(
'ORDER' => $order));
2271 protected static function deleteEntities(OrderBase $order)
2279 $basketItem->delete();
2289 $property->delete();
2301 $dbRes = static::getList(array(
'filter' => array(
'ID' => $id)));
2302 if ($dbRes->fetch())
2317 public function isAllowPay()
2322 $orderClassName = $registry->getOrderStatusClassName();
2323 return $orderClassName::isAllowPay($this->
getField(
'STATUS_ID'));
2338 protected static function deleteEntitiesNoDemand($orderId)
2343 $basketClassName = $registry->getBasketClassName();
2344 $r = $basketClassName::deleteNoDemand($orderId);
2345 if (!$r->isSuccess())
2351 $propertyValueCollectionClassName = $registry->getPropertyValueCollectionClassName();
2352 $r = $propertyValueCollectionClassName::deleteNoDemand($orderId);
2353 if (!$r->isSuccess())
2359 $orderDiscountClassName = $registry->getOrderDiscountClassName();
2360 $orderDiscountClassName::deleteByOrder($orderId);
2362 return new Result();
2372 if ($this->discount ===
null)
2374 $this->discount = $this->loadDiscount();
2385 protected function loadTax()
2390 $taxClassName = $registry->getTaxClassName();
2391 return $taxClassName::load($this);
2399 protected function loadDiscount()
2404 $discountClassName = $registry->getDiscountClassName();
2405 return $discountClassName::buildFromOrder($this);
2411 private function refreshOrderPrice()
2424 return $basket->getPrice() + $taxPrice;
2441 public function doFinalAction($hasMeaningfulField =
false)
2447 $r = Internals\ActionEntity::runActions($orderInternalId);
2448 if (!$r->isSuccess())
2450 $result->addErrors($r->getErrors());
2453 if (!$hasMeaningfulField)
2460 if ($r->hasWarnings())
2462 $result->addWarnings($r->getWarnings());
2472 $eventManager = Main\EventManager::getInstance();
2473 $eventsList = $eventManager->findEventHandlers(
'sale',
'OnBeforeSaleOrderFinalAction');
2474 if (!empty($eventsList))
2476 $event =
new Main\Event(
'sale',
'OnBeforeSaleOrderFinalAction', array(
2478 'HAS_MEANINGFUL_FIELD' => $hasMeaningfulField,
2479 'BASKET' => $basket,
2483 if ($event->getResults())
2486 foreach($event->getResults() as $eventResult)
2488 if($eventResult->getType() == Main\EventResult::ERROR)
2490 $errorMsg =
new ResultError(
2492 'SALE_EVENT_ON_BEFORE_SALEORDER_FINAL_ACTION_ERROR'
2494 'SALE_EVENT_ON_BEFORE_SALEORDER_FINAL_ACTION_ERROR'
2497 $eventResultData = $eventResult->getParameters();
2498 if ($eventResultData)
2500 if (isset($eventResultData) && $eventResultData instanceof ResultError)
2503 $errorMsg = $eventResultData;
2507 $result->addError($errorMsg);
2513 if (!$result->isSuccess())
2521 if (!$r->isSuccess())
2528 if ($r->isSuccess() && ($discountData = $r->getData()) && !empty($discountData) && is_array($discountData))
2531 $r = $this->applyDiscount($discountData);
2532 if (!$r->isSuccess())
2534 $result->addErrors($r->getErrors());
2542 $r =
$tax->refreshData();
2543 if (!$r->isSuccess())
2545 $result->addErrors($r->getErrors());
2549 $taxResult = $r->getData();
2551 $taxChanged =
false;
2552 if (isset($taxResult[
'TAX_PRICE']) && floatval($taxResult[
'TAX_PRICE']) >= 0)
2556 $taxChanged = $this->
getField(
'TAX_PRICE') !== $taxResult[
'TAX_PRICE'];
2559 $this->
setField(
'TAX_PRICE', $taxResult[
'TAX_PRICE']);
2560 $this->refreshOrderPrice();
2566 if (array_key_exists(
'VAT_SUM', $taxResult))
2570 $this->
setField(
'VAT_SUM', $taxResult[
'VAT_SUM']);
2577 if (floatval($taxValue) != floatval($this->
getField(
'TAX_VALUE')))
2579 $this->
setField(
'TAX_VALUE', floatval($taxValue));
2585 if (!$currentIsMathActionOnly)
2590 $eventManager = Main\EventManager::getInstance();
2591 if ($eventsList = $eventManager->findEventHandlers(
'sale',
'OnAfterSaleOrderFinalAction'))
2593 $event =
new Main\Event(
2595 'OnAfterSaleOrderFinalAction',
2596 array(
'ENTITY' => $this)
2613 public function applyDiscount(array $data)
2615 if (!empty($data[
'BASKET_ITEMS']) && is_array($data[
'BASKET_ITEMS']))
2619 $basketResult = $basket->applyDiscount($data[
'BASKET_ITEMS']);
2620 if (!$basketResult->isSuccess())
2621 return $basketResult;
2622 unset($basketResult, $basket);
2624 $this->refreshOrderPrice();
2627 return new Result();
2635 protected function setReasonMarked($value)
2637 $result =
new Result();
2641 $orderReasonMarked = $this->
getField(
'REASON_MARKED');
2642 if (is_array($value))
2644 $newOrderReasonMarked =
'';
2646 foreach ($value as $err)
2648 $newOrderReasonMarked .= (strval($newOrderReasonMarked) !=
'' ?
"\n" :
"") . $err;
2653 $newOrderReasonMarked = $value;
2657 $r = $this->
setField(
'REASON_MARKED', $orderReasonMarked. (strval($orderReasonMarked) !=
'' ?
"\n" :
""). $newOrderReasonMarked);
2658 if (!$r->isSuccess())
2660 $result->addErrors($r->getErrors());
2677 if (in_array(
'PRICE', $select))
2682 if (in_array(
'PRICE_DELIVERY', $select))
2684 $this->
setField(
'PRICE_DELIVERY', 0);
2697 protected function isStatusChangedOnPay($value, $oldValue)
2702 $orderStatus = $registry->getOrderStatusClassName();
2704 $allowPayStatus = $orderStatus::getAllowPayStatusList();
2705 $disallowPayStatus = $orderStatus::getDisallowPayStatusList();
2707 return !empty($disallowPayStatus)
2708 && in_array($oldValue, $disallowPayStatus)
2709 && !empty($allowPayStatus)
2710 && in_array($value, $allowPayStatus);
2721 public function createClone()
2723 $cloneEntity = new \SplObjectStorage();
2726 $orderClone = clone $this;
2727 $orderClone->isClone =
true;
2730 if ($fields = $this->fields)
2732 $orderClone->fields = $fields->createClone($cloneEntity);
2741 if (!$cloneEntity->contains($this))
2743 $cloneEntity[$this] = $orderClone;
2746 $this->cloneEntities($cloneEntity);
2757 protected function cloneEntities(\SplObjectStorage $cloneEntity)
2759 if (!$cloneEntity->contains($this))
2761 throw new Main\SystemException();
2764 $orderClone = $cloneEntity[$this];
2769 $orderClone->basketCollection = $basket->createClone($cloneEntity);
2780 $orderClone->tax =
$tax->createClone($cloneEntity);
2785 $orderClone->discount =
$discount->createClone($cloneEntity);
2834 return static::getAvailableFields();
2849 $result = parent::toArray();
2851 $result[
'BASKET_ITEMS'] = $this->
getBasket()->toArray();