Пример #1
0
 /**
  * Remove discount list.
  *
  * @param array|int $discount			Order discount list.
  * @return void
  */
 public static function clearList($discount)
 {
     if (!is_array($discount)) {
         $discount = array($discount);
     }
     Main\Type\Collection::normalizeArrayValuesByInt($discount, true);
     if (empty($discount)) {
         return;
     }
     $conn = Main\Application::getConnection();
     $helper = $conn->getSqlHelper();
     $discountRows = array_chunk($discount, 500);
     $query = 'delete from ' . $helper->quote(self::getTableName()) . ' where ' . $helper->quote('ID') . ' in (';
     foreach ($discountRows as &$row) {
         $conn->queryExecute($query . implode(', ', $row) . ')');
         OrderModulesTable::clearByDiscount($row);
     }
     unset($row, $query, $discountRows, $helper, $conn);
 }
Пример #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;
 }