Refactor price handling in product and raw material requests, models, and migrations to use integers instead of decimals. Update validation rules and data types in related TypeScript files for consistency across the application.

This commit is contained in:
Yoga Pangestu 2026-06-12 23:37:44 +07:00
parent 4c91834389
commit e77f0ebc8a
9 changed files with 13 additions and 13 deletions

View File

@ -45,7 +45,7 @@ public function rules(): array
'variants.*.stock' => ['required', 'integer', 'min:0'],
'variants.*.prices' => ['required', 'array', 'min:1'],
'variants.*.prices.*.type' => ['required', Rule::enum(PriceType::class)],
'variants.*.prices.*.price' => ['required', 'numeric', 'decimal:0,2', 'gt:0'],
'variants.*.prices.*.price' => ['required', 'integer', 'gt:0'],
...$this->variantImageRules(),
];
}

View File

@ -39,7 +39,7 @@ public function rules(): array
->whereNull('deleted_at'),
],
'prices.*.variant' => ['required', 'string', 'max:200'],
'prices.*.price' => ['required', 'numeric', 'decimal:0,2', 'gt:0'],
'prices.*.price' => ['required', 'integer', 'gt:0'],
'prices.*.stock' => ['required', 'numeric', 'decimal:0,4', 'min:0'],
...$this->variantImageRules('prices'),
];

View File

@ -22,7 +22,7 @@ protected function casts(): array
{
return [
'type' => PriceType::class,
'price' => 'decimal:2',
'price' => 'integer',
];
}
@ -34,14 +34,14 @@ public function variant(): BelongsTo
public function priceFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format((float) $this->price, 0, ',', '.'),
get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
);
}
public function priceInput(): Attribute
{
return Attribute::make(
get: fn () => (string) (int) $this->price,
get: fn () => (string) $this->price,
);
}

View File

@ -26,7 +26,7 @@ class RawMaterialPrice extends Model implements HasMedia
protected function casts(): array
{
return [
'price' => 'decimal:2',
'price' => 'integer',
'stock' => 'decimal:4',
];
}
@ -44,14 +44,14 @@ public function rawMaterial(): BelongsTo
public function priceFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format((float) $this->price, 0, ',', '.'),
get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
);
}
public function priceInput(): Attribute
{
return Attribute::make(
get: fn () => (string) (int) $this->price,
get: fn () => (string) $this->price,
);
}

View File

@ -204,7 +204,7 @@ private function syncVariantImages(ProductVariant $variant, array $variantData,
}
/**
* @param list<array{type: string, price: float|int|string}> $prices
* @param list<array{type: string, price: int}> $prices
*/
private function syncVariantPrices(ProductVariant $variant, array $prices): void
{

View File

@ -14,7 +14,7 @@ public function up(): void
$table->foreignId('variant_id')->constrained('product_variants')->cascadeOnDelete();
$table->string('type', 20);
$table->decimal('price', 18, 2);
$table->unsignedInteger('price');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

View File

@ -14,7 +14,7 @@ public function up(): void
$table->foreignId('raw_material_id')->constrained()->cascadeOnDelete();
$table->string('variant', 200);
$table->decimal('price', 18, 2);
$table->unsignedInteger('price');
$table->decimal('stock', 18, 4)->default(0);
$table->timestamp('created_at')->useCurrent();

View File

@ -13,7 +13,7 @@ export type CategoryOption = {
export type ProductPriceItem = {
type: string;
type_label: string;
price: string;
price: number;
price_formatted: string;
price_input: string;
};

View File

@ -10,7 +10,7 @@ export type RawMaterialPrice = {
variant: string;
stock: string;
stock_formatted: string;
price: string;
price: number;
price_formatted: string;
price_input: string;
stock_input: string;