51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Concerns;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
trait HasRawMaterialPriceRules
|
|
{
|
|
use ValidatesMediaUploads;
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function rawMaterialPriceRules(string $pricesKey = 'prices', ?int $rawMaterialId = null): array
|
|
{
|
|
$rules = [
|
|
"{$pricesKey}" => ['required', 'array', 'min:1'],
|
|
"{$pricesKey}.*.variant" => ['required', 'string', 'max:200'],
|
|
"{$pricesKey}.*.price" => ['required', 'integer', 'gt:0'],
|
|
"{$pricesKey}.*.stock" => ['required', 'numeric', 'decimal:0,4', 'min:0'],
|
|
...$this->variantImageRules($pricesKey, required: true),
|
|
];
|
|
|
|
if ($rawMaterialId) {
|
|
$rules["{$pricesKey}.*.id"] = [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('raw_material_prices', 'id')
|
|
->where('raw_material_id', $rawMaterialId)
|
|
->whereNull('deleted_at'),
|
|
];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function rawMaterialPriceAttributes(string $pricesKey = 'prices'): array
|
|
{
|
|
return [
|
|
$pricesKey => 'varian',
|
|
"{$pricesKey}.*.variant" => 'nama varian',
|
|
"{$pricesKey}.*.price" => 'harga',
|
|
"{$pricesKey}.*.stock" => 'stok',
|
|
...$this->variantImageAttributes($pricesKey, 'foto varian'),
|
|
];
|
|
}
|
|
}
|