完成订单时,增加商品销量
parent
4a28da9861
commit
1fffe87887
|
|
@ -213,6 +213,27 @@ class Order extends Model
|
|||
return $this->status === static::STATUS_CANCELLED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将订单标记为已完成
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function markAsCompleted()
|
||||
{
|
||||
// 加载订单商品
|
||||
$this->loadMissing('products');
|
||||
|
||||
foreach ($this->products->loadMissing(['spu', 'sku']) as $product) {
|
||||
$product->spu?->increment('sales', $product->quantity);
|
||||
$product->sku?->increment('sales', $product->quantity);
|
||||
}
|
||||
|
||||
$this->update([
|
||||
'status' => static::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单券优惠金额
|
||||
*
|
||||
|
|
|
|||
|
|
@ -58,6 +58,15 @@ class OrderProduct extends Model
|
|||
return $this->belongsToMany(OrderPackage::class, 'order_package_products', 'order_product_id', 'order_package_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 此订单商品所属的SPU
|
||||
*
|
||||
*/
|
||||
public function spu()
|
||||
{
|
||||
return $this->belongsTo(ProductSpu::class, 'spu_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 此订单商品所属的SKU
|
||||
*
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\OrderPackageAlreadyCheckedException;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderPackage;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
|
|
@ -40,11 +39,7 @@ class OrderPackageService
|
|||
return;
|
||||
}
|
||||
|
||||
// 自动完成订单
|
||||
$package->order->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
$package->order->markAsCompleted();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -633,22 +633,16 @@ class OrderService
|
|||
throw new BizException('订单包裹未发完');
|
||||
}
|
||||
|
||||
$time = now();
|
||||
|
||||
$orderPackageService = new OrderPackageService();
|
||||
|
||||
// 获取订单的未作废未签收包裹
|
||||
$packages = $order->packages()->unchecked()->where('is_failed', false)->get();
|
||||
|
||||
foreach ($packages as $package) {
|
||||
$orderPackageService->checkPackage($package, true, $time);
|
||||
$orderPackageService->checkPackage($package, true);
|
||||
}
|
||||
|
||||
// 将订单标记为已完成
|
||||
$order->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => $time,
|
||||
]);
|
||||
$order->markAsCompleted();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue