From 17569d93090b5f11fb814197579a1c8e9a9ae80b Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 26 Jan 2017 18:50:28 +0200 Subject: [PATCH] -Order product log almost ready -update form prettifying --- components/LoggerInterface.php | 9 +++++++++ components/OrderLogger.php | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ components/OrderProductLogger.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ controllers/OrderController.php | 46 ++++++++++++++++++++++++++-------------------- models/Order.php | 116 ++++++++++++-------------------------------------------------------------------------------------------------------- models/OrderLog.php | 7 ------- models/OrderProduct.php | 15 ++++++++++++--- models/OrderProductLog.php | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/order/_form.php | 46 +++++++++++++++++++++++++++++++++++++++++++--- views/order/_log_product_item.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ views/order/log.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++- views/order/update.php | 2 +- 12 files changed, 507 insertions(+), 139 deletions(-) create mode 100644 components/LoggerInterface.php create mode 100644 components/OrderLogger.php create mode 100644 components/OrderProductLogger.php create mode 100644 models/OrderProductLog.php create mode 100644 views/order/_log_product_item.php diff --git a/components/LoggerInterface.php b/components/LoggerInterface.php new file mode 100644 index 0000000..ce8b123 --- /dev/null +++ b/components/LoggerInterface.php @@ -0,0 +1,9 @@ + $attribute) { + if ($oldAttributes[ $key ] != $attribute && $key != 'updated_at') { + $data[ $key ] = self::getOrderLogAttributes( + $key, + [ + 'old' => $attribute, + 'new' => $oldAttributes[ $key ], + ] + ); + } + } + + return $data; + } + + /** + * @param array $data + * @param int $identityId + * @param array $params + */ + public static function saveData(array $data, int $identityId, $params = []) + { + if (!empty($data) && empty($data[ 'edit_time' ])) { + $log = new OrderLog(); + $log->order_id = (integer) $identityId; + $log->user_id = (integer) \Yii::$app->user->identity->id; + $log->data = Json::encode($data); + + $log->save(); + } + } + + /** + * @param array $attributes + * @param $label + * @param int $id + */ + public static function saveOrderLabelHistory(array $attributes, $label, int $id) + { + if (!empty($attributes[ 'label' ])) { + if ($label != (string) $attributes[ 'label' ]) { + $history = new OrderLabelHistory(); + + $history->label_id = (integer) $label; + $history->order_id = (integer) $id; + $history->user_id = (integer) \Yii::$app->user->identity->id; + + if ($history->save()) { + \Yii::$app->session->setFlash('label_update', 'Статус заказа обновлен'); + } + } + } + } + + /** + * @param string $attr + * @param array $values + * + * @return array + * Return array in form ['old'=>'old value ...', 'new' => 'new value ...'] + */ + protected static function getOrderLogAttributes(string $attr, array $values) + { + if ($attr == 'deadline') { + return [ + 'old' => empty($values[ 'old' ]) ? '' : date('d.m.Y', $values[ 'old' ]), + 'new' => empty($values[ 'new' ]) ? '' : date('d.m.Y', $values[ 'new' ]), + ]; + } elseif ($attr == 'reason') { + return [ + 'old' => empty($values[ 'old' ]) ? '' : Order::REASONS[ $values[ 'old' ] ], + 'new' => empty($values[ 'new' ]) ? '' : Order::REASONS[ $values[ 'new' ] ], + ]; + } elseif ($attr == 'label') { + $labels = Label::find() + ->with('lang') + ->indexBy('id') + ->all(); + return [ + 'old' => empty($values[ 'old' ]) ? '' : $labels[ $values[ 'old' ] ]->lang->title, + 'new' => empty($values[ 'new' ]) ? '' : $labels[ $values[ 'new' ] ]->lang->title, + ]; + } elseif ($attr == 'delivery') { + $deliveries = Delivery::find() + ->with('lang') + ->indexBy('id') + ->all(); + return [ + 'old' => empty($values[ 'old' ]) ? '' : $deliveries[ $values[ 'old' ] ]->lang->title, + 'new' => empty($values[ 'new' ]) ? '' : $deliveries[ $values[ 'new' ] ]->lang->title, + ]; + } elseif ($attr == 'manager_id') { + $users = User::find() + ->indexBy('id') + ->all(); + return [ + 'old' => empty($values[ 'old' ]) ? '' : $users[ $values[ 'old' ] ]->username, + 'new' => empty($values[ 'new' ]) ? '' : $users[ $values[ 'new' ] ]->username, + ]; + } elseif ($attr == 'payment') { + $payment = OrderPayment::find() + ->with('lang') + ->indexBy('id') + ->all(); + return [ + 'old' => empty($values[ 'old' ]) ? '' : $payment[ $values[ 'old' ] ]->lang->title, + 'new' => empty($values[ 'new' ]) ? '' : $payment[ $values[ 'new' ] ]->lang->title, + ]; + } elseif ($attr == 'shipping_by') { + return [ + 'old' => empty($values[ 'old' ]) ? '' : Order::SHIPPING_BY[ $values[ 'old' ] ][ 'label' ], + 'new' => empty($values[ 'new' ]) ? '' : Order::SHIPPING_BY[ $values[ 'new' ] ][ 'label' ], + ]; + } elseif ($attr == 'pay') { + return [ + 'old' => ( $values[ 'old' ] == true ) ? 'Оплачен' : 'Не оплачен', + 'new' => ( $values[ 'new' ] == true ) ? 'Оплачен' : 'Не оплачен', + ]; + } else { + return $values; + } + } + } \ No newline at end of file diff --git a/components/OrderProductLogger.php b/components/OrderProductLogger.php new file mode 100644 index 0000000..c9dd58a --- /dev/null +++ b/components/OrderProductLogger.php @@ -0,0 +1,49 @@ + $attribute) { + if ($oldAttributes[ $key ] != $attribute) { + $data[ $key ] = [ + 'old' => $attribute, + 'new' => $oldAttributes[ $key ], + ]; + } + } + + return $data; + } + + /** + * @param array $data + * @param int $identityId + * @param array $params + */ + public static function saveData(array $data, int $identityId, $params = []) + { + if (!empty($data)) { + $log = new OrderProductLog(); + $log->order_product_id = (integer) $identityId; + $log->order_id = $params['order_id']; + $log->user_id = (integer) \Yii::$app->user->identity->id; + $log->data = Json::encode($data); + + $log->save(); + } + } + } \ No newline at end of file diff --git a/controllers/OrderController.php b/controllers/OrderController.php index d8956e1..2b2a1d5 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -109,14 +109,14 @@ public function actionView($id) { $model = $this->findModel($id); - + $historyData = new ActiveDataProvider( [ 'query' => $model->getLabelsHistory() ->with('order', 'label', 'user'), ] ); - + $dataProvider = new ActiveDataProvider( [ 'query' => $model->getProducts(), @@ -142,22 +142,29 @@ public function actionLog($id) { $model = Order::findOne($id); - + $logData = new ActiveDataProvider( [ 'query' => $model->getLogs(), ] ); - + + $productLogData = new ActiveDataProvider( + [ + 'query' => $model->getProductLogs(), + ] + ); + return $this->render( 'log', [ - 'model' => $model, - 'logData' => $logData, + 'model' => $model, + 'logData' => $logData, + 'productLogData' => $productLogData, ] ); } - + public function actionDelete($id) { if (\Yii::$app->user->identity->isAdmin()) { @@ -354,7 +361,7 @@ } $model = $this->findModel($id); - + if ($model->payment == 10) { $model->validators->append( new NumberValidator( @@ -366,7 +373,7 @@ ) ); } - + /** * @var User $user */ @@ -394,14 +401,14 @@ $headers->set('Access-Control-Allow-Origin', '*'); if ($model->load(Yii::$app->request->post()) && $model->save()) { - + if ($model->published != true) { $model->published = true; $model->save(); /** * @var SmsSender $sender */ - + $sender = \Yii::$app->sender; $sender->send( $model->phone, @@ -413,8 +420,7 @@ ) ); } - - + $this->unblockOrder($model->id); return $this->render( 'update', @@ -574,7 +580,7 @@ $model->save(); } - public function actionPublishOrder($id,$phone) + public function actionPublishOrder($id, $phone) { $model = Order::findOne($id); if ($model->published == true) { @@ -582,23 +588,23 @@ } $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 */ $sender = \Yii::$app->sender; - if(!empty($phone)){ + if (!empty($phone)) { $sender->send( $phone, $this->renderPartial( @@ -609,6 +615,6 @@ ) ); } - + } } diff --git a/models/Order.php b/models/Order.php index 9988607..1b80ef6 100755 --- a/models/Order.php +++ b/models/Order.php @@ -1,6 +1,7 @@ 'old value ...', 'new' => 'new value ...'] - */ - public function getLogAttributes(string $attr, array $values) - { - if ($attr == 'deadline') { - return [ - 'old' => empty($values[ 'old' ]) ? '' : date('d.m.Y', $values[ 'old' ]), - 'new' => empty($values[ 'new' ]) ? '' : date('d.m.Y', $values[ 'new' ]), - ]; - } elseif ($attr == 'reason') { - return [ - 'old' => empty($values[ 'old' ]) ? '' : self::REASONS[ $values[ 'old' ] ], - 'new' => empty($values[ 'new' ]) ? '' : self::REASONS[ $values[ 'new' ] ], - ]; - } elseif ($attr == 'label') { - $labels = Label::find() - ->with('lang') - ->indexBy('id') - ->all(); - return [ - 'old' => empty($values[ 'old' ]) ? '' : $labels[ $values[ 'old' ] ]->lang->title, - 'new' => empty($values[ 'new' ]) ? '' : $labels[ $values[ 'new' ] ]->lang->title, - ]; - } elseif ($attr == 'delivery') { - $deliveries = Delivery::find() - ->with('lang') - ->indexBy('id') - ->all(); - return [ - 'old' => empty($values[ 'old' ]) ? '' : $deliveries[ $values[ 'old' ] ]->lang->title, - 'new' => empty($values[ 'new' ]) ? '' : $deliveries[ $values[ 'new' ] ]->lang->title, - ]; - } elseif ($attr == 'manager_id') { - $users = User::find() - ->indexBy('id') - ->all(); - return [ - 'old' => empty($values[ 'old' ]) ? '' : $users[ $values[ 'old' ] ]->username, - 'new' => empty($values[ 'new' ]) ? '' : $users[ $values[ 'new' ] ]->username, - ]; - } elseif ($attr == 'payment') { - $payment = OrderPayment::find() - ->with('lang') - ->indexBy('id') - ->all(); - return [ - 'old' => empty($values[ 'old' ]) ? '' : $payment[ $values[ 'old' ] ]->lang->title, - 'new' => empty($values[ 'new' ]) ? '' : $payment[ $values[ 'new' ] ]->lang->title, - ]; - } elseif ($attr == 'shipping_by') { - return [ - 'old' => empty($values[ 'old' ]) ? '' : self::SHIPPING_BY[ $values[ 'old' ] ][ 'label' ], - 'new' => empty($values[ 'new' ]) ? '' : self::SHIPPING_BY[ $values[ 'new' ] ][ 'label' ], - ]; - } elseif ($attr == 'pay') { - return [ - 'old' => ( $values[ 'old' ] == true ) ? 'Оплачен' : 'Не оплачен', - 'new' => ( $values[ 'new' ] == true ) ? 'Оплачен' : 'Не оплачен', - ]; - } else { - return $values; - } - } - - /** * @inheritdoc */ public function behaviors() @@ -314,41 +244,11 @@ public function afterSave($insert, $changedAttributes) { - $data = []; - foreach ($changedAttributes as $key => $attribute) { - if ($this->oldAttributes[ $key ] != $attribute && $key != 'updated_at') { - $data[ $key ] = $this->getLogAttributes( - $key, - [ - 'old' => $attribute, - 'new' => $this->oldAttributes[ $key ], - ] - ); - } - } + $data = OrderLogger::generateData($changedAttributes, $this->oldAttributes, $insert); + OrderLogger::saveData($data, $this->id); - if (!empty($data) && empty($data[ 'edit_time' ])) { - $log = new OrderLog(); - $log->order_id = (integer) $this->id; - $log->user_id = (integer) \Yii::$app->user->identity->id; - $log->data = Json::encode($data); - - $log->save(); - } + OrderLogger::saveOrderLabelHistory($changedAttributes, $this->label, $this->id); - 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); } @@ -504,6 +404,14 @@ { return $this->hasMany(OrderLog::className(), [ 'order_id' => 'id' ]); } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProductLogs() + { + return $this->hasMany(OrderProductLog::className(), [ 'order_id' => 'id' ]); + } /** * If deadline is fucked up returns true, diff --git a/models/OrderLog.php b/models/OrderLog.php index cc083f6..65ac995 100644 --- a/models/OrderLog.php +++ b/models/OrderLog.php @@ -66,13 +66,6 @@ 'targetClass' => Order::className(), 'targetAttribute' => [ 'order_id' => 'id' ], ], - // [ - // [ 'user_id' ], - // 'exist', - // 'skipOnError' => true, - // 'targetClass' => User::className(), - // 'targetAttribute' => [ 'user_id' => 'id' ], - // ], ]; } diff --git a/models/OrderProduct.php b/models/OrderProduct.php index 2d890a4..8bd3dfa 100755 --- a/models/OrderProduct.php +++ b/models/OrderProduct.php @@ -2,6 +2,7 @@ namespace artweb\artbox\ecommerce\models; + use artweb\artbox\ecommerce\components\OrderProductLogger; use Yii; use yii\db\ActiveRecord; @@ -33,11 +34,19 @@ return 'order_product'; } + public function afterSave($insert, $changedAttributes) + { + $data = OrderProductLogger::generateData($changedAttributes, $this->oldAttributes, $insert); + OrderProductLogger::saveData($data, $this->id, [ 'order_id' => $this->order_id ]); + + parent::afterSave($insert, $changedAttributes); + } + public function beforeSave($insert) { $this->price = $this->productVariant->price; $this->sum_cost = $this->price * $this->count; - return parent::beforeSave($insert); // TODO: Change the autogenerated stub + return parent::beforeSave($insert); } public function rules() @@ -82,8 +91,8 @@ 'product_name' => Yii::t('app', 'Наименование'), 'name' => Yii::t('app', 'op_name'), 'art' => Yii::t('app', 'art'), - 'cost' => Yii::t('app', 'cost'), - 'count' => Yii::t('app', 'count'), + 'cost' => Yii::t('app', 'Сумма'), + 'count' => Yii::t('app', 'Количество'), 'sum_cost' => Yii::t('app', 'Сумма'), 'status' => Yii::t('app', 'Статус'), 'booking' => Yii::t('app', 'Бронь'), diff --git a/models/OrderProductLog.php b/models/OrderProductLog.php new file mode 100644 index 0000000..c434a03 --- /dev/null +++ b/models/OrderProductLog.php @@ -0,0 +1,117 @@ + TimestampBehavior::className(), + 'updatedAtAttribute' => false, + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'order_product_id', + 'created_at', + 'user_id', + 'order_id', + ], + 'integer', + ], + [ + [ 'data' ], + 'string', + ], + [ + [ 'order_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Order::className(), + 'targetAttribute' => [ 'order_id' => 'id' ], + ], + [ + [ 'order_product_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => OrderProduct::className(), + 'targetAttribute' => [ 'order_product_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'order_product_id' => Yii::t('app', 'Order Product ID'), + 'created_at' => Yii::t('app', 'Created At'), + 'user_id' => Yii::t('app', 'User ID'), + 'order_id' => Yii::t('app', 'Order ID'), + 'data' => Yii::t('app', 'Data'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getOrder() + { + return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getOrderProduct() + { + return $this->hasOne(OrderProduct::className(), [ 'id' => 'order_product_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); + } + } diff --git a/views/order/_form.php b/views/order/_form.php index 2d99f30..181d2ad 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -149,9 +149,25 @@ JS; 'id' => 'main-form', ] ); ?> + + +
+
+

История

+
+ +
+
+
+ +

+ + +
@@ -363,12 +379,28 @@ JS;
+ + +
+
+
+ -
-
+ + +
+
+

История

+
+ +
+
+
+
+ + +
+ +
+
+ +
+
isNewRecord ? \Yii::t('app', 'Создать') : \Yii::t('app', 'Сохранить'), @@ -708,7 +749,6 @@ JS; ] ) ?>
-


diff --git a/views/order/_log_product_item.php b/views/order/_log_product_item.php new file mode 100644 index 0000000..64720b3 --- /dev/null +++ b/views/order/_log_product_item.php @@ -0,0 +1,44 @@ + + + +
  • + + +
    + formatter->asDatetime($model->created_at) ?> + +

    Пользователь: user->username ?>

    + +
    + + + + + + + data) as $key => $item) { + echo Html::tag( + 'tr', + Html::tag('td', $key ) . Html::tag('td', $item[ 'old' ]) . Html::tag( + 'td', + $item[ 'new' ] + ) + ); + } + ?> +
    ПолеСтарое значениеНовое значение
    +
    + +
    +
  • diff --git a/views/order/log.php b/views/order/log.php index f4c3a37..6bbef0f 100755 --- a/views/order/log.php +++ b/views/order/log.php @@ -7,6 +7,7 @@ /** * @var View $this * @var ActiveDataProvider $logData + * @var ActiveDataProvider $productLogData * @var Order $model */ @@ -25,9 +26,15 @@ ?>
    +

    История

    +
    + +
    +
    -
    + +
    +
    +

    История

    +
    + +
    +
    +
    + + $productLogData, + 'layout' => '{items}', + 'itemView' => '_log_product_item', + 'itemOptions' => [ + 'tag' => false, + ], + 'options' => [ + 'tag' => $productLogData->totalCount == 0 ? 'div' : 'ul', + 'class' => $productLogData->totalCount == 0 ? 'list-view' : 'list-view timeline', + ], + 'viewParams' => [ + 'order' => $model, + ], + 'emptyText' => 'У этого заказа пока нет истории', + 'emptyTextOptions' => [ + 'class' => 'callout callout-info' + ], + ] + ); + ?> + +
    +
    + +
    diff --git a/views/order/update.php b/views/order/update.php index 6a4f2f6..5197cde 100755 --- a/views/order/update.php +++ b/views/order/update.php @@ -72,7 +72,7 @@ $.notify({ ?>
    -
    +

    title) ?> | created_at -- libgit2 0.21.4