Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 /**
  * Clear rule list.
  *
  * @param array $filter				Filter for clear rules.
  * @return void
  */
 protected static function clear($filter)
 {
     if (empty($filter) || !is_array($filter)) {
         return;
     }
     $ruleList = array();
     $orderDiscountList = array();
     $orderCouponList = array();
     $ruleIterator = self::getList(array('select' => array('ID', 'ORDER_DISCOUNT_ID', 'COUPON_ID'), 'filter' => $filter));
     while ($rule = $ruleIterator->fetch()) {
         $rule['ID'] = (int) $rule['ID'];
         $rule['ORDER_DISCOUNT_ID'] = (int) $rule['ORDER_DISCOUNT_ID'];
         $rule['COUPON_ID'] = (int) $rule['COUPON_ID'];
         $ruleList[] = $rule['ID'];
     }
     unset($rule, $ruleIterator);
     if (empty($ruleList)) {
         return;
     }
     $conn = Main\Application::getConnection();
     $helper = $conn->getSqlHelper();
     $ruleRows = array_chunk($ruleList, 500);
     $mainQuery = 'delete from ' . $helper->quote(self::getTableName()) . ' where ' . $helper->quote('ID') . ' in (';
     $descrQuery = 'delete from ' . $helper->quote(OrderRulesDescrTable::getTableName()) . ' where ' . $helper->quote('RULE_ID') . ' in (';
     foreach ($ruleRows as &$row) {
         $conn->queryExecute($mainQuery . implode(', ', $row) . ')');
         $conn->queryExecute($descrQuery . implode(', ', $row) . ')');
     }
     unset($row, $descrQuery, $mainQuery, $ruleRows, $ruleList);
     unset($helper, $conn);
     if (!empty($orderDiscountList)) {
         OrderDiscountTable::clearList($orderDiscountList);
     }
     unset($orderDiscountList);
     if (!empty($orderCouponList)) {
         OrderCouponsTable::clearList($orderCouponList);
     }
     unset($orderCouponList);
 }
