示例#1
0
 public function actionAddAddonBinding($remove = '0')
 {
     if (Yii::$app->request->isAjax === false) {
         throw new BadRequestHttpException();
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     $addon_id = Yii::$app->request->post('addon_id', null);
     $object_id = Yii::$app->request->get('object_id', null);
     $object_model_id = Yii::$app->request->get('object_model_id', null);
     if ($addon_id === null || $object_id === null || $object_model_id === null) {
         throw new BadRequestHttpException();
     }
     $addon = Addon::findById($addon_id);
     $object = Object::findById($object_id);
     if ($addon === null || $object === null) {
         throw new NotFoundHttpException();
     }
     $modelClassName = $object->object_class;
     $model = $this->loadModel($modelClassName, $object_model_id);
     // ok, now all's ok, addon and model exist!
     try {
         if ($remove === '1') {
             $model->unlink('bindedAddons', $addon, true);
         } else {
             $model->link('bindedAddons', $addon, ['sort_order' => count($model->bindedAddons), 'appliance_object_id' => $object->id]);
         }
     } catch (\Exception $e) {
         if (intval($e->getCode()) == 23000) {
             return ['data' => Html::tag('div', Yii::t('app', 'Addon is already added'), ['class' => 'alert alert-info']) . AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false];
         } else {
             return ['data' => Html::tag('div', $e->getMessage(), ['class' => 'alert alert-danger']), 'error' => $e->getMessage()];
         }
     }
     TagDependency::invalidate(Yii::$app->cache, [Addon::className()]);
     return ['data' => AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false];
 }
示例#2
0
 /**
  * Returns instance of OrderItem entity - product or addon
  * Used for calculating prices, etc.
  * @return Product|Addon
  */
 public function sellingItem()
 {
     if ($this->addon_id !== 0) {
         return Addon::findById($this->addon_id);
     } else {
         return Product::findById($this->product_id);
     }
 }
示例#3
0
 /**
  * @param Order $order
  * @param array $products
  * @param $result
  * @param int $parentId
  * @return mixed
  * @throws BadRequestHttpException
  * @throws NotFoundHttpException
  */
 protected function addProductsToOrder(Order $order, $products = [], $result, $parentId = 0)
 {
     $parentId = intval($parentId);
     if ($parentId !== 0) {
         // if parent id is set - order item should exist in this order!
         $parentOrderItem = OrderItem::findOne(['order_id' => $order->id, 'id' => $parentId]);
         if ($parentOrderItem === null) {
             throw new BadRequestHttpException();
         }
     }
     foreach ($products as $product) {
         $productModel = $addonModel = null;
         if (!isset($product['id'])) {
             if (isset($product['addon_id'])) {
                 $addonModel = Addon::findById($product['addon_id']);
             }
         } else {
             $productModel = Product::findById($product['id']);
         }
         if ($addonModel === null && $productModel === null) {
             $result['errors'][] = Yii::t('app', 'Product not found.');
             continue;
         }
         /** @var Product $productModel */
         $quantity = isset($product['quantity']) && (double) $product['quantity'] > 0 ? (double) $product['quantity'] : 1;
         $condition = ['order_id' => $order->id, 'parent_id' => 0];
         if ($productModel !== null) {
             $condition['product_id'] = $productModel->id;
             $thisItemModel = $productModel;
             $quantity = $productModel->measure->ceilQuantity($quantity);
         } else {
             $condition['addon_id'] = $addonModel->id;
             $thisItemModel = $addonModel;
             if (!$addonModel->can_change_quantity) {
                 $quantity = 1;
             }
         }
         $orderItem = OrderItem::findOne($condition);
         if ($this->module->allowToAddSameProduct || null === $orderItem) {
             $orderItem = new OrderItem();
             $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'quantity' => $quantity, 'price_per_pcs' => PriceHelper::getProductPrice($thisItemModel, $order, 1, SpecialPriceList::TYPE_CORE)];
             if (empty($product['customName']) === false) {
                 $orderItem->custom_name = $product['customName'];
             }
             if ($productModel !== null) {
                 $orderItem->product_id = $thisItemModel->id;
             } else {
                 $orderItem->addon_id = $thisItemModel->id;
             }
         } else {
             /** @var OrderItem $orderItem */
             if ($addonModel !== null && !$addonModel->can_change_quantity) {
                 // quantity can not be changed
                 $quantity = 0;
             }
             if (null !== $orderItem) {
                 $orderItem->quantity += $quantity;
             }
         }
         if (false === $orderItem->save()) {
             $result['errors'][] = Yii::t('app', 'Cannot save order item.');
         } else {
             // refresh order
             Order::clearStaticOrder();
             $order = $this->loadOrder(false);
         }
         if (null !== $productModel) {
             $result['products'][] = ['model' => $productModel, 'quantity' => $quantity, 'orderItem' => $orderItem];
         }
         if (isset($product['children']) && is_array($product['children'])) {
             $result = $this->addProductsToOrder($order, $product['children'], $result, $orderItem->id);
         }
     }
     return $result;
 }