fix(order): calculate diskon

This commit is contained in:
Yoga Pangestu 2025-11-11 01:41:10 +07:00
parent 9d2bbbd2c8
commit 0019633886
6 changed files with 43 additions and 10 deletions

View File

@ -99,7 +99,7 @@ public function store()
// Apply manual discount // Apply manual discount
$afterManual = $subtotal - $manualDiscount; $afterManual = $subtotal - $manualDiscount;
$voucherDiscount = $this->calculateDiscount($afterManual); $voucherDiscount = $this->calculateDiscount($subtotal, $this->voucher_id);
if (is_array($voucherDiscount)) { if (is_array($voucherDiscount)) {
return $voucherDiscount; return $voucherDiscount;
@ -168,7 +168,9 @@ public function store()
->first(); ->first();
if ($userVoucher) { if ($userVoucher) {
if ($userVoucher->quantity > 1) { if (! $userVoucher->quantity) {
return;
} elseif ($userVoucher->quantity > 1) {
DB::table('user_voucher') DB::table('user_voucher')
->where('id', $userVoucher->id) ->where('id', $userVoucher->id)
->update([ ->update([

View File

@ -39,6 +39,8 @@ class Create extends Component
public string $subtotal = ''; public string $subtotal = '';
public int $voucherDiscount = 0;
public string $total = ''; public string $total = '';
public $items; public $items;

View File

@ -11,6 +11,6 @@ public function getSubTotal()
public function getTotal() 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);
} }
} }

View File

@ -9,15 +9,12 @@ trait WithDiscount
{ {
public function updatedFormDiscount(string $value) public function updatedFormDiscount(string $value)
{ {
$total = $this->getTotal(); $this->total = $this->getTotal();
$discount = replaceCurrency($value);
$this->total = $total - $discount;
} }
public function calculateDiscount(int $subtotal) public function calculateDiscount(int $subtotal, string $voucherId)
{ {
$voucher = Voucher::find($this->voucher_id); $voucher = Voucher::find($voucherId);
$discount = 0; $discount = 0;
if ($voucher) { if ($voucher) {
@ -27,7 +24,7 @@ public function calculateDiscount(int $subtotal)
return [ return [
'status' => false, '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.',
]; ];
} }

View File

@ -29,10 +29,38 @@ public function members()
->toArray(); ->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) public function updatedFormMemberId(?string $memberId = null)
{ {
$customer = Customer::with('user')->find($memberId); $customer = Customer::with('user')->find($memberId);
if (! $customer) { if (! $customer) {
$this->voucherDiscount = 0;
$this->form->voucher_id = '';
$this->vouchers = [];
$this->total = $this->getTotal();
return; return;
} }

View File

@ -252,6 +252,10 @@ class="text-xs text-gray-400">{{ currency($item->quantity, '') }}
<div> <div>
<flux:text>{{ currency($subtotal, 'Rp') }}</flux:text> <flux:text>{{ currency($subtotal, 'Rp') }}</flux:text>
</div> </div>
<div>
<flux:text class="italic text-gray-500">Diskon Voucher</flux:text>
<flux:text>{{ currency($voucherDiscount, 'Rp') }}</flux:text>
</div>
<div class="my-2"> <div class="my-2">
<flux:field> <flux:field>
<flux:label></flux:label> <flux:label></flux:label>