From eb190b1f8c6d212102f92c22e30a1ede32b6c064 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 1 Dec 2016 13:58:03 +0200 Subject: [PATCH] -Order form add product in create action --- controllers/OrderController.php | 15 +++++++++++++-- models/Order.php | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------- models/OrderSearch.php | 3 ++- views/order/create.php | 18 ++++++++++++++++++ 4 files changed, 95 insertions(+), 44 deletions(-) diff --git a/controllers/OrderController.php b/controllers/OrderController.php index ab9b5ab..85fbe3c 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -158,7 +158,7 @@ $count = 1; } $productVariant = ProductVariant::findOne($id); - + $model = OrderProduct::find() ->where( [ @@ -234,6 +234,7 @@ $model = new Order(); $model->phone = '+38(000)000-00-00'; $model->name = \Yii::t('app', 'Новый заказ'); + $model->published = false; $model->save(); $dataProvider = new ActiveDataProvider( @@ -244,6 +245,8 @@ ); if ($model->load(Yii::$app->request->post()) && $model->save()) { + $model->published = true; + $model->save(); return $this->redirect([ 'index' ]); } else { return $this->render( @@ -413,10 +416,18 @@ public function actionExitOrder($id) { - $model = $this->findModel($id); + try { + $model = $this->findModel($id); + } catch (NotFoundHttpException $e) { + return $this->redirect('index'); + } if ($model->edit_id == \Yii::$app->user->id) { $this->unblockOrder($id); } + + if (!$model->published) { + $model->deleteUnpublished(); + } return $this->redirect('index'); } diff --git a/models/Order.php b/models/Order.php index 606f7e8..2749887 100755 --- a/models/Order.php +++ b/models/Order.php @@ -13,45 +13,47 @@ * * @todo Write docs and refactor * @package artweb\artbox\ecommerce\models - * @property integer $created_at - * @property integer $updated_at - * @property integer $deleted_at - * @property integer $deadline - * @property boolean $wasted - * @property string $delivery_cost - * @property integer $reason - * @property string $check - * @property string $sms - * @property int $id - * @property integer $edit_id - * @property integer $edit_time - * @property integer $manager_id - * @property int $user_id - * @property string $name - * @property string $phone - * @property string $phone2 - * @property string $email - * @property string $adress - * @property string $body - * @property double $total - * @property string $date_time - * @property string $date_dedline - * @property string $reserve - * @property string $status - * @property string $comment - * @property int $label - * @property int $pay - * @property int $numbercard - * @property int $delivery - * @property string $declaration - * @property string $stock - * @property string $consignment - * @property string $payment - * @property string $insurance - * @property double $amount_imposed - * @property string $shipping_by - * @property string $city - * @property string $deliveryString + * @property OrderProduct[] $products + * @property integer $created_at + * @property integer $updated_at + * @property integer $deleted_at + * @property integer $deadline + * @property boolean $wasted + * @property string $delivery_cost + * @property integer $reason + * @property string $check + * @property string $sms + * @property int $id + * @property integer $edit_id + * @property integer $edit_time + * @property integer $manager_id + * @property int $user_id + * @property string $name + * @property string $phone + * @property string $phone2 + * @property string $email + * @property string $adress + * @property string $body + * @property double $total + * @property string $date_time + * @property string $date_dedline + * @property string $reserve + * @property string $status + * @property string $comment + * @property int $label + * @property int $pay + * @property int $numbercard + * @property int $delivery + * @property string $declaration + * @property string $stock + * @property string $consignment + * @property string $payment + * @property string $insurance + * @property double $amount_imposed + * @property string $shipping_by + * @property string $city + * @property string $deliveryString + * @property boolean $published */ class Order extends ActiveRecord { @@ -106,7 +108,10 @@ { return [ [ - [ 'pay' ], + [ + 'pay', + 'published', + ], 'boolean', ], [ @@ -220,6 +225,7 @@ 'consignment' => Yii::t('app', 'Номер накладной'), 'manager_id' => Yii::t('app', 'Менеджер'), 'delivery_cost' => Yii::t('app', 'Стоимость доставки'), + 'published' => Yii::t('app', 'Опубликован'), ]; } @@ -317,10 +323,25 @@ $products = $this->products; $newTotal = 0; foreach ($products as $product) { - if ($product->removed) continue; + if ($product->removed) { + continue; + } $newTotal += $product->count * $product->price; } $this->total = $newTotal; $this->save(); } + + public function deleteUnpublished() + { + /** + * @var OrderProduct[] $products + */ + $products = $this->products; + foreach ($products as $product) { + $product->delete(); + } + + $this->delete(); + } } \ No newline at end of file diff --git a/models/OrderSearch.php b/models/OrderSearch.php index e353033..6608293 100755 --- a/models/OrderSearch.php +++ b/models/OrderSearch.php @@ -68,7 +68,8 @@ */ public function search($params) { - $query = Order::find(); + $query = Order::find() + ->where([ 'published' => true ]); // add conditions that should always apply here diff --git a/views/order/create.php b/views/order/create.php index aa61dd6..6b93163 100755 --- a/views/order/create.php +++ b/views/order/create.php @@ -15,6 +15,24 @@ 'url' => [ 'index' ], ]; $this->params[ 'breadcrumbs' ][] = $this->title; + + $js = ' + window.onbeforeunload = function(e) { + $.ajax({ + type: "GET", + url: "/admin/ecommerce/order/exit-order", + data: { + id: ' . $model->id . ', + }, + success: function() { + console.log("Exit order"); + } + }); +}; +'; + + $this->registerJs($js, View::POS_READY); + ?>
-- libgit2 0.21.4