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

66 lines
1.9 KiB
PHP

<?php
namespace App\Bonus;
use App\Models\Profit;
use App\Models\ProfitLog;
use App\Models\User;
use RuLong\Bonus\Contracts\Settlement;
use RuLong\Bonus\Models\Bonus;
use RuLong\Bonus\Traits\Settlementable;
/**
* 购卡佣金
*/
class ProfitStart implements Settlement
{
use Settlementable;
public $profit;
public function __construct(Profit $profit)
{
$this->profit = $profit->fresh();
}
/**
* 购卡佣金
* @return [type] [description]
*/
public function fire()
{
if ($this->profit && $this->profit->sett === 0) {
$users = User::when($this->profit->identity_id, function ($query) {
$query->whereHas('identity', function ($q) {
$q->where('identity_id', $this->profit->identity_id);
});
})->get();
$this->profit->num = $users->count();
if ($this->profit->num == 0) {
$this->profit->single = 0;
} else {
$this->profit->single = bcadd($this->profit->price / $users->count(), 0, 2);
}
$this->profit->realPrice = $this->profit->single * $this->profit->num;
$this->profit->sett = 1;
$this->profit->setted_at = now();
if ($this->profit->save()) {
$source = [
'profit_id' => $this->profit->id,
];
foreach ($users as $key => $user) {
$user->rule('profit', $this->profit->single, false, $source);
ProfitLog::create([
'profit_id' => $this->profit->id,
'user_id' => $user->id,
'price' => $this->profit->single,
]);
}
}
} else {
\Log::channel('bonus')->error('全球分红不正确(ID:' . ($this->profit->id ?? '无') . ')');
}
}
}