1
0
Files
lkafu/app/Bonus/DirectAgency.php
2020-08-06 14:45:56 +08:00

70 lines
2.5 KiB
PHP

<?php
namespace App\Bonus;
use App\Models\UpgradePayment;
use RuLong\Bonus\Contracts\Settlement;
use RuLong\Bonus\Models\Bonus;
use RuLong\Bonus\Traits\Settlementable;
/**
* 推荐代理
*/
class DirectAgency implements Settlement
{
use Settlementable;
public $payment;
public function __construct(UpgradePayment $payment)
{
$this->payment = $payment->fresh();
}
/**
* 推荐代理
* @return [type] [description]
*/
public function fire()
{
if ($this->payment && $this->payment->type == 'agent' && $this->payment->state == 'SUCCESS') {
//判断订单状态
$user = $this->payment->user; //获取会员
if ($user) {
//基础判断
$parent = $user->parent; //获取推荐人
$eventName = 'direct_agency'; //默认直推
while ($parent) {
$rate = $parent->identity->info->configs[$eventName] ?? 0; //结算比例
if ($rate) {
$amount = $this->payment->amount; //代理金额
$cash = round($amount * $rate / 100, 2); //实际佣金
$log = $parent->accountLogs()->whereIn('rule_id', [5, 6])->where('source->user_id', $user->id)->first(); //查看是否结算过佣金
if ($cash > 0 && !$log) {
$source = [
'user_id' => $user->id, //会员ID
'payment_id' => $this->payment->id, //升级订单ID
'rate' => $rate, //结算比例
'amount' => $amount, //结算金额
];
$parent->rule($eventName, $cash, false, $source); //执行立即到账
}
if ($eventName == 'indirect_agency') {
break;
} else {
$eventName = 'indirect_agency'; //更改间接推荐
}
}
$parent = $parent->parent;
}
return true;
} else {
\Log::channel('bonus')->error('升级代理不存在' . $this->payment->user_id);
}
} else {
\Log::channel('bonus')->error('升级代理状态不正确或不存在(ID:' . ($this->payment->id ?? '无') . ')');
}
}
}