182 lines
3.8 KiB
PHP
182 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\JsonArray;
|
|
use App\Enums\DealerEarningStatus;
|
|
use App\Enums\DealerLvl;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DealerEarning extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
use Filterable;
|
|
|
|
protected $attributes = [
|
|
'status' => DealerEarningStatus::Pending,
|
|
];
|
|
|
|
protected $casts = [
|
|
'lvl' => DealerLvl::class,
|
|
'status' => DealerEarningStatus::class,
|
|
'pay_at' => 'datetime',
|
|
'settle_at'=> 'datetime',
|
|
'pay_info' => JsonArray::class,
|
|
];
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'earningable_type',
|
|
'earningable_id',
|
|
'lvl',
|
|
'total_amount',
|
|
'total_earnings',
|
|
'fee',
|
|
'fee_rate',
|
|
'payer_id',
|
|
'pay_info',
|
|
'pay_image',
|
|
'pay_at',
|
|
'settle_at',
|
|
'status',
|
|
'remark',
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function payer()
|
|
{
|
|
return $this->belongsTo(User::class, 'payer_id');
|
|
}
|
|
|
|
public function earningable()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* 待打款
|
|
*
|
|
* @param [type] $query
|
|
*/
|
|
public function scopeOnlyPending($query)
|
|
{
|
|
return $query->where('status', DealerEarningStatus::Pending);
|
|
}
|
|
|
|
/**
|
|
* 待收款
|
|
*
|
|
* @param [type] $query
|
|
*/
|
|
public function scopeOnlyPaid($query)
|
|
{
|
|
return $query->where('status', DealerEarningStatus::Paid);
|
|
}
|
|
|
|
/**
|
|
* 待打款状态
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPending()
|
|
{
|
|
return $this->status == DealerEarningStatus::Pending;
|
|
}
|
|
|
|
/**
|
|
* 待收款
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPaid()
|
|
{
|
|
return $this->status == DealerEarningStatus::Paid;
|
|
}
|
|
|
|
/**
|
|
* 是否自己的
|
|
*
|
|
* @param [type] $userId
|
|
* @return boolean
|
|
*/
|
|
public function isUser($userId)
|
|
{
|
|
return $this->user_id == $userId;
|
|
}
|
|
|
|
/**
|
|
* 是否是支付人
|
|
*
|
|
* @param [type] $userId
|
|
* @return boolean
|
|
*/
|
|
public function isPayer($userId)
|
|
{
|
|
return $this->payer_id ? ($this->payer_id == $userId) : false;
|
|
}
|
|
|
|
public function getStatusNameAttribute()
|
|
{
|
|
if ($this->settle_at) {
|
|
return $this->status->text();
|
|
} else {
|
|
return '待结算';
|
|
}
|
|
}
|
|
|
|
public function getTypeNameAttribute()
|
|
{
|
|
$name = '未知';
|
|
switch ($this->attributes['earningable_type']) {
|
|
case (new DealerManagerSubsidy())->getMorphClass():
|
|
$name = '管理者津贴';
|
|
break;
|
|
case (new DealerManageSubsidy())->getMorphClass():
|
|
$name = '管理津贴';
|
|
break;
|
|
case (new DealerChannelSubsidyLog())->getMorphClass():
|
|
$name = '渠道补贴';
|
|
break;
|
|
default:
|
|
$name = '进货补贴';
|
|
break;
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
|
|
/**
|
|
* 获取用户的打款信息
|
|
*
|
|
* @return void
|
|
*/
|
|
public function getPayInfo()
|
|
{
|
|
if ($this->isPending()) {//待打款订单显示发货人收款信息
|
|
$payInfo = $this->user->dealer->pay_info;
|
|
} else {
|
|
$payInfo = $this->pay_info;
|
|
}
|
|
return $payInfo ?: null;
|
|
}
|
|
|
|
/**
|
|
* 获取进货补贴的日志
|
|
*
|
|
* @return void
|
|
*/
|
|
public function getPurchaseSubsidyLogs()
|
|
{
|
|
if ($this->earningable_type == (new DealerPurchaseSubsidy())->getMorphClass()) {
|
|
return $this->earningable?->logs;
|
|
}
|
|
return null;
|
|
}
|
|
}
|