/** * Convert discount for old order. * * @param int $orderId Order id. * @param array &$data Discount data. * @return bool * @throws Main\LoaderException */ private static function migrateDiscount($orderId, &$data) { if (self::$catalogIncluded === null) { self::$catalogIncluded = Main\Loader::includeModule('catalog'); } if (!self::$catalogIncluded) { return false; } $discountData = array('COUPON' => '', 'NAME' => '', 'DISCOUNT_ID' => 0); if ($data['DISCOUNT_NAME'] != '') { $discountName = array(); if (preg_match('/^\\[(\\d+)\\][ ](.+)$/', $data['DISCOUNT_NAME'], $discountName) == 1) { $discountData['NAME'] = $discountName[2]; $discountData['DISCOUNT_ID'] = $discountName[1]; } unset($discountName); } if ($data['DISCOUNT_COUPON'] != '') { $discountData['COUPON'] = $data['DISCOUNT_COUPON']; if (!self::checkMigrateCoupon($discountData['COUPON'])) { return false; } if ($discountData['DISCOUNT_ID'] == 0) { $discountData['NAME'] = self::$migrateCouponsCache[$discountData['COUPON']]['DISCOUNT_NAME']; $discountData['DISCOUNT_ID'] = self::$migrateCouponsCache[$discountData['COUPON']]['DISCOUNT_ID']; } else { if (self::$migrateCouponsCache[$discountData['COUPON']]['TYPE'] != Internals\DiscountCouponTable::TYPE_ARCHIVED && self::$migrateCouponsCache[$discountData['COUPON']]['DISCOUNT_ID'] >= 0 && $discountData['DISCOUNT_ID'] != self::$migrateCouponsCache[$discountData['COUPON']]['DISCOUNT_ID']) { $discountData['DISCOUNT_ID'] = 0; } } } if ($discountData['DISCOUNT_ID'] == 0) { if ($discountData['COUPON'] == '') { return false; } self::createEmptyDiscount($discountData); } else { self::checkMigrateDiscount($discountData); } $saveResult = self::saveMigrateDiscount($discountData); if (!$saveResult->isSuccess()) { return false; } $migrateDiscountData = $saveResult->getData(); unset($saveResult); $orderDiscountId = $migrateDiscountData['ORDER_DISCOUNT_ID']; $orderCouponId = 0; $discountDescr = current($migrateDiscountData['ACTIONS_DESCR']['BASKET']); if ($discountData['COUPON'] != '') { $couponData = self::$migrateCouponsCache[$discountData['COUPON']]; $couponData['ORDER_ID'] = $orderId; $couponData['ORDER_DISCOUNT_ID'] = $migrateDiscountData['ORDER_DISCOUNT_ID']; $couponData['DATA']['DISCOUNT_ID'] = $migrateDiscountData['DISCOUNT_ID']; if (array_key_exists('DISCOUNT_ID', $couponData)) { unset($couponData['DISCOUNT_ID']); } if (array_key_exists('DISCOUNT_NAME', $couponData)) { unset($couponData['DISCOUNT_NAME']); } $saveResult = self::saveCoupon($couponData); if (!$saveResult->isSuccess()) { return false; } $migrateCoupon = $saveResult->getData(); $orderCouponId = $migrateCoupon['ID']; } foreach ($data['ITEMS'] as $basketItem) { $applyDescr = $discountDescr; if ($basketItem['DISCOUNT_VALUE'] != '') { if ($applyDescr['TYPE'] == self::DESCR_TYPE_SIMPLE) { $applyDescr['DESCR'] .= ' (' . $basketItem['DISCOUNT_VALUE'] . ')'; } else { $valueData = array(); if (preg_match('/^(|\\+|-)(\\d+|[.,]\\d+|\\d+[.,]\\d+)\\s?%$/', $basketItem['DISCOUNT_VALUE'], $valueData) == 1) { $applyDescr['RESULT_VALUE'] = (double) $basketItem['DISCOUNT_VALUE']; $applyDescr['RESULT_UNIT'] = self::DESCR_VALUE_TYPE_PERCENT; } unset($valueData); } } $ruleRow = array('MODULE_ID' => 'catalog', 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'ENTITY_TYPE' => Internals\OrderRulesTable::ENTITY_TYPE_BASKET, 'ENTITY_ID' => $basketItem['ID'], 'ENTITY_VALUE' => $basketItem['ID'], 'COUPON_ID' => $orderCouponId, 'APPLY' => 'Y'); $ruleDescr = array('MODULE_ID' => 'catalog', 'ORDER_DISCOUNT_ID' => $orderDiscountId, 'ORDER_ID' => $orderId, 'DESCR' => array($applyDescr)); $ruleResult = Internals\OrderRulesTable::add($ruleRow); if ($ruleResult->isSuccess()) { $ruleDescr['RULE_ID'] = $ruleResult->getId(); $descrResult = Internals\OrderRulesDescrTable::add($ruleDescr); if (!$descrResult->isSuccess()) { return false; } } else { return false; } unset($ruleResult); } unset($basketItem); return true; }
/** * 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; }