Esempio n. 1
0
 /**
  * @param array $select
  * @param BasketItem $refreshItem
  * @return Result
  * @throws Main\ArgumentNullException
  * @throws Main\NotSupportedException
  * @throws Main\ObjectNotFoundException
  */
 public function refreshData($select = array(), BasketItem $refreshItem = null)
 {
     $result = new Result();
     $isStartField = $this->isStartField();
     $discount = null;
     /** @var Order $order */
     if ($order = $this->getOrder()) {
         $discount = $order->getDiscount();
     }
     $settableFields = array_fill_keys(BasketItem::getSettableFields(), true);
     $data = Provider::getProductData($this, $select, $refreshItem);
     foreach ($data as $key => $value) {
         //$item = $this->getItemByBasketCode($key);
         if (!($item = $this->getItemByBasketCode($key))) {
             continue;
         }
         if (isset($value['DELETE']) && $value['DELETE']) {
             $item->delete();
             if ($discount !== null) {
                 $discount->clearBasketItemData($key);
             }
             continue;
         }
         $basePrice = false;
         $currency = false;
         $discountList = false;
         $value1 = array();
         $roundFields = static::getRoundFields();
         foreach ($value as $k => $v) {
             //TODO: create method for save data in discount
             if ($k == 'BASE_PRICE') {
                 $basePrice = true;
             }
             if ($k == 'CURRENCY') {
                 $currency = true;
             }
             if ($k == 'DISCOUNT_LIST' && !empty($v)) {
                 $discountList = true;
             }
             //TODO END
             if (isset($settableFields[$k])) {
                 if ($item) {
                     if ($k == "PRICE" && $item->isCustomPrice()) {
                         $v = $item->getPrice();
                     }
                     if (in_array($k, $roundFields)) {
                         $v = roundEx($v, SALE_VALUE_PRECISION);
                     }
                 }
                 $value1[$k] = $v;
             }
         }
         if (!$item->isCustomPrice()) {
             $value1['PRICE'] = $value1['BASE_PRICE'] - $value1['DISCOUNT_PRICE'];
         }
         if (!$item) {
             $item = $this->createItem($value['MODULE_ID'], $value['PRODUCT_ID']);
         }
         if (!$item) {
             continue;
         }
         if ($discount !== null) {
             if ($basePrice && $currency) {
                 $discount->setBasketItemBasePrice($key, $value['BASE_PRICE'], $value['CURRENCY']);
             }
             if ($discountList) {
                 $discount->setBasketItemDiscounts($key, $value['DISCOUNT_LIST']);
             }
         }
         /** @var Result $r */
         $r = $item->setFields($value1);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     /** @var Order $order */
     if ($order = $this->getOrder()) {
         $r = $order->refreshData(array('PRICE', 'PRICE_DELIVERY'));
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if ($isStartField) {
         $hasMeaningfulFields = $this->hasMeaningfulField();
         /** @var Result $r */
         $r = $this->doFinalAction($hasMeaningfulFields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     return $result;
 }
Esempio n. 2
0
 /**
  * @param array $select
  * @param BasketItem $refreshItem
  * @return Result
  * @throws Main\ArgumentNullException
  * @throws Main\NotSupportedException
  * @throws Main\ObjectNotFoundException
  */
 public function refreshData($select = array(), BasketItem $refreshItem = null)
 {
     $result = new Result();
     $isStartField = $this->isStartField();
     $discount = null;
     /** @var Order $order */
     if ($order = $this->getOrder()) {
         $discount = $order->getDiscount();
     }
     $settableFields = array_fill_keys(BasketItem::getSettableFields(), true);
     $data = Provider::getProductData($this, $select, $refreshItem);
     foreach ($data as $key => $value) {
         /** @var null|BasketItem $item */
         $item = null;
         //$item = $this->getItemByBasketCode($key);
         //			if(!($item = $this->getItemByBasketCode($key)))
         //			{
         //				continue;
         //			}
         $item = $this->getItemByBasketCode($key);
         if (isset($value['DELETE']) && $value['DELETE']) {
             if ($item) {
                 $item->delete();
                 if ($discount !== null) {
                     $discount->clearBasketItemData($key);
                 }
             }
             continue;
         }
         $value1 = array();
         $roundFields = static::getRoundFields();
         foreach ($value as $k => $v) {
             if (isset($settableFields[$k])) {
                 if ($item) {
                     if ($k == "PRICE" && $item->isCustomPrice()) {
                         $v = $item->getPrice();
                     }
                     if (in_array($k, $roundFields)) {
                         $v = PriceMaths::roundPrecision($v);
                     }
                 }
                 $value1[$k] = $v;
             }
         }
         if (!$item) {
             $item = $this->createItem($value['MODULE_ID'], $value['PRODUCT_ID']);
         }
         if (!$item) {
             continue;
         }
         if (!$item->isCustomPrice() && array_key_exists('DISCOUNT_PRICE', $value1) && array_key_exists('BASE_PRICE', $value1)) {
             $value1['PRICE'] = $value1['BASE_PRICE'] - $value1['DISCOUNT_PRICE'];
         }
         if ($discount instanceof Discount) {
             $discount->setBasketItemData($key, $value);
         }
         $isBundleParent = (bool) ($item && $item->isBundleParent());
         /** @var Result $r */
         $r = $item->setFields($value1);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
         if (($isBundleParent || $item->isBundleParent()) && array_key_exists('BUNDLE_ITEMS', $value)) {
             /** @var BasketBundleCollection $bundleCollection */
             if ($bundleCollection = $item->getBundleCollection()) {
                 $bundleIndexList = array();
                 /** @var BasketItem $bundleBasketItem */
                 foreach ($bundleCollection as $bundleBasketItem) {
                     $bundleIndexList[$bundleBasketItem->getBasketCode()] = true;
                 }
                 /** @var array $bundleBasketItemData */
                 foreach ($value['BUNDLE_ITEMS'] as $bundleBasketItemData) {
                     if (empty($bundleBasketItemData['MODULE']) || empty($bundleBasketItemData['PRODUCT_ID'])) {
                         continue;
                     }
                     /** @var BasketItem $bundleBasketItem */
                     if ($bundleBasketItem = $bundleCollection->getExistsItem($bundleBasketItemData['MODULE'], $bundleBasketItemData['PRODUCT_ID'], !empty($bundleBasketItemData['PROPS']) && is_array($bundleBasketItemData['PROPS']) ? $bundleBasketItemData['PROPS'] : array())) {
                         if (isset($bundleIndexList[$bundleBasketItem->getBasketCode()])) {
                             unset($bundleIndexList[$bundleBasketItem->getBasketCode()]);
                         }
                     } else {
                         /** @var BasketItem $bundleBasketItem */
                         $bundleBasketItem = BasketItem::create($bundleCollection, $bundleBasketItemData['MODULE'], $bundleBasketItemData['PRODUCT_ID']);
                     }
                     if (!$bundleBasketItem) {
                         continue;
                     }
                     $fields = array_intersect_key($bundleBasketItemData, $settableFields);
                     $r = $bundleBasketItem->setFields($fields);
                     if (!$r->isSuccess()) {
                         $result->addErrors($r->getErrors());
                     }
                 }
                 if (!empty($bundleIndexList) && is_array($bundleIndexList)) {
                     foreach ($bundleIndexList as $bundleBasketItemCode => $bundleItemValue) {
                         /** @var BasketItem $bundleBasketItem */
                         if ($bundleBasketItem = $bundleCollection->getItemByBasketCode($bundleBasketItemCode)) {
                             $bundleBasketItem->delete();
                         }
                     }
                 }
             }
         }
     }
     /** @var Order $order */
     if ($order = $this->getOrder()) {
         $r = $order->refreshData(array('PRICE', 'PRICE_DELIVERY'));
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if ($isStartField) {
         $hasMeaningfulFields = $this->hasMeaningfulField();
         /** @var Result $r */
         $r = $this->doFinalAction($hasMeaningfulFields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     return $result;
 }