Commit 17569d93090b5f11fb814197579a1c8e9a9ae80b
1 parent
44caf85a
-Order product log almost ready
-update form prettifying
Showing
12 changed files
with
507 additions
and
139 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | + namespace artweb\artbox\ecommerce\components; | ||
| 3 | + | ||
| 4 | + interface LoggerInterface | ||
| 5 | + { | ||
| 6 | + public static function generateData(array $changedAttributes, array $oldAttributes, bool $insert); | ||
| 7 | + | ||
| 8 | + public static function saveData(array $data, int $identityId, $params = []); | ||
| 9 | + } | ||
| 0 | \ No newline at end of file | 10 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + namespace artweb\artbox\ecommerce\components; | ||
| 3 | + | ||
| 4 | + use artweb\artbox\ecommerce\models\Delivery; | ||
| 5 | + use artweb\artbox\ecommerce\models\Label; | ||
| 6 | + use artweb\artbox\ecommerce\models\Order; | ||
| 7 | + use artweb\artbox\ecommerce\models\OrderLabelHistory; | ||
| 8 | + use artweb\artbox\ecommerce\models\OrderLog; | ||
| 9 | + use artweb\artbox\ecommerce\models\OrderPayment; | ||
| 10 | + use common\models\User; | ||
| 11 | + use yii\base\Object; | ||
| 12 | + use yii\helpers\Json; | ||
| 13 | + | ||
| 14 | + class OrderLogger extends Object implements LoggerInterface | ||
| 15 | + { | ||
| 16 | + /** | ||
| 17 | + * @param array $changedAttributes | ||
| 18 | + * @param array $oldAttributes | ||
| 19 | + * @param bool $insert | ||
| 20 | + * | ||
| 21 | + * @return array | ||
| 22 | + */ | ||
| 23 | + public static function generateData(array $changedAttributes, array $oldAttributes, bool $insert) | ||
| 24 | + { | ||
| 25 | + $data = []; | ||
| 26 | + foreach ($changedAttributes as $key => $attribute) { | ||
| 27 | + if ($oldAttributes[ $key ] != $attribute && $key != 'updated_at') { | ||
| 28 | + $data[ $key ] = self::getOrderLogAttributes( | ||
| 29 | + $key, | ||
| 30 | + [ | ||
| 31 | + 'old' => $attribute, | ||
| 32 | + 'new' => $oldAttributes[ $key ], | ||
| 33 | + ] | ||
| 34 | + ); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + return $data; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * @param array $data | ||
| 43 | + * @param int $identityId | ||
| 44 | + * @param array $params | ||
| 45 | + */ | ||
| 46 | + public static function saveData(array $data, int $identityId, $params = []) | ||
| 47 | + { | ||
| 48 | + if (!empty($data) && empty($data[ 'edit_time' ])) { | ||
| 49 | + $log = new OrderLog(); | ||
| 50 | + $log->order_id = (integer) $identityId; | ||
| 51 | + $log->user_id = (integer) \Yii::$app->user->identity->id; | ||
| 52 | + $log->data = Json::encode($data); | ||
| 53 | + | ||
| 54 | + $log->save(); | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * @param array $attributes | ||
| 60 | + * @param $label | ||
| 61 | + * @param int $id | ||
| 62 | + */ | ||
| 63 | + public static function saveOrderLabelHistory(array $attributes, $label, int $id) | ||
| 64 | + { | ||
| 65 | + if (!empty($attributes[ 'label' ])) { | ||
| 66 | + if ($label != (string) $attributes[ 'label' ]) { | ||
| 67 | + $history = new OrderLabelHistory(); | ||
| 68 | + | ||
| 69 | + $history->label_id = (integer) $label; | ||
| 70 | + $history->order_id = (integer) $id; | ||
| 71 | + $history->user_id = (integer) \Yii::$app->user->identity->id; | ||
| 72 | + | ||
| 73 | + if ($history->save()) { | ||
| 74 | + \Yii::$app->session->setFlash('label_update', 'Статус заказа обновлен'); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * @param string $attr | ||
| 82 | + * @param array $values | ||
| 83 | + * | ||
| 84 | + * @return array | ||
| 85 | + * Return array in form ['old'=>'old value ...', 'new' => 'new value ...'] | ||
| 86 | + */ | ||
| 87 | + protected static function getOrderLogAttributes(string $attr, array $values) | ||
| 88 | + { | ||
| 89 | + if ($attr == 'deadline') { | ||
| 90 | + return [ | ||
| 91 | + 'old' => empty($values[ 'old' ]) ? '' : date('d.m.Y', $values[ 'old' ]), | ||
| 92 | + 'new' => empty($values[ 'new' ]) ? '' : date('d.m.Y', $values[ 'new' ]), | ||
| 93 | + ]; | ||
| 94 | + } elseif ($attr == 'reason') { | ||
| 95 | + return [ | ||
| 96 | + 'old' => empty($values[ 'old' ]) ? '' : Order::REASONS[ $values[ 'old' ] ], | ||
| 97 | + 'new' => empty($values[ 'new' ]) ? '' : Order::REASONS[ $values[ 'new' ] ], | ||
| 98 | + ]; | ||
| 99 | + } elseif ($attr == 'label') { | ||
| 100 | + $labels = Label::find() | ||
| 101 | + ->with('lang') | ||
| 102 | + ->indexBy('id') | ||
| 103 | + ->all(); | ||
| 104 | + return [ | ||
| 105 | + 'old' => empty($values[ 'old' ]) ? '' : $labels[ $values[ 'old' ] ]->lang->title, | ||
| 106 | + 'new' => empty($values[ 'new' ]) ? '' : $labels[ $values[ 'new' ] ]->lang->title, | ||
| 107 | + ]; | ||
| 108 | + } elseif ($attr == 'delivery') { | ||
| 109 | + $deliveries = Delivery::find() | ||
| 110 | + ->with('lang') | ||
| 111 | + ->indexBy('id') | ||
| 112 | + ->all(); | ||
| 113 | + return [ | ||
| 114 | + 'old' => empty($values[ 'old' ]) ? '' : $deliveries[ $values[ 'old' ] ]->lang->title, | ||
| 115 | + 'new' => empty($values[ 'new' ]) ? '' : $deliveries[ $values[ 'new' ] ]->lang->title, | ||
| 116 | + ]; | ||
| 117 | + } elseif ($attr == 'manager_id') { | ||
| 118 | + $users = User::find() | ||
| 119 | + ->indexBy('id') | ||
| 120 | + ->all(); | ||
| 121 | + return [ | ||
| 122 | + 'old' => empty($values[ 'old' ]) ? '' : $users[ $values[ 'old' ] ]->username, | ||
| 123 | + 'new' => empty($values[ 'new' ]) ? '' : $users[ $values[ 'new' ] ]->username, | ||
| 124 | + ]; | ||
| 125 | + } elseif ($attr == 'payment') { | ||
| 126 | + $payment = OrderPayment::find() | ||
| 127 | + ->with('lang') | ||
| 128 | + ->indexBy('id') | ||
| 129 | + ->all(); | ||
| 130 | + return [ | ||
| 131 | + 'old' => empty($values[ 'old' ]) ? '' : $payment[ $values[ 'old' ] ]->lang->title, | ||
| 132 | + 'new' => empty($values[ 'new' ]) ? '' : $payment[ $values[ 'new' ] ]->lang->title, | ||
| 133 | + ]; | ||
| 134 | + } elseif ($attr == 'shipping_by') { | ||
| 135 | + return [ | ||
| 136 | + 'old' => empty($values[ 'old' ]) ? '' : Order::SHIPPING_BY[ $values[ 'old' ] ][ 'label' ], | ||
| 137 | + 'new' => empty($values[ 'new' ]) ? '' : Order::SHIPPING_BY[ $values[ 'new' ] ][ 'label' ], | ||
| 138 | + ]; | ||
| 139 | + } elseif ($attr == 'pay') { | ||
| 140 | + return [ | ||
| 141 | + 'old' => ( $values[ 'old' ] == true ) ? 'Оплачен' : 'Не оплачен', | ||
| 142 | + 'new' => ( $values[ 'new' ] == true ) ? 'Оплачен' : 'Не оплачен', | ||
| 143 | + ]; | ||
| 144 | + } else { | ||
| 145 | + return $values; | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + } | ||
| 0 | \ No newline at end of file | 149 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + namespace artweb\artbox\ecommerce\components; | ||
| 3 | + | ||
| 4 | + use artweb\artbox\ecommerce\models\OrderProductLog; | ||
| 5 | + use yii\base\Object; | ||
| 6 | + use yii\helpers\Json; | ||
| 7 | + | ||
| 8 | + class OrderProductLogger extends Object implements LoggerInterface | ||
| 9 | + { | ||
| 10 | + /** | ||
| 11 | + * @param array $changedAttributes | ||
| 12 | + * @param array $oldAttributes | ||
| 13 | + * @param bool $insert | ||
| 14 | + * | ||
| 15 | + * @return array | ||
| 16 | + */ | ||
| 17 | + public static function generateData(array $changedAttributes, array $oldAttributes, bool $insert) | ||
| 18 | + { | ||
| 19 | + $data = []; | ||
| 20 | + foreach ($changedAttributes as $key => $attribute) { | ||
| 21 | + if ($oldAttributes[ $key ] != $attribute) { | ||
| 22 | + $data[ $key ] = [ | ||
| 23 | + 'old' => $attribute, | ||
| 24 | + 'new' => $oldAttributes[ $key ], | ||
| 25 | + ]; | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + return $data; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * @param array $data | ||
| 34 | + * @param int $identityId | ||
| 35 | + * @param array $params | ||
| 36 | + */ | ||
| 37 | + public static function saveData(array $data, int $identityId, $params = []) | ||
| 38 | + { | ||
| 39 | + if (!empty($data)) { | ||
| 40 | + $log = new OrderProductLog(); | ||
| 41 | + $log->order_product_id = (integer) $identityId; | ||
| 42 | + $log->order_id = $params['order_id']; | ||
| 43 | + $log->user_id = (integer) \Yii::$app->user->identity->id; | ||
| 44 | + $log->data = Json::encode($data); | ||
| 45 | + | ||
| 46 | + $log->save(); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + } | ||
| 0 | \ No newline at end of file | 50 | \ No newline at end of file |
controllers/OrderController.php
| @@ -109,14 +109,14 @@ | @@ -109,14 +109,14 @@ | ||
| 109 | public function actionView($id) | 109 | public function actionView($id) |
| 110 | { | 110 | { |
| 111 | $model = $this->findModel($id); | 111 | $model = $this->findModel($id); |
| 112 | - | 112 | + |
| 113 | $historyData = new ActiveDataProvider( | 113 | $historyData = new ActiveDataProvider( |
| 114 | [ | 114 | [ |
| 115 | 'query' => $model->getLabelsHistory() | 115 | 'query' => $model->getLabelsHistory() |
| 116 | ->with('order', 'label', 'user'), | 116 | ->with('order', 'label', 'user'), |
| 117 | ] | 117 | ] |
| 118 | ); | 118 | ); |
| 119 | - | 119 | + |
| 120 | $dataProvider = new ActiveDataProvider( | 120 | $dataProvider = new ActiveDataProvider( |
| 121 | [ | 121 | [ |
| 122 | 'query' => $model->getProducts(), | 122 | 'query' => $model->getProducts(), |
| @@ -142,22 +142,29 @@ | @@ -142,22 +142,29 @@ | ||
| 142 | public function actionLog($id) | 142 | public function actionLog($id) |
| 143 | { | 143 | { |
| 144 | $model = Order::findOne($id); | 144 | $model = Order::findOne($id); |
| 145 | - | 145 | + |
| 146 | $logData = new ActiveDataProvider( | 146 | $logData = new ActiveDataProvider( |
| 147 | [ | 147 | [ |
| 148 | 'query' => $model->getLogs(), | 148 | 'query' => $model->getLogs(), |
| 149 | ] | 149 | ] |
| 150 | ); | 150 | ); |
| 151 | - | 151 | + |
| 152 | + $productLogData = new ActiveDataProvider( | ||
| 153 | + [ | ||
| 154 | + 'query' => $model->getProductLogs(), | ||
| 155 | + ] | ||
| 156 | + ); | ||
| 157 | + | ||
| 152 | return $this->render( | 158 | return $this->render( |
| 153 | 'log', | 159 | 'log', |
| 154 | [ | 160 | [ |
| 155 | - 'model' => $model, | ||
| 156 | - 'logData' => $logData, | 161 | + 'model' => $model, |
| 162 | + 'logData' => $logData, | ||
| 163 | + 'productLogData' => $productLogData, | ||
| 157 | ] | 164 | ] |
| 158 | ); | 165 | ); |
| 159 | } | 166 | } |
| 160 | - | 167 | + |
| 161 | public function actionDelete($id) | 168 | public function actionDelete($id) |
| 162 | { | 169 | { |
| 163 | if (\Yii::$app->user->identity->isAdmin()) { | 170 | if (\Yii::$app->user->identity->isAdmin()) { |
| @@ -354,7 +361,7 @@ | @@ -354,7 +361,7 @@ | ||
| 354 | } | 361 | } |
| 355 | 362 | ||
| 356 | $model = $this->findModel($id); | 363 | $model = $this->findModel($id); |
| 357 | - | 364 | + |
| 358 | if ($model->payment == 10) { | 365 | if ($model->payment == 10) { |
| 359 | $model->validators->append( | 366 | $model->validators->append( |
| 360 | new NumberValidator( | 367 | new NumberValidator( |
| @@ -366,7 +373,7 @@ | @@ -366,7 +373,7 @@ | ||
| 366 | ) | 373 | ) |
| 367 | ); | 374 | ); |
| 368 | } | 375 | } |
| 369 | - | 376 | + |
| 370 | /** | 377 | /** |
| 371 | * @var User $user | 378 | * @var User $user |
| 372 | */ | 379 | */ |
| @@ -394,14 +401,14 @@ | @@ -394,14 +401,14 @@ | ||
| 394 | $headers->set('Access-Control-Allow-Origin', '*'); | 401 | $headers->set('Access-Control-Allow-Origin', '*'); |
| 395 | 402 | ||
| 396 | if ($model->load(Yii::$app->request->post()) && $model->save()) { | 403 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
| 397 | - | 404 | + |
| 398 | if ($model->published != true) { | 405 | if ($model->published != true) { |
| 399 | $model->published = true; | 406 | $model->published = true; |
| 400 | $model->save(); | 407 | $model->save(); |
| 401 | /** | 408 | /** |
| 402 | * @var SmsSender $sender | 409 | * @var SmsSender $sender |
| 403 | */ | 410 | */ |
| 404 | - | 411 | + |
| 405 | $sender = \Yii::$app->sender; | 412 | $sender = \Yii::$app->sender; |
| 406 | $sender->send( | 413 | $sender->send( |
| 407 | $model->phone, | 414 | $model->phone, |
| @@ -413,8 +420,7 @@ | @@ -413,8 +420,7 @@ | ||
| 413 | ) | 420 | ) |
| 414 | ); | 421 | ); |
| 415 | } | 422 | } |
| 416 | - | ||
| 417 | - | 423 | + |
| 418 | $this->unblockOrder($model->id); | 424 | $this->unblockOrder($model->id); |
| 419 | return $this->render( | 425 | return $this->render( |
| 420 | 'update', | 426 | 'update', |
| @@ -574,7 +580,7 @@ | @@ -574,7 +580,7 @@ | ||
| 574 | $model->save(); | 580 | $model->save(); |
| 575 | } | 581 | } |
| 576 | 582 | ||
| 577 | - public function actionPublishOrder($id,$phone) | 583 | + public function actionPublishOrder($id, $phone) |
| 578 | { | 584 | { |
| 579 | $model = Order::findOne($id); | 585 | $model = Order::findOne($id); |
| 580 | if ($model->published == true) { | 586 | if ($model->published == true) { |
| @@ -582,23 +588,23 @@ | @@ -582,23 +588,23 @@ | ||
| 582 | } | 588 | } |
| 583 | $model->published = true; | 589 | $model->published = true; |
| 584 | $model->save(); | 590 | $model->save(); |
| 585 | - | 591 | + |
| 586 | /** | 592 | /** |
| 587 | * Add order to history | 593 | * Add order to history |
| 588 | */ | 594 | */ |
| 589 | $history = new OrderLabelHistory(); | 595 | $history = new OrderLabelHistory(); |
| 590 | - | 596 | + |
| 591 | $history->label_id = (integer) $model->label; | 597 | $history->label_id = (integer) $model->label; |
| 592 | $history->order_id = (integer) $model->id; | 598 | $history->order_id = (integer) $model->id; |
| 593 | $history->user_id = (integer) \Yii::$app->user->identity->id; | 599 | $history->user_id = (integer) \Yii::$app->user->identity->id; |
| 594 | - | 600 | + |
| 595 | $history->save(); | 601 | $history->save(); |
| 596 | - | 602 | + |
| 597 | /** | 603 | /** |
| 598 | * @var SmsSender $sender | 604 | * @var SmsSender $sender |
| 599 | */ | 605 | */ |
| 600 | $sender = \Yii::$app->sender; | 606 | $sender = \Yii::$app->sender; |
| 601 | - if(!empty($phone)){ | 607 | + if (!empty($phone)) { |
| 602 | $sender->send( | 608 | $sender->send( |
| 603 | $phone, | 609 | $phone, |
| 604 | $this->renderPartial( | 610 | $this->renderPartial( |
| @@ -609,6 +615,6 @@ | @@ -609,6 +615,6 @@ | ||
| 609 | ) | 615 | ) |
| 610 | ); | 616 | ); |
| 611 | } | 617 | } |
| 612 | - | 618 | + |
| 613 | } | 619 | } |
| 614 | } | 620 | } |
models/Order.php
| 1 | <?php | 1 | <?php |
| 2 | namespace artweb\artbox\ecommerce\models; | 2 | namespace artweb\artbox\ecommerce\models; |
| 3 | 3 | ||
| 4 | + use artweb\artbox\ecommerce\components\OrderLogger; | ||
| 4 | use artweb\artbox\models\Customer; | 5 | use artweb\artbox\models\Customer; |
| 5 | use common\behaviors\DefaultLabelBehavior; | 6 | use common\behaviors\DefaultLabelBehavior; |
| 6 | use common\models\User; | 7 | use common\models\User; |
| @@ -8,8 +9,6 @@ | @@ -8,8 +9,6 @@ | ||
| 8 | use yii\behaviors\TimestampBehavior; | 9 | use yii\behaviors\TimestampBehavior; |
| 9 | use yii\db\ActiveRecord; | 10 | use yii\db\ActiveRecord; |
| 10 | use yii\db\Query; | 11 | use yii\db\Query; |
| 11 | - use yii\helpers\Json; | ||
| 12 | - use yii\helpers\VarDumper; | ||
| 13 | 12 | ||
| 14 | /** | 13 | /** |
| 15 | * Class Order | 14 | * Class Order |
| @@ -133,75 +132,6 @@ | @@ -133,75 +132,6 @@ | ||
| 133 | } | 132 | } |
| 134 | 133 | ||
| 135 | /** | 134 | /** |
| 136 | - * @param string $attr | ||
| 137 | - * @param array $values | ||
| 138 | - * | ||
| 139 | - * @return array | ||
| 140 | - * Return array in form ['old'=>'old value ...', 'new' => 'new value ...'] | ||
| 141 | - */ | ||
| 142 | - public function getLogAttributes(string $attr, array $values) | ||
| 143 | - { | ||
| 144 | - if ($attr == 'deadline') { | ||
| 145 | - return [ | ||
| 146 | - 'old' => empty($values[ 'old' ]) ? '' : date('d.m.Y', $values[ 'old' ]), | ||
| 147 | - 'new' => empty($values[ 'new' ]) ? '' : date('d.m.Y', $values[ 'new' ]), | ||
| 148 | - ]; | ||
| 149 | - } elseif ($attr == 'reason') { | ||
| 150 | - return [ | ||
| 151 | - 'old' => empty($values[ 'old' ]) ? '' : self::REASONS[ $values[ 'old' ] ], | ||
| 152 | - 'new' => empty($values[ 'new' ]) ? '' : self::REASONS[ $values[ 'new' ] ], | ||
| 153 | - ]; | ||
| 154 | - } elseif ($attr == 'label') { | ||
| 155 | - $labels = Label::find() | ||
| 156 | - ->with('lang') | ||
| 157 | - ->indexBy('id') | ||
| 158 | - ->all(); | ||
| 159 | - return [ | ||
| 160 | - 'old' => empty($values[ 'old' ]) ? '' : $labels[ $values[ 'old' ] ]->lang->title, | ||
| 161 | - 'new' => empty($values[ 'new' ]) ? '' : $labels[ $values[ 'new' ] ]->lang->title, | ||
| 162 | - ]; | ||
| 163 | - } elseif ($attr == 'delivery') { | ||
| 164 | - $deliveries = Delivery::find() | ||
| 165 | - ->with('lang') | ||
| 166 | - ->indexBy('id') | ||
| 167 | - ->all(); | ||
| 168 | - return [ | ||
| 169 | - 'old' => empty($values[ 'old' ]) ? '' : $deliveries[ $values[ 'old' ] ]->lang->title, | ||
| 170 | - 'new' => empty($values[ 'new' ]) ? '' : $deliveries[ $values[ 'new' ] ]->lang->title, | ||
| 171 | - ]; | ||
| 172 | - } elseif ($attr == 'manager_id') { | ||
| 173 | - $users = User::find() | ||
| 174 | - ->indexBy('id') | ||
| 175 | - ->all(); | ||
| 176 | - return [ | ||
| 177 | - 'old' => empty($values[ 'old' ]) ? '' : $users[ $values[ 'old' ] ]->username, | ||
| 178 | - 'new' => empty($values[ 'new' ]) ? '' : $users[ $values[ 'new' ] ]->username, | ||
| 179 | - ]; | ||
| 180 | - } elseif ($attr == 'payment') { | ||
| 181 | - $payment = OrderPayment::find() | ||
| 182 | - ->with('lang') | ||
| 183 | - ->indexBy('id') | ||
| 184 | - ->all(); | ||
| 185 | - return [ | ||
| 186 | - 'old' => empty($values[ 'old' ]) ? '' : $payment[ $values[ 'old' ] ]->lang->title, | ||
| 187 | - 'new' => empty($values[ 'new' ]) ? '' : $payment[ $values[ 'new' ] ]->lang->title, | ||
| 188 | - ]; | ||
| 189 | - } elseif ($attr == 'shipping_by') { | ||
| 190 | - return [ | ||
| 191 | - 'old' => empty($values[ 'old' ]) ? '' : self::SHIPPING_BY[ $values[ 'old' ] ][ 'label' ], | ||
| 192 | - 'new' => empty($values[ 'new' ]) ? '' : self::SHIPPING_BY[ $values[ 'new' ] ][ 'label' ], | ||
| 193 | - ]; | ||
| 194 | - } elseif ($attr == 'pay') { | ||
| 195 | - return [ | ||
| 196 | - 'old' => ( $values[ 'old' ] == true ) ? 'Оплачен' : 'Не оплачен', | ||
| 197 | - 'new' => ( $values[ 'new' ] == true ) ? 'Оплачен' : 'Не оплачен', | ||
| 198 | - ]; | ||
| 199 | - } else { | ||
| 200 | - return $values; | ||
| 201 | - } | ||
| 202 | - } | ||
| 203 | - | ||
| 204 | - /** | ||
| 205 | * @inheritdoc | 135 | * @inheritdoc |
| 206 | */ | 136 | */ |
| 207 | public function behaviors() | 137 | public function behaviors() |
| @@ -314,41 +244,11 @@ | @@ -314,41 +244,11 @@ | ||
| 314 | 244 | ||
| 315 | public function afterSave($insert, $changedAttributes) | 245 | public function afterSave($insert, $changedAttributes) |
| 316 | { | 246 | { |
| 317 | - $data = []; | ||
| 318 | - foreach ($changedAttributes as $key => $attribute) { | ||
| 319 | - if ($this->oldAttributes[ $key ] != $attribute && $key != 'updated_at') { | ||
| 320 | - $data[ $key ] = $this->getLogAttributes( | ||
| 321 | - $key, | ||
| 322 | - [ | ||
| 323 | - 'old' => $attribute, | ||
| 324 | - 'new' => $this->oldAttributes[ $key ], | ||
| 325 | - ] | ||
| 326 | - ); | ||
| 327 | - } | ||
| 328 | - } | 247 | + $data = OrderLogger::generateData($changedAttributes, $this->oldAttributes, $insert); |
| 248 | + OrderLogger::saveData($data, $this->id); | ||
| 329 | 249 | ||
| 330 | - if (!empty($data) && empty($data[ 'edit_time' ])) { | ||
| 331 | - $log = new OrderLog(); | ||
| 332 | - $log->order_id = (integer) $this->id; | ||
| 333 | - $log->user_id = (integer) \Yii::$app->user->identity->id; | ||
| 334 | - $log->data = Json::encode($data); | ||
| 335 | - | ||
| 336 | - $log->save(); | ||
| 337 | - } | 250 | + OrderLogger::saveOrderLabelHistory($changedAttributes, $this->label, $this->id); |
| 338 | 251 | ||
| 339 | - if (!empty($changedAttributes[ 'label' ])) { | ||
| 340 | - if ($this->label != (string) $changedAttributes[ 'label' ]) { | ||
| 341 | - $history = new OrderLabelHistory(); | ||
| 342 | - | ||
| 343 | - $history->label_id = (integer) $this->label; | ||
| 344 | - $history->order_id = (integer) $this->id; | ||
| 345 | - $history->user_id = (integer) \Yii::$app->user->identity->id; | ||
| 346 | - | ||
| 347 | - if ($history->save()) { | ||
| 348 | - \Yii::$app->session->setFlash('label_update', 'Статус заказа обновлен'); | ||
| 349 | - } | ||
| 350 | - } | ||
| 351 | - } | ||
| 352 | parent::afterSave($insert, $changedAttributes); | 252 | parent::afterSave($insert, $changedAttributes); |
| 353 | } | 253 | } |
| 354 | 254 | ||
| @@ -504,6 +404,14 @@ | @@ -504,6 +404,14 @@ | ||
| 504 | { | 404 | { |
| 505 | return $this->hasMany(OrderLog::className(), [ 'order_id' => 'id' ]); | 405 | return $this->hasMany(OrderLog::className(), [ 'order_id' => 'id' ]); |
| 506 | } | 406 | } |
| 407 | + | ||
| 408 | + /** | ||
| 409 | + * @return \yii\db\ActiveQuery | ||
| 410 | + */ | ||
| 411 | + public function getProductLogs() | ||
| 412 | + { | ||
| 413 | + return $this->hasMany(OrderProductLog::className(), [ 'order_id' => 'id' ]); | ||
| 414 | + } | ||
| 507 | 415 | ||
| 508 | /** | 416 | /** |
| 509 | * If deadline is fucked up returns true, | 417 | * If deadline is fucked up returns true, |
models/OrderLog.php
| @@ -66,13 +66,6 @@ | @@ -66,13 +66,6 @@ | ||
| 66 | 'targetClass' => Order::className(), | 66 | 'targetClass' => Order::className(), |
| 67 | 'targetAttribute' => [ 'order_id' => 'id' ], | 67 | 'targetAttribute' => [ 'order_id' => 'id' ], |
| 68 | ], | 68 | ], |
| 69 | - // [ | ||
| 70 | - // [ 'user_id' ], | ||
| 71 | - // 'exist', | ||
| 72 | - // 'skipOnError' => true, | ||
| 73 | - // 'targetClass' => User::className(), | ||
| 74 | - // 'targetAttribute' => [ 'user_id' => 'id' ], | ||
| 75 | - // ], | ||
| 76 | ]; | 69 | ]; |
| 77 | } | 70 | } |
| 78 | 71 |
models/OrderProduct.php
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace artweb\artbox\ecommerce\models; | 3 | namespace artweb\artbox\ecommerce\models; |
| 4 | 4 | ||
| 5 | + use artweb\artbox\ecommerce\components\OrderProductLogger; | ||
| 5 | use Yii; | 6 | use Yii; |
| 6 | use yii\db\ActiveRecord; | 7 | use yii\db\ActiveRecord; |
| 7 | 8 | ||
| @@ -33,11 +34,19 @@ | @@ -33,11 +34,19 @@ | ||
| 33 | return 'order_product'; | 34 | return 'order_product'; |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 37 | + public function afterSave($insert, $changedAttributes) | ||
| 38 | + { | ||
| 39 | + $data = OrderProductLogger::generateData($changedAttributes, $this->oldAttributes, $insert); | ||
| 40 | + OrderProductLogger::saveData($data, $this->id, [ 'order_id' => $this->order_id ]); | ||
| 41 | + | ||
| 42 | + parent::afterSave($insert, $changedAttributes); | ||
| 43 | + } | ||
| 44 | + | ||
| 36 | public function beforeSave($insert) | 45 | public function beforeSave($insert) |
| 37 | { | 46 | { |
| 38 | $this->price = $this->productVariant->price; | 47 | $this->price = $this->productVariant->price; |
| 39 | $this->sum_cost = $this->price * $this->count; | 48 | $this->sum_cost = $this->price * $this->count; |
| 40 | - return parent::beforeSave($insert); // TODO: Change the autogenerated stub | 49 | + return parent::beforeSave($insert); |
| 41 | } | 50 | } |
| 42 | 51 | ||
| 43 | public function rules() | 52 | public function rules() |
| @@ -82,8 +91,8 @@ | @@ -82,8 +91,8 @@ | ||
| 82 | 'product_name' => Yii::t('app', 'Наименование'), | 91 | 'product_name' => Yii::t('app', 'Наименование'), |
| 83 | 'name' => Yii::t('app', 'op_name'), | 92 | 'name' => Yii::t('app', 'op_name'), |
| 84 | 'art' => Yii::t('app', 'art'), | 93 | 'art' => Yii::t('app', 'art'), |
| 85 | - 'cost' => Yii::t('app', 'cost'), | ||
| 86 | - 'count' => Yii::t('app', 'count'), | 94 | + 'cost' => Yii::t('app', 'Сумма'), |
| 95 | + 'count' => Yii::t('app', 'Количество'), | ||
| 87 | 'sum_cost' => Yii::t('app', 'Сумма'), | 96 | 'sum_cost' => Yii::t('app', 'Сумма'), |
| 88 | 'status' => Yii::t('app', 'Статус'), | 97 | 'status' => Yii::t('app', 'Статус'), |
| 89 | 'booking' => Yii::t('app', 'Бронь'), | 98 | 'booking' => Yii::t('app', 'Бронь'), |
| 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_product_log". | ||
| 12 | + * | ||
| 13 | + * @property integer $id | ||
| 14 | + * @property integer $order_product_id | ||
| 15 | + * @property integer $created_at | ||
| 16 | + * @property integer $user_id | ||
| 17 | + * @property integer $order_id | ||
| 18 | + * @property string $data | ||
| 19 | + * @property Order $order | ||
| 20 | + * @property OrderProduct $orderProduct | ||
| 21 | + * @property User $user | ||
| 22 | + */ | ||
| 23 | + class OrderProductLog extends ActiveRecord | ||
| 24 | + { | ||
| 25 | + /** | ||
| 26 | + * @inheritdoc | ||
| 27 | + */ | ||
| 28 | + public static function tableName() | ||
| 29 | + { | ||
| 30 | + return 'order_product_log'; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + public function behaviors() | ||
| 34 | + { | ||
| 35 | + return [ | ||
| 36 | + [ | ||
| 37 | + 'class' => TimestampBehavior::className(), | ||
| 38 | + 'updatedAtAttribute' => false, | ||
| 39 | + ], | ||
| 40 | + ]; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * @inheritdoc | ||
| 45 | + */ | ||
| 46 | + public function rules() | ||
| 47 | + { | ||
| 48 | + return [ | ||
| 49 | + [ | ||
| 50 | + [ | ||
| 51 | + 'order_product_id', | ||
| 52 | + 'created_at', | ||
| 53 | + 'user_id', | ||
| 54 | + 'order_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 | + [ 'order_product_id' ], | ||
| 71 | + 'exist', | ||
| 72 | + 'skipOnError' => true, | ||
| 73 | + 'targetClass' => OrderProduct::className(), | ||
| 74 | + 'targetAttribute' => [ 'order_product_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_product_id' => Yii::t('app', 'Order Product ID'), | ||
| 87 | + 'created_at' => Yii::t('app', 'Created At'), | ||
| 88 | + 'user_id' => Yii::t('app', 'User ID'), | ||
| 89 | + 'order_id' => Yii::t('app', 'Order ID'), | ||
| 90 | + 'data' => Yii::t('app', 'Data'), | ||
| 91 | + ]; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * @return \yii\db\ActiveQuery | ||
| 96 | + */ | ||
| 97 | + public function getOrder() | ||
| 98 | + { | ||
| 99 | + return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * @return \yii\db\ActiveQuery | ||
| 104 | + */ | ||
| 105 | + public function getOrderProduct() | ||
| 106 | + { | ||
| 107 | + return $this->hasOne(OrderProduct::className(), [ 'id' => 'order_product_id' ]); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * @return \yii\db\ActiveQuery | ||
| 112 | + */ | ||
| 113 | + public function getUser() | ||
| 114 | + { | ||
| 115 | + return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); | ||
| 116 | + } | ||
| 117 | + } |
views/order/_form.php
| @@ -149,9 +149,25 @@ JS; | @@ -149,9 +149,25 @@ JS; | ||
| 149 | 'id' => 'main-form', | 149 | 'id' => 'main-form', |
| 150 | ] | 150 | ] |
| 151 | ); ?> | 151 | ); ?> |
| 152 | + | ||
| 153 | + | ||
| 154 | +<div class="box box-default"> | ||
| 155 | + <div class="box-header with-border"> | ||
| 156 | + <h3 class="box-title">История</h3> | ||
| 157 | + <div class="box-tools pull-right"> | ||
| 158 | + <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> | ||
| 159 | + </button> | ||
| 160 | + </div> | ||
| 161 | + </div> | ||
| 162 | + <div class="box-body"> | ||
| 163 | + | ||
| 164 | + | ||
| 152 | <div class="container"> | 165 | <div class="container"> |
| 153 | <div class="form-group"> | 166 | <div class="form-group"> |
| 154 | <br> | 167 | <br> |
| 168 | + | ||
| 169 | + | ||
| 170 | + | ||
| 155 | <div class="row"> | 171 | <div class="row"> |
| 156 | <div class="col-sm-6"> | 172 | <div class="col-sm-6"> |
| 157 | 173 | ||
| @@ -363,12 +379,28 @@ JS; | @@ -363,12 +379,28 @@ JS; | ||
| 363 | 379 | ||
| 364 | </div> | 380 | </div> |
| 365 | </div> | 381 | </div> |
| 382 | + | ||
| 383 | + | ||
| 384 | + | ||
| 366 | </div> | 385 | </div> |
| 367 | </div> | 386 | </div> |
| 368 | 387 | ||
| 388 | + </div><!-- /.box-body --> | ||
| 389 | +</div><!-- /.box --> | ||
| 390 | + | ||
| 369 | <?php ActiveForm::end(); ?> | 391 | <?php ActiveForm::end(); ?> |
| 370 | -<br> | ||
| 371 | -<br> | 392 | + |
| 393 | + | ||
| 394 | +<div class="box box-default"> | ||
| 395 | + <div class="box-header with-border"> | ||
| 396 | + <h3 class="box-title">История</h3> | ||
| 397 | + <div class="box-tools pull-right"> | ||
| 398 | + <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> | ||
| 399 | + </button> | ||
| 400 | + </div> | ||
| 401 | + </div> | ||
| 402 | + <div class="box-body"> | ||
| 403 | + | ||
| 372 | <div class="container"> | 404 | <div class="container"> |
| 373 | <div class="row"> | 405 | <div class="row"> |
| 374 | <?php | 406 | <?php |
| @@ -673,7 +705,16 @@ JS; | @@ -673,7 +705,16 @@ JS; | ||
| 673 | <?php ActiveForm::end(); ?> | 705 | <?php ActiveForm::end(); ?> |
| 674 | </div> | 706 | </div> |
| 675 | 707 | ||
| 708 | + | ||
| 709 | + | ||
| 710 | +</div> | ||
| 711 | + | ||
| 712 | + </div><!-- /.box-body --> | ||
| 713 | +</div><!-- /.box --> | ||
| 714 | + | ||
| 715 | + | ||
| 676 | <br> | 716 | <br> |
| 717 | +<div class="container"> | ||
| 677 | <div class="row"> | 718 | <div class="row"> |
| 678 | <?= Html::button( | 719 | <?= Html::button( |
| 679 | $model->isNewRecord ? \Yii::t('app', 'Создать') : \Yii::t('app', 'Сохранить'), | 720 | $model->isNewRecord ? \Yii::t('app', 'Создать') : \Yii::t('app', 'Сохранить'), |
| @@ -708,7 +749,6 @@ JS; | @@ -708,7 +749,6 @@ JS; | ||
| 708 | ] | 749 | ] |
| 709 | ) ?> | 750 | ) ?> |
| 710 | </div> | 751 | </div> |
| 711 | - | ||
| 712 | </div> | 752 | </div> |
| 713 | <br> | 753 | <br> |
| 714 | <br> | 754 | <br> |
| 1 | +<?php | ||
| 2 | + /** | ||
| 3 | + * @var OrderProductLog $model | ||
| 4 | + * @var Order $order | ||
| 5 | + */ | ||
| 6 | + use artweb\artbox\ecommerce\models\Order; | ||
| 7 | + use artweb\artbox\ecommerce\models\OrderProductLog; | ||
| 8 | + use yii\helpers\Html; | ||
| 9 | + use yii\helpers\Json; | ||
| 10 | + | ||
| 11 | +?> | ||
| 12 | + | ||
| 13 | +<!-- timeline item --> | ||
| 14 | +<li> | ||
| 15 | + <!-- timeline icon --> | ||
| 16 | + <i class="fa fa-user bg-green"></i> | ||
| 17 | + <div class="timeline-item"> | ||
| 18 | + <span class="time"><i class="fa fa-calendar"></i> <?= Yii::$app->formatter->asDatetime($model->created_at) ?></span> | ||
| 19 | + | ||
| 20 | + <h3 class="timeline-header">Пользователь: <span class="text-green"><?= $model->user->username ?></span></h3> | ||
| 21 | + | ||
| 22 | + <div class="timeline-body"> | ||
| 23 | + <table class="table table-bordered table-striped"> | ||
| 24 | + <tr> | ||
| 25 | + <th>Поле</th> | ||
| 26 | + <th>Старое значение</th> | ||
| 27 | + <th>Новое значение</th> | ||
| 28 | + </tr> | ||
| 29 | + <?php | ||
| 30 | + foreach (Json::decode($model->data) as $key => $item) { | ||
| 31 | + echo Html::tag( | ||
| 32 | + 'tr', | ||
| 33 | + Html::tag('td', $key ) . Html::tag('td', $item[ 'old' ]) . Html::tag( | ||
| 34 | + 'td', | ||
| 35 | + $item[ 'new' ] | ||
| 36 | + ) | ||
| 37 | + ); | ||
| 38 | + } | ||
| 39 | + ?> | ||
| 40 | + </table> | ||
| 41 | + </div> | ||
| 42 | + | ||
| 43 | + </div> | ||
| 44 | +</li> |
views/order/log.php
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | /** | 7 | /** |
| 8 | * @var View $this | 8 | * @var View $this |
| 9 | * @var ActiveDataProvider $logData | 9 | * @var ActiveDataProvider $logData |
| 10 | + * @var ActiveDataProvider $productLogData | ||
| 10 | * @var Order $model | 11 | * @var Order $model |
| 11 | */ | 12 | */ |
| 12 | 13 | ||
| @@ -25,9 +26,15 @@ | @@ -25,9 +26,15 @@ | ||
| 25 | ?> | 26 | ?> |
| 26 | 27 | ||
| 27 | <div class="order-log"> | 28 | <div class="order-log"> |
| 29 | + | ||
| 28 | <div class="box box-default"> | 30 | <div class="box box-default"> |
| 29 | <div class="box-header with-border"> | 31 | <div class="box-header with-border"> |
| 30 | <h3 class="box-title">История</h3> | 32 | <h3 class="box-title">История</h3> |
| 33 | + <div class="box-tools pull-right"> | ||
| 34 | + <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> | ||
| 35 | + </button> | ||
| 36 | + </div> | ||
| 37 | + </div> | ||
| 31 | <div class="box-body"> | 38 | <div class="box-body"> |
| 32 | 39 | ||
| 33 | <?php | 40 | <?php |
| @@ -56,4 +63,42 @@ | @@ -56,4 +63,42 @@ | ||
| 56 | 63 | ||
| 57 | </div><!-- /.box-body --> | 64 | </div><!-- /.box-body --> |
| 58 | </div><!-- /.box --> | 65 | </div><!-- /.box --> |
| 59 | - </div> | 66 | + |
| 67 | + <div class="box box-default"> | ||
| 68 | + <div class="box-header with-border"> | ||
| 69 | + <h3 class="box-title">История</h3> | ||
| 70 | + <div class="box-tools pull-right"> | ||
| 71 | + <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> | ||
| 72 | + </button> | ||
| 73 | + </div> | ||
| 74 | + </div> | ||
| 75 | + <div class="box-body"> | ||
| 76 | + | ||
| 77 | + <?php | ||
| 78 | + echo ListView::widget( | ||
| 79 | + [ | ||
| 80 | + 'dataProvider' => $productLogData, | ||
| 81 | + 'layout' => '{items}', | ||
| 82 | + 'itemView' => '_log_product_item', | ||
| 83 | + 'itemOptions' => [ | ||
| 84 | + 'tag' => false, | ||
| 85 | + ], | ||
| 86 | + 'options' => [ | ||
| 87 | + 'tag' => $productLogData->totalCount == 0 ? 'div' : 'ul', | ||
| 88 | + 'class' => $productLogData->totalCount == 0 ? 'list-view' : 'list-view timeline', | ||
| 89 | + ], | ||
| 90 | + 'viewParams' => [ | ||
| 91 | + 'order' => $model, | ||
| 92 | + ], | ||
| 93 | + 'emptyText' => 'У этого заказа пока нет истории', | ||
| 94 | + 'emptyTextOptions' => [ | ||
| 95 | + 'class' => 'callout callout-info' | ||
| 96 | + ], | ||
| 97 | + ] | ||
| 98 | + ); | ||
| 99 | + ?> | ||
| 100 | + | ||
| 101 | + </div><!-- /.box-body --> | ||
| 102 | + </div><!-- /.box --> | ||
| 103 | + | ||
| 104 | +</div> |
views/order/update.php
| @@ -72,7 +72,7 @@ $.notify({ | @@ -72,7 +72,7 @@ $.notify({ | ||
| 72 | 72 | ||
| 73 | ?> | 73 | ?> |
| 74 | <div class="order-update"> | 74 | <div class="order-update"> |
| 75 | - <div class="container"> | 75 | + <div class="container callout callout-info"> |
| 76 | <h1><?php echo Html::encode($this->title) ?> | <?php echo date( | 76 | <h1><?php echo Html::encode($this->title) ?> | <?php echo date( |
| 77 | 'd-m-Y G:i', | 77 | 'd-m-Y G:i', |
| 78 | $model->created_at | 78 | $model->created_at |