328 lines
7.2 KiB
PHP
328 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\JsonArray;
|
|
use App\Enums\DealerOrderSettleState;
|
|
use App\Enums\DealerOrderStatus;
|
|
use App\Enums\PayWay;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DealerOrder extends Model
|
|
{
|
|
use Filterable;
|
|
use HasDateTimeFormatter;
|
|
|
|
protected $attributes = [
|
|
'status' => DealerOrderStatus::Pending,
|
|
'settle_state' => DealerOrderSettleState::Pending,
|
|
];
|
|
|
|
protected $casts = [
|
|
'status' => DealerOrderStatus::class,
|
|
'settle_state' => DealerOrderSettleState::class,
|
|
'pay_info'=>JsonArray::class,
|
|
'pay_way' => PayWay::class,
|
|
'pay_time'=> 'datetime',
|
|
'paied_time'=>'datetime',
|
|
'shipping_time'=>'datetime',
|
|
'shippinged_time'=>'datetime',
|
|
'allocated_at'=>'datetime',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'status',
|
|
'pay_info',
|
|
'pay_image',
|
|
'consignor_id',
|
|
'settle_state',
|
|
'consignee_name',
|
|
'consignee_telephone',
|
|
'consignee_zone',
|
|
'consignee_address',
|
|
'pay_way',
|
|
'pay_time',
|
|
'paied_time',
|
|
'shipping_time',
|
|
'shippinged_time',
|
|
'allocated_at', //分配时间
|
|
'remark',
|
|
'pay_sn',
|
|
'out_trade_no',
|
|
'local_status',
|
|
'deposit_status',
|
|
];
|
|
|
|
/**
|
|
* 仅获取待结算的已付款订单
|
|
*/
|
|
public function scopeSettlePending($query)
|
|
{
|
|
return $query->whereNotNull('paied_time')
|
|
->where('settle_state', DealerOrderSettleState::Pending)
|
|
->whereIn('status', [
|
|
DealerOrderStatus::Paid,
|
|
DealerOrderStatus::Shipped,
|
|
DealerOrderStatus::Completed,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取待确认订单
|
|
*/
|
|
public function scopeOnlyPending($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Pending);
|
|
}
|
|
|
|
/**
|
|
* 已确认/待付款
|
|
*/
|
|
public function scopeOnlyPendinged($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Paying);
|
|
}
|
|
|
|
/**
|
|
* 待收款
|
|
*/
|
|
public function scopeOnlyPaid($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Confirming);
|
|
}
|
|
|
|
/**
|
|
* 待发货
|
|
*/
|
|
public function scopeOnlyShipping($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Paid);
|
|
}
|
|
|
|
/**
|
|
* 已发货/待收货
|
|
*/
|
|
public function scopeOnlyShippinged($query)
|
|
{
|
|
// return $query->whereIn('status', [
|
|
// DealerOrderStatus::Confirming, DealerOrderStatus::Paid, DealerOrderStatus::Shipped,
|
|
// ]);
|
|
return $query->where('status', DealerOrderStatus::Shipped);
|
|
}
|
|
|
|
/**
|
|
* 已完成
|
|
*/
|
|
public function scopeOnlyCompleted($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Completed);
|
|
}
|
|
|
|
/**
|
|
* 已取消
|
|
*/
|
|
public function scopeOnlyCancelled($query)
|
|
{
|
|
return $query->where('status', DealerOrderStatus::Cancelled);
|
|
}
|
|
|
|
public function dealerDeliveryBill()
|
|
{
|
|
return $this->belongsTo(DealerDeliveryBill::class, 'order_id');
|
|
}
|
|
|
|
/**
|
|
* 此订单所属的用户的信息
|
|
*/
|
|
public function userInfo()
|
|
{
|
|
return $this->belongsTo(UserInfo::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 此订单所属的经销商
|
|
*/
|
|
public function dealer()
|
|
{
|
|
return $this->belongsTo(Dealer::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 属于此订单的商品
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->hasMany(DealerOrderProduct::class, 'order_id');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function consignor()
|
|
{
|
|
return $this->belongsTo(User::class, 'consignor_id');
|
|
}
|
|
|
|
public function refuseLogs()
|
|
{
|
|
return $this->hasMany(DealerOrderRefuseLog::class, 'order_id');
|
|
}
|
|
|
|
/**
|
|
* 属于此订单的支付记录
|
|
*/
|
|
public function payLogs()
|
|
{
|
|
return $this->morphMany(PayLog::class, 'payable');
|
|
}
|
|
|
|
public function isUser($userId)
|
|
{
|
|
return $this->user_id == $userId;
|
|
}
|
|
|
|
public function isConsignor($userId)
|
|
{
|
|
return $this->consignor_id == $userId;
|
|
}
|
|
|
|
public function getOrderStatusAttribute()
|
|
{
|
|
return $this->status->value;
|
|
}
|
|
|
|
/**
|
|
* 是否待确认订单
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPending()
|
|
{
|
|
return $this->status == DealerOrderStatus::Pending;
|
|
}
|
|
|
|
/**
|
|
* 是否待打款订单
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPendinged()
|
|
{
|
|
return $this->status == DealerOrderStatus::Paying;
|
|
}
|
|
|
|
/**
|
|
* 是待确认打款订单
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPay()
|
|
{
|
|
return $this->status == DealerOrderStatus::Confirming;
|
|
}
|
|
|
|
/**
|
|
* 是已确认收款的订单
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isPaid()
|
|
{
|
|
return $this->status == DealerOrderStatus::Paid;
|
|
}
|
|
|
|
/**
|
|
* 是否待收货订单
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isShipping()
|
|
{
|
|
return $this->status == DealerOrderStatus::Shipped;
|
|
}
|
|
|
|
/**
|
|
* 是否已取消
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isCancelled()
|
|
{
|
|
return $this->status == DealerOrderStatus::Cancelled;
|
|
}
|
|
|
|
/**
|
|
* 支付方式是否是线下打款
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPayWayOffline(): bool
|
|
{
|
|
return $this->pay_way === PayWay::Offline;
|
|
}
|
|
|
|
/**
|
|
* 支付方式是否是余额支付
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPayWayWallet(): bool
|
|
{
|
|
return $this->pay_way === PayWay::Wallet;
|
|
}
|
|
|
|
/**
|
|
* 支付方式是否是微信支付
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPayWayWxpay(): bool
|
|
{
|
|
return in_array($this->pay_way, [
|
|
PayWay::WxpayH5,
|
|
PayWay::WxpayJsApi,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 能否操作
|
|
*
|
|
* @param [type] $userId
|
|
* @return boolean
|
|
*/
|
|
public function canCurd($userId)
|
|
{
|
|
return $this->isUser($userId) || $this->isConsignor($userId);
|
|
}
|
|
|
|
/**
|
|
* 获取发货人的打款信息
|
|
*
|
|
* @return void
|
|
*/
|
|
public function getConsignorPayInfo()
|
|
{
|
|
if ($this->isPendinged()) {//待打款订单显示发货人收款信息
|
|
if ($this->consignor?->dealer) {
|
|
$payInfo = $this->consignor->dealer->pay_info;
|
|
} else {
|
|
$payInfo = [
|
|
//todo-公司的收款信息
|
|
'bank'=>app_settings('dealer.bank'),
|
|
'alipay'=>app_settings('dealer.alipay'),
|
|
'wechat'=>app_settings('dealer.wechat'),
|
|
];
|
|
}
|
|
} elseif ($this->isPending() || $this->isCancelled()) {//如果是已取消订单或者待确认订单不显示收款信息
|
|
$payInfo = [];
|
|
} else {
|
|
$payInfo = $this->pay_info;
|
|
}
|
|
return $payInfo ?: null;
|
|
}
|
|
}
|