AllianceCouponSettle.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\modules\admin\models\HuifuForm;
  9. use app\utils\OrderNo;
  10. use yii\db\Exception;
  11. /**
  12. * This is the model class for table "{{%alliance_coupon_settle}}".
  13. *
  14. * @property integer $id
  15. * @property integer $store_id
  16. * @property string $price
  17. * @property string $settle_no
  18. * @property integer $settle_type
  19. * @property integer $is_settle
  20. * @property integer $unusually_condition
  21. * @property string $unusually_msg
  22. * @property integer $is_delete
  23. * @property string $settled_at
  24. * @property string $created_at
  25. * @property string $updated_at
  26. */
  27. class AllianceCouponSettle extends \yii\db\ActiveRecord
  28. {
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%alliance_coupon_settle}}';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['settle_type', 'store_id', 'unusually_condition', 'is_settle', 'is_delete'], 'integer'],
  43. [['price'], 'number'],
  44. [['updated_at', 'created_at', 'settled_at', 'settle_no', 'unusually_msg'], 'string'],
  45. ];
  46. }
  47. public function beforeSave($insert)
  48. {
  49. if (parent::beforeSave($insert)) {
  50. if ($this->isNewRecord) {
  51. $this->created_at = date('Y-m-d H:i:s');
  52. } else {
  53. $this->updated_at = date('Y-m-d H:i:s');
  54. }
  55. return true;
  56. }
  57. return false;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. ];
  67. }
  68. public static function addAllianceCouponSettleLog($order, $order_type)
  69. {
  70. if (!in_array($order_type, [1, 2])) return false;
  71. $store = Store::findOne($order->store_id);
  72. if ($order->take_price <= 0 || $store->small_model_proportion >= 100) return false;
  73. $settle_type = Option::get('alliance_coupon_settle_type', $order->store_id, 'store', 3)['value'];
  74. $settle = null;
  75. if ($settle_type != 0 && $settle_type != 1) {
  76. $settle = AllianceCouponSettle::find()->where(['store_id' => $order->store_id, 'is_settle' => 0, 'is_delete' => 0])->one();
  77. }
  78. if (!$settle) {
  79. $settle = new AllianceCouponSettle();
  80. $settle->store_id = $order->store_id;
  81. $settle->settle_no = OrderNo::getOrderNo(OrderNo::ALLIANCE_COUPON_SETTLE);
  82. $settle->price = 0;
  83. }
  84. $price = $order->take_price * ($store->small_model_proportion / 100);
  85. $add_price = $order->take_price - $price;
  86. $settle->price += $add_price;
  87. if (!$settle->save()) {
  88. ActionLog::addLog(1, 'addAllianceCouponSettleLog', $settle->getErrorSummary(false)[0]);
  89. }
  90. $detail = new AllianceCouponSettleDetail();
  91. $detail->order_id = $order->id;
  92. $detail->store_id = $order->store_id;
  93. $detail->price = $add_price;
  94. $detail->take_price = $order->take_price;
  95. $detail->rl_rate = $store->small_model_proportion;
  96. $detail->order_type = $order_type;
  97. $detail->settle_id = $settle->id;
  98. if (!$detail->save()) {
  99. ActionLog::addLog(1, 'addAllianceCouponSettleLog', $detail->getErrorSummary(false)[0]);
  100. }
  101. if ($settle_type == 1 && $detail->take_price > 0) {
  102. $form = new HuifuForm();
  103. $res = $form->actionHuifuyunpan($settle->id);
  104. switch ($res['code']) {
  105. case 0:
  106. $settle->settled_at = date('Y-m-d H:i:s');
  107. $settle->is_settle = 1;
  108. break;
  109. case 1:
  110. $settle->unusually_condition = 1;
  111. $settle->unusually_msg = $res['msg'];
  112. break;
  113. case 3:
  114. $settle->unusually_condition = 1;
  115. $settle->settle_type = 3;
  116. $settle->unusually_msg = $res['msg'];
  117. break;
  118. default:
  119. $settle->unusually_condition = 1;
  120. $settle->settle_type = 3;
  121. $settle->unusually_msg = '打款状态不明,请检查是否打款成功';
  122. break;
  123. }
  124. if (!$settle->save()) {
  125. ActionLog::addLog(1, 'addAllianceCouponSettleLog', $settle->getErrorSummary(false)[0]);
  126. }
  127. }
  128. return true;
  129. }
  130. }