diff --git a/app/Livewire/Forms/Studio/Manage/OrderForm.php b/app/Livewire/Forms/Studio/Manage/OrderForm.php index 79ef6a9..e453877 100644 --- a/app/Livewire/Forms/Studio/Manage/OrderForm.php +++ b/app/Livewire/Forms/Studio/Manage/OrderForm.php @@ -99,7 +99,7 @@ public function store() // Apply manual discount $afterManual = $subtotal - $manualDiscount; - $voucherDiscount = $this->calculateDiscount($afterManual); + $voucherDiscount = $this->calculateDiscount($subtotal, $this->voucher_id); if (is_array($voucherDiscount)) { return $voucherDiscount; @@ -168,7 +168,9 @@ public function store() ->first(); if ($userVoucher) { - if ($userVoucher->quantity > 1) { + if (! $userVoucher->quantity) { + return; + } elseif ($userVoucher->quantity > 1) { DB::table('user_voucher') ->where('id', $userVoucher->id) ->update([ diff --git a/app/Livewire/Studio/Manage/Order/Create.php b/app/Livewire/Studio/Manage/Order/Create.php index 3438683..48d378f 100644 --- a/app/Livewire/Studio/Manage/Order/Create.php +++ b/app/Livewire/Studio/Manage/Order/Create.php @@ -39,6 +39,8 @@ class Create extends Component public string $subtotal = ''; + public int $voucherDiscount = 0; + public string $total = ''; public $items; diff --git a/app/Traits/Order/WithCalculateTotal.php b/app/Traits/Order/WithCalculateTotal.php index beb36b4..c5a753f 100644 --- a/app/Traits/Order/WithCalculateTotal.php +++ b/app/Traits/Order/WithCalculateTotal.php @@ -11,6 +11,6 @@ public function getSubTotal() public function getTotal() { - return $this->items->sum(fn ($item) => $item->unit_price * $item->quantity - $item->discount); + return $this->items->sum(fn ($item) => $item->unit_price * $item->quantity - replaceCurrency($this->form->discount) - $this->voucherDiscount); } } diff --git a/app/Traits/Order/WithDiscount.php b/app/Traits/Order/WithDiscount.php index 506ee94..881bfd8 100644 --- a/app/Traits/Order/WithDiscount.php +++ b/app/Traits/Order/WithDiscount.php @@ -9,15 +9,12 @@ trait WithDiscount { public function updatedFormDiscount(string $value) { - $total = $this->getTotal(); - $discount = replaceCurrency($value); - - $this->total = $total - $discount; + $this->total = $this->getTotal(); } - public function calculateDiscount(int $subtotal) + public function calculateDiscount(int $subtotal, string $voucherId) { - $voucher = Voucher::find($this->voucher_id); + $voucher = Voucher::find($voucherId); $discount = 0; if ($voucher) { @@ -27,7 +24,7 @@ public function calculateDiscount(int $subtotal) return [ 'status' => false, - 'message' => 'Minimum pembelian harus sebesar '.currency($voucher->min_purchase, 'Rp').' untuk menggunakan voucher ini.', + 'message' => 'Minimal pembelian harus sebesar '.currency($voucher->min_purchase, 'Rp').' untuk menggunakan voucher ini.', ]; } diff --git a/app/Traits/Order/WithMember.php b/app/Traits/Order/WithMember.php index 60cd31a..043e957 100644 --- a/app/Traits/Order/WithMember.php +++ b/app/Traits/Order/WithMember.php @@ -29,10 +29,38 @@ public function members() ->toArray(); } + public function updatedFormVoucherId(?string $voucherId = null) + { + $voucher = Voucher::find($voucherId); + + if (! $voucher) { + $this->voucherDiscount = 0; + + $this->total = $this->getTotal(); + + return; + } + + $subtotal = $this->getSubtotal(); + + $this->voucherDiscount = $this->calculateDiscount($subtotal, $voucher->id); + + $this->total = $this->getTotal(); + } + public function updatedFormMemberId(?string $memberId = null) { $customer = Customer::with('user')->find($memberId); + if (! $customer) { + $this->voucherDiscount = 0; + + $this->form->voucher_id = ''; + + $this->vouchers = []; + + $this->total = $this->getTotal(); + return; } diff --git a/resources/views/livewire/studio/manage/order/form.blade.php b/resources/views/livewire/studio/manage/order/form.blade.php index 1d7ce99..d82b257 100644 --- a/resources/views/livewire/studio/manage/order/form.blade.php +++ b/resources/views/livewire/studio/manage/order/form.blade.php @@ -252,6 +252,10 @@ class="text-xs text-gray-400">{{ currency($item->quantity, '') }}
{{ currency($subtotal, 'Rp') }}
+
+ Diskon Voucher + {{ currency($voucherDiscount, 'Rp') }} +