Example #1
0
 /**
  *
  *
  */
 public static function DoSaveOrder(&$arOrder, $arAdditionalFields, $orderId, &$arErrors, $arCoupons = array(), $arStoreBarcodeOrderFormData = array(), $bSaveBarcodes = false)
 {
     global $APPLICATION;
     $orderId = (int) $orderId;
     $isNew = $orderId <= 0;
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $arFields = array("ID" => $arOrder["ID"], "LID" => $arOrder["SITE_ID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "PRICE" => $arOrder["PRICE"], "CURRENCY" => $arOrder["CURRENCY"], "USER_ID" => $arOrder["USER_ID"], "PAY_SYSTEM_ID" => $arOrder["PAY_SYSTEM_ID"], "PRICE_DELIVERY" => $arOrder["DELIVERY_PRICE"], "DELIVERY_ID" => strlen($arOrder["DELIVERY_ID"]) > 0 ? $arOrder["DELIVERY_ID"] : false, "DISCOUNT_VALUE" => $arOrder["DISCOUNT_PRICE"], "TAX_VALUE" => $arOrder["TAX_VALUE"], "TRACKING_NUMBER" => $arOrder["TRACKING_NUMBER"]);
     if ($arOrder["DELIVERY_PRICE"] == $arOrder["PRICE_DELIVERY"] && isset($arOrder['PRICE_DELIVERY_DIFF']) && floatval($arOrder['PRICE_DELIVERY_DIFF']) > 0) {
         $arFields["DELIVERY_PRICE"] = $arOrder['PRICE_DELIVERY_DIFF'] + $arOrder["PRICE_DELIVERY"];
     }
     if ($orderId <= 0) {
         $arFields["PAYED"] = "N";
         $arFields["CANCELED"] = "N";
         $arFields["STATUS_ID"] = "N";
     }
     $arFields = array_merge($arFields, $arAdditionalFields);
     if (!$arOrder['LOCATION_IN_CODES']) {
         // it comes from places like crm_invoice`s Add() and tells us if we need to convert location props from ID to CODE
         static::TranslateLocationPropertyValues($arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"]);
     }
     unset($arOrder['LOCATION_IN_CODES']);
     if ($isOrderConverted == "Y") {
         $orderFields = array_merge($arOrder, $arFields, $arAdditionalFields);
         if (isset($orderFields['CUSTOM_DISCOUNT_PRICE']) && $orderFields['CUSTOM_DISCOUNT_PRICE'] === true) {
             Sale\Compatible\DiscountCompatibility::reInit(Sale\Compatible\DiscountCompatibility::MODE_DISABLED);
         }
         if (!empty($arStoreBarcodeOrderFormData)) {
             $orderFields['BARCODE_LIST'] = $arStoreBarcodeOrderFormData;
         }
         $orderFields['BARCODE_SAVE'] = $bSaveBarcodes;
         if ($orderId > 0) {
             $orderFields['ID'] = $orderId;
         }
         /** @var Sale\Result $r */
         $r = Sale\Compatible\OrderCompatibility::modifyOrder(Sale\Compatible\OrderCompatibility::ORDER_COMPAT_ACTION_SAVE, $orderFields);
         if ($r->isSuccess()) {
             $orderId = $r->getId();
         } else {
             foreach ($r->getErrorMessages() as $error) {
                 $arErrors[] = $error;
                 $APPLICATION->ThrowException($error);
             }
             return false;
         }
     } else {
         if ($orderId > 0) {
             $orderId = CSaleOrder::Update($orderId, $arFields);
         } else {
             if (COption::GetOptionString("sale", "product_reserve_condition", "O") == "O") {
                 $arFields["RESERVED"] = "Y";
             }
             $orderId = CSaleOrder::Add($arFields);
         }
         $orderId = (int) $orderId;
         if ($orderId <= 0) {
             if ($ex = $APPLICATION->GetException()) {
                 $arErrors[] = $ex->GetString();
             } else {
                 $arErrors[] = Loc::getMessage("SOA_ERROR_ORDER");
             }
         }
         if (!empty($arErrors)) {
             return null;
         }
         CSaleBasket::DoSaveOrderBasket($orderId, $arOrder["SITE_ID"], $arOrder["USER_ID"], $arOrder["BASKET_ITEMS"], $arErrors, $arCoupons, $arStoreBarcodeOrderFormData, $bSaveBarcodes);
         CSaleTax::DoSaveOrderTax($orderId, $arOrder["TAX_LIST"], $arErrors);
         CSaleOrderProps::DoSaveOrderProps($orderId, $arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"], $arErrors);
         Sale\DiscountCouponsManager::finalApply();
         Sale\DiscountCouponsManager::saveApplied();
         foreach (GetModuleEvents("sale", "OnOrderSave", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($orderId, $arFields, $arOrder, $isNew));
         }
     }
     return $orderId;
 }
Example #2
0
 /**
  *
  *
  */
 public static function DoSaveOrder(&$arOrder, $arAdditionalFields, $orderId, &$arErrors, $arCoupons = array(), $arStoreBarcodeOrderFormData = array(), $bSaveBarcodes = false)
 {
     global $APPLICATION;
     $orderId = (int) $orderId;
     $isNew = $orderId <= 0;
     $arFields = array("LID" => $arOrder["SITE_ID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "PRICE" => $arOrder["PRICE"], "CURRENCY" => $arOrder["CURRENCY"], "USER_ID" => $arOrder["USER_ID"], "PAY_SYSTEM_ID" => $arOrder["PAY_SYSTEM_ID"], "PRICE_DELIVERY" => $arOrder["DELIVERY_PRICE"], "DELIVERY_ID" => strlen($arOrder["DELIVERY_ID"]) > 0 ? $arOrder["DELIVERY_ID"] : false, "DISCOUNT_VALUE" => $arOrder["DISCOUNT_PRICE"], "TAX_VALUE" => $arOrder["TAX_VALUE"], "TRACKING_NUMBER" => $arOrder["TRACKING_NUMBER"]);
     if ($orderId <= 0) {
         $arFields["PAYED"] = "N";
         $arFields["CANCELED"] = "N";
         $arFields["STATUS_ID"] = "N";
     }
     $arFields = array_merge($arFields, $arAdditionalFields);
     if (!$arOrder['LOCATION_IN_CODES']) {
         // it comes from places like crm_invoice`s Add() and tells us if we need to convert location props from ID to CODE
         static::TranslateLocationPropertyValues($arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"]);
     }
     unset($arOrder['LOCATION_IN_CODES']);
     if ($orderId > 0) {
         $orderId = CSaleOrder::Update($orderId, $arFields);
     } else {
         if (COption::GetOptionString("sale", "product_reserve_condition", "O") == "O") {
             $arFields["RESERVED"] = "Y";
         }
         $orderId = CSaleOrder::Add($arFields);
     }
     $orderId = (int) $orderId;
     if ($orderId <= 0) {
         if ($ex = $APPLICATION->GetException()) {
             $arErrors[] = $ex->GetString();
         } else {
             $arErrors[] = Loc::getMessage("SOA_ERROR_ORDER");
         }
     }
     if (!empty($arErrors)) {
         return null;
     }
     CSaleBasket::DoSaveOrderBasket($orderId, $arOrder["SITE_ID"], $arOrder["USER_ID"], $arOrder["BASKET_ITEMS"], $arErrors, $arCoupons, $arStoreBarcodeOrderFormData, $bSaveBarcodes);
     CSaleTax::DoSaveOrderTax($orderId, $arOrder["TAX_LIST"], $arErrors);
     CSaleOrderProps::DoSaveOrderProps($orderId, $arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"], $arErrors);
     DiscountCouponsManager::finalApply();
     DiscountCouponsManager::saveApplied();
     foreach (GetModuleEvents("sale", "OnOrderSave", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($orderId, $arFields, $arOrder, $isNew));
     }
     /*
     			// mail message
     			if (empty($arResult["ERROR"]))
     			{
     				$strOrderList = "";
     				$dbBasketItems = CSaleBasket::GetList(
     						array("NAME" => "ASC"),
     						array("ORDER_ID" => $arResult["ORDER_ID"]),
     						false,
     						false,
     						array("ID", "NAME", "QUANTITY", "PRICE", "CURRENCY")
     					);
     				while ($arBasketItems = $dbBasketItems->Fetch())
     				{
     					$strOrderList .= $arBasketItems["NAME"]." - ".$arBasketItems["QUANTITY"]." ".Loc::getMessage("SOA_SHT").": ".SaleFormatCurrency($arBasketItems["PRICE"], $arBasketItems["CURRENCY"]);
     					$strOrderList .= "\n";
     				}
     
     				$arFields = Array(
     					"ORDER_ID" => $arResult["ORDER_ID"],
     					"ORDER_DATE" => Date($DB->DateFormatToPHP(CLang::GetDateFormat("SHORT", SITE_ID))),
     					"ORDER_USER" => ( (strlen($arUserResult["PAYER_NAME"]) > 0) ? $arUserResult["PAYER_NAME"] : $USER->GetFullName() ),
     					"PRICE" => SaleFormatCurrency($totalOrderPrice, $arResult["BASE_LANG_CURRENCY"]),
     					"BCC" => COption::GetOptionString("sale", "order_email", "order@".$SERVER_NAME),
     					"EMAIL" => (strlen($arUserResult["USER_EMAIL"])>0 ? $arUserResult["USER_EMAIL"] : $USER->GetEmail()),
     					"ORDER_LIST" => $strOrderList,
     					"SALE_EMAIL" => COption::GetOptionString("sale", "order_email", "order@".$SERVER_NAME),
     					"DELIVERY_PRICE" => $arResult["DELIVERY_PRICE"],
     				);
     				$eventName = "SALE_NEW_ORDER";
     
     				$bSend = true;
     				foreach(GetModuleEvents("sale", "OnOrderNewSendEmail", true) as $arEvent)
     					if (ExecuteModuleEventEx($arEvent, Array($arResult["ORDER_ID"], &$eventName, &$arFields))===false)
     						$bSend = false;
     
     				if($bSend)
     				{
     					$event = new CEvent;
     					$event->Send($eventName, SITE_ID, $arFields, "N");
     				}
     			}
     
     			if(CModule::IncludeModule("statistic"))
     			{
     				$event1 = "eStore";
     				$event2 = "order_confirm";
     				$event3 = $arResult["ORDER_ID"];
     
     				$e = $event1."/".$event2."/".$event3;
     
     				if(!is_array($_SESSION["ORDER_EVENTS"]) || (is_array($_SESSION["ORDER_EVENTS"]) && !in_array($e, $_SESSION["ORDER_EVENTS"])))
     				{
     						CStatistic::Set_Event($event1, $event2, $event3);
     						$_SESSION["ORDER_EVENTS"][] = $e;
     				}
     			}
     			$arOrder = CSaleOrder::GetByID($arResult["ORDER_ID"]);
     			foreach(GetModuleEvents("sale", "OnSaleComponentOrderOneStepComplete", true) as $arEvent)
     				ExecuteModuleEventEx($arEvent, Array($arResult["ORDER_ID"], $arOrder));
     */
     return $orderId;
 }
Example #3
0
 function OrderBasket($orderID, $fuserID = 0, $strLang = SITE_ID, $arDiscounts = False)
 {
     $orderID = (int) $orderID;
     if ($orderID <= 0) {
         return false;
     }
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         $fuserID = (int) CSaleBasket::GetBasketUserID(true);
     }
     if ($fuserID <= 0) {
         return false;
     }
     $arOrder = array();
     if (empty($arOrder)) {
         $rsOrders = CSaleOrder::GetList(array(), array('ID' => $orderID), false, false, array('ID', 'USER_ID', 'RECURRING_ID', 'LID', 'RESERVED'));
         if (!($arOrder = $rsOrders->Fetch())) {
             return false;
         }
         $arOrder['RECURRING_ID'] = (int) $arOrder['RECURRING_ID'];
     }
     $boolRecurring = $arOrder['RECURRING_ID'] > 0;
     $needSaveCoupons = false;
     $dbBasketList = CSaleBasket::GetList(array("PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $strLang, "ORDER_ID" => 0), false, false, array('ID', 'ORDER_ID', 'PRODUCT_ID', 'MODULE', 'CAN_BUY', 'DELAY', 'ORDER_CALLBACK_FUNC', 'PRODUCT_PROVIDER_CLASS', 'QUANTITY'));
     while ($arBasket = $dbBasketList->Fetch()) {
         $arFields = array();
         if ($arBasket["DELAY"] == "N" && $arBasket["CAN_BUY"] == "Y") {
             if (!empty($arBasket["ORDER_CALLBACK_FUNC"]) || !empty($arBasket["PRODUCT_PROVIDER_CLASS"])) {
                 /** @var $productProvider IBXSaleProductProvider */
                 if ($productProvider = CSaleBasket::GetProductProvider($arBasket)) {
                     $arQuery = array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "QUANTITY" => $arBasket["QUANTITY"], 'BASKET_ID' => $arBasket['ID']);
                     if ($boolRecurring) {
                         $arQuery['RENEWAL'] = 'Y';
                         $arQuery['USER_ID'] = $arOrder['USER_ID'];
                         $arQuery['SITE_ID'] = $strLang;
                     }
                     $arFields = $productProvider::OrderProduct($arQuery);
                 } else {
                     if ($boolRecurring) {
                         $arFields = CSaleBasket::ExecuteCallbackFunction($arBasket["ORDER_CALLBACK_FUNC"], $arBasket["MODULE"], $arBasket["PRODUCT_ID"], $arBasket["QUANTITY"], 'Y', $arOrder['USER_ID'], $strLang);
                     } else {
                         $arFields = CSaleBasket::ExecuteCallbackFunction($arBasket["ORDER_CALLBACK_FUNC"], $arBasket["MODULE"], $arBasket["PRODUCT_ID"], $arBasket["QUANTITY"]);
                     }
                 }
                 if (!empty($arFields) && is_array($arFields)) {
                     $arFields["CAN_BUY"] = "Y";
                     $arFields["ORDER_ID"] = $orderID;
                     $needSaveCoupons = true;
                 } else {
                     $arFields = array('CAN_BUY' => 'N');
                     $removeCoupon = DiscountCouponsManager::deleteApplyByProduct(array('MODULE' => $arBasket['MODULE'], 'PRODUCT_ID' => $arBasket['PRODUCT_ID'], 'BASKET_ID' => $arBasket['ID']));
                 }
             } else {
                 $arFields["ORDER_ID"] = $orderID;
                 $needSaveCoupons = true;
             }
             if (!empty($arFields)) {
                 if (CSaleBasket::Update($arBasket["ID"], $arFields)) {
                     $_SESSION["SALE_BASKET_NUM_PRODUCTS"][SITE_ID]--;
                 }
             }
         }
     }
     //end of while
     if ($_SESSION["SALE_BASKET_NUM_PRODUCTS"][SITE_ID] < 0) {
         $_SESSION["SALE_BASKET_NUM_PRODUCTS"][SITE_ID] = 0;
     }
     foreach (GetModuleEvents("sale", "OnBasketOrder", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($orderID, $fuserID, $strLang, $arDiscounts));
     }
     if ($needSaveCoupons) {
         DiscountCouponsManager::finalApply();
         DiscountCouponsManager::saveApplied();
     }
     //reservation
     if ($arOrder['RESERVED'] != "Y" && COption::GetOptionString("sale", "product_reserve_condition") == "O") {
         if (!CSaleOrder::ReserveOrder($orderID, "Y")) {
             return false;
         }
     }
     return true;
 }
Example #4
0
 /**
  * Save discount result for new order.
  *
  * @return Result
  */
 protected function saveFull()
 {
     $result = new Result();
     $process = true;
     $order = $this->getOrder();
     $orderId = $order->getId();
     $basketResult = $this->getBasketTables();
     if (!$basketResult->isSuccess()) {
         $process = false;
         $result->addErrors($basketResult->getErrors());
     }
     if ($process) {
         DiscountCouponsManager::finalApply();
         DiscountCouponsManager::saveApplied();
         $couponsResult = $this->saveCoupons();
         if (!$couponsResult->isSuccess()) {
             $process = false;
             $result->addErrors($couponsResult->getErrors());
         }
     }
     if ($process) {
         //TODO: add in check - is old public (CSaleOrder::Add + CSaleBasket::OrderBasket)
         /*			if (!$this->newOrder)
         			{ */
         Internals\OrderRulesTable::clearByOrder($orderId);
         Internals\OrderDiscountDataTable::clearByOrder($orderId);
         //}
         $rulesList = array();
         $ruleDescr = array();
         $ruleIndex = 0;
         if (!empty($this->discountResult['BASKET'])) {
             foreach ($this->discountResult['BASKET'] as $basketCode => $discountList) {
                 $basketId = $this->forwardBasketTable[$basketCode];
                 foreach ($discountList as $discount) {
                     $orderDiscountId = $discount['DISCOUNT_ID'];
                     $rulesList[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'ENTITY_TYPE' => Internals\OrderRulesTable::ENTITY_TYPE_BASKET, 'ENTITY_ID' => $basketId, 'ENTITY_VALUE' => $basketId, 'COUPON_ID' => $discount['COUPON_ID'] != '' ? $this->couponsCache[$discount['COUPON_ID']]['ID'] : 0, 'APPLY' => $discount['RESULT']['APPLY']);
                     $ruleDescr[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'DESCR' => $discount['RESULT']['DESCR_DATA']);
                     $ruleIndex++;
                 }
                 unset($discount);
             }
             unset($basketId, $basketCode, $discountList);
         }
         if (!empty($this->discountResult['ORDER'])) {
             foreach ($this->discountResult['ORDER'] as $discount) {
                 $orderDiscountId = $discount['DISCOUNT_ID'];
                 if (!empty($discount['RESULT']['BASKET'])) {
                     foreach ($discount['RESULT']['BASKET'] as $basketCode => $applyData) {
                         $basketId = $this->forwardBasketTable[$basketCode];
                         $rulesList[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'ENTITY_TYPE' => Internals\OrderRulesTable::ENTITY_TYPE_BASKET, 'ENTITY_ID' => $basketId, 'ENTITY_VALUE' => $basketId, 'COUPON_ID' => $discount['COUPON_ID'] != '' ? $this->couponsCache[$discount['COUPON_ID']]['ID'] : 0, 'APPLY' => $applyData['APPLY']);
                         $ruleDescr[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'DESCR' => $applyData['DESCR_DATA']);
                         $ruleIndex++;
                     }
                     unset($basketCode, $applyData);
                 }
                 if (!empty($discount['RESULT']['DELIVERY'])) {
                     $rulesList[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'ENTITY_TYPE' => Internals\OrderRulesTable::ENTITY_TYPE_DELIVERY, 'ENTITY_ID' => (int) $discount['RESULT']['DELIVERY']['DELIVERY_ID'], 'ENTITY_VALUE' => (string) $discount['RESULT']['DELIVERY']['DELIVERY_ID'], 'COUPON_ID' => $discount['COUPON_ID'] != '' ? $this->couponsCache[$discount['COUPON_ID']]['ID'] : 0, 'APPLY' => $discount['RESULT']['DELIVERY']['APPLY']);
                     $ruleDescr[$ruleIndex] = array('MODULE_ID' => $this->discountsCache[$orderDiscountId]['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'DESCR' => $discount['RESULT']['DELIVERY']['DESCR_DATA']);
                     $ruleIndex++;
                 }
             }
             unset($discount);
         }
         if (!empty($rulesList)) {
             foreach ($rulesList as $index => $ruleRow) {
                 $ruleResult = Internals\OrderRulesTable::add($ruleRow);
                 if ($ruleResult->isSuccess()) {
                     $ruleDescr[$index]['RULE_ID'] = $ruleResult->getId();
                     $descrResult = Internals\OrderRulesDescrTable::add($ruleDescr[$index]);
                     if (!$descrResult->isSuccess()) {
                         $process = false;
                         $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'), self::ERROR_ID));
                         unset($descrResult);
                         break;
                     }
                     unset($descrResult);
                 } else {
                     $process = false;
                     $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'), self::ERROR_ID));
                     unset($ruleResult);
                     break;
                 }
                 unset($ruleResult);
             }
             unset($ruleRow);
         }
     }
     if ($process) {
         $fields = array('ORDER_ID' => $orderId, 'ENTITY_TYPE' => Internals\OrderDiscountDataTable::ENTITY_TYPE_ORDER, 'ENTITY_ID' => $orderId, 'ENTITY_VALUE' => $orderId, 'ENTITY_DATA' => array('OPTIONS' => array('USE_BASE_PRICE' => Main\Config\Option::get('sale', 'get_discount_percent_from_base_price'), 'SALE_DISCOUNT_ONLY' => Main\Config\Option::get('sale', 'use_sale_discount_only'), 'APPLY_MODE' => Main\Config\Option::get('sale', 'discount_apply_mode')), 'DELIVERY' => array('DELIVERY_ID' => $this->orderData['DELIVERY_ID'], 'CUSTOM_PRICE_DELIVERY' => $this->orderData['CUSTOM_PRICE_DELIVERY'], 'SHIPMENT_ID' => 0)));
         if ($this->shipment instanceof Shipment) {
             $fields['ENTITY_DATA']['DELIVERY']['SHIPMENT_ID'] = $this->shipment->getId();
         }
         $dataResult = Internals\OrderDiscountDataTable::add($fields);
         if (!$dataResult->isSuccess()) {
             $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'), self::ERROR_ID));
         }
         unset($dataResult, $fields);
         if (!empty($this->basketBasePrice)) {
             $orderCurrency = $order->getCurrency();
             foreach ($this->basketBasePrice as $basketCode => $basePrice) {
                 if (!isset($this->forwardBasketTable[$basketCode])) {
                     continue;
                 }
                 $fields = array('BASE_PRICE' => (string) $basePrice, 'BASE_PRICE_CURRENCY' => $orderCurrency);
                 Internals\OrderDiscountDataTable::saveBasketItemData($orderId, $this->forwardBasketTable[$basketCode], $fields, false);
             }
             unset($basketCode, $basePrice);
         }
     }
     if ($process) {
         if (DiscountCouponsManager::usedByManager()) {
             DiscountCouponsManager::clear(true);
         }
     }
     return $result;
 }