Exemple #3
0
 /**
  * Save discount result for exist order.
  *
  * @return Result
  */
 protected function saveApply()
 {
     $result = new Result();
     $process = true;
     $order = $this->getOrder();
     $orderId = $order->getId();
     if ($this->convertedOrder) {
         return $result;
     }
     $deleteList = array();
     $rulesIterator = Internals\OrderRulesTable::getList(array('select' => array('ID', 'ORDER_ID'), 'filter' => array('=ORDER_ID' => $orderId)));
     while ($rule = $rulesIterator->fetch()) {
         $rule['ID'] = (int) $rule['ID'];
         $deleteList[$rule['ID']] = $rule['ID'];
     }
     unset($rule, $rulesIterator);
     $rulesList = array();
     if (!empty($this->discountResult['BASKET'])) {
         foreach ($this->discountResult['BASKET'] as $basketCode => $discountList) {
             foreach ($discountList as $discount) {
                 if (!isset($discount['RULE_ID']) || (int) $discount['RULE_ID'] < 0) {
                     $process = false;
                     $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_EXT_DISCOUNT'), self::ERROR_ID));
                     continue;
                 }
                 $orderDiscountId = $discount['DISCOUNT_ID'];
                 $ruleData = array('RULE_ID' => $discount['RULE_ID'], 'APPLY' => $discount['RESULT']['APPLY'], 'DESCR_ID' => isset($discount['RULE_DESCR_ID']) ? (int) $discount['RULE_DESCR_ID'] : 0, 'DESCR' => $discount['RESULT']['DESCR_DATA']['BASKET']);
                 if ($ruleData['DESCR_ID'] <= 0) {
                     $ruleData['DESCR_ID'] = 0;
                     $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID'];
                     $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId;
                     $ruleData['ORDER_ID'] = $orderId;
                 }
                 $rulesList[] = $ruleData;
                 unset($ruleData);
             }
             unset($discount);
         }
         unset($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) {
                     if (!isset($applyData['RULE_ID']) || (int) $applyData['RULE_ID'] < 0) {
                         $process = false;
                         $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_SALE_DISCOUNT'), self::ERROR_ID));
                         continue;
                     }
                     $ruleData = array('RULE_ID' => $applyData['RULE_ID'], 'APPLY' => $applyData['APPLY'], 'DESCR_ID' => isset($applyData['RULE_DESCR_ID']) ? (int) $applyData['RULE_DESCR_ID'] : 0, 'DESCR' => $applyData['DESCR_DATA']);
                     if ($ruleData['DESCR_ID'] <= 0) {
                         $ruleData['DESCR_ID'] = 0;
                         $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID'];
                         $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId;
                         $ruleData['ORDER_ID'] = $orderId;
                     }
                     $rulesList[] = $ruleData;
                     unset($ruleData);
                 }
                 unset($basketCode, $applyData);
             }
             if (!empty($discount['RESULT']['DELIVERY'])) {
                 if (!isset($discount['RESULT']['DELIVERY']['RULE_ID']) || (int) $discount['RESULT']['DELIVERY']['RULE_ID'] < 0) {
                     $process = false;
                     $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_SALE_DISCOUNT'), self::ERROR_ID));
                     continue;
                 }
                 $ruleData = array('RULE_ID' => $discount['RESULT']['DELIVERY']['RULE_ID'], 'APPLY' => $discount['RESULT']['DELIVERY']['APPLY'], 'DESCR_ID' => isset($discount['RESULT']['DELIVERY']['RULE_DESCR_ID']) ? (int) $discount['RESULT']['DELIVERY']['RULE_DESCR_ID'] : 0, 'DESCR' => $discount['RESULT']['DELIVERY']['DESCR_DATA']);
                 if ($ruleData['DESCR_ID'] <= 0) {
                     $ruleData['DESCR_ID'] = 0;
                     $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID'];
                     $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId;
                     $ruleData['ORDER_ID'] = $orderId;
                 }
                 $rulesList[] = $ruleData;
                 unset($ruleData);
             }
         }
         unset($discount);
     }
     if ($process && !empty($rulesList)) {
         foreach ($rulesList as &$ruleRow) {
             $ruleResult = Internals\OrderRulesTable::update($ruleRow['RULE_ID'], array('APPLY' => $ruleRow['APPLY']));
             if ($ruleResult->isSuccess()) {
                 if (isset($deleteList[$ruleRow['RULE_ID']])) {
                     unset($deleteList[$ruleRow['RULE_ID']]);
                 }
                 if ($ruleRow['DESCR_ID'] > 0) {
                     $descrResult = Internals\OrderRulesDescrTable::update($ruleRow['DESCR_ID'], array('DESCR' => $ruleRow['DESCR']));
                 } else {
                     $descrData = array('DESCR' => $ruleRow['DESCR'], 'MODULE_ID' => $ruleRow['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $ruleRow['ORDER_DISCOUNT_ID'], 'ORDER_ID' => $ruleRow['ORDER_ID']);
                     $descrResult = Internals\OrderRulesDescrTable::add($descrData);
                 }
                 if (!$descrResult->isSuccess(true)) {
                     $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 && !empty($deleteList)) {
         $conn = Main\Application::getConnection();
         $helper = $conn->getSqlHelper();
         $ruleRows = array_chunk($deleteList, 500);
         $mainQuery = 'delete from ' . $helper->quote(Internals\OrderRulesTable::getTableName()) . ' where ' . $helper->quote('ID') . ' in (';
         $descrQuery = 'delete from ' . $helper->quote(Internals\OrderRulesDescrTable::getTableName()) . ' where ' . $helper->quote('RULE_ID') . ' in (';
         foreach ($ruleRows as &$row) {
             $conn->queryExecute($mainQuery . implode(', ', $row) . ')');
             $conn->queryExecute($descrQuery . implode(', ', $row) . ')');
         }
         unset($row, $descrQuery, $mainQuery, $ruleRows);
         unset($helper, $conn);
     }
     unset($deleteList);
     if ($process) {
         if (DiscountCouponsManager::usedByManager()) {
             DiscountCouponsManager::clear(true);
         }
     }
     return $result;
 }