From 3bee34454a1e3929cbc5c747836491f85fe25cbd Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 12 Jan 2017 19:18:25 +0200 Subject: [PATCH] -Order history ready --- controllers/OrderController.php | 45 +++++++++++++++++++++++++++++++-------------- controllers/StatisticsController.php | 17 ++++++----------- models/Order.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- models/OrderLabelHistory.php | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/order/_timeline_item.php | 32 ++++++++++++++++++++++++++++++++ views/order/update.php | 13 ++++++++++++- views/order/view.php | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------- views/statistics/index.php | 2 +- 8 files changed, 392 insertions(+), 85 deletions(-) create mode 100644 models/OrderLabelHistory.php create mode 100644 views/order/_timeline_item.php diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 6070352..4e9ef0f 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -3,19 +3,18 @@ namespace artweb\artbox\ecommerce\controllers; use artweb\artbox\components\SmsSender; + use artweb\artbox\ecommerce\models\OrderLabelHistory; use artweb\artbox\ecommerce\models\OrderSearch; use common\models\User; - use phpDocumentor\Reflection\Types\Null_; use Yii; use yii\data\ArrayDataProvider; - use yii\helpers\ArrayHelper; + use yii\db\ActiveQuery; use yii\helpers\Json; use yii\helpers\VarDumper; use yii\web\Controller; use yii\filters\VerbFilter; use yii\data\ActiveDataProvider; use yii\web\ForbiddenHttpException; - use yii\web\HttpException; use artweb\artbox\ecommerce\models\Order; use artweb\artbox\ecommerce\models\OrderProduct; use artweb\artbox\ecommerce\models\ProductVariant; @@ -107,6 +106,11 @@ public function actionView($id) { $model = $this->findModel($id); + + $historyData = new ActiveDataProvider([ + 'query' => $model->getLabelsHistory()->with('order', 'label', 'user'), + ]); + $dataProvider = new ActiveDataProvider( [ 'query' => $model->getProducts(), @@ -117,6 +121,7 @@ [ 'model' => $model, 'products' => $dataProvider, + 'historyData' => $historyData, ] ); } @@ -151,10 +156,10 @@ public function actionAdd() { - if (!empty( \Yii::$app->request->post() )) { + if (!empty(\Yii::$app->request->post())) { $id = \Yii::$app->request->post('OrderProduct')[ 'id' ]; $order_id = \Yii::$app->request->post('OrderProduct')[ 'order_id' ]; - if (!empty( \Yii::$app->request->post('OrderProduct')[ 'count' ] )) { + if (!empty(\Yii::$app->request->post('OrderProduct')[ 'count' ])) { $count = \Yii::$app->request->post('OrderProduct')[ 'count' ]; } else { $count = 1; @@ -174,7 +179,7 @@ ) ->one(); - if (!empty( $model )) { + if (!empty($model)) { $model->count += $count; } else { $model = new OrderProduct(); @@ -219,7 +224,7 @@ if ($orderProduct->load($post)) { $orderProduct->save(); $output = ''; - if (isset( $posted[ 'count' ] )) { + if (isset($posted[ 'count' ])) { $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); } $out = Json::encode( @@ -281,7 +286,7 @@ return $this->renderPartial( 'print', [ - 'order' => $order, + 'order' => $order, 'dataProvider' => $dataProvider, ] ); @@ -307,7 +312,7 @@ $orderProduct->save(); $orderProduct->order->totalRecount(); $output = ''; - if (isset( $posted[ 'count' ] )) { + if (isset($posted[ 'count' ])) { $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); } $out = Json::encode( @@ -342,7 +347,7 @@ ] ); - if (empty( $model->manager_id )) { + if (empty($model->manager_id)) { $model->manager_id = \Yii::$app->user->id; } @@ -369,7 +374,7 @@ } } - public function actionFindProduct($q = null, $id = null) + public function actionFindProduct($q = NULL, $id = NULL) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $out = [ @@ -428,7 +433,7 @@ protected function findModel($id) { - if (( $model = Order::findOne($id) ) !== null) { + if (( $model = Order::findOne($id) ) !== NULL) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); @@ -471,7 +476,7 @@ public function actionBlockOrder() { - if (!empty( \Yii::$app->request->post() )) { + if (!empty(\Yii::$app->request->post())) { \Yii::$app->response->format = Response::FORMAT_JSON; $model = $this->findModel(\Yii::$app->request->post('id')); @@ -488,7 +493,7 @@ if ($model->save()) { return [ 'time' => $date, - 'user' => !empty( $user ) ? $user->username : '', + 'user' => !empty($user) ? $user->username : '', ]; } else { return [ @@ -516,6 +521,18 @@ } $model->published = true; $model->save(); + + /** + * Add order to history + */ + $history = new OrderLabelHistory(); + + $history->label_id = (integer) $model->label; + $history->order_id = (integer) $model->id; + $history->user_id = (integer) \Yii::$app->user->identity->id; + + $history->save(); + /** * @var SmsSender $sender */ diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index cd991ab..880f1fc 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -69,20 +69,15 @@ ] ); - $chartData = []; - foreach ($labelStatistics as $statistic) { - $chartData[] = $statistic[ 'sum' ]; - } - $chartLabels = []; - foreach ($labelStatistics as $name => $statistic) { - $chartLabels[] = $name; - } $labelChartData = [ - 'labels' => $chartLabels, + 'labels' => array_keys($labelStatistics), 'datasets' => [ [ - 'label' => 'На сумму, грн.', - 'data' => $chartData, + 'label' => 'На сумму, грн.', + 'data' => ArrayHelper::getColumn($labelStatistics, 'sum', false), + 'backgroundColor' => 'rgba(255, 99, 132, 0.2)', + 'borderColor' => 'rgba(255,99,132,1)', + 'borderWidth' => 1, ], ], ]; diff --git a/models/Order.php b/models/Order.php index 5e54089..2a7d810 100755 --- a/models/Order.php +++ b/models/Order.php @@ -55,6 +55,9 @@ * @property string $city * @property string $deliveryString * @property boolean $published + * @property Label $orderLabel + * @property Delivery $orderDelivery + * @property OrderPayment $orderPayment */ class Order extends ActiveRecord { @@ -92,7 +95,7 @@ { return 'order'; } - + /** * @param array $where * @@ -113,12 +116,17 @@ [ 'reason' => $id, ] - )->andFilterWhere($where)->one(); + ) + ->andFilterWhere($where) + ->one(); } return $result; } + /** + * @inheritdoc + */ public function behaviors() { return [ @@ -131,6 +139,9 @@ ]; } + /** + * @inheritdoc + */ public function rules() { return [ @@ -209,6 +220,27 @@ ]; } + public function afterSave($insert, $changedAttributes) + { + if (!empty($changedAttributes[ 'label' ])) { + if ($this->label != (string) $changedAttributes[ 'label' ]) { + $history = new OrderLabelHistory(); + + $history->label_id = (integer) $this->label; + $history->order_id = (integer) $this->id; + $history->user_id = (integer) \Yii::$app->user->identity->id; + + if ($history->save()) { + \Yii::$app->session->setFlash('label_update', 'Статус заказа обновлен'); + } + } + } + parent::afterSave($insert, $changedAttributes); + } + + /** + * @inheritdoc + */ public function afterFind() { parent::afterFind(); @@ -216,6 +248,9 @@ } + /** + * @inheritdoc + */ public function beforeSave($insert) { if (parent::beforeSave($insert)) { @@ -227,6 +262,9 @@ } + /** + * Convert some date + */ protected function convertDate() { if (!empty($this->deadline)) { @@ -239,6 +277,9 @@ } + /** + * @inheritdoc + */ public function attributeLabels() { return [ @@ -276,6 +317,9 @@ ]; } + /** + * @return \yii\db\ActiveQuery + */ public function getUser() { return $this->hasOne(Customer::className(), [ 'id' => 'user_id' ]); @@ -314,6 +358,14 @@ } /** + * @return \yii\db\ActiveQuery + */ + public function getLabelsHistory() + { + return $this->hasMany(OrderLabelHistory::className(), [ 'order_id' => 'id' ]); + } + + /** * @return string */ public function getDeliveryString() @@ -345,13 +397,18 @@ } /** - * + * @return \yii\db\ActiveQuery */ public function getManager() { return $this->hasOne(User::className(), [ 'id' => 'manager_id' ]); } + /** + * Check if order is blocked for updating + * + * @return bool + */ public function isBlocked() { if ($this->edit_id === 0) { @@ -365,6 +422,9 @@ } } + /** + * If order products changed recount te total value + */ public function totalRecount() { $products = $this->products; @@ -379,6 +439,9 @@ $this->save(); } + /** + * If exit unpublished order - delete it + */ public function deleteUnpublished() { /** diff --git a/models/OrderLabelHistory.php b/models/OrderLabelHistory.php new file mode 100644 index 0000000..af592a5 --- /dev/null +++ b/models/OrderLabelHistory.php @@ -0,0 +1,121 @@ + TimestampBehavior::className(), + 'updatedAtAttribute' => false, + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'label_id', + 'order_id', + 'user_id', + 'created_at', + ], + 'integer', + ], + [ + [ 'order_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Order::className(), + 'targetAttribute' => [ 'order_id' => 'id' ], + ], + [ + [ 'label_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Label::className(), + 'targetAttribute' => [ 'label_id' => 'id' ], + ], +// [ +// [ 'user_id' ], +// 'exist', +// 'skipOnError' => true, +// 'targetClass' => User::className(), +// 'targetAttribute' => [ 'user_id' => 'id' ], +// ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'label_id' => Yii::t('app', 'Label ID'), + 'order_id' => Yii::t('app', 'Order ID'), + 'user_id' => Yii::t('app', 'User ID'), + 'created_at' => Yii::t('app', 'Created At'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getOrder() + { + return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLabel() + { + return $this->hasOne(Label::className(), [ 'id' => 'label_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); + } + } diff --git a/views/order/_timeline_item.php b/views/order/_timeline_item.php new file mode 100644 index 0000000..a69b768 --- /dev/null +++ b/views/order/_timeline_item.php @@ -0,0 +1,32 @@ + + + +
  • + + +
    + formatter->asDatetime($model->created_at)?> + +

    label->lang->title?>

    + +
    + user)) { + echo Html::tag('p', 'Поступил с сайта', ['class' => 'text-green']); + } else { + echo 'Статус присвоил: ' . Html::tag('p', $model->user->username, [ + 'class' => 'text-blue' + ]); + } + ?> +
    + +
    +
  • diff --git a/views/order/update.php b/views/order/update.php index 879d04e..ef4fc11 100755 --- a/views/order/update.php +++ b/views/order/update.php @@ -54,7 +54,18 @@ $.ajax({ '; $this->registerJs($js, View::POS_READY); - + + if (!empty(\Yii::$app->session->getFlash('label_update'))) { + $js = ' +$.notify({ + message: "Статус заказа обновлен" +}, { + type : "warning" +}) +'; + $this->registerJs($js, View::POS_READY); + } + ?>
    diff --git a/views/order/view.php b/views/order/view.php index e0e3deb..18cc86f 100755 --- a/views/order/view.php +++ b/views/order/view.php @@ -2,13 +2,18 @@ use artweb\artbox\ecommerce\models\Order; use kartik\grid\GridView; + use yii\data\ActiveDataProvider; + use yii\grid\SerialColumn; use yii\helpers\Html; use yii\web\View; use yii\widgets\DetailView; + use yii\widgets\ListView; /** - * @var View $this - * @var Order $model + * @var View $this + * @var Order $model + * @var ActiveDataProvider $products + * @var ActiveDataProvider $historyData */ $this->title = 'Заказ #' . $model->id; @@ -19,63 +24,126 @@ $this->params[ 'breadcrumbs' ][] = $this->title; ?>
    - -

    title) ?>

    - -

    - $model->id, - ], - [ 'class' => 'btn btn-primary' ] - ) ?> - -

    - - $model, - 'attributes' => [ - 'id', - 'deadline', - 'pay', - 'reason', - 'label', - 'name', - 'phone', - 'email', - 'comment', - 'delivery', - 'declaration', - 'stock', - 'payment', - 'insurance', - 'city', - 'adress', - 'body', - 'check', - 'sms', - ], - ] - ) ?> - - title) ?> + +

    + $model->id, + ], + [ 'class' => 'btn btn-primary' ] + ) ?> + +

    + +
    +
    +

    Данные заказа

    +
    + +
    +
    +
    + $products, - 'columns' => [ + 'model' => $model, + 'attributes' => [ 'id', - 'product_name', + 'deadline', + 'pay', + 'reason', + [ + 'label' => 'Статус', + 'value' => $model->orderLabel->lang->title, + ], 'name', - 'sku', - 'price', - 'count', - 'sum_cost', + 'phone', + 'email', + 'comment', + [ + 'label' => 'Способ доставки', + 'value' => $model->orderDelivery->lang->title, + ], + 'declaration', + 'stock', + [ + 'label' => 'Способ оплаты', + 'value' => $model->orderPayment->lang->title, + ], + 'insurance', + 'city', + 'adress', + 'body', + 'check', + 'sms', ], ] - ); - ?> + ) ?> +
    +
    + + +
    +
    +

    Товары

    +
    + +
    +
    +
    + $products, + 'columns' => [ + [ + 'class' => SerialColumn::className(), + ], + 'product_name', + 'name', + 'sku', + 'price', + 'count', + 'sum_cost', + ], + ] + ); + ?> +
    +
    + +
    +
    +

    История

    +
    + +
    +
    +
    + + + $historyData, + 'layout' => '{items}', + 'itemView' => '_timeline_item', + 'itemOptions' => [ + 'tag' => false, + ], + 'options' => [ + 'tag' => 'ul', + 'class' => 'list-view timeline', + ], + ] + ); + ?> + + +
    +
    diff --git a/views/statistics/index.php b/views/statistics/index.php index 6b25c1a..37f394c 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -126,7 +126,7 @@ 'clientOptions' => [ 'title' => [ 'display' => true, - 'text' => 'Custom Chart Title', + 'text' => 'Статистика меток', ], 'scales' => [ 'xAxes' => [ -- libgit2 0.21.4