94 lines
2.8 KiB
PHP
94 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Carbon\Carbon;
|
||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class OrderPackage extends Model
|
||
{
|
||
use HasFactory;
|
||
use HasDateTimeFormatter;
|
||
|
||
public const STATUS_WAIT = 1;//揽收
|
||
public const STATUS_ONTHEWAY = 0;//途中
|
||
public const STATUS_DISTRIBUTE = 5;//派送
|
||
public const STATUS_CHECK = 3;//签收
|
||
public const STATUS_QUESTION = 2;//疑难:问题包裹
|
||
public const STATUS_REFUND = 4;//退签
|
||
public const STATUS_REFUSE = 6;//拒签
|
||
public const STATUS_OTHER = 10;//其他
|
||
public const STATUS_AUTOCHECK = 11;//自动签收
|
||
|
||
public static $kuaidi100StatusMap = [
|
||
'wait' => self::STATUS_WAIT,
|
||
'on_the_way' => self::STATUS_ONTHEWAY,
|
||
'distribute' => self::STATUS_DISTRIBUTE,
|
||
'check' => self::STATUS_CHECK,
|
||
'refund' => self::STATUS_REFUND,
|
||
'refuse' => self::STATUS_REFUSE,
|
||
'questions'=> self::STATUS_QUESTION,
|
||
];
|
||
|
||
protected $attributes = [
|
||
'status' => self::STATUS_WAIT,
|
||
'is_failed'=> false,
|
||
];
|
||
|
||
protected $casts = [
|
||
'is_failed' => 'bool',
|
||
];
|
||
|
||
protected $fillable = [
|
||
'remarks', 'status', 'is_failed', 'checked_at',
|
||
];
|
||
|
||
/**
|
||
* 订单
|
||
*/
|
||
public function order()
|
||
{
|
||
return $this->belongsTo(Order::class, 'order_id');
|
||
}
|
||
|
||
/**
|
||
* 货运单商品
|
||
*
|
||
*/
|
||
public function packageProducts()
|
||
{
|
||
return $this->hasMany(OrderPackageProduct::class, 'order_package_id');
|
||
}
|
||
|
||
public function orderProducts()
|
||
{
|
||
return $this->belongsToMany(OrderProduct::class, 'order_package_products', 'order_package_id', 'order_product_id');
|
||
}
|
||
|
||
/**
|
||
* 更新包裹物流状态
|
||
*/
|
||
public function updatePackageStatus(int $status, ?Carbon $time = null)
|
||
{
|
||
//如果是签收或者自动签收,填入签收时间; 并填入包裹商品里面的售后有效期;
|
||
$this->status = $status;
|
||
|
||
if ($status == self::STATUS_CHECK || $status == self::STATUS_AUTOCHECK) {
|
||
$this->checked_at = $time ?? now();
|
||
$this->orderProducts()->update([
|
||
'after_expire_at'=>$this->checked_at->addDays(7),
|
||
]);
|
||
//看这个包裹的订单下是否还有未签收的包裹,没有,则把订单status改为已完成, 并完成订单
|
||
if ($this->order->packages()->where('is_failed', false)->whereNotIn('status', [self::STATUS_CHECK, self::STATUS_AUTOCHECK])->doesntExist()) {
|
||
$this->order()->update([
|
||
'status' => Order::STATUS_COMPLETED,
|
||
'completed_at' => now(),
|
||
]);
|
||
}
|
||
}
|
||
$this->save();
|
||
}
|
||
}
|