Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public static function getUnitQtyFormComponent(): TextInput
__('lunarpanel::productvariant.form.unit_quantity.label')
)->helperText(
__('lunarpanel::productvariant.form.unit_quantity.helper_text')
)->numeric();
)
->numeric()
->minValue(1);
}

public static function getQuantityIncrementFormComponent(): TextInput
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/Pricing/DefaultPriceFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function __construct(
public ?CurrencyContract $currency = null,
public int $unitQty = 1
) {
if ($this->unitQty < 1) {
$this->unitQty = 1;
}

if (! $this->currency) {
$this->currency = Currency::getDefault();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Livewire\Livewire;
use Lunar\Admin\Filament\Resources\ProductVariantResource\Pages\ManageVariantInventory;
use Lunar\Models\Currency;
use Lunar\Models\Language;
use Lunar\Models\Product;
use Lunar\Models\ProductVariant;
use Lunar\Tests\Admin\Feature\Filament\TestCase;

uses(TestCase::class)
->group('resource.product-variant');

it('rejects a unit quantity below one', function () {
Language::factory()->create(['default' => true]);
Currency::factory()->create(['default' => true]);

$product = Product::factory()->create();

$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
]);

$this->asStaff();

Livewire::test(
ManageVariantInventory::class, [
'record' => $variant->getRouteKey(),
])->fillForm([
'unit_quantity' => 0,
])->call('save')
->assertHasFormErrors(['unit_quantity' => 'min']);
});
12 changes: 12 additions & 0 deletions tests/core/Unit/DataTypes/PriceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
expect($dataType->unitFormatted(null, NumberFormatter::CURRENCY, 4))->toEqual('£0.1155');
});

test('unit qty below one is clamped to one', function () {
$currency = Currency::factory()->create([
'code' => 'GBP',
'decimal_places' => 2,
]);

$dataType = new Price(1500, $currency, 0);

expect($dataType->unitDecimal())->toEqual(15.00);
expect($dataType->unitFormatted())->toEqual('£15.00');
});

test('can handle no decimal places', function () {
$currency = Currency::factory()->create([
'code' => 'VND',
Expand Down