Пример #1
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);
 }
Пример #2
0
 /**
  * Save converted discount.
  *
  * @param array $discountData				Discount data.
  * @return Result
  * @throws Main\ArgumentException
  * @throws \Exception
  */
 private static function saveMigrateDiscount($discountData)
 {
     $result = new Result();
     $process = true;
     $hash = false;
     $resultData = array();
     $fields = Internals\OrderDiscountTable::prepareDiscountData($discountData);
     if (empty($fields) || !is_array($fields)) {
         $process = false;
         $result->addError(new Main\Entity\EntityError(Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_BAD_PREPARE_DISCOUNT'), self::ERROR_ID));
     }
     if ($process) {
         $hash = Internals\OrderDiscountTable::calculateHash($fields);
         if ($hash === false) {
             $process = false;
             $result->addError(new Main\Entity\EntityError(Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_BAD_DISCOUNT_HASH'), self::ERROR_ID));
         }
     }
     if ($process) {
         if (!isset(self::$migrateDiscountsCache[$hash])) {
             $orderDiscountIterator = Internals\OrderDiscountTable::getList(array('select' => array('*'), 'filter' => array('=DISCOUNT_HASH' => $hash)));
             if ($orderDiscount = $orderDiscountIterator->fetch()) {
                 self::$migrateDiscountsCache[$hash] = $orderDiscount;
             }
             unset($orderDiscount, $orderDiscountIterator);
         }
         if (!empty(self::$migrateDiscountsCache[$hash])) {
             $resultData = self::$migrateDiscountsCache[$hash];
             $resultData['ID'] = (int) $resultData['ID'];
             $resultData['NAME'] = (string) $resultData['NAME'];
             $resultData['ORDER_DISCOUNT_ID'] = $resultData['ID'];
             $result->setId($resultData['ID']);
         } else {
             $fields['DISCOUNT_HASH'] = $hash;
             $fields['ACTIONS_DESCR'] = array();
             if (isset($discountData['ACTIONS_DESCR'])) {
                 $fields['ACTIONS_DESCR'] = $discountData['ACTIONS_DESCR'];
             }
             $tableResult = Internals\OrderDiscountTable::add($fields);
             if ($tableResult->isSuccess()) {
                 $resultData = $fields;
                 $resultData['ID'] = (int) $tableResult->getId();
                 $resultData['NAME'] = (string) $resultData['NAME'];
                 $resultData['ORDER_DISCOUNT_ID'] = $resultData['ID'];
                 $result->setId($resultData['ID']);
             } else {
                 $process = false;
                 $result->addErrors($tableResult->getErrors());
             }
             unset($tableResult, $fields);
             if ($process) {
                 $moduleList = Internals\OrderDiscountTable::getDiscountModules($discountData);
                 if (!empty($moduleList)) {
                     $resultModule = Internals\OrderModulesTable::saveOrderDiscountModules($resultData['ORDER_DISCOUNT_ID'], $moduleList);
                     if (!$resultModule) {
                         Internals\OrderDiscountTable::clearList($resultData['ORDER_DISCOUNT_ID']);
                         $resultData = array();
                         $process = false;
                         $result->addError(new Main\Entity\EntityError(Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_SAVE_DISCOUNT_MODULES'), self::ERROR_ID));
                     }
                     unset($resultModule);
                 }
                 unset($needDiscountModules, $moduleList);
             }
         }
     }
     if ($process) {
         $result->setData($resultData);
     }
     unset($resultData, $process);
     return $result;
 }