- Updated ProductVariantService to handle multiple photo keys and URLs. - Introduced FileUploadMultiple component for uploading multiple images. - Modified product creation and editing forms to accommodate multiple photos. - Adjusted data structures in product drafts and tests to reflect changes in photo handling. - Enhanced image preview modal to navigate through multiple images.
96 lines
3.8 KiB
PHP
96 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin\Master\Product;
|
|
|
|
use App\Enums\PriceType;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Override;
|
|
|
|
class ProductRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
#[Override]
|
|
public function prepareForValidation()
|
|
{
|
|
$sharedPrices = $this->shared_prices;
|
|
if (is_array($sharedPrices)) {
|
|
foreach ($sharedPrices as $i => $price) {
|
|
if (isset($price['price']) && is_string($price['price'])) {
|
|
$this->request->set("shared_prices.$i.price", (int) str_replace('.', '', $price['price']));
|
|
}
|
|
}
|
|
}
|
|
|
|
$variants = $this->variants;
|
|
if (is_array($variants)) {
|
|
foreach ($variants as $i => $variant) {
|
|
if (isset($variant['prices']) && is_array($variant['prices'])) {
|
|
foreach ($variant['prices'] as $j => $price) {
|
|
if (isset($price['price']) && is_string($price['price'])) {
|
|
$this->request->set("variants.$i.prices.$j.price", (int) str_replace('.', '', $price['price']));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$useSamePrice = $this->boolean('use_same_price');
|
|
|
|
return [
|
|
'name' => [
|
|
'required',
|
|
'string',
|
|
'max:200',
|
|
],
|
|
'description' => ['nullable', 'string'],
|
|
'status' => ['nullable', Rule::in(['active', 'inactive', 'draft'])],
|
|
'category_ids' => ['required', 'array', 'min:1'],
|
|
'category_ids.*' => ['exists:categories,id'],
|
|
'use_same_price' => ['nullable', 'boolean'],
|
|
'shared_prices' => ['required_if:use_same_price,true', 'nullable', 'array', ...($useSamePrice ? ['size:9'] : [])],
|
|
'shared_prices.*.type' => ['required_if:use_same_price,true', 'nullable', Rule::in(PriceType::values())],
|
|
'shared_prices.*.price' => ['required_if:use_same_price,true', 'nullable', 'integer', 'min:0'],
|
|
'variants' => ['required', 'array', 'min:1'],
|
|
'variants.*.id' => ['nullable', 'integer'],
|
|
'variants.*.name' => ['required', 'string', 'max:200'],
|
|
'variants.*.stock' => ['required', 'integer', 'min:0'],
|
|
'variants.*.reject_stock' => ['required', 'integer', 'min:0'],
|
|
'variants.*.retail_stock' => ['required', 'integer', 'min:0'],
|
|
'variants.*.photo_keys' => ['required', 'array', 'min:1', 'max:5'],
|
|
'variants.*.photo_keys.*' => ['required', 'string', 'max:500'],
|
|
'variants.*.prices' => ['required_if:use_same_price,false', 'nullable', 'array', ...(! $useSamePrice ? ['size:9'] : [])],
|
|
'variants.*.prices.*.id' => ['nullable', 'integer'],
|
|
'variants.*.prices.*.type' => ['required_if:use_same_price,false', 'nullable', Rule::in(PriceType::values())],
|
|
'variants.*.prices.*.price' => ['required_if:use_same_price,false', 'nullable', 'integer', 'min:0'],
|
|
];
|
|
}
|
|
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => 'Nama Produk',
|
|
'description' => 'Deskripsi',
|
|
'status' => 'Status',
|
|
'category_ids' => 'Kategori',
|
|
'variants' => 'Varian',
|
|
'variants.*.name' => 'Nama Varian',
|
|
'variants.*.stock' => 'Stok',
|
|
'variants.*.reject_stock' => 'Stok Reject',
|
|
'variants.*.retail_stock' => 'Stok Retail',
|
|
'variants.*.photo_keys' => 'Foto',
|
|
'variants.*.photo_keys.*' => 'Foto',
|
|
'variants.*.prices' => 'Harga',
|
|
'variants.*.prices.*.type' => 'Tipe Harga',
|
|
'variants.*.prices.*.price' => 'Harga',
|
|
];
|
|
}
|
|
}
|