69 public static function makeSelectHtml($name, array $data, $selected =
"", $showNotUse =
true, $attributes = array())
74 if(!is_array($attributes))
77 $result =
'<select name="'.htmlspecialcharsbx($name).
'"';
79 foreach($attributes as $attrName => $attrValue )
80 $result.=
" ".$attrName.
"=\"".htmlspecialcharsbx($attrValue).
"\"";
85 $result .=
'<option value="">'.GetMessage(
"SALE_ORDEREDIT_NOT_USE").
'</option>';
87 foreach($data as $value => $title)
88 $result .=
'<option value="'.htmlspecialcharsbx($value).
'"'.($selected == $value ?
" selected" :
"").
'>'.htmlspecialcharsbx(TruncateText($title, 40)).
'</option>';
90 $result .=
'</select>';
109 if(!is_array($attributes))
112 $result =
'<select name="'.htmlspecialcharsbx($name).
'"';
114 foreach($attributes as $attrName => $attrValue )
115 $result .=
" ".$attrName.
"=\"".htmlspecialcharsbx($attrValue).
"\"";
120 $result .=
'</select>';
133 $activePaySystems =
'';
136 $activePaySystems .=
'<option value="">'.GetMessage(
"SALE_ORDEREDIT_NOT_USE").
'</option>';
138 $restrictedPaySystems =
'';
139 foreach($data as $item)
141 if (!isset($item[
'RESTRICTED']))
143 $activePaySystems .=
'<option value="'.htmlspecialcharsbx($item[
'ID']).
'"'.($selected == $item[
'ID'] ?
" selected" :
"").
'>'.htmlspecialcharsbx(TruncateText($item[
'NAME'], 40)).
'</option>';
147 $restrictedPaySystems .=
'<option value="'.htmlspecialcharsbx($item[
'ID']).
'"'.($selected == $item[
'ID'] ?
" selected" :
"").
' class="bx-admin-service-restricted">'.htmlspecialcharsbx(TruncateText($item[
'NAME'], 40)).
'</option>';
151 return $activePaySystems.$restrictedPaySystems;
161 Asset::getInstance()->addJs(
"/bitrix/js/sale/admin/order_edit.js");
162 Asset::getInstance()->addJs(
"/bitrix/js/sale/admin/order_ajaxer.js");
163 $currencyId = $order->getCurrency();
164 $currencies = array();
166 if(Loader::includeModule(
'currency'))
168 \CJSCore::Init(array(
'currency'));
169 $currencyFormat = \CCurrencyLang::getFormatDescription($currencyId);
172 'CURRENCY' => $currencyId,
174 'FORMAT_STRING' => $currencyFormat[
'FORMAT_STRING'],
175 'DEC_POINT' => $currencyFormat[
'DEC_POINT'],
176 'THOUSANDS_SEP' => $currencyFormat[
'THOUSANDS_SEP'],
177 'DECIMALS' => $currencyFormat[
'DECIMALS'],
178 'THOUSANDS_VARIANT' => $currencyFormat[
'THOUSANDS_VARIANT'],
185 $connectedB24Portal =
'';
187 if(Loader::includeModule(
'b24connector'))
189 $connectedB24Portal = \Bitrix\B24Connector\Connection::getDomain();
192 $curFormat = \CCurrencyLang::GetFormatDescription($currencyId);
193 $currencyLang = preg_replace(
"/(^|[^&])#/",
'$1', $curFormat[
"FORMAT_STRING"]);
194 $langPhrases = array(
"SALE_ORDEREDIT_DISCOUNT_UNKNOWN",
"SALE_ORDEREDIT_REFRESHING_DATA",
"SALE_ORDEREDIT_FIX",
195 "SALE_ORDEREDIT_UNFIX",
"SALE_ORDEREDIT_CLOSE",
"SALE_ORDEREDIT_MESSAGE",
"SALE_ORDEREDIT_CONFIRM",
196 "SALE_ORDEREDIT_CONFIRM_CONTINUE",
"SALE_ORDEREDIT_CONFIRM_ABORT");
199 <script type="text/javascript">
201 BX.Sale.Admin.OrderEditPage.orderId = "'.$order->getId().
'";
202 BX.Sale.Admin.OrderEditPage.siteId = "'.\CUtil::JSEscape($order->getSiteId()).
'";
203 BX.Sale.Admin.OrderEditPage.languageId = "'.LANGUAGE_ID.
'";
204 BX.Sale.Admin.OrderEditPage.formId = "'.$formId.
'_form";
205 BX.Sale.Admin.OrderEditPage.connectedB24Portal = "'.\CUtil::JSEscape($connectedB24Portal).
'";
206 BX.Sale.Admin.OrderEditPage.adminTabControlId = "'.$formId.
'";
207 '.(!empty($currencies) ?
'BX.Currency.setCurrencies('.\CUtil::PhpToJSObject($currencies,
false,
true,
true).
');' :
'').
208 'BX.Sale.Admin.OrderEditPage.currency = "'.\CUtil::JSEscape($currencyId).
'";
209 BX.Sale.Admin.OrderEditPage.currencyLang = "'.\CUtil::JSEscape($currencyLang).
'";';
211 if($formId ==
"sale_order_create")
213 BX.Sale.Admin.OrderEditPage.registerFieldsUpdaters(BX.Sale.Admin.OrderPayment.prototype.getCreateOrderFieldsUpdaters());';
215 foreach($langPhrases as $phrase)
216 $result .=
' BX.message({'.$phrase.
': "'.\CUtil::JSEscape(
Loc::getMessage($phrase)).
'"});';
233 if(intval($userId) <= 0)
236 static $userNames = array();
238 if(!isset($userNames[$userId]))
240 $res = UserTable::getById($userId);
242 if($buyer = $res->fetch())
244 $userNames[$userId] = \CUser::FormatName(
245 \CSite::GetNameFormat(
256 return $userNames[$userId];
270 <div class="adm-bus-orderproblem" id="sale-adm-order-problem-block">
271 <div class="adm-bus-orderproblem-container">
274 <td class="adm-bus-orderproblem-title">'.Loc::getMessage(
"SALE_ORDEREDIT_ORDER_PROBLEM").
':</td>
275 <td class="adm-bus-orderproblem-text">'.$text.
'</td>
278 <span class="adm-bus-orderproblem-close" title="'.
Loc::getMessage(
"SALE_ORDEREDIT_CLOSE").
'" onclick="BX.Sale.Admin.OrderEditPage.onProblemCloseClick(\''.$orderId.
'\',\
'sale-adm-order-problem-block\');"></span>
297 <div class="adm-bus-fastnav adm-detail-tabs-block-pin" id="sale-order-edit-block-fast-nav">
298 <div class="adm-bus-fastnav-container">
301 <td class="adm-bus-fastnav-title">'.Loc::getMessage(
'SALE_ORDEREDIT_NAVIGATION').
':</td>
303 <ul class="adm-bus-fastnav-navlist" id="adm-bus-fastnav-navlist">';
305 foreach($items as $anchor => $itemName)
307 if($formId <>
'' && $tabId <>
'')
309 $href =
'javascript:void(0);';
310 $onClick =
' onclick="BX.Sale.Admin.OrderEditPage.fastNavigation.onClickItem(\''.$formId.
'\', \
''.$tabId.
'\', \
''.$anchor.
'\')
"';
318 if ($anchor == 'relprops')
319 $result .= '<li style="display:none;
"><a href="'.$href.'" id="nav_
'.$anchor.'"'.$onClick.'>'.$itemName.'</a></li>';
321 $result .= '<li><a href="'.$href.'" id="nav_
'.$anchor.'"'.$onClick.'>'.$itemName.'</a></li>';
329 <div id="sale-order-edit-block-fast-nav-pin
" onclick="BX.Sale.Admin.OrderEditPage.toggleFix(this.
id, \
'sale-order-edit-block-fast-nav\');" class="adm-detail-pin-btn-tabs" style="top: 9px;right: 0px;"></div>
333 $orderEditOpts = \CUserOptions::GetOption(
"sale_admin",
"sale_order_edit", array());
334 $isFixed = isset($orderEditOpts[
"fix_sale-order-edit-block-fast-nav"]) && $orderEditOpts[
"fix_sale-order-edit-block-fast-nav"] ==
"Y" ? true :
false;
337 <script type="text/javascript">
339 BX.bind(window, "scroll", BX.Sale.Admin.OrderEditPage.fastNavigation.markItem);
340 setTimeout(function(){
341 BX.Sale.Admin.OrderEditPage.fastNavigation.markItem();'
342 .($isFixed ?
'BX.Sale.Admin.OrderEditPage.toggleFix("sale-order-edit-block-fast-nav-pin", "sale-order-edit-block-fast-nav");' :
'').
362 $orderProps = $order->getPropertyCollection();
364 if($email = $orderProps->getUserEmail())
365 $email = $email->getValue();
367 if($name = $orderProps->getPayerName())
368 $name = $name->getValue();
370 if($phone = $orderProps->getPhone())
371 $phone = $phone->getValue();
373 $userId = \CSaleUser::DoAutoRegisterUser(
376 $formData[
"SITE_ID"],
379 'PERSONAL_PHONE' => $phone,
380 'PHONE_NUMBER' => $phone
388 foreach($errors as $val)
389 $errorMessage .= $val[
"TEXT"];
397 public static function getUserId($order, $formData, $createUserIfNeed,
Result &$result)
399 if(intval($formData[
"USER_ID"]) > 0)
400 return intval($formData[
"USER_ID"]);
404 if($createUserIfNeed && (!isset($formData[
"USER_ID"]) || intval($formData[
"USER_ID"]) <= 0))
429 public static function createOrderFromForm(array $formData, $creatorUserId, $createUserIfNeed, array $files,
Result &$opResult)
431 if(!isset($formData[
"SITE_ID"]) || $formData[
"SITE_ID"] ==
'')
436 $orderClass = $registry->getOrderClassName();
438 global $APPLICATION, $USER;
439 $order = $orderClass::create($formData[
"SITE_ID"]);
440 $saleModulePermissions = $APPLICATION->GetGroupRight(
"sale");
441 $userCompanyId =
null;
443 if($saleModulePermissions ==
"P")
445 $userCompanyList = Company\Manager::getUserCompanyList($USER->GetID());
446 if (!empty($userCompanyList) && is_array($userCompanyList))
448 $userCompanyId = reset($userCompanyList);
449 if (intval($userCompanyId) > 0 && empty($formData[
'ORDER'][
'COMPANY_ID']))
451 $formData[
'ORDER'][
'COMPANY_ID'] = $userCompanyId;
457 $res = self::fillSimpleFields($order, $formData, $creatorUserId);
459 if(!$res->isSuccess())
463 if(!$res->isSuccess())
467 $order->setFieldNoDemand(
469 self::getUserId($order, $formData, $createUserIfNeed, $opResult)
474 if ($order->getUserId() > 0)
479 $needDataUpdate = array();
480 $basketCodeMap = array();
483 if(isset($formData[
"PRODUCT"]) && is_array($formData[
"PRODUCT"]) && !empty($formData[
"PRODUCT"]))
485 $isStartField = $order->isStartField();
487 $basketClass = $registry->getBasketClassName();
489 $basket = $basketClass::create($formData[
"SITE_ID"]);
491 $res = $order->setBasket($basket);
493 if(!$res->isSuccess())
496 $basket->setFUserId($fUserId);
497 \Bitrix\Sale\ProviderBase::setUsingTrustData(
true);
500 $maxBasketCodeIdx = 0;
502 foreach($formData[
"PRODUCT"] as $basketCode => $productData)
504 $formData[
"PRODUCT"][$basketCode][
"SORT"] = $sort;
511 if(self::isBasketItemNew($basketCode))
513 $basketInternalId = intval(mb_substr($basketCode, 1));
515 if($basketInternalId > $maxBasketCodeIdx)
516 $maxBasketCodeIdx = $basketInternalId;
518 if(self::$needUpdateNewProductPrice)
520 unset($formData[
"PRODUCT"][$basketCode][
"PROVIDER_DATA"]);
521 unset($formData[
"PRODUCT"][$basketCode][
"SET_ITEMS_DATA"]);
526 sortByColumn($formData[
"PRODUCT"], array(
"BASE_PRICE" => SORT_DESC,
"PRICE" => SORT_DESC),
'',
null,
true);
528 foreach($formData[
"PRODUCT"] as $basketCode => $productData)
530 if (($productData[
'IS_SET_ITEM'] ??
null) ===
'Y')
535 if(!isset($productData[
"PROPS"]) || !is_array($productData[
"PROPS"]))
537 $productData[
"PROPS"] = array();
542 $basketCode != self::BASKET_CODE_NEW
543 ? $basket->getItemByBasketCode($basketCode)
547 if($item ==
null && $basketCode != self::BASKET_CODE_NEW)
549 $item = $basket->getItemByBasketCode($basketCode);
552 if($item && $item->isBundleChild())
560 if(!empty($productData[
"PROVIDER_DATA"]) && CheckSerializedData($productData[
"PROVIDER_DATA"]))
562 $providerData = unserialize($productData[
"PROVIDER_DATA"], [
'allowed_classes' =>
false]);
563 self::setProviderTrustData($item, $order, $providerData);
566 if(!empty($productData[
"SET_ITEMS_DATA"]) && CheckSerializedData($productData[
"SET_ITEMS_DATA"]))
567 $productData[
"SET_ITEMS"] = unserialize($productData[
"SET_ITEMS_DATA"], [
'allowed_classes' =>
false]);
569 $res = $item->setField(
"QUANTITY", $item->getField(
"QUANTITY")+$productData[
"QUANTITY"]);
571 if(!$res->isSuccess())
576 if($basketCode != self::BASKET_CODE_NEW)
577 $setBasketCode = $basketCode;
578 elseif(intval($maxBasketCodeIdx) > 0)
579 $setBasketCode = 'n'.strval($maxBasketCodeIdx+1);
581 $setBasketCode = null;
583 $productId = $productData["PRODUCT_ID"];
584 if (isset($productData["OFFER_ID"]) && !empty($productData["OFFER_ID"]))
589 $item = $basket->createItem($productData[
"MODULE"], $productId, $setBasketCode);
591 if ($basketCode != $productData[
"BASKET_CODE"])
592 $productData[
"BASKET_CODE"] = $item->getBasketCode();
594 if($basketCode == self::BASKET_CODE_NEW)
596 $opResult->
setData(array(
"NEW_ITEM_BASKET_CODE" => $productData[
"BASKET_CODE"]));
597 $needDataUpdate[] = $item->getBasketCode();
600 if(!empty($productData[
'REPLACED']) && $productData[
'REPLACED'] ==
'Y')
601 $needDataUpdate[] = $item->getBasketCode();
603 if($basketCode != $item->getBasketCode())
604 $basketCodeMap[$basketCode] = $item->getBasketCode();
606 if(isset($productData[
"PROPS"]) && !empty($productData[
"PROPS"]) && is_array($productData[
"PROPS"]))
609 $property = $item->getPropertyCollection();
610 $property->setProperty($productData[
"PROPS"]);
615 $productsData = $formData[
"PRODUCT"];
617 if(!empty($basketCodeMap))
619 foreach($basketCodeMap as $old => $new)
621 $productsData[$new] = $productsData[$old];
622 unset($productsData[$old]);
626 $res = self::fillBasketItems($basket, $productsData, $order, array_unique($needDataUpdate));
628 if(!$res->isSuccess())
632 if($res->isTerminal())
640 $hasMeaningfulFields = $order->hasMeaningfulField();
643 $r = $order->doFinalAction($hasMeaningfulFields);
644 if (!$r->isSuccess())
650 if(isset($formData[
"DISCOUNTS"]) && is_array($formData[
"DISCOUNTS"]))
651 $order->getDiscount()->setApplyResult($formData[
"DISCOUNTS"]);
664 return $propCollection->setValuesFromPost($formData, $files);
674 if(intval($userId) <= 0)
683 if(isset($formData[
"COUPONS"]) && $formData[
"COUPONS"] <>
'')
685 $coupons = explode(
",", $formData[
"COUPONS"]);
687 if(is_array($coupons) && count($coupons) > 0)
689 foreach($coupons as $coupon)
703 if($profileName = $order->getPropertyCollection()->getProfileName())
704 $name = $profileName->getValue();
706 $res = \CSaleOrderUserProps::DoSaveUserProfile(
710 $order->getPersonTypeId(),
711 $propCollection = $formData[
"PROPERTIES"],
719 foreach($errors as $error)
720 $result->addError(
new EntityError($error.
"<br>\n"));
742 public static function editOrderByFormData(array $formData,
Order $order, $userId, $createUserIfNeed, array $files,
Result &$result)
745 $res = self::fillSimpleFields($order, $formData, $userId);
747 if(!$res->isSuccess())
750 $propCollection = $order->getPropertyCollection();
751 $res = $propCollection->setValuesFromPost($formData, $files);
753 if(!$res->isSuccess())
763 foreach ($paymentCollection as $payment)
765 if ($payment->isPaid())
767 $result->addError(new EntityError(
768 Loc::getMessage("SALE_ORDEREDIT_ERROR_CHANGE_USER_WITH_PAID_PAYMENTS")
769 , 'SALE_ORDEREDIT_ERROR_CHANGE_USER_WITH_PAID_PAYMENTS'));
776 $order->setFieldNoDemand(
778 self::getUserId($order, $formData, $createUserIfNeed, $result)
781 if(isset($formData[
"DISCOUNTS"]) && is_array($formData[
"DISCOUNTS"]))
782 $order->getDiscount()->setApplyResult($formData[
"DISCOUNTS"]);
785 $basket = $order->getBasket();
786 $itemsBasketCodes = array();
787 $maxBasketCodeIdx = 0;
788 $productAdded =
false;
790 if(isset($formData[
"PRODUCT"]) && is_array($formData[
"PRODUCT"]) && !empty($formData[
"PRODUCT"]))
794 foreach($formData[
"PRODUCT"] as $basketCode => $productData)
796 $formData[
"PRODUCT"][$basketCode][
"SORT"] = $sort;
803 if(self::isBasketItemNew($basketCode))
805 $basketInternalId = intval(mb_substr($basketCode, 1));
807 if($basketInternalId > $maxBasketCodeIdx)
808 $maxBasketCodeIdx = $basketInternalId;
810 $needDataUpdate[] = $basketCode;
811 unset($formData[
"PRODUCT"][$basketCode][
"PROVIDER_DATA"]);
812 unset($formData[
"PRODUCT"][$basketCode][
"SET_ITEMS_DATA"]);
816 sortByColumn($formData[
"PRODUCT"], array(
"BASE_PRICE" => SORT_DESC,
"PRICE" => SORT_DESC),
'',
null,
true);
819 if(!empty($formData[
"ALREADY_IN_BASKET_CODE"]))
821 $item = $basket->getItemByBasketCode($formData[
"ALREADY_IN_BASKET_CODE"]);
825 $res = $item->delete();
827 if (!$res->isSuccess())
831 foreach($res->getErrors() as $error)
832 $errMess .= $error->getMessage().
"\n";
843 foreach($formData[
"PRODUCT"] as $basketCode => $productData)
845 if (!isset($productData[
"PROPS"]))
847 $productData[
"PROPS"] = array();
852 $basketCode != self::BASKET_CODE_NEW
853 ? $basket->getItemByBasketCode($basketCode)
862 if($item && $item->isBundleChild())
872 $itemsBasketCodes[] = $item->getBasketCode();
877 $basketItems = $basket->getBasketItems();
879 foreach($basketItems as $item)
881 if(!in_array($item->getBasketCode(), $itemsBasketCodes))
883 $res = $item->delete();
885 if (!$res->isSuccess())
889 foreach($res->getErrors() as $error)
890 $errMess .= $error->getMessage().
"\n";
901 \Bitrix\Sale\ProviderBase::setUsingTrustData(
true);
902 $isStartField = $order->isStartField();
903 $needDataUpdate = array();
904 $basketCodeMap = array();
906 if(isset($formData[
"PRODUCT"]) && is_array($formData[
"PRODUCT"]) && !empty($formData[
"PRODUCT"]))
908 foreach($formData[
"PRODUCT"] as $basketCode => $productData)
910 $providerData = array();
912 if($productData[
"IS_SET_ITEM"] ==
"Y")
917 if(!isset($productData[
"PROPS"]) || !is_array($productData[
"PROPS"]))
919 $productData[
"PROPS"] = array();
924 $basketCode != self::BASKET_CODE_NEW
925 ? $basket->getItemByBasketCode($basketCode)
930 if($item ==
null && $basketCode != self::BASKET_CODE_NEW)
932 if($item = $basket->getItemByBasketCode($basketCode))
934 $res = $item->delete();
936 if(!$res->isSuccess())
946 if($item && $item->isBundleChild())
951 if($basketCode != self::BASKET_CODE_NEW)
952 $setBasketCode = $basketCode;
953 elseif(intval($maxBasketCodeIdx) > 0)
954 $setBasketCode = 'n'.strval($maxBasketCodeIdx+1);
956 $setBasketCode = null;
958 $item = $basket->createItem(
959 $productData["MODULE"],
960 $productData["OFFER_ID"],
964 if ($basketCode != $productData["BASKET_CODE"])
965 $productData["BASKET_CODE"] = $item->getBasketCode();
969 $result->
setData(array(
"NEW_ITEM_BASKET_CODE" => $productData[
"BASKET_CODE"]));
970 $needDataUpdate[] = $item->getBasketCode();
973 if(!empty($productData[
'REPLACED']) && $productData[
'REPLACED'] ==
'Y')
974 $needDataUpdate[] = $item->getBasketCode();
977 $productAdded =
true;
981 if ($basketCode != $productData[
"BASKET_CODE"])
982 $productData[
"BASKET_CODE"] = $item->getBasketCode();
984 if(isset($productData[
"OFFER_ID"]) || intval($productData[
"OFFER_ID"]) >= 0)
985 $productData[
"PRODUCT_ID"] = $productData[
"OFFER_ID"];
987 $itemFields = array_intersect_key($productData, array_flip($item::getAvailableFields()));
989 if(isset($itemFields[
"MEASURE_CODE"]) && $itemFields[
"MEASURE_CODE"] <>
'')
991 $measures = OrderBasket::getCatalogMeasures();
993 if(isset($measures[$itemFields[
"MEASURE_CODE"]]) && $measures[$itemFields[
"MEASURE_CODE"]] <>
'')
994 $itemFields[
"MEASURE_NAME"] = $measures[$itemFields[
"MEASURE_CODE"]];
997 if(!empty($productData[
"PROVIDER_DATA"]) && !self::$needUpdateNewProductPrice && CheckSerializedData($productData[
"PROVIDER_DATA"]))
999 $providerData = unserialize($productData[
"PROVIDER_DATA"], [
'allowed_classes' =>
false]);
1002 if(is_array($providerData) && !empty($providerData))
1003 self::setProviderTrustData($item, $order, $providerData);
1005 if(!empty($productData[
"SET_ITEMS_DATA"]) && CheckSerializedData($productData[
"SET_ITEMS_DATA"]))
1006 $productData[
"SET_ITEMS"] = unserialize($productData[
"SET_ITEMS_DATA"], [
'allowed_classes' =>
false]);
1011 if (!$res->isSuccess())
1016 elseif($res->hasWarnings())
1018 foreach($res->getWarningMessages() as $warning)
1029 if($basketCode != $item->getBasketCode())
1030 $basketCodeMap[$basketCode] = $item->getBasketCode();
1032 if(!empty($productData[
"PROPS"]) && is_array($productData[
"PROPS"]))
1035 $property = $item->getPropertyCollection();
1036 $property->setProperty($productData[
"PROPS"]);
1040 $productsData = $formData[
"PRODUCT"];
1042 if(!empty($basketCodeMap))
1044 foreach($basketCodeMap as $old => $new)
1046 $productsData[$new] = $productsData[$old];
1047 unset($productsData[$old]);
1051 $res = self::fillBasketItems($basket, $productsData, $order, array_unique($needDataUpdate));
1053 if(!$res->isSuccess())
1057 if($res->isTerminal())
1065 $hasMeaningfulFields = $order->hasMeaningfulField();
1068 $r = $order->doFinalAction($hasMeaningfulFields);
1069 if (!$r->isSuccess())
1075 if(isset($formData[
"DISCOUNTS"]) && is_array($formData[
"DISCOUNTS"]))
1076 $order->getDiscount()->setApplyResult($formData[
"DISCOUNTS"]);
1093 public static function fillSimpleFields(Order $order, array $formData, $userId = 0)
1095 $result = new \Bitrix\Sale\Result();
1096 if(isset($formData[
"ORDER"][
"RESPONSIBLE_ID"]))
1098 if (intval($formData[
"ORDER"][
"RESPONSIBLE_ID"]) != intval($order->getField(
'RESPONSIBLE_ID')))
1101 $r = $order->setField(
"RESPONSIBLE_ID", $formData[
"ORDER"][
"RESPONSIBLE_ID"]);
1102 if (!$r->isSuccess())
1109 if(!empty($formData[
"ORDER"]) && array_key_exists(
'COMPANY_ID', $formData[
"ORDER"]))
1112 $r = $order->setField(
"COMPANY_ID", (isset($formData[
"ORDER"][
'COMPANY_ID']) && $formData[
"ORDER"][
'COMPANY_ID'] > 0) ? $formData[
"ORDER"][
'COMPANY_ID'] : 0);
1113 if (!$r->isSuccess())
1119 if(isset($formData[
"PERSON_TYPE_ID"]) && intval($formData[
"PERSON_TYPE_ID"]) > 0)
1122 $r = $order->setPersonTypeId(intval($formData[
'PERSON_TYPE_ID']));
1127 $r = $order->setPersonTypeId(
1128 Blocks\OrderBuyer::getDefaultPersonType(
1134 if (!$r->isSuccess())
1139 if(isset($formData[
"ORDER"][
"COMMENTS"]))
1142 $r = $order->setField(
"COMMENTS", $formData[
"ORDER"][
"COMMENTS"]);
1143 if (!$r->isSuccess())
1149 if(isset($formData[
"USER_DESCRIPTION"]))
1152 $r = $order->setField(
"USER_DESCRIPTION", $formData[
"USER_DESCRIPTION"]);
1153 if (!$r->isSuccess())
1159 if(isset($formData[
"RESPONSIBLE_ID"]))
1162 $r = $order->setField(
"RESPONSIBLE_ID", $formData[
"RESPONSIBLE_ID"]);
1163 if (!$r->isSuccess())
1169 if(isset($formData[
"STATUS_ID"]) && $formData[
"STATUS_ID"] <>
'')
1171 $statusesList = \Bitrix\Sale\OrderStatus::getAllowedUserStatuses(
1176 if(array_key_exists($formData[
"STATUS_ID"], $statusesList))
1179 $r = $order->setField(
"STATUS_ID", $formData[
"STATUS_ID"]);
1180 if (!$r->isSuccess())
1190 public static function fillBasketItems(Basket &$basket, array $productsFormData, Order $order, array $needDataUpdate = array())
1192 $basketItems = $basket->getBasketItems();
1193 $result =
new OrderEditResult();
1194 $catalogProductsIds = array();
1195 $trustData = array();
1199 foreach($basketItems as $item)
1201 $basketCode = $item->getBasketCode();
1203 if(empty($productsFormData[$basketCode]))
1206 $productData = $productsFormData[$basketCode];
1207 $isDataNeedUpdate = in_array($basketCode, $needDataUpdate);
1209 if(isset($productData[
"PRODUCT_PROVIDER_CLASS"]) && $productData[
"PRODUCT_PROVIDER_CLASS"] <>
'')
1211 $item->setField(
"PRODUCT_PROVIDER_CLASS", trim($productData[
"PRODUCT_PROVIDER_CLASS"]));
1218 if(self::$isTrustProductFormData && !$isDataNeedUpdate)
1220 if(!empty($productData[
"PROVIDER_DATA"]) && CheckSerializedData($productData[
"PROVIDER_DATA"]))
1221 $trustData[$basketCode] = unserialize($productData[
"PROVIDER_DATA"], [
'allowed_classes' =>
false]);
1224 if(!empty($trustData[$basketCode]) && $trustData[$basketCode][
"QUANTITY"] == $productData[
"QUANTITY"])
1226 if(!empty($productData[
"SET_ITEMS_DATA"]) && CheckSerializedData($productData[
"SET_ITEMS_DATA"]))
1227 $productData[
"SET_ITEMS"] = unserialize($productData[
"SET_ITEMS_DATA"], [
'allowed_classes' =>
false]);
1229 if(is_array($trustData[$basketCode]) && !empty($trustData[$basketCode]))
1230 self::setProviderTrustData($item, $order, $trustData[$basketCode]);
1234 unset($trustData[$basketCode]);
1238 if (isset($productData[
'NAME']))
1240 $item->setField(
'NAME', $productData[
'NAME']);
1242 $res = $item->setField(
"QUANTITY", $productData[
"QUANTITY"]);
1244 if(!$res->isSuccess())
1247 $justAdded = isset($productsFormData[$basketCode][
'JUST_ADDED']) && $productsFormData[$basketCode][
'JUST_ADDED'] ==
'Y';
1251 foreach($res->getErrors() as $error)
1253 if($error->getCode() ==
'SALE_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY')
1255 $result->setIsTerminal(
true);
1262 if(isset($productData[
"MODULE"]) && $productData[
"MODULE"] ==
"catalog")
1264 $catalogProductsIds[] = $item->getField(
'PRODUCT_ID');
1266 elseif(empty($productData[
"PRODUCT_PROVIDER_CLASS"]))
1269 $availableFields = array_fill_keys($availableFields,
true);
1270 $fillFields = array_intersect_key($productData, $availableFields);
1271 if (!empty($fillFields))
1273 $r = $item->setFields($fillFields);
1278 $catalogData = array();
1280 if(!empty($catalogProductsIds))
1281 $catalogData = OrderBasket::getProductsData($catalogProductsIds, $order->getSiteId(), array(), $order->getUserId());
1283 $providerData = array();
1285 if(!self::$isTrustProductFormData || !empty($needDataUpdate) || self::$needUpdateNewProductPrice)
1287 $params = array(
"AVAILABLE_QUANTITY");
1289 if($order->getId() <= 0)
1290 $params[] =
"PRICE";
1327 foreach($basketItems as $item)
1329 $basketCode = $item->getBasketCode();
1330 $productData = $productsFormData[$basketCode];
1336 $isDataNeedUpdate = in_array($basketCode, $needDataUpdate);
1337 $data[$basketCode] = $item->getFieldValues();
1339 if(!empty($providerData[$basketCode]))
1341 if (static::$isRefreshData ===
true)
1343 unset($providerData[$basketCode][
'QUANTITY']);
1346 $data[$basketCode] = $providerData[$basketCode];
1348 elseif(!empty($trustData[$basketCode]))
1350 $data[$basketCode] = $trustData[$basketCode];
1356 if(is_array($data[$basketCode]) && !empty($data[$basketCode]))
1357 self::setProviderTrustData($item, $order, $data[$basketCode]);
1367 if($order->getId() <= 0 && (empty($data[$basketCode]) || !self::$isTrustProductFormData || $isDataNeedUpdate))
1369 if(empty($providerData[$basketCode]) && $productData[
"PRODUCT_PROVIDER_CLASS"] <>
'')
1373 if(!empty($productData[
"NAME"]))
1374 $name = $productData[
"NAME"];
1376 if(!empty($productData[
"PRODUCT_ID"]))
1377 $name .=
" (".$productData[
'PRODUCT_ID'].
")";
1382 "SALE_ORDEREDIT_PRODUCT_IS_NOT_AVAILABLE",
1384 "#NAME_ID#" => $name
1396 if(isset($data[$basketCode]) && !empty($data[$basketCode]))
1398 $product = $data[$basketCode];
1400 if(isset($productData[
'PRICE']) && isset($productData[
'CUSTOM_PRICE']) && $productData[
'CUSTOM_PRICE'] ==
'Y')
1401 $product[
'PRICE'] = $productData[
'PRICE'];
1402 elseif(isset($product[
'BASE_PRICE']))
1403 $product[
'PRICE'] = $product[
'BASE_PRICE'] - $product[
'DISCOUNT_PRICE'];
1406 if($item->getField(
"MODULE") ==
"catalog")
1408 if(!empty($catalogData[$item->getProductId()]))
1410 $product = array_merge($product, $catalogData[$item->getProductId()]);
1411 unset($productData[
"CURRENCY"]);
1415 if(!self::$isTrustProductFormData || $isDataNeedUpdate)
1417 $product = array_merge($productData, $product);
1422 $isPriceCustom = isset($productData[
'CUSTOM_PRICE']) && $productData[
'CUSTOM_PRICE'] ==
'Y';
1424 if(($order->getId() <= 0 && !$isPriceCustom) || $needUpdateItemPrice)
1425 unset($productData[
'PRICE'], $productData[
'PRICE_BASE'], $productData[
'BASE_PRICE']);
1427 $product = array_merge($product, $productData);
1430 if(isset($product[
"OFFER_ID"]) && intval($product[
"OFFER_ID"]) > 0)
1431 $product[
"PRODUCT_ID"] = $product[
"OFFER_ID"];
1433 $product = array_intersect_key($product, array_flip($item::getAvailableFields()));
1435 if(isset($product[
"MEASURE_CODE"]) && $product[
"MEASURE_CODE"] <>
'')
1437 $measures = OrderBasket::getCatalogMeasures();
1439 if(isset($measures[$product[
"MEASURE_CODE"]]) && $measures[$product[
"MEASURE_CODE"]] <>
'')
1440 $product[
"MEASURE_NAME"] = $measures[$product[
"MEASURE_CODE"]];
1443 if(!isset($product[
"CURRENCY"]) || $product[
"CURRENCY"] ==
'')
1444 $product[
"CURRENCY"] = $order->getCurrency();
1446 if (($productData[
'IS_SET_PARENT'] ??
null) ===
'Y')
1452 $productData[
"OFFER_ID"],
1453 $order->getUserId(),
1454 $order->getSiteId(),
1455 array_merge($product, $productData)
1458 if (!empty($item->getProvider()))
1460 unset($product[
'PRODUCT_PROVIDER_CLASS']);
1465 if(!$res->isSuccess())
1467 foreach($res->getErrors() as $newError)
1469 foreach($result->
getErrors() as $existError)
1470 if($newError->getMessage() == $existError->getMessage())
1501 if(isset($productData[
"PRODUCT_PROVIDER_CLASS"]) && $productData[
"PRODUCT_PROVIDER_CLASS"] <>
'')
1502 $item->
setField(
"PRODUCT_PROVIDER_CLASS", trim($productData[
"PRODUCT_PROVIDER_CLASS"]));
1510 if(self::$isTrustProductFormData && !$needDataUpdate)
1512 if(!empty($productData[
"PROVIDER_DATA"]) && CheckSerializedData($productData[
"PROVIDER_DATA"]))
1513 $data[$basketCode] = unserialize($productData[
"PROVIDER_DATA"], [
'allowed_classes' =>
false]);
1516 if(!empty($data[$basketCode]) && $data[$basketCode] == $productData[
"QUANTITY"])
1518 if(!empty($productData[
"SET_ITEMS_DATA"]) && CheckSerializedData($productData[
"SET_ITEMS_DATA"]))
1519 $productData[
"SET_ITEMS"] = unserialize($productData[
"SET_ITEMS_DATA"], [
'allowed_classes' =>
false]);
1521 if(is_array($data[$basketCode]) && !empty($data[$basketCode]))
1522 self::setProviderTrustData($item, $order, $data[$basketCode]);
1526 unset($data[$basketCode]);
1530 $item->
setField(
"NAME", $productData[
"NAME"]);
1531 $res = $item->
setField(
"QUANTITY", $productData[
"QUANTITY"]);
1533 if(!$res->isSuccess())
1547 if(empty($data[$basketCode]) || !self::$isTrustProductFormData || $needDataUpdate)
1551 if(empty($data[$basketCode]) && $productData[
"PRODUCT_PROVIDER_CLASS"] <>
'')
1555 if(!empty($productData[
"NAME"]))
1556 $name = $productData[
"NAME"];
1558 if(!empty($productData[
"PRODUCT_ID"]))
1559 $name .=
" (".$productData[
'PRODUCT_ID'].
")";
1564 "SALE_ORDEREDIT_PRODUCT_IS_NOT_AVAILABLE",
1566 "#NAME_ID#" => $name
1575 if(is_array($data[$basketCode]) && !empty($data[$basketCode]))
1576 self::setProviderTrustData($item, $order, $data[$basketCode]);
1579 if(isset($data[$basketCode]) && !empty($data[$basketCode]))
1581 $product = $data[$basketCode];
1583 if(isset($productData[
'PRICE']) && isset($productData[
'CUSTOM_PRICE']) && $productData[
'CUSTOM_PRICE'] ==
'Y')
1584 $product[
'PRICE'] = $productData[
'PRICE'];
1585 elseif(isset($product[
'BASE_PRICE']))
1586 $product[
'PRICE'] = $product[
'BASE_PRICE'] - $product[
'DISCOUNT_PRICE'];
1589 if(!self::$isTrustProductFormData)
1591 if(isset($productData[
"MODULE"]) && $productData[
"MODULE"] ==
"catalog")
1593 $data = OrderBasket::getProductDetails(
1595 $productData[
"QUANTITY"],
1596 $order->getUserId(),
1600 $product = array_merge($product, $data);
1603 unset($productData[
"CURRENCY"]);
1606 $product = array_merge($product, $productData);
1608 if(isset($product[
"OFFER_ID"]) || intval($product[
"OFFER_ID"]) >= 0)
1609 $product[
"PRODUCT_ID"] = $product[
"OFFER_ID"];
1611 $product = array_intersect_key($product, array_flip($item::getAvailableFields()));
1613 if(isset($product[
"MEASURE_CODE"]) && $product[
"MEASURE_CODE"] <>
'')
1615 $measures = OrderBasket::getCatalogMeasures();
1617 if(isset($measures[$product[
"MEASURE_CODE"]]) && $measures[$product[
"MEASURE_CODE"]] <>
'')
1618 $product[
"MEASURE_NAME"] = $measures[$product[
"MEASURE_CODE"]];
1621 if(!isset($product[
"CURRENCY"]) || $product[
"CURRENCY"] ==
'')
1622 $product[
"CURRENCY"] = $order->getCurrency();
1624 if($productData[
"IS_SET_PARENT"] ==
"Y")
1628 $productData[
"OFFER_ID"],
1629 $order->getUserId(),
1630 $order->getSiteId(),
1631 array_merge($product, $productData)
1638 public static function setProviderTrustData(
BasketItem $item,
Order $order, array $data)
1650 foreach ($bundle as $bundleItem)
1652 $bundleItemData = $bundleItem->getFields()->getValues();
1672 $res = \CSite::GetList(
"id",
"asc", array(
"ACTIVE" =>
"Y",
"DEF" =>
"Y"));
1674 if($site = $res->Fetch())
1676 $siteId = $site[
"ID"];
1677 $siteName = $site[
"NAME"].
" (".$siteId.
")";
1682 $res = \CSite::GetByID($siteId);
1684 if($site = $res->Fetch())
1685 $siteName = $site[
"NAME"].
" (".$siteId.
")";
1695 foreach($data as $fieldName => $fieldValue)
1697 $fullName = ($path ==
"" ? $fieldName : $path.
"[".$fieldName.
"]");
1699 if(is_array($fieldValue))
1700 $result = array_merge($result, self::restoreFieldsNames($fieldValue, $fullName));
1702 $result[$fullName] = $fieldValue;
1715 $newUserId = (int)$newUserId;
1716 $orderId = (int)$orderId;
1718 $params = array(
'userId' => $newUserId);
1720 if ($oldUserId !==
null)
1722 $oldUserId = (int)$oldUserId;
1723 if ($oldUserId != $newUserId)
1724 $params[
"oldUserId"] = $oldUserId;
1729 $params[
'orderId'] = $orderId;
1758 public static function getDiscountsApplyResult(
Order $order, $needRecalculate =
false)
1760 static $calcResults =
null;
1762 if ($order instanceof
Sale\Archive\
Order)
1765 return $order->getDiscountData();
1768 if ($calcResults ===
null || $needRecalculate)
1770 $discounts = $order->getDiscount();
1772 if ($needRecalculate)
1775 $r = $discounts->calculate();
1777 if ($r->isSuccess())
1779 $discountData = $r->getData();
1780 $order->applyDiscount($discountData);
1784 $calcResults = $discounts->getApplyResult(
true);
1788 return $calcResults ===
null ? array() : $calcResults;
1793 $discounts = self::getDiscountsApplyResult($order, $needRecalculate);
1794 $discounts[
"ORDER"] = array();
1796 if(isset($discounts[
"DISCOUNT_LIST"]) && is_array($discounts[
"DISCOUNT_LIST"]))
1797 $discounts[
"ORDER"][
"DISCOUNT_LIST"] = array_keys($discounts[
"DISCOUNT_LIST"]);
1805 $discounts = array();
1807 if ($order instanceof
Sale\Archive\
Order)
1809 $discounts = $order->getDiscountData();
1810 return $discounts[
'COUPON_LIST'];
1816 $discounts = self::getDiscountsApplyResult($order, $needRecalculate);
1818 if (!empty($couponsList))
1820 foreach ($couponsList as &$oneCoupon)
1823 $oneCoupon[
'JS_STATUS'] =
'BAD';
1825 $oneCoupon[
'JS_STATUS'] =
'ENTERED';
1827 $oneCoupon[
'JS_STATUS'] =
'APPLYED';
1829 $oneCoupon[
'JS_CHECK_CODE'] =
'';
1831 if (isset($oneCoupon[
'CHECK_CODE_TEXT']))
1833 $oneCoupon[
'JS_CHECK_CODE'] = (
1834 is_array($oneCoupon[
'CHECK_CODE_TEXT'])
1835 ? implode(
', ', $oneCoupon[
'CHECK_CODE_TEXT'])
1836 : $oneCoupon[
'CHECK_CODE_TEXT']
1840 if(!empty($discounts) && isset($discounts[
"COUPON_LIST"]) && is_array($discounts[
"COUPON_LIST"]))
1842 foreach($discounts[
"COUPON_LIST"] as $coupon => $couponParams)
1844 $couponsList[$coupon][
"APPLY"] = $couponParams[
"APPLY"];
1845 $couponsList[$coupon][
"DISCOUNT_SIZE"] =
"";
1847 if(isset($couponParams[
"ORDER_DISCOUNT_ID"]) && $couponParams[
"ORDER_DISCOUNT_ID"] <>
'')
1849 $couponsList[$coupon][
"ORDER_DISCOUNT_ID"] = $couponParams[
"ORDER_DISCOUNT_ID"];
1851 if(isset($discounts[
"DISCOUNT_LIST"][$couponParams[
"ORDER_DISCOUNT_ID"]]))
1853 $couponDiscountParams = $discounts[
"DISCOUNT_LIST"][$couponParams[
"ORDER_DISCOUNT_ID"]];
1855 if(isset($couponDiscountParams[
"ACTIONS_DESCR"]) && is_array($couponDiscountParams[
"ACTIONS_DESCR"]))
1856 foreach($couponDiscountParams[
"ACTIONS_DESCR"] as $key => $val)
1857 $couponsList[$coupon][
"DISCOUNT_SIZE"] .= $val;
1864 $result = array_values($couponsList);
1873 'PRICE_TOTAL' => $order->getPrice(),
1874 'TAX_VALUE' => $order->getTaxValue(),
1875 'PRICE_DELIVERY_DISCOUNTED' => $order->getDeliveryPrice(),
1876 'SUM_PAID' => $order->getSumPaid(),
1877 'ORDER_DISCOUNT_VALUE' => $order->getField(
'DISCOUNT_VALUE')
1880 $result[
"SUM_UNPAID"] = $result[
"PRICE_TOTAL"] - $result[
"SUM_PAID"];
1882 if(!$result[
"PRICE_DELIVERY_DISCOUNTED"])
1883 $result[
"PRICE_DELIVERY_DISCOUNTED"] = 0;
1885 if(!$result[
"TAX_VALUE"])
1886 $result[
"TAX_VALUE"] = 0;
1888 $orderDiscount = $order->getDiscount();
1891 $discountsList = self::getDiscountsApplyResult($order, $needRecalculate);
1893 $discountsList = array();
1895 if(isset($discountsList[
"PRICES"][
"DELIVERY"][
"DISCOUNT"]))
1896 $result[
'DELIVERY_DISCOUNT'] = $discountsList[
"PRICES"][
"DELIVERY"][
"DISCOUNT"];
1898 $result[
'DELIVERY_DISCOUNT'] = 0;
1900 $result[
'PRICE_DELIVERY'] = $result[
'PRICE_DELIVERY_DISCOUNTED'] + $result[
'DELIVERY_DISCOUNT'];
1901 $basketData = $orderBasket->
getPrices($discountsList);
1902 $result[
"PRICE_BASKET_DISCOUNTED"] = $basketData[
"BASKET_PRICE"];
1903 $result[
"PRICE_BASKET"] = $basketData[
"BASKET_PRICE_BASE"];
1917 if($productId ==
'')
1926 self::$productsDetails[$productId.
"_".$userId.
"_".$siteId] = $params;
1931 if($productId ==
'')
1942 if(isset(self::$productsDetails[$productId.
"_".$userId.
"_".$siteId]))
1943 $result = self::$productsDetails[$productId.
"_".$userId.
"_".$siteId];
1956 $dbRes = Company\Manager::getList(
1958 'select' => array(
'ID',
'NAME'),
1959 'filter' => array(
'ACTIVE' =>
'Y')
1963 while ($company = $dbRes->fetch())
1964 $result[$company[
"ID"]] = $company[
"NAME"].
" [".$company[
"ID"].
"]";
1969 public static function getLockingMessage($orderId)
1976 $orderClass = $registry->getOrderClassName();
1978 $r = $orderClass::getLockedStatus($orderId);
1980 if ($r->isSuccess())
1982 $lockResult = $r->getData();
1984 if (array_key_exists(
'LOCKED_BY', $lockResult) && intval($lockResult[
'LOCKED_BY']) > 0)
1985 $intLockUserID = intval($lockResult[
'LOCKED_BY']);
1987 if (array_key_exists(
'DATE_LOCK', $lockResult) && $lockResult[
'DATE_LOCK'] instanceof \
Bitrix\Main\
Type\
DateTime)
1988 $strLockTime = $lockResult[
'DATE_LOCK']->toString();
1991 $strLockUserInfo = $intLockUserID;
1993 $userIterator = \Bitrix\Main\UserTable::getList(array(
1994 'select' => array(
'ID',
'LOGIN',
'NAME',
'LAST_NAME',
'SECOND_NAME',
'EMAIL'),
1995 'filter' => array(
'=ID' => $intLockUserID)
1998 if ($arOneUser = $userIterator->fetch())
2000 $strNameFormat = \CSite::GetNameFormat(
true);
2001 $strLockUser = \CUser::FormatName($strNameFormat, $arOneUser);
2002 $strLockUserInfo =
'<a href="/bitrix/admin/user_edit.php?lang='.LANGUAGE_ID.
'&ID='.$intLockUserID.
'">'.$strLockUser.
'</a>';
2006 'SALE_ORDEREDIT_LOCKED',
2008 '#ID#' => $strLockUserInfo,
2009 '#DATE#' => $strLockTime,