feat(order): tambah poin saat update
This commit is contained in:
parent
ac59863be3
commit
464ce3df39
@ -111,11 +111,15 @@ public function store()
|
|||||||
// Calculate total (subtotal - manual discount - voucher discount)
|
// Calculate total (subtotal - manual discount - voucher discount)
|
||||||
$total = $afterManual - $voucherDiscount;
|
$total = $afterManual - $voucherDiscount;
|
||||||
|
|
||||||
$member = User::whereHas('customer', fn ($q) => $q->where('id', $this->member_id))->first();
|
$pointsEarned = (int) floor($total / 5000);
|
||||||
|
|
||||||
// Wrap the entire operation in a database transaction
|
// Wrap the entire operation in a database transaction
|
||||||
// Ensures atomicity — if any step fails, all changes are rolled back
|
// Ensures atomicity — if any step fails, all changes are rolled back
|
||||||
DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $member) {
|
DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $pointsEarned) {
|
||||||
|
$member = User::whereHas('customer', fn ($q) => $q->where('id', $this->member_id))
|
||||||
|
->with(['membership' => fn ($q) => $q->lockForUpdate()])
|
||||||
|
->first();
|
||||||
|
|
||||||
$order = Order::create([
|
$order = Order::create([
|
||||||
'outlet_id' => $this->outlet_id,
|
'outlet_id' => $this->outlet_id,
|
||||||
'user_id' => auth()->id(),
|
'user_id' => auth()->id(),
|
||||||
@ -174,6 +178,14 @@ public function store()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update member tier points
|
||||||
|
if ($member && $member->membership) {
|
||||||
|
$member->membership()->update([
|
||||||
|
'tier_points' => DB::raw("tier_points + $pointsEarned"),
|
||||||
|
'reward_points' => DB::raw("reward_points + $pointsEarned"),
|
||||||
|
'last_transaction_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user