diff --git a/assets/OrderAsset.php b/assets/OrderAsset.php index 967a501..e43ea82 100755 --- a/assets/OrderAsset.php +++ b/assets/OrderAsset.php @@ -25,4 +25,8 @@ public $js = [ 'js/order.js', ]; + + public $depends = [ + 'yii\widgets\PjaxAsset', + ]; } diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 9e81d8b..6cf5f27 100644 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -1,18 +1,22 @@ search(Yii::$app->request->queryParams); $labels = Label::find() - ->select( - [ - 'title', - 'id', - ] - ) - ->joinWith('lang') - ->andWhere( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - + ->select( + [ + 'title', + 'id', + ] + ) + ->joinWith('lang') + ->andWhere( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); + return $this->render( '@artbox/order/views/order/index', [ @@ -67,7 +71,7 @@ ] ); } - + /** * Displays a single Order model. * @@ -84,7 +88,7 @@ ] ); } - + /** * Creates a new Order model. * If creation is successful, the browser will be redirected to the 'view' page. @@ -94,7 +98,7 @@ public function actionCreate() { $model = new Order(); - + if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect( [ @@ -104,50 +108,50 @@ ); } else { $labels = Label::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); $deliveries = Delivery::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); $payments = Payment::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); return $this->render( '@artbox/order/views/order/create', [ @@ -159,7 +163,7 @@ ); } } - + /** * Updates an existing Order model. * If update is successful, the browser will be redirected to the 'view' page. @@ -171,16 +175,9 @@ public function actionUpdate($id) { $model = $this->findModel($id); - + if ($model->load(Yii::$app->request->post()) && $model->save()) { - if (Model::loadMultiple($model->orderProducts, \Yii::$app->request->post()) && Model::validateMultiple( - $model->orderProducts - ) - ) { - foreach ($model->orderProducts as $orderProduct) { - $orderProduct->save(false); - } - } + OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id); return $this->redirect( [ 'view', @@ -204,35 +201,35 @@ ->indexBy('id') ->column(); $deliveries = Delivery::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); $payments = Payment::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); return $this->render( '@artbox/order/views/order/update', [ @@ -244,7 +241,7 @@ ); } } - + /** * Deletes an existing Order model. * If deletion is successful, the browser will be redirected to the 'index' page. @@ -257,10 +254,116 @@ { $this->findModel($id) ->delete(); - + return $this->redirect([ 'index' ]); } - + + public function actionProductList($q = null, $id = null) + { + $response = \Yii::$app->response; + $response->format = $response::FORMAT_JSON; + $out = [ + 'results' => [ + 'id' => '', + 'text' => '', + ], + ]; + if (!is_null($q)) { + $out[ 'results' ] = []; + /** + * @var Variant[] $variants + */ + $variants = Variant::find() + ->joinWith('lang', false) + ->joinWith('product.lang', false) + ->andWhere( + [ + 'like', + 'product_lang.title', + $q, + ] + ) + ->orWhere( + [ + 'like', + 'variant_lang.title', + $q, + ] + ) + ->orWhere([ 'variant.sku' => $q ]) + ->all(); + foreach ($variants as $variant) { + $out[ 'results' ][] = [ + 'id' => $variant->id, + 'text' => $variant->product->lang->title, + ]; + } + } elseif ($id > 0) { + /** + * @var Variant $variant + */ + $variant = Variant::find() + ->with('lang', 'product.lang') + ->where([ 'id' => $id ]) + ->one(); + $out[ 'results' ] = [ + 'id' => $id, + 'text' => $variant->product->lang->title, + ]; + } + return $out; + } + + public function actionAddToOrder() + { + $id = \Yii::$app->request->post('id'); + $count = \Yii::$app->request->post('count'); + $orderId = \Yii::$app->request->post('order'); + if (empty($id) || empty($count)) { + throw new InvalidParamException(\Yii::t('order', 'Set id and count')); + } + $order = Order::find() + ->where([ 'id' => $orderId ]) + ->one(); + if (empty($order)) { + throw new NotFoundHttpException(\Yii::t('order', 'Order not found')); + } + /** + * @var Variant $variant + */ + $variant = Variant::find() + ->where([ 'id' => $id ]) + ->one(); + if (empty($variant)) { + throw new NotFoundHttpException(\Yii::t('order', 'Variant not found')); + } + /** + * @var OrderProduct $model + */ + $model = OrderProduct::find() + ->where( + [ + 'order_id' => $orderId, + 'variant_id' => $id, + ] + ) + ->one(); + if ($model) { + $model->count += $count; + } else { + $model = new OrderProduct( + [ + 'order_id' => $orderId, + 'variant_id' => $id, + 'sku' => $variant->sku, + 'price' => $variant->price, + 'count' => $count, + ] + ); + } + $model->save(); + } + /** * Finds the Order model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. diff --git a/models/OrderProduct.php b/models/OrderProduct.php index 899ee01..23e8686 100644 --- a/models/OrderProduct.php +++ b/models/OrderProduct.php @@ -4,6 +4,7 @@ use artbox\catalog\models\Variant; use Yii; + use yii\helpers\ArrayHelper; /** * This is the model class for table "order_product". @@ -105,4 +106,74 @@ { return $this->hasOne(Variant::className(), [ 'id' => 'variant_id' ]); } + + /** + * @param array $items + * @param int $orderId + * + * @return \artbox\order\models\OrderProduct[] + */ + public static function saveItems(array $items, int $orderId): array + { + $variantIds = ArrayHelper::getColumn($items, 'variant_id', false); + /** + * @var \artbox\order\models\OrderProduct[] $deletion + */ + $deletion = self::find() + ->where( + [ + 'not', + [ 'variant_id' => $variantIds ], + ] + ) + ->andWhere([ 'order_id' => $orderId ]) + ->all(); + foreach ($deletion as $record) { + $record->delete(); + } + /** + * @var \artbox\order\models\OrderProduct[] $orderProducts + */ + $orderProducts = self::find() + ->where( + [ + 'variant_id' => $variantIds, + 'order_id' => $orderId, + ] + ) + ->all(); + $newItems = []; + foreach ($items as $item) { + $id = $item[ 'variant_id' ]; + $count = $item[ 'count' ]; + foreach ($orderProducts as $orderProduct) { + if ($orderProduct->variant_id == $id) { + $orderProduct->count = $count; + break 2; + } + } + /** + * @var Variant $variant + */ + $variant = Variant::find() + ->where([ 'id' => $id ]) + ->one(); + if ($variant) { + $newItems[] = new OrderProduct( + [ + 'order_id' => $orderId, + 'variant_id' => $id, + 'sku' => $variant->sku, + 'price' => $variant->price, + 'count' => $count, + ] + ); + } + } + $orderProducts = array_merge($orderProducts, $newItems); + foreach ($orderProducts as $orderProduct) { + $orderProduct->save(); + } + return $orderProducts; + } } diff --git a/views/order/_form.php b/views/order/_form.php index 288f285..79eaf5f 100644 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -1,6 +1,9 @@ isNewRecord) { ?>