/** * @inheritdoc */ public function beforeValidate() { $this->total_price = PriceHelper::getProductPrice($this->product, $this->order, $this->quantity); $this->discount_amount = $this->quantity * $this->price_per_pcs - $this->total_price; $this->total_price_without_discount = $this->total_price + $this->discount_amount; return parent::beforeValidate(); }
public function actionChangeQuantity() { Yii::$app->response->format = Response::FORMAT_JSON; $id = Yii::$app->request->post('id'); $quantity = Yii::$app->request->post('quantity'); if (is_null($id) || is_null($quantity) || (double) $quantity <= 0) { throw new BadRequestHttpException(); } $orderItem = $this->loadOrderItem($id); $order = $this->loadOrder(); if (is_null($order->stage) || $order->stage->immutable_by_user == 1) { throw new BadRequestHttpException(); } $orderItem->quantity = $orderItem->product->measure->ceilQuantity($quantity); // @todo Consider lock_product_price ? if ($orderItem->lock_product_price == 0) { $orderItem->price_per_pcs = PriceHelper::getProductPrice($orderItem->product, $order, 1, SpecialPriceList::TYPE_CORE); } $orderItem->save(); $mainCurrency = Currency::getMainCurrency(); if ($order->calculate(true)) { return ['success' => true, 'itemsCount' => $order->items_count, 'itemPrice' => $mainCurrency->format($orderItem->total_price), 'totalPrice' => $mainCurrency->format($order->total_price)]; } else { return ['success' => false, 'message' => Yii::t('app', 'Cannot change quantity')]; } }
public function actionAddProduct($orderId, $productId, $parentId = 0) { $order = $this->findModel($orderId); /** @var OrderItem $orderItem */ $orderItem = OrderItem::findOne(['product_id' => $productId, 'order_id' => $orderId]); /** @var Product $product */ $product = Product::findById($productId); if (is_null($orderItem)) { $orderItem = new OrderItem(); $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'product_id' => $product->id, 'quantity' => $product->measure->nominal, 'price_per_pcs' => PriceHelper::getProductPrice($product, $order, 1, SpecialPriceList::TYPE_CORE)]; } else { $orderItem->quantity++; } $orderItem->save(); $order->calculate(true); return $this->redirect(['view', 'id' => $orderId]); }
/** * @return array * @throws BadRequestHttpException * @throws NotFoundHttpException */ public function actionChangeQuantity() { Yii::$app->response->format = Response::FORMAT_JSON; $result = []; $id = Yii::$app->request->post('id'); $quantity = floatval(Yii::$app->request->post('quantity', 0)); if (null === $id || $quantity <= 0) { throw new BadRequestHttpException(); } $orderItem = $this->loadOrderItem($id); $order = $this->loadOrder(); if (null === $order->stage || true === $order->getImmutability(Order::IMMUTABLE_USER)) { throw new BadRequestHttpException(); } $model = $orderItem->product; $product = [['model' => $model, 'quantity' => $model->measure->ceilQuantity($quantity) - $orderItem->quantity, 'orderItem' => $orderItem]]; $orderItem->quantity = $orderItem->product->measure->ceilQuantity($quantity); // @todo Consider lock_product_price ? if ($orderItem->lock_product_price == 0) { $orderItem->price_per_pcs = PriceHelper::getProductPrice($orderItem->product, $order, 1, SpecialPriceList::TYPE_CORE); } $orderItem->save(); $event = new CartActionEvent($order, $product); Event::trigger($this, self::EVENT_ACTION_QUANTITY, $event); $result['additional'] = $event->getEventData(); $result['success'] = $order->calculate(true); $result['message'] = false === $result['success'] ? Yii::t('app', 'Cannot change quantity') : ''; $result['itemsCount'] = $order->items_count; $result['itemPrice'] = CurrencyHelper::getMainCurrency()->format($orderItem->total_price); $result['totalPrice'] = CurrencyHelper::getMainCurrency()->format($order->total_price); $result['calculatedQuantity'] = $orderItem->quantity; $result['products'] = $this->productsModelsToArray($product); return $result; }