diff --git a/media/js/admin.js b/media/js/admin.js index 13ccfaa..fec0169 100644 --- a/media/js/admin.js +++ b/media/js/admin.js @@ -66,6 +66,14 @@ $(function(){ }); }); + $(document).bind('keydown', function (e) { + if(e.ctrlKey && e.which === 83){ // Check for the Ctrl key being pressed, and if the key = [S] (83) + $('.model-form').submit(); + e.preventDefault(); + return false; + } + }); + window.notify = new Notify(); }); diff --git a/modules/shopcart/api/GoodObject.php b/modules/shopcart/api/GoodObject.php index d8bdddb..edb255a 100644 --- a/modules/shopcart/api/GoodObject.php +++ b/modules/shopcart/api/GoodObject.php @@ -1,7 +1,7 @@ '', + 'successUrl' => '' + ]; + public function api_items() { - if(!$this->_items){ - $this->_items = []; - if(!$this->order->isNewRecord){ - foreach(Good::find()->where(['order_id' => $this->order->order_id])->with('item')->all() as $good){ - $this->_items[] = new GoodObject($good); - } - } - } - return $this->_items; + return $this->items; } public function api_order() @@ -31,7 +31,36 @@ public function api_order() return new OrderObject($this->order); } - public function api_add($item_id, $options = '', $count = 1, $increaseOnDublicate = true) + public function api_form($options = []) + { + $model = new Order; + $model->scenario = 'confirm'; + $settings = Yii::$app->getModule('admin')->activeModules['shopcart']->settings; + $options = array_merge($this->_defaultFormOptions, $options); + + ob_start(); + $form = ActiveForm::begin([ + 'action' => Url::to(['/admin/shopcart/send']) + ]); + + echo Html::hiddenInput('errorUrl', $options['errorUrl'] ? $options['errorUrl'] : Url::current([self::SENT_VAR => 0])); + echo Html::hiddenInput('successUrl', $options['successUrl'] ? $options['successUrl'] : Url::current([self::SENT_VAR => 1])); + + echo $form->field($model, 'name'); + echo $form->field($model, 'address'); + + if($settings['enableEmail']) echo $form->field($model, 'email'); + if($settings['enablePhone']) echo $form->field($model, 'phone'); + + echo $form->field($model, 'comment')->textarea(); + + echo Html::submitButton(Yii::t('easyii', 'Send'), ['class' => 'btn btn-primary']); + ActiveForm::end(); + + return ob_get_clean(); + } + + public function api_add($item_id, $count = 1, $options = '', $increaseOnDublicate = true) { $shopItem = Item::findOne($item_id); if(!$shopItem){ @@ -69,14 +98,14 @@ public function api_add($item_id, $options = '', $count = 1, $increaseOnDublicat } if($good->save()){ - $response = [ - 'result' => 'success', - 'order_id' => $good->order_id, - 'good_id' => $good->primaryKey, - 'item_id' => $shopItem->primaryKey, - 'options' => $good->options, - 'discount' => $good->discount, - ]; + $response = [ + 'result' => 'success', + 'order_id' => $good->order_id, + 'good_id' => $good->primaryKey, + 'item_id' => $shopItem->primaryKey, + 'options' => $good->options, + 'discount' => $good->discount, + ]; if($response['discount']){ $response['price'] = round($good->price * (1 - $good->discount / 100)); $response['old_price'] = $good->price; @@ -95,19 +124,34 @@ public function api_remove($good_id) if(!$good){ return ['result' => 'error', 'code' => 1, 'error' => 'Good not found']; } - $order = $good->order; - if($order->access_token != $this->token){ + if($good->order_id != $this->order->order_id){ return ['result' => 'error', 'code' => 2, 'error' => 'Access denied']; } $good->delete(); - return ['result' => 'success', 'good_id' => $good_id, 'order_id' => $order->primaryKey]; + return ['result' => 'success', 'good_id' => $good_id, 'order_id' => $good->order_id]; + } + + public function api_update($goods) + { + if(is_array($goods) && count($this->items)) { + foreach($this->items as $good){ + if(!empty($goods[$good->id])) + { + $count = (int)$goods[$good->id]; + if($count > 0){ + $good->model->count = $count; + $good->model->update(); + } + } + } + } } - public function api_confirm($data) + public function api_send($data) { - if($this->order->isNewRecord || $this->order->status != Order::STATUS_NEW){ + if($this->order->isNewRecord || $this->order->status != Order::STATUS_BLANK){ return ['result' => 'error', 'code' => 1, 'error' => 'Order not found']; } if(!count($this->order->goods)){ @@ -116,9 +160,6 @@ public function api_confirm($data) $this->order->setAttributes($data); $this->order->status = Order::STATUS_PENDING; if($this->order->save()){ - if(Yii::$app->getModule('admin')->activeModules['shopcart']->settings['mailAdminOnNewOrder']) { - $this->api_mailAdmin($this->order->primaryKey); - } return [ 'result' => 'success', 'order_id' => $this->order->primaryKey, @@ -129,21 +170,28 @@ public function api_confirm($data) } } - public function api_mailAdmin($order_id) + public function api_cost() { - $settings = Yii::$app->getModule('admin')->activeModules['shopcart']->settings; - $template = $settings['templateOnNewOrder']; - $subject = $settings['subjectOnNewOrder']; - - if($template && $subject) - { - Yii::$app->mailer->compose($template, ['order_id' => $order_id, 'link' => Url::to(['/admin/shopcart/a/view', 'id' => $order_id])]) - ->setFrom(Setting::get('robot_email')) - ->setTo(Setting::get('admin_email')) - ->setSubject($subject) - ->send(); + $cost = 0; + if(count($this->items)){ + foreach($this->items as $good){ + $cost += $good->price * $good->count; + } } + return $cost; + } + public function getItems() + { + if(!$this->_items){ + $this->_items = []; + if(!$this->order->isNewRecord){ + foreach(Good::find()->where(['order_id' => $this->order->order_id])->with('item')->all() as $good){ + $this->_items[] = new GoodObject($good); + } + } + } + return $this->_items; } public function getOrder() @@ -151,7 +199,7 @@ public function getOrder() if(!$this->_order){ $access_token = $this->token; - if(!$access_token || !($this->_order = Order::find()->where(['access_token' => $access_token])->status(Order::STATUS_NEW)->one())){ + if(!$access_token || !($this->_order = Order::find()->where(['access_token' => $access_token])->status(Order::STATUS_BLANK)->one())){ $this->_order = new Order(); } } diff --git a/modules/shopcart/controllers/AController.php b/modules/shopcart/controllers/AController.php index d7c2f1d..a854834 100644 --- a/modules/shopcart/controllers/AController.php +++ b/modules/shopcart/controllers/AController.php @@ -11,7 +11,7 @@ class AController extends Controller { public $pending = 0; - public $confirmed = 0; + public $processed = 0; public $sent = 0; public function init() @@ -19,7 +19,7 @@ public function init() parent::init(); $this->pending = Order::find()->status(Order::STATUS_PENDING)->count(); - $this->confirmed = Order::find()->status(Order::STATUS_CONFIRMED)->count(); + $this->processed = Order::find()->status(Order::STATUS_PROCESSED)->count(); $this->sent = Order::find()->status(Order::STATUS_SENT)->count(); } @@ -33,18 +33,20 @@ public function actionIndex() ]); } - public function actionConfirmed() + public function actionProcessed() { + $this->setReturnUrl(); return $this->render('index', [ 'data' => new ActiveDataProvider([ - 'query' => Order::find()->with('goods')->status(Order::STATUS_CONFIRMED)->asc(), - 'totalCount' => $this->confirmed + 'query' => Order::find()->with('goods')->status(Order::STATUS_PROCESSED)->asc(), + 'totalCount' => $this->processed ]) ]); } public function actionSent() { + $this->setReturnUrl(); return $this->render('index', [ 'data' => new ActiveDataProvider([ 'query' => Order::find()->with('goods')->status(Order::STATUS_SENT)->asc(), @@ -55,6 +57,7 @@ public function actionSent() public function actionCompleted() { + $this->setReturnUrl(); return $this->render('index', [ 'data' => new ActiveDataProvider([ 'query' => Order::find()->with('goods')->status(Order::STATUS_COMPLETED)->desc() @@ -64,15 +67,27 @@ public function actionCompleted() public function actionFails() { + $this->setReturnUrl(); return $this->render('index', [ 'data' => new ActiveDataProvider([ - 'query' => Order::find()->with('goods')->where(['in', 'status', [Order::STATUS_DECLINED, Order::STATUS_ERROR, Order::STATUS_RETURNED, Order::STATUS_NEW]])->desc() + 'query' => Order::find()->with('goods')->where(['in', 'status', [Order::STATUS_DECLINED, Order::STATUS_ERROR, Order::STATUS_RETURNED]])->desc() + ]) + ]); + } + + public function actionBlank() + { + $this->setReturnUrl(); + return $this->render('index', [ + 'data' => new ActiveDataProvider([ + 'query' => Order::find()->with('goods')->status(Order::STATUS_BLANK)->desc() ]) ]); } public function actionView($id) { + $request = Yii::$app->request; $order = Order::findOne($id); if($order === null){ @@ -80,37 +95,47 @@ public function actionView($id) return $this->redirect(['/admin/'.$this->module->id]); } - if($order->new > 0){ - $order->new = 0; - $order->update(); - } - $goods = Good::find()->where(['order_id' => $order->primaryKey])->with('item')->asc()->all(); + if($request->post('status')){ + $newStatus = $request->post('status'); + $oldStatus = $order->status; - return $this->render('view', [ - 'order' => $order, - 'goods' => $goods - ]); + $order->status = $newStatus; + $order->remark = filter_var($request->post('remark'), FILTER_SANITIZE_STRING); + + if($order->save()){ + if($newStatus != $oldStatus && $request->post('notify')){ + $order->notifyUser(); + } + $this->flash('success', Yii::t('easyii/shopcart', 'Order updated')); + } + else { + $this->flash('error', Yii::t('easyii', 'Update error. {0}', $order->formatErrors())); + } + return $this->refresh(); + } + else { + if ($order->new > 0) { + $order->new = 0; + $order->update(); + } + + $goods = Good::find()->where(['order_id' => $order->primaryKey])->with('item')->asc()->all(); + + return $this->render('view', [ + 'order' => $order, + 'goods' => $goods + ]); + } } public function actionDelete($id) { if(($model = Order::findOne($id))){ $model->delete(); - } else{ + } else { $this->error = Yii::t('easyii', 'Not found'); } return $this->formatResponse(Yii::t('easyii/shopcart', 'Order deleted')); } - - public function actionStatus($id, $status) - { - if(($model = Order::findOne($id)) && array_key_exists($status, Order::states())){ - $model->status = $status; - $model->save(); - } else{ - $this->error = Yii::t('easyii', 'Not found'); - } - return $this->formatResponse(Yii::t('easyii/shopcart', 'Order status changed')); - } } \ No newline at end of file diff --git a/modules/shopcart/controllers/GoodsController.php b/modules/shopcart/controllers/GoodsController.php index d3f3f01..df03dec 100644 --- a/modules/shopcart/controllers/GoodsController.php +++ b/modules/shopcart/controllers/GoodsController.php @@ -12,7 +12,7 @@ public function actionDelete($id) { if(($model = Good::findOne($id))){ $model->delete(); - } else{ + } else { $this->error = Yii::t('easyii', 'Not found'); } return $this->formatResponse(Yii::t('easyii/shopcart', 'Order deleted')); diff --git a/modules/shopcart/controllers/SendController.php b/modules/shopcart/controllers/SendController.php new file mode 100644 index 0000000..bafad1c --- /dev/null +++ b/modules/shopcart/controllers/SendController.php @@ -0,0 +1,22 @@ +request; + + if($model->load($request->post())) { + $returnUrl = Shopcart::send($model->attributes) ? $request->post('successUrl') : $request->post('errorUrl'); + return $this->redirect($returnUrl); + } else { + return $this->redirect(Yii::$app->request->baseUrl); + } + } +} \ No newline at end of file diff --git a/modules/shopcart/mail/en/new_order.php b/modules/shopcart/mail/en/new_order.php new file mode 100644 index 0000000..bc315d1 --- /dev/null +++ b/modules/shopcart/mail/en/new_order.php @@ -0,0 +1,8 @@ +title = $subject; +?> + +

New order created #order_id ?>.

+

You can view it .

\ No newline at end of file diff --git a/modules/shopcart/mail/en/notify_user.php b/modules/shopcart/mail/en/notify_user.php new file mode 100644 index 0000000..46ccf34 --- /dev/null +++ b/modules/shopcart/mail/en/notify_user.php @@ -0,0 +1,40 @@ +title = $subject; + +$total = 0; +?> + +

The status of your order #primaryKey ?> changed.

+

New status: statusName ?>

+
+ + + + + + + + goods as $good) : ?> + discount ? round($good->price * (1 - $good->discount / 100)) : $good->price; + $unitTotal = $good->count * $price; + $total += $unitTotal; + ?> + + + + + + + + + + +
ItemQuantityUnit priceTotal
item->title ?> options ? "($good->options)" : '' ?>count ?>
+ Total: +
+

You can view it .

+
+

This is an automatically generated email – please do not reply to it.

\ No newline at end of file diff --git a/modules/shopcart/mail/ru/new_order.php b/modules/shopcart/mail/ru/new_order.php new file mode 100644 index 0000000..4c12229 --- /dev/null +++ b/modules/shopcart/mail/ru/new_order.php @@ -0,0 +1,10 @@ +title = $subject; +?> + +

Создан новый заказ #order_id ?>.

+

Просмотреть его вы можете .

+
+

Это автоматическое сообщение и на него не нужно отвечать.

\ No newline at end of file diff --git a/modules/shopcart/mail/ru/notify_user.php b/modules/shopcart/mail/ru/notify_user.php new file mode 100644 index 0000000..9538cd5 --- /dev/null +++ b/modules/shopcart/mail/ru/notify_user.php @@ -0,0 +1,40 @@ +title = $subject; + +$total = 0; +?> + +

Статус вашего заказа #primaryKey ?> изменен.

+

Новый статус: statusName ?>

+
+ + + + + + + + goods as $good) : ?> + discount ? round($good->price * (1 - $good->discount / 100)) : $good->price; + $unitTotal = $good->count * $price; + $total += $unitTotal; + ?> + + + + + + + + + + +
ТоварКол-воЦенаВсего
item->title ?> options ? "($good->options)" : '' ?>count ?>
+ Итого: +
+

Посмотреть свой заказ на сайте Вы можете .

+
+

Это автоматическое сообщение и на него не нужно отвечать.

\ No newline at end of file diff --git a/modules/shopcart/messages/ru/admin.php b/modules/shopcart/messages/ru/admin.php index 18d9839..3fbc4b0 100644 --- a/modules/shopcart/messages/ru/admin.php +++ b/modules/shopcart/messages/ru/admin.php @@ -2,12 +2,13 @@ return [ 'Orders' => 'Заказы', 'Order' => 'Заказ', - 'New' => 'Новый', + 'Order updated' => 'Заказ обновлен', + 'Blank' => 'Незаполнен', 'Pending' => 'Ожидает', - 'Confirmed' => 'Подтверждено', - 'Sent' => 'Отправлено', - 'Completed' => 'Завершено', - 'Declined' => 'Отклонено', + 'Processed' => 'В обработке', + 'Sent' => 'Отправлен', + 'Completed' => 'Завершен', + 'Declined' => 'Отклонен', 'Returned' => 'Вовзрат', 'Error' => 'Ошибка', 'Fails' => 'Неудачи', @@ -15,6 +16,7 @@ 'Cost' => 'Стоимость', 'Phone' => 'Телефон', 'Comment' => 'Комментарий', + 'Admin remark' => 'Пометка', 'Items' => 'Товары', 'Options' => 'Опции', 'Count' => 'Кол-во', @@ -22,4 +24,6 @@ 'Price' => 'Цена', 'Total' => 'Всего', 'View' => 'Просмотр', + 'Order status changed' => 'Статус заказа изменен', + 'Notify user on E-mail' => 'Отправить уведомление пользователю на E-mail' ]; \ No newline at end of file