| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\plugins\scanCodePay\models\Order as ScanOrder;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use Yii;
- /**
- * This is the model class for table "{{%district_agent_divvy}}".
- *
- * @property integer $id
- * @property integer $start_time
- * @property integer $end_time
- * @property double $all_district_agent_divvy
- * @property double $all_order_money
- * @property double $all_dividend_amount
- * @property integer $all_agent_num
- * @property integer $send_time
- * @property integer $is_scan
- * @property integer $is_send
- * @property integer $created_at
- * @property integer $updated_at
- */
- class DistrictAgentDivvy extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%district_agent_divvy}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['send_time', 'start_time', 'end_time', 'all_agent_num','is_send','is_scan'], 'integer'],
- [['all_order_money', 'all_dividend_amount', 'all_district_agent_divvy'], 'number'],
- [['created_at', 'updated_at'], 'safe']
- ];
- }
- public static function poolSend($id,$change_time = false){
- //所有区县分红代理商
- $districtAgentDivvy = DistrictAgentDivvy::findOne($id);
- if ($change_time) $districtAgentDivvy->end_time = strtotime(date('Y-m-d H:i'))+60;
- $allDividendAmount = $districtAgentDivvy->all_dividend_amount;
- $allCommission = 0.00;
- $admin = Admin::find()->where(['type' => 'area_agent', 'is_delete' => 0, 'is_enable' => 1])->asArray()->all();
- $adminCount = count($admin);
- if ($adminCount > 0 && $allDividendAmount > 0) {
- $commissionMoney = bcdiv($allDividendAmount, $adminCount, 2);
- if ($commissionMoney <= 0) {
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:【{$allDividendAmount}】所有区县分红代理商人数:【{$adminCount}】代理商平均分红 【{$commissionMoney}】小于0.01不分红"], "all_district_agent_divvy_controller.log");
- }
- foreach ($admin as $item) {
- if ($item['saas_user_id']) {
- $res = self::transferAddUserWallet($id, $item['saas_user_id'], $commissionMoney);
- if ($res) {
- $allCommission += $commissionMoney;
- }
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$commissionMoney} 区县代理商ID:{$item['saas_user_id']}"], "all_district_agent_divvy_controller.log");
- } else {
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$commissionMoney} 区县代理商未绑定 SaasId不分红"], "all_district_agent_divvy_controller.log");
- }
- }
- }
- //当分红金额小于总待分红金额,剩余金额发放给特殊联盟用户
- if ($allCommission < $allDividendAmount) {
- $saas_id = $setting['string_code_transfer_saas_id'] ?? 0;
- $remainingAmount = $allDividendAmount - $allCommission;
- if ($saas_id) {
- self::transferAddUserWallet($id, $saas_id, $remainingAmount, 1, true);
- //debug_log([__METHOD__, __LINE__, "所有区县代理商分红金额:{$allCommission} 剩余金额:{$remainingAmount} ,发放给特殊联盟用户:【{$saas_id}】"], "all_district_agent_divvy_controller.log");
- } else {
- //debug_log([__METHOD__, __LINE__, "所有区县代理商分红金额:{$allCommission} 剩余金额:{$remainingAmount} ,特殊联盟用户也不存在不发放"], "all_district_agent_divvy_controller.log");
- }
- }
- $districtAgentDivvy->all_agent_num = $adminCount; //总区县代理商人数
- $districtAgentDivvy->send_time = time();
- $districtAgentDivvy->is_send = 1;
- $districtAgentDivvy->save();
- //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");
- //debug_log([__METHOD__, __LINE__, "所有区县代理商分红-----end"], "all_district_agent_divvy_controller.log");
- }
- 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)
- {
- if (!$saas_id) {
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 区县代理商ID:{$saas_id} 不存在"], "all_district_agent_divvy_controller.log");
- return false;
- }
- if (bccomp($money, 0, 6) <= 0) {
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 有误"], "all_district_agent_divvy_controller.log");
- return false;
- }
- if ($isSurplus){
- $title = UserStringCodeOrder::TYPE_LIST[$type].'剩余让利用户';
- }else{
- $title = UserStringCodePlus::MODEL_NAME_LIST[UserStringCodePlus::MODEL_ALLIANCE] . " " . UserStringCodeOrder::TYPE_LIST[$type];
- }
- UserStringCodeOrder::set(
- UserStringCodePlus::ALLIANCE_STORE_ID,
- 0,
- $saas_id,
- $is_scan,
- 0,
- $money,
- UserStringCodeOrder::STATUS_SUCCESS,
- $title,
- "",
- $type,
- UserStringCodePlus::MODEL_ALLIANCE
- );
- $log_type = $is_scan ? UserWallet::TYPE_SCAN : UserWallet::TYPE_ORDER;
- $log_desc = UserWallet::TYPE_NAME_LIST[$log_type] . UserStringCodeOrder::TYPE_LIST[$type] . " 发放【{$title}】红包";
- $source_table = $is_scan ? "\app\plugins\scanCodePay\models\Order" : "app\models\Order";
- $currency = Currency::findOne(['code' => Currency::CURRENCY_STRING_CODE]);
- UserWallet::addLog($currency, UserStringCodePlus::ALLIANCE_STORE_ID, $saas_id, $money, $log_desc, $log_type, $source_table,UserStringCodePlus::ALLIANCE_STORE_ID,$type);
- //记录所有区县分红代理商
- $allDistrictAgent = new AllDistrictAgent();
- $allDistrictAgent->divvy_id = $divvyId;
- $allDistrictAgent->saas_id = $saas_id;
- $allDistrictAgent->type = $agentType;
- $allDistrictAgent->dividend_amount = $money;
- $allDistrictAgent->send_time = time();
- $allDistrictAgent->save();
- //debug_log([__METHOD__, __LINE__, "所有区县分红代理商金额:{$money} 区县代理商ID:{$saas_id} 分红完成"], "all_district_agent_divvy_controller.log");
- return true;
- }
- }
|