UnitFounderPool.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\constants\OptionSetting;
  9. use app\plugins\scanCodePay\models\Order as ScanCodePayOrder;
  10. use app\plugins\scanCodePay\models\Order as ScanOrder;
  11. use yii\db\ActiveRecord;
  12. use yii\behaviors\TimestampBehavior;
  13. use Yii;
  14. use yii\helpers\Json;
  15. /**
  16. * This is the model class for table "{{%UnitFounder_pool}}".
  17. *
  18. * @property integer $id
  19. * @property integer $admin_id
  20. * @property integer $start_time
  21. * @property integer $end_time
  22. * @property float $money
  23. * @property float $rangli_rate
  24. * @property float $moxing_rate
  25. * @property double $dividend_amount
  26. * @property double $balance_amount
  27. * @property integer $is_send
  28. * @property integer $is_scan
  29. * @property integer $send_time
  30. * @property integer $created_at
  31. * @property integer $updated_at
  32. */
  33. class UnitFounderPool extends \yii\db\ActiveRecord
  34. {
  35. /**
  36. * @inheritdoc
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%unit_founder_pool}}';
  41. }
  42. public function behaviors()
  43. {
  44. return [
  45. [
  46. 'class' => TimestampBehavior::class,
  47. 'attributes' => [
  48. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  49. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  50. ]
  51. ]
  52. ];
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function rules()
  58. {
  59. return [
  60. [['is_send', 'send_time', 'start_time', 'end_time', 'is_scan', 'admin_id'], 'integer'],
  61. [['money', 'balance_amount', 'dividend_amount', 'rangli_rate', 'moxing_rate'], 'number'],
  62. [['created_at', 'updated_at'], 'safe']
  63. ];
  64. }
  65. /**
  66. * 增加合伙人分红池
  67. * @param $order
  68. * @param $money
  69. * @param string $desc
  70. * @return false|void
  71. */
  72. public static function poolPush($order, $money, $desc = '', $pay_price = 0, $rl_rate = 0, $mx_rate = 0, $model = 0, $add_money = 0)
  73. {
  74. $salesman_id = Store::find()->where(['id' => $order->store_id])->select(['salesman_id'])->asArray()->scalar() ?: null;
  75. $salesman = Salesman::findOne(['id' => $salesman_id, 'is_delete' => 0, 'status' => 1]);
  76. if (empty($salesman) || !$salesman->admin_id) return false;
  77. $admin = Admin::findOne(['id' => $salesman->admin_id, 'is_delete' => 0]);
  78. if (empty($admin)) return false;
  79. $is_scan = $order instanceof ScanOrder ? 1 : 0;
  80. //debug_log([__METHOD__, __LINE__, "执行poolpush"], "app_debug.log");
  81. $pool = self::find()->where([
  82. 'and',
  83. [
  84. '<', 'start_time', time()
  85. ],
  86. [
  87. '>=', 'end_time', time()
  88. ],
  89. [
  90. '=', 'is_scan', $is_scan
  91. ],
  92. [
  93. '=', 'is_send', 0
  94. ],
  95. [
  96. '=', 'admin_id', $admin->id
  97. ]
  98. ])->orderBy(['id' => SORT_DESC])->one();
  99. //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池1 -------------------"], "app_debug.log");
  100. // 如果没有奖金池,重新创建奖金池
  101. if (!$pool) {
  102. $pool = new self();
  103. $pool->start_time = strtotime(date('Y-m-d', time()));
  104. $end_time = $pool->start_time + (24 * 3600);
  105. $pool->end_time = $end_time;
  106. $pool->is_send = 0;
  107. $pool->send_time = 0;
  108. $pool->is_scan = $is_scan;
  109. $pool->admin_id = $admin->id;
  110. }
  111. $pool->moxing_rate = $mx_rate;
  112. $pool->rangli_rate = $rl_rate;
  113. $before_money = $pool->money ? $pool->money : 0;
  114. $pool->money += $money;
  115. if (!$pool->save()) {
  116. //debug_log([__METHOD__, __LINE__, "---------------- 合伙人奖金池更新失败 -------------------" . json_encode($pool->errors)], "app_debug.log");
  117. return false;
  118. }
  119. $pool_log = new UnitFounderPoolDetail();
  120. $pool_log->order_id = $order['id'];
  121. $pool_log->admin_id = $admin->id;
  122. $pool_log->order_no = $order->order_no;
  123. $pool_log->money = $money;
  124. $pool_log->pay_price = $is_scan ? $pay_price : $order->pay_price;
  125. $pool_log->store_id = $order->store_id;
  126. $pool_log->before_money = $before_money;
  127. $pool_log->after_money = $pool->money;
  128. $pool_log->total_pv = $add_money;
  129. $pool_log->rl_rate = $rl_rate;
  130. $pool_log->mx_rate = $mx_rate;
  131. $pool_log->money = $money;
  132. $pool_log->total_price = $order->total_price;
  133. $pool_log->is_scan = $is_scan;
  134. $pool_log->pool_id = $pool->id;
  135. $pool_log->model_type = $model;
  136. $pool_log->desc = $desc ?: '订单号:' . $order->order_no . '追加';
  137. if (!$pool_log->save()) {
  138. //debug_log([__METHOD__, __LINE__, "---------------- 奖金池记录保存失败 -------------------" . json_encode($pool_log->errors)], "app_debug.log");
  139. return false;
  140. }
  141. if ($is_scan) {
  142. switch ($model) {
  143. case 0:
  144. ScanCodePayOrder::updateAll(['small_model_pv_bonus' => $money], ['id' => $order['id']]);
  145. break;
  146. case 1:
  147. ScanCodePayOrder::updateAll(['big_model_pv_bonus' => $money], ['id' => $order['id']]);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池结束 -------------------"], "app_debug.log");
  154. }
  155. }