fix: allow custom creation dates for order numbering and prevent overwriting existing user IDs in OrderObserver
This commit is contained in:
parent
7ce9409202
commit
b384a699de
@ -11,14 +11,18 @@ class OrderObserver
|
||||
*/
|
||||
public function creating(Order $order): void
|
||||
{
|
||||
$lastOrder = Order::whereDate('created_at', now()->format('Y-m-d'))
|
||||
$date = $order->created_at ?? now();
|
||||
|
||||
$lastOrder = Order::whereDate('created_at', $date->format('Y-m-d'))
|
||||
->lockForUpdate()
|
||||
->count();
|
||||
|
||||
$sequence = str_pad($lastOrder + 1, 3, '0', STR_PAD_LEFT);
|
||||
$order->order_number = 'ORD-'.now()->format('Ymd').'-'.$sequence;
|
||||
$order->order_number = 'ORD-'.$date->format('Ymd').'-'.$sequence;
|
||||
|
||||
$order->user_id = auth()->id();
|
||||
if (! $order->user_id) {
|
||||
$order->user_id = auth()->id();
|
||||
}
|
||||
|
||||
$order->total = $order->subtotal - $order->discount;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user