From e77f0ebc8a480d4ae4d6548ae153ab69d5e2388c Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Fri, 12 Jun 2026 23:37:44 +0700 Subject: [PATCH] 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. --- app/Http/Requests/Admin/Master/ProductRequest.php | 2 +- app/Http/Requests/Admin/Master/RawMaterialRequest.php | 2 +- app/Models/ProductPrice.php | 6 +++--- app/Models/RawMaterialPrice.php | 6 +++--- app/Services/Master/ProductService.php | 2 +- .../2026_06_10_100004_create_product_prices_table.php | 2 +- .../2026_06_10_110002_create_raw_material_prices_table.php | 2 +- resources/js/types/product.ts | 2 +- resources/js/types/raw-material.ts | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/Http/Requests/Admin/Master/ProductRequest.php b/app/Http/Requests/Admin/Master/ProductRequest.php index 164fbad..fd1add2 100644 --- a/app/Http/Requests/Admin/Master/ProductRequest.php +++ b/app/Http/Requests/Admin/Master/ProductRequest.php @@ -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(), ]; } diff --git a/app/Http/Requests/Admin/Master/RawMaterialRequest.php b/app/Http/Requests/Admin/Master/RawMaterialRequest.php index 4af1adf..bb32df8 100644 --- a/app/Http/Requests/Admin/Master/RawMaterialRequest.php +++ b/app/Http/Requests/Admin/Master/RawMaterialRequest.php @@ -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'), ]; diff --git a/app/Models/ProductPrice.php b/app/Models/ProductPrice.php index a64c2f0..63bc7a7 100644 --- a/app/Models/ProductPrice.php +++ b/app/Models/ProductPrice.php @@ -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, ); } diff --git a/app/Models/RawMaterialPrice.php b/app/Models/RawMaterialPrice.php index 62c035b..d6bf476 100644 --- a/app/Models/RawMaterialPrice.php +++ b/app/Models/RawMaterialPrice.php @@ -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, ); } diff --git a/app/Services/Master/ProductService.php b/app/Services/Master/ProductService.php index 53968d6..eb58f93 100644 --- a/app/Services/Master/ProductService.php +++ b/app/Services/Master/ProductService.php @@ -204,7 +204,7 @@ private function syncVariantImages(ProductVariant $variant, array $variantData, } /** - * @param list $prices + * @param list $prices */ private function syncVariantPrices(ProductVariant $variant, array $prices): void { diff --git a/database/migrations/2026_06_10_100004_create_product_prices_table.php b/database/migrations/2026_06_10_100004_create_product_prices_table.php index fea2fad..0fc482b 100644 --- a/database/migrations/2026_06_10_100004_create_product_prices_table.php +++ b/database/migrations/2026_06_10_100004_create_product_prices_table.php @@ -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(); diff --git a/database/migrations/2026_06_10_110002_create_raw_material_prices_table.php b/database/migrations/2026_06_10_110002_create_raw_material_prices_table.php index e942ef8..af6e358 100644 --- a/database/migrations/2026_06_10_110002_create_raw_material_prices_table.php +++ b/database/migrations/2026_06_10_110002_create_raw_material_prices_table.php @@ -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(); diff --git a/resources/js/types/product.ts b/resources/js/types/product.ts index c90b048..30cb555 100644 --- a/resources/js/types/product.ts +++ b/resources/js/types/product.ts @@ -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; }; diff --git a/resources/js/types/raw-material.ts b/resources/js/types/raw-material.ts index c89bd0d..75cd903 100644 --- a/resources/js/types/raw-material.ts +++ b/resources/js/types/raw-material.ts @@ -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;