242 lines
5.3 KiB
PHP
242 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\JsonArray;
|
|
use App\Enums\DealerEarningStatus;
|
|
use App\Enums\DealerLvl;
|
|
use App\Enums\PayWay;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
|
|
class DealerEarning extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
use Filterable;
|
|
|
|
protected $attributes = [
|
|
'status' => DealerEarningStatus::Pending,
|
|
];
|
|
|
|
public const PAY_WAY_WALLET = 'wallet'; // 余额
|
|
public const PAY_WAY_OFFLINE = 'offline'; // 线下支付
|
|
|
|
protected $casts = [
|
|
'lvl' => DealerLvl::class,
|
|
'status' => DealerEarningStatus::class,
|
|
'pay_way' => PayWay::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',
|
|
'pay_way',
|
|
];
|
|
|
|
public static $payWayText = [
|
|
self::PAY_WAY_WALLET => '余额支付',
|
|
self::PAY_WAY_OFFLINE => '线下打款',
|
|
];
|
|
|
|
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 \Illuminate\Database\Eloquent\Builder $query
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeWithoutPayer($query)
|
|
{
|
|
return $query->whereNull('payer_id');
|
|
}
|
|
|
|
/**
|
|
* 仅查询渠道补贴收益
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeChannelSubsidy($query)
|
|
{
|
|
return $query->where('earningable_type', (new DealerChannelSubsidyLog())->getMorphClass());
|
|
}
|
|
|
|
/**
|
|
* 待打款
|
|
*
|
|
* @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 $query->whereIn('status', [
|
|
DealerEarningStatus::Pending,
|
|
DealerEarningStatus::Paid,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 确认此收益是否结算
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isSettled(): bool
|
|
{
|
|
return $this->settle_at !== null;
|
|
}
|
|
|
|
/**
|
|
* 待打款状态
|
|
*
|
|
* @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 getStatusFormatAttribute()
|
|
{
|
|
return $this->settle_at ? $this->status->value : -1;
|
|
}
|
|
|
|
public function getEarningableTypeTextAttribute()
|
|
{
|
|
$text = '其它';
|
|
|
|
switch (Relation::getMorphedModel($this->attributes['earningable_type'])) {
|
|
// 管理津贴
|
|
case DealerManageSubsidy::class:
|
|
$text = '管理津贴';
|
|
break;
|
|
|
|
// 管理者津贴
|
|
case DealerManagerSubsidy::class:
|
|
$text = '管理者津贴';
|
|
break;
|
|
|
|
// 进货津贴
|
|
case DealerPurchaseSubsidy::class:
|
|
$text = '进货补贴';
|
|
break;
|
|
|
|
case DealerChannelSubsidyLog::class:
|
|
$text = '渠道补贴';
|
|
break;
|
|
}
|
|
|
|
return $text;
|
|
}
|
|
|
|
/**
|
|
* 获取用户的打款信息
|
|
*
|
|
* @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;
|
|
}
|
|
}
|