diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 1f505b5..ab9b5ab 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -139,8 +139,10 @@ public function actionDeleteProduct($id) { $model = OrderProduct::findOne($id); + $model->removed = true; $orderId = $model->order_id; - if ($model->delete()) { + if ($model->save()) { + $model->order->totalRecount(); return $this->actionUpdate($orderId); } } @@ -156,7 +158,7 @@ $count = 1; } $productVariant = ProductVariant::findOne($id); - + $model = OrderProduct::find() ->where( [ @@ -186,6 +188,7 @@ \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if ($model->save()) { + $model->order->totalRecount(); return [ 'status' => 'success' ]; } else { return [ 'status' => 'fail' ]; diff --git a/models/Order.php b/models/Order.php index 5c19845..606f7e8 100755 --- a/models/Order.php +++ b/models/Order.php @@ -18,6 +18,7 @@ * @property integer $deleted_at * @property integer $deadline * @property boolean $wasted + * @property string $delivery_cost * @property integer $reason * @property string $check * @property string $sms @@ -165,6 +166,7 @@ 'status', 'check', 'sms', + 'delivery_cost', ], 'string', 'max' => 255, @@ -204,19 +206,20 @@ public function attributeLabels() { return [ - 'name' => Yii::t('app', 'order_name'), - 'phone' => Yii::t('app', 'order_phone'), - 'email' => Yii::t('app', 'order_email'), - 'comment' => Yii::t('app', 'order_comment'), - 'created_at' => Yii::t('app', 'Дата добавления'), - 'updated_at' => Yii::t('app', 'Дата обновления'), - 'deleted_at' => Yii::t('app', 'Дата удаления'), - 'deadline' => Yii::t('app', 'Дедлайн'), - 'reason' => Yii::t('app', 'Причина'), - 'check' => Yii::t('app', 'Чек'), - 'sms' => Yii::t('app', 'СМС'), - 'consignment' => Yii::t('app', 'Номер накладной'), - 'manager_id' => Yii::t('app', 'Менеджер'), + 'name' => Yii::t('app', 'order_name'), + 'phone' => Yii::t('app', 'order_phone'), + 'email' => Yii::t('app', 'order_email'), + 'comment' => Yii::t('app', 'order_comment'), + 'created_at' => Yii::t('app', 'Дата добавления'), + 'updated_at' => Yii::t('app', 'Дата обновления'), + 'deleted_at' => Yii::t('app', 'Дата удаления'), + 'deadline' => Yii::t('app', 'Дедлайн'), + 'reason' => Yii::t('app', 'Причина'), + 'check' => Yii::t('app', 'Чек'), + 'sms' => Yii::t('app', 'СМС'), + 'consignment' => Yii::t('app', 'Номер накладной'), + 'manager_id' => Yii::t('app', 'Менеджер'), + 'delivery_cost' => Yii::t('app', 'Стоимость доставки'), ]; } @@ -308,4 +311,16 @@ } } } + + public function totalRecount() + { + $products = $this->products; + $newTotal = 0; + foreach ($products as $product) { + if ($product->removed) continue; + $newTotal += $product->count * $product->price; + } + $this->total = $newTotal; + $this->save(); + } } \ No newline at end of file diff --git a/models/OrderProduct.php b/models/OrderProduct.php index 14ee6bd..bba44a1 100755 --- a/models/OrderProduct.php +++ b/models/OrderProduct.php @@ -21,6 +21,7 @@ * @property int $count * @property double $sum_cost * @property Order $order + * @property boolean $removed * @property ProductVariant $productVariant * @package artweb\artbox\ecommerce\models */ @@ -47,7 +48,10 @@ 'required', ], [ - [ 'return' ], + [ + 'return', + 'removed', + ], 'boolean', ], [ diff --git a/views/order/_form.php b/views/order/_form.php index 91f951d..4fa150c 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -23,8 +23,11 @@ * @var Order $model * @var ActiveForm $form * @var ActiveDataProvider $dataProvider + * @var User $user */ + $user = \Yii::$app->user->identity; + $js = <<< JS $(document).on('submit', '#add-product-form', function(e) { e.preventDefault(); @@ -119,17 +122,20 @@ JS; ) ?> field($model, 'pay') - ->widget( - SwitchInput::className(), - [ - 'name' => 'pay', - 'pluginOptions' => [ - 'onText' => \Yii::t('app', 'Оплачено'), - 'offText' => \Yii::t('app', 'Не оплачено'), - ], - ] - ); ?> + if ($user->isAdmin()) { + echo $form->field($model, 'pay') + ->widget( + SwitchInput::className(), + [ + 'name' => 'pay', + 'pluginOptions' => [ + 'onText' => \Yii::t('app', 'Оплачено'), + 'offText' => \Yii::t('app', 'Не оплачено'), + ], + ] + ); + } + ?> field($model, 'reason') ->dropDownList( @@ -151,11 +157,13 @@ JS; field($model, 'name') ?> - field($model, 'phone') ?> + field($model, 'phone') + ->textInput([ 'readonly' => $user->isAdmin() ? false : true ]) ?> field($model, 'phone2') ?> - field($model, 'email') ?> + field($model, 'email') + ->textInput([ 'readonly' => $user->isAdmin() ? false : true ]) ?> field( $model, @@ -180,7 +188,7 @@ JS; user->identity->isAdmin()) { + if ($user->isAdmin()) { echo $form->field($model, 'manager_id') ->dropDownList( ArrayHelper::map( @@ -306,6 +314,8 @@ JS; field($model, 'check') ?> field($model, 'sms') ?> + + field($model, 'delivery_cost') ?> @@ -321,6 +331,13 @@ JS; echo GridView::widget( [ 'dataProvider' => $dataProvider, + 'rowOptions' => function($model) { + if ($model->removed) { + return [ 'class' => 'danger' ]; + } else { + return []; + } + }, 'layout' => '{items}{pager}', 'columns' => [ 'id', @@ -356,10 +373,10 @@ JS; 'class' => 'kartik\grid\EditableColumn', 'attribute' => 'booking', 'editableOptions' => [ - 'header' => \Yii::t('app', 'Бронь'), - 'inputType' => kartik\editable\Editable::INPUT_TEXT, - 'options' => [ - 'class' => 'booking-typeahead', + 'header' => \Yii::t('app', 'Бронь'), + 'inputType' => kartik\editable\Editable::INPUT_TEXT, + 'options' => [ + 'class' => 'booking-typeahead', 'pluginOptions' => [ 'min' => 0, 'max' => 20, @@ -378,10 +395,10 @@ JS; 'class' => 'kartik\grid\EditableColumn', 'attribute' => 'status', 'editableOptions' => [ - 'header' => \Yii::t('app', 'Статус'), - 'inputType' => kartik\editable\Editable::INPUT_TEXT, - 'options' => [ - 'class' => 'status-typeahead', + 'header' => \Yii::t('app', 'Статус'), + 'inputType' => kartik\editable\Editable::INPUT_TEXT, + 'options' => [ + 'class' => 'status-typeahead', 'pluginOptions' => [ 'min' => 0, 'max' => 20, @@ -414,19 +431,23 @@ JS; 'template' => '{delete}', 'buttons' => [ 'delete' => function($url, $model) { - return Html::a( - Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-trash' ]), - [ - 'delete-product', - 'id' => $model->id, - ], - [ - 'data' => [ - 'confirm' => 'Вы уверены, что хотите удалить этот элемент?', - 'method' => 'POST', + if ($model->removed) { + return ''; + } else { + return Html::a( + Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-trash' ]), + [ + 'delete-product', + 'id' => $model->id, ], - ] - ); + [ + 'data' => [ + 'confirm' => 'Вы уверены, что хотите удалить этот элемент?', + 'method' => 'POST', + ], + ] + ); + } }, ], ], diff --git a/views/order/index.php b/views/order/index.php index ea8467b..85bf342 100755 --- a/views/order/index.php +++ b/views/order/index.php @@ -60,116 +60,122 @@ JS; ) ?>

- -field($searchModel, 'label') - ->widget( - Select2::className(), - [ - 'data' => Label::find() - ->joinWith('lang') - ->select( - [ - 'CONCAT(order_label.label,order_label_lang.title) AS name', - 'id', - ] - ) - ->indexBy('id') - ->asArray() - ->column(), - 'options' => [ 'placeholder' => 'Select a state ...' ], - 'pluginOptions' => [ - 'allowClear' => true, - 'multiple' => true, - ], - ] - ); -?> -
-field($searchModel, 'delivery') - ->widget( - Select2::className(), - [ - 'data' => Delivery::find() - ->joinWith('lang') - ->select('order_delivery_lang.title, id') - ->indexBy('id') - ->asArray() - ->column(), - 'options' => [ 'placeholder' => 'Select a state ...' ], - 'pluginOptions' => [ - 'allowClear' => true, - 'multiple' => true, - ], - ] - ); -?> - -field($searchModel, 'sku') - ->widget( - Select2::className(), - [ - 'options' => [ 'placeholder' => 'Search for a product ...' ], - 'pluginOptions' => [ - 'allowClear' => true, - 'minimumInputLength' => 3, - 'language' => [ - 'errorLoading' => new JsExpression( - "function () { return 'Waiting for results...'; }" - ), - ], - 'ajax' => [ - 'url' => \yii\helpers\Url::to([ 'find-product' ]), - 'dataType' => 'json', - 'data' => $query, - ], - 'escapeMarkup' => new JsExpression( - 'function (markup) { return markup; }' - ), - 'templateResult' => new JsExpression( - 'function(data) { return data.sku; }' - ), - 'templateSelection' => new JsExpression( - 'function (data) { +
+
+ field($searchModel, 'label') + ->widget( + Select2::className(), + [ + 'data' => Label::find() + ->joinWith('lang') + ->select( + [ + 'CONCAT(order_label.label,order_label_lang.title) AS name', + 'id', + ] + ) + ->indexBy('id') + ->asArray() + ->column(), + 'options' => [ 'placeholder' => 'Select a state ...' ], + 'pluginOptions' => [ + 'allowClear' => true, + 'multiple' => true, + ], + ] + ); + ?> + field($searchModel, 'delivery') + ->widget( + Select2::className(), + [ + 'data' => Delivery::find() + ->joinWith('lang') + ->select('order_delivery_lang.title, id') + ->indexBy('id') + ->asArray() + ->column(), + 'options' => [ 'placeholder' => 'Select a state ...' ], + 'pluginOptions' => [ + 'allowClear' => true, + 'multiple' => true, + ], + ] + ); + ?> + + field($searchModel, 'sku') + ->widget( + Select2::className(), + [ + 'options' => [ 'placeholder' => 'Search for a product ...' ], + 'pluginOptions' => [ + 'allowClear' => true, + 'minimumInputLength' => 3, + 'language' => [ + 'errorLoading' => new JsExpression( + "function () { return 'Waiting for results...'; }" + ), + ], + 'ajax' => [ + 'url' => \yii\helpers\Url::to([ 'find-product' ]), + 'dataType' => 'json', + 'data' => $query, + ], + 'escapeMarkup' => new JsExpression( + 'function (markup) { return markup; }' + ), + 'templateResult' => new JsExpression( + 'function(data) { return data.sku; }' + ), + 'templateSelection' => new JsExpression( + 'function (data) { if(data.sku == undefined) { return "sku"; } else { return data.sku; } }' - ), - ], - ] - ); - -?> - -field($searchModel, 'manager_id') - ->dropDownList( - ArrayHelper::map( - User::find() - ->asArray() - ->all(), - 'id', - 'username' - ), - [ 'prompt' => \Yii::t('app', 'Выберите менеджера ...') ] - ) ?> - -field($searchModel, 'email') - ->textInput() ?> - -field($searchModel, 'declaration') - ->textInput() ?> - -field($searchModel, 'consignment') - ->textInput() ?> - + ), + ], + ] + ); + + ?> +
+ + +
+ field($searchModel, 'manager_id') + ->dropDownList( + ArrayHelper::map( + User::find() + ->asArray() + ->all(), + 'id', + 'username' + ), + [ 'prompt' => \Yii::t('app', 'Выберите менеджера ...') ] + ) ?> + + field($searchModel, 'email') + ->textInput() ?> + +
+
+ field($searchModel, 'declaration') + ->textInput() ?> + + field($searchModel, 'consignment') + ->textInput() ?> +
+

'id', 'filter' => $searchForm->field($searchModel, 'id') ->textInput(), - 'content' => function($model) { + 'content' => function($model) { $manager = $model->manager; - if (empty($manager)) { + if (empty( $manager )) { return $model->id; } else { return $model->id . ' ' . $manager->username; } - } + }, ], [ 'attribute' => 'created_at', @@ -269,8 +275,18 @@ JS; ], 'sms', [ - 'class' => 'yii\grid\ActionColumn', - 'template' => \Yii::$app->user->identity->isAdmin() ? '{view} {update} {delete}' : '{view} {update}' + 'class' => 'yii\grid\ActionColumn', + 'template' => \Yii::$app->user->identity->isAdmin( + ) ? '{view} {update} {delete}' : '{view} {update}', + 'buttons' => [ + 'update' => function($url, $model) { + return Html::a( + Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-pencil' ]), + $url, + [ 'target' => '_blank' ] + ); + }, + ], ], ], ] -- libgit2 0.21.4