/** * @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]); }
} ?> <?php } ?> <tr> <th colspan="2"><?php echo Yii::t('app', 'Summary'); ?> </th> <th><?php echo $model->items_count; ?> </th> <th colspan="2"><?php echo Yii::$app->formatter->asDecimal(PriceHelper::getOrderPrice($model), 2); ?> </th> </tr> </tbody> </table> <div class="do-not-print"> <br /> <div class="row"> <div class="col-xs-6"> <label for="add-product"><?php echo Yii::t('app', 'Add a new product to order'); ?> </label> </div>
/** * Calculate order total price and items count with all additional markups. * @param bool $callSave Call save method after calculating. * @param bool $deleteNotActiveProducts Delete Order Item if product is not active or is not exist. * @return bool */ public function calculate($callSave = false, $deleteNotActiveProducts = true) { $itemsCount = 0; foreach ($this->items as $item) { if (null === OrderItem::findOne(['id' => $item->id])) { $item->delete(); continue; } if ($deleteNotActiveProducts && (null === $item->product || $item->product->active == 0)) { $item->delete(); continue; } if (Yii::$app->getModule('shop')->countChildrenProducts == 1) { $itemsCount += Yii::$app->getModule('shop')->countUniqueProductsOnly == 1 ? 1 : $item->quantity; } else { if ($item->parent_id == 0) { $itemsCount += Yii::$app->getModule('shop')->countUniqueProductsOnly == 1 ? 1 : $item->quantity; } } } $event = new OrderCalculateEvent(); $event->order = $this; $event->price = PriceHelper::getOrderPrice($this, SpecialPriceList::TYPE_CORE); EventTriggeringHelper::triggerSpecialEvent($event); $this->items_count = $itemsCount; $this->total_price = PriceHelper::getOrderPrice($this); $event->state = OrderCalculateEvent::AFTER_CALCULATE; EventTriggeringHelper::triggerSpecialEvent($event); TagDependency::invalidate(Yii::$app->cache, ['Session:' . Yii::$app->session->id]); return $callSave ? $this->save(true, ['items_count', 'total_price', 'total_price_with_shipping']) : true; }
/** * @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; }