Commit d3406983fd8e580842c649d5c9e77b7890df32f8

Authored by Alexey Boroda
1 parent 5c177682

-Order history half way done

controllers/OrderController.php
... ... @@ -4,6 +4,7 @@
4 4  
5 5 use artweb\artbox\components\SmsSender;
6 6 use artweb\artbox\ecommerce\models\OrderLabelHistory;
  7 + use artweb\artbox\ecommerce\models\OrderLog;
7 8 use artweb\artbox\ecommerce\models\OrderSearch;
8 9 use common\models\User;
9 10 use Yii;
... ... @@ -106,10 +107,13 @@
106 107 public function actionView($id)
107 108 {
108 109 $model = $this->findModel($id);
109   -
110   - $historyData = new ActiveDataProvider([
111   - 'query' => $model->getLabelsHistory()->with('order', 'label', 'user'),
112   - ]);
  110 +
  111 + $historyData = new ActiveDataProvider(
  112 + [
  113 + 'query' => $model->getLabelsHistory()
  114 + ->with('order', 'label', 'user'),
  115 + ]
  116 + );
113 117  
114 118 $dataProvider = new ActiveDataProvider(
115 119 [
... ... @@ -119,8 +123,8 @@
119 123 return $this->render(
120 124 'view',
121 125 [
122   - 'model' => $model,
123   - 'products' => $dataProvider,
  126 + 'model' => $model,
  127 + 'products' => $dataProvider,
124 128 'historyData' => $historyData,
125 129 ]
126 130 );
... ... @@ -133,6 +137,25 @@
133 137 $model->save();
134 138 }
135 139  
  140 + public function actionLog($id)
  141 + {
  142 + $model = Order::findOne($id);
  143 +
  144 + $logData = new ActiveDataProvider(
  145 + [
  146 + 'query' => $model->getLogs(),
  147 + ]
  148 + );
  149 +
  150 + return $this->render(
  151 + 'log',
  152 + [
  153 + 'model' => $model,
  154 + 'logData' => $logData,
  155 + ]
  156 + );
  157 + }
  158 +
136 159 public function actionDelete($id)
137 160 {
138 161 if (\Yii::$app->user->identity->isAdmin()) {
... ...
models/Order.php
... ... @@ -8,6 +8,8 @@
8 8 use yii\behaviors\TimestampBehavior;
9 9 use yii\db\ActiveRecord;
10 10 use yii\db\Query;
  11 + use yii\helpers\Json;
  12 + use yii\helpers\VarDumper;
11 13  
12 14 /**
13 15 * Class Order
... ... @@ -58,6 +60,7 @@
58 60 * @property Label $orderLabel
59 61 * @property Delivery $orderDelivery
60 62 * @property OrderPayment $orderPayment
  63 + * @property OrderLog[] $logs
61 64 */
62 65 class Order extends ActiveRecord
63 66 {
... ... @@ -124,6 +127,18 @@
124 127 return $result;
125 128 }
126 129  
  130 + public function getLogAttributes(string $attr, array $values)
  131 + {
  132 + if ($attr == 'deadline') {
  133 + return [
  134 + 'old' => ''
  135 + /**
  136 + * @todo Start here
  137 + */
  138 + ];
  139 + }
  140 + }
  141 +
127 142 /**
128 143 * @inheritdoc
129 144 */
... ... @@ -222,6 +237,25 @@
222 237  
223 238 public function afterSave($insert, $changedAttributes)
224 239 {
  240 + $data = [];
  241 + foreach ($changedAttributes as $key => $attribute) {
  242 + if ($this->oldAttributes[ $key ] != $attribute) {
  243 + $data[ $key ] = [
  244 + 'old' => $attribute,
  245 + 'new' => $this->oldAttributes[ $key ],
  246 + ];
  247 + }
  248 + }
  249 +
  250 + if (!empty($data) && empty($data[ 'edit_time' ])) {
  251 + $log = new OrderLog();
  252 + $log->order_id = (integer) $this->id;
  253 + $log->user_id = (integer) \Yii::$app->user->identity->id;
  254 + $log->data = Json::encode($data);
  255 +
  256 + $log->save();
  257 + }
  258 +
225 259 if (!empty($changedAttributes[ 'label' ])) {
226 260 if ($this->label != (string) $changedAttributes[ 'label' ]) {
227 261 $history = new OrderLabelHistory();
... ... @@ -382,6 +416,14 @@
382 416 }
383 417  
384 418 /**
  419 + * @return \yii\db\ActiveQuery
  420 + */
  421 + public function getLogs()
  422 + {
  423 + return $this->hasMany(OrderLog::className(), [ 'order_id' => 'id' ]);
  424 + }
  425 +
  426 + /**
385 427 * If deadline is fucked up returns true,
386 428 * if deadline is not setted return false, like everything is ok
387 429 *
... ...
models/OrderLog.php 0 → 100644
  1 +<?php
  2 +
  3 + namespace artweb\artbox\ecommerce\models;
  4 +
  5 + use common\models\User;
  6 + use Yii;
  7 + use yii\behaviors\TimestampBehavior;
  8 + use yii\db\ActiveRecord;
  9 +
  10 + /**
  11 + * This is the model class for table "order_log".
  12 + *
  13 + * @property integer $id
  14 + * @property integer $order_id
  15 + * @property integer $created_at
  16 + * @property integer $user_id
  17 + * @property string $data
  18 + * @property Order $order
  19 + * @property User $user
  20 + */
  21 + class OrderLog extends ActiveRecord
  22 + {
  23 + /**
  24 + * @inheritdoc
  25 + */
  26 + public static function tableName()
  27 + {
  28 + return 'order_log';
  29 + }
  30 +
  31 + /**
  32 + * @inheritdoc
  33 + */
  34 + public function behaviors()
  35 + {
  36 + return [
  37 + [
  38 + 'class' => TimestampBehavior::className(),
  39 + 'updatedAtAttribute' => false,
  40 + ],
  41 + ];
  42 + }
  43 +
  44 + /**
  45 + * @inheritdoc
  46 + */
  47 + public function rules()
  48 + {
  49 + return [
  50 + [
  51 + [
  52 + 'order_id',
  53 + 'created_at',
  54 + 'user_id',
  55 + ],
  56 + 'integer',
  57 + ],
  58 + [
  59 + [ 'data' ],
  60 + 'string',
  61 + ],
  62 + [
  63 + [ 'order_id' ],
  64 + 'exist',
  65 + 'skipOnError' => true,
  66 + 'targetClass' => Order::className(),
  67 + 'targetAttribute' => [ 'order_id' => 'id' ],
  68 + ],
  69 + // [
  70 + // [ 'user_id' ],
  71 + // 'exist',
  72 + // 'skipOnError' => true,
  73 + // 'targetClass' => User::className(),
  74 + // 'targetAttribute' => [ 'user_id' => 'id' ],
  75 + // ],
  76 + ];
  77 + }
  78 +
  79 + /**
  80 + * @inheritdoc
  81 + */
  82 + public function attributeLabels()
  83 + {
  84 + return [
  85 + 'id' => Yii::t('app', 'ID'),
  86 + 'order_id' => Yii::t('app', 'Order ID'),
  87 + 'created_at' => Yii::t('app', 'Created At'),
  88 + 'user_id' => Yii::t('app', 'User ID'),
  89 + 'data' => Yii::t('app', 'Data'),
  90 + ];
  91 + }
  92 +
  93 + /**
  94 + * @return \yii\db\ActiveQuery
  95 + */
  96 + public function getOrder()
  97 + {
  98 + return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]);
  99 + }
  100 +
  101 + /**
  102 + * @return \yii\db\ActiveQuery
  103 + */
  104 + public function getUser()
  105 + {
  106 + return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
  107 + }
  108 + }
... ...
views/order/_log_item.php 0 → 100644
  1 +<?php
  2 + /**
  3 + * @var OrderLog $model
  4 + * @var Order $order
  5 + */
  6 + use artweb\artbox\ecommerce\models\Order;
  7 + use artweb\artbox\ecommerce\models\OrderLog;
  8 + use yii\helpers\Json;
  9 +
  10 +?>
  11 +
  12 +<!-- timeline item -->
  13 +<li>
  14 + <!-- timeline icon -->
  15 + <i class="fa fa-tag bg-blue"></i>
  16 + <div class="timeline-item">
  17 + <span class="time"><i class="fa fa-calendar"></i> <?= Yii::$app->formatter->asDatetime($model->created_at) ?></span>
  18 +
  19 + <h3 class="timeline-header">Title</h3>
  20 +
  21 + <div class="timeline-body">
  22 + <table class="table table-bordered">
  23 + <tr>
  24 + <th>Поле</th>
  25 + <th>с</th>
  26 + <th>на</th>
  27 + </tr>
  28 + <?php
  29 + foreach (Json::decode($model->data) as $key => $item) {
  30 + ?>
  31 + <tr>
  32 + <td><?=$order->attributeLabels()[$key]?></td>
  33 + <td><?=$item['old']?></td>
  34 + <td><?=$item['new']?></td>
  35 + </tr>
  36 + <?php
  37 + }
  38 + ?>
  39 + </table>
  40 + </div>
  41 +
  42 + </div>
  43 +</li>
... ...
views/order/log.php 0 → 100755
  1 +<?php
  2 + use artweb\artbox\ecommerce\models\Order;
  3 + use yii\data\ActiveDataProvider;
  4 + use yii\web\View;
  5 + use yii\widgets\ListView;
  6 +
  7 + /**
  8 + * @var View $this
  9 + * @var ActiveDataProvider $logData
  10 + * @var Order $model
  11 + */
  12 +
  13 +?>
  14 +
  15 +<div class="order-log">
  16 + <div class="box box-default">
  17 + <div class="box-header with-border">
  18 + <h3 class="box-title">История</h3>
  19 +<!-- <div class="box-tools pull-right">-->
  20 +<!-- <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>-->
  21 +<!-- </div>-->
  22 + </div><!-- /.box-header -->
  23 + <div class="box-body">
  24 +
  25 +
  26 + <?php
  27 + echo ListView::widget(
  28 + [
  29 + 'dataProvider' => $logData,
  30 + 'layout' => '{items}',
  31 + 'itemView' => '_log_item',
  32 + 'itemOptions' => [
  33 + 'tag' => false,
  34 + ],
  35 + 'options' => [
  36 + 'tag' => 'ul',
  37 + 'class' => 'list-view timeline',
  38 + ],
  39 + 'viewParams' => [
  40 + 'order' => $model,
  41 + ],
  42 + ]
  43 + );
  44 + ?>
  45 +
  46 +
  47 + </div><!-- /.box-body -->
  48 + </div><!-- /.box -->
  49 +</div>
... ...