OrderPeriodAdjustForm.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\helpers\Date;
  5. use common\components\Model;
  6. use common\helpers\LoggerTool;
  7. use common\libs\logging\operate\AdminOperate;
  8. use common\models\DecOrder;
  9. use common\models\FlowExchangePoints;
  10. use common\models\FlowGaragePoints;
  11. use common\models\FlowReconsumePoints;
  12. use common\models\FlowTourismPoints;
  13. use common\models\FlowVillaPoints;
  14. use common\models\FlowWallet;
  15. use common\models\Order;
  16. use common\models\OrderPeriodAdjust;
  17. use common\models\Period;
  18. use common\models\User;
  19. use common\models\UserInfo;
  20. /**
  21. * OrderPeriodAdjustForm form
  22. */
  23. class OrderPeriodAdjustForm extends Model
  24. {
  25. public $orderSn;
  26. public $modernPeriod;
  27. public $order;
  28. public function init() {
  29. parent::init();
  30. $this->adminOperateLogger = new AdminOperate([
  31. 'fetchClass' => OrderPeriodAdjust::class,
  32. ]);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['orderSn', 'modernPeriod'], 'trim'],
  41. [['orderSn', 'modernPeriod'], 'required'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'orderSn' => '订单号',
  48. 'modernPeriod' => '调整后期数',
  49. ];
  50. }
  51. /**
  52. * 指定校验场景
  53. * @return array
  54. */
  55. public function scenarios()
  56. {
  57. $parentScenarios = parent::scenarios();
  58. $customScenarios = [
  59. 'orderPeriodAdjust' => ['orderSn', 'modernPeriod'],
  60. ];
  61. return array_merge($parentScenarios, $customScenarios);
  62. }
  63. /**
  64. * 校验之前
  65. * @return bool
  66. */
  67. public function beforeValidate()
  68. {
  69. $parentValidate = parent::beforeValidate();
  70. // 校验订单
  71. $this->order = Order::findOne(['SN' => $this->orderSn, 'IS_DELETE' => 0]);
  72. if (!$this->order) {
  73. $this->addError('orderSn', '订单不存在');
  74. return false;
  75. }
  76. // 目前期数:已封期、未挂网可调整
  77. $period = Period::getInfoByPeriodNum($this->modernPeriod);
  78. if (!$period) {
  79. $this->addError('modernPeriod', '期数不存在');
  80. return false;
  81. }
  82. if (!((+$period['IS_CLOSED'] == 1) && (+$period['IS_SENT'] == 0))) {
  83. $this->addError('orderSn', '调整后的期数必须是已封期且未挂网');
  84. return false;
  85. }
  86. return $parentValidate;
  87. }
  88. /**
  89. * @return bool|null
  90. */
  91. public function periodAdjust()
  92. {
  93. if (!$this->validate()) {
  94. return null;
  95. }
  96. $db = \Yii::$app->db;
  97. $transaction = $db->beginTransaction();
  98. try {
  99. // 调整订单期数
  100. Order::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'SN = :SN', [':SN' => $this->orderSn]);
  101. if ($this->order->ORDER_TYPE === 'ZC') {
  102. // 调整报单期数
  103. DecOrder::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  104. // 调整会员的期数
  105. User::updateAll(['PERIOD_AT' => $this->modernPeriod], 'ID = :USER_ID', ['USER_ID' => $this->order->USER_ID]);
  106. UserInfo::updateAll(['HIGHEST_EMP_LV_PERIOD' => $this->modernPeriod], 'USER_ID = :USER_ID', ['USER_ID' => $this->order->USER_ID]);
  107. }
  108. // 调整流水的期数
  109. switch ($this->order->PAY_TYPE){
  110. case 'cash':
  111. FlowWallet::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  112. break;
  113. case 'exchange':
  114. FlowExchangePoints::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  115. break;
  116. case 'tourism_points':
  117. FlowTourismPoints::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  118. break;
  119. case 'garage_points':
  120. FlowGaragePoints::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  121. break;
  122. case 'villa_points':
  123. FlowVillaPoints::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  124. break;
  125. case 'reconsume_points':
  126. FlowReconsumePoints::updateAll(['PERIOD_NUM' => $this->modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $this->orderSn]);
  127. break;
  128. default:
  129. break;
  130. }
  131. // 写入调整记录
  132. $orderModel = new OrderPeriodAdjust();
  133. $orderModel->ORDER_SN = $this->orderSn;
  134. $orderModel->DEC_SN = $this->order->DEC_SN ?? '';
  135. $orderModel->ORIGIN_PERIOD = $this->order->PERIOD_NUM;
  136. $orderModel->MODERN_PERIOD = $this->modernPeriod;
  137. $orderModel->ADMIN_ID = Admin::getAdminNameById(\Yii::$app->user->id);
  138. $orderModel->CREATED_AT = Date::nowTime();
  139. if (!$orderModel->save()) {
  140. $this->addErrors($orderModel->getErrors());
  141. return false;
  142. }
  143. $transaction->commit();
  144. } catch(\Exception $e) {
  145. $transaction->rollBack();
  146. $this->addError('periodAdjust', $e->getMessage());
  147. return null;
  148. }
  149. return true;
  150. }
  151. }