DistrictAgentDivvy.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\plugins\scanCodePay\models\Order as ScanOrder;
  9. use yii\db\ActiveRecord;
  10. use yii\behaviors\TimestampBehavior;
  11. use Yii;
  12. /**
  13. * This is the model class for table "{{%district_agent_divvy}}".
  14. *
  15. * @property integer $id
  16. * @property integer $start_time
  17. * @property integer $end_time
  18. * @property double $all_district_agent_divvy
  19. * @property double $all_order_money
  20. * @property double $all_dividend_amount
  21. * @property integer $all_agent_num
  22. * @property integer $send_time
  23. * @property integer $is_scan
  24. * @property integer $is_send
  25. * @property integer $created_at
  26. * @property integer $updated_at
  27. */
  28. class DistrictAgentDivvy extends \yii\db\ActiveRecord
  29. {
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%district_agent_divvy}}';
  36. }
  37. public function behaviors()
  38. {
  39. return [
  40. [
  41. 'class' => TimestampBehavior::class,
  42. 'attributes' => [
  43. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  44. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  45. ]
  46. ]
  47. ];
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['send_time', 'start_time', 'end_time', 'all_agent_num','is_send','is_scan'], 'integer'],
  56. [['all_order_money', 'all_dividend_amount', 'all_district_agent_divvy'], 'number'],
  57. [['created_at', 'updated_at'], 'safe']
  58. ];
  59. }
  60. public static function poolSend($id,$change_time = false){
  61. //所有区县分红代理商
  62. $districtAgentDivvy = DistrictAgentDivvy::findOne($id);
  63. if ($change_time) $districtAgentDivvy->end_time = strtotime(date('Y-m-d H:i'))+60;
  64. $allDividendAmount = $districtAgentDivvy->all_dividend_amount;
  65. $allCommission = 0.00;
  66. $admin = Admin::find()->where(['type' => 'area_agent', 'is_delete' => 0, 'is_enable' => 1])->asArray()->all();
  67. $adminCount = count($admin);
  68. if ($adminCount > 0 && $allDividendAmount > 0) {
  69. $commissionMoney = bcdiv($allDividendAmount, $adminCount, 2);
  70. if ($commissionMoney <= 0) {
  71. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:【{$allDividendAmount}】所有区县分红代理商人数:【{$adminCount}】代理商平均分红 【{$commissionMoney}】小于0.01不分红"], "all_district_agent_divvy_controller.log");
  72. }
  73. foreach ($admin as $item) {
  74. if ($item['saas_user_id']) {
  75. $res = self::transferAddUserWallet($id, $item['saas_user_id'], $commissionMoney);
  76. if ($res) {
  77. $allCommission += $commissionMoney;
  78. }
  79. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$commissionMoney} 区县代理商ID:{$item['saas_user_id']}"], "all_district_agent_divvy_controller.log");
  80. } else {
  81. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$commissionMoney} 区县代理商未绑定 SaasId不分红"], "all_district_agent_divvy_controller.log");
  82. }
  83. }
  84. }
  85. //当分红金额小于总待分红金额,剩余金额发放给特殊联盟用户
  86. if ($allCommission < $allDividendAmount) {
  87. $saas_id = $setting['string_code_transfer_saas_id'] ?? 0;
  88. $remainingAmount = $allDividendAmount - $allCommission;
  89. if ($saas_id) {
  90. self::transferAddUserWallet($id, $saas_id, $remainingAmount, 1, true);
  91. //debug_log([__METHOD__, __LINE__, "所有区县代理商分红金额:{$allCommission} 剩余金额:{$remainingAmount} ,发放给特殊联盟用户:【{$saas_id}】"], "all_district_agent_divvy_controller.log");
  92. } else {
  93. //debug_log([__METHOD__, __LINE__, "所有区县代理商分红金额:{$allCommission} 剩余金额:{$remainingAmount} ,特殊联盟用户也不存在不发放"], "all_district_agent_divvy_controller.log");
  94. }
  95. }
  96. $districtAgentDivvy->all_agent_num = $adminCount; //总区县代理商人数
  97. $districtAgentDivvy->send_time = time();
  98. $districtAgentDivvy->is_send = 1;
  99. $districtAgentDivvy->save();
  100. //debug_log([__METHOD__, __LINE__, "【所有区县代理商分红】开始时间:【".date('Y-m-d H:i:s',$districtAgentDivvy->start_time)."】 结束时间:【".date('Y-m-d H:i:s',$districtAgentDivvy->end_time)."】总订单金额:【{$districtAgentDivvy->all_order_money}】 总待分金额:【{$allDividendAmount}】 总区县代理商人数:【{$adminCount}】"], "all_district_agent_divvy_controller.log");
  101. //debug_log([__METHOD__, __LINE__, "所有区县代理商分红-----end"], "all_district_agent_divvy_controller.log");
  102. }
  103. public static function transferAddUserWallet($divvyId, $saas_id, $money, $agentType = 0, $isSurplus = false, $type = UserStringCodeOrder::TYPE_STRING_CODE_STORE_ALL_DISTRICT_AGENT_DIVVY, $is_scan = 1)
  104. {
  105. if (!$saas_id) {
  106. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 区县代理商ID:{$saas_id} 不存在"], "all_district_agent_divvy_controller.log");
  107. return false;
  108. }
  109. if (bccomp($money, 0, 6) <= 0) {
  110. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 有误"], "all_district_agent_divvy_controller.log");
  111. return false;
  112. }
  113. if ($isSurplus){
  114. $title = UserStringCodeOrder::TYPE_LIST[$type].'剩余让利用户';
  115. }else{
  116. $title = UserStringCodePlus::MODEL_NAME_LIST[UserStringCodePlus::MODEL_ALLIANCE] . " " . UserStringCodeOrder::TYPE_LIST[$type];
  117. }
  118. UserStringCodeOrder::set(
  119. UserStringCodePlus::ALLIANCE_STORE_ID,
  120. 0,
  121. $saas_id,
  122. $is_scan,
  123. 0,
  124. $money,
  125. UserStringCodeOrder::STATUS_SUCCESS,
  126. $title,
  127. "",
  128. $type,
  129. UserStringCodePlus::MODEL_ALLIANCE
  130. );
  131. $log_type = $is_scan ? UserWallet::TYPE_SCAN : UserWallet::TYPE_ORDER;
  132. $log_desc = UserWallet::TYPE_NAME_LIST[$log_type] . UserStringCodeOrder::TYPE_LIST[$type] . " 发放【{$title}】红包";
  133. $source_table = $is_scan ? "\app\plugins\scanCodePay\models\Order" : "app\models\Order";
  134. $currency = Currency::findOne(['code' => Currency::CURRENCY_STRING_CODE]);
  135. UserWallet::addLog($currency, UserStringCodePlus::ALLIANCE_STORE_ID, $saas_id, $money, $log_desc, $log_type, $source_table,UserStringCodePlus::ALLIANCE_STORE_ID,$type);
  136. //记录所有区县分红代理商
  137. $allDistrictAgent = new AllDistrictAgent();
  138. $allDistrictAgent->divvy_id = $divvyId;
  139. $allDistrictAgent->saas_id = $saas_id;
  140. $allDistrictAgent->type = $agentType;
  141. $allDistrictAgent->dividend_amount = $money;
  142. $allDistrictAgent->send_time = time();
  143. $allDistrictAgent->save();
  144. //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 区县代理商ID:{$saas_id} 分红完成"], "all_district_agent_divvy_controller.log");
  145. return true;
  146. }
  147. }