75 lines
1.6 KiB
PHP
75 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Forms\Studio\Master;
|
|
|
|
use App\Models\Formula;
|
|
use App\Rules\UnsignedInteger;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Form;
|
|
|
|
class FormulaForm extends Form
|
|
{
|
|
public ?Formula $formula = null;
|
|
|
|
public string $quality = '';
|
|
|
|
public string $size = '';
|
|
|
|
public string $volume = '';
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'quality' => ['required', 'string', 'max:20'],
|
|
'size' => ['required', 'integer', 'min:1'],
|
|
'volume' => ['required', new UnsignedInteger, 'lte:size'],
|
|
];
|
|
}
|
|
|
|
public function validationAttributes(): array
|
|
{
|
|
return [
|
|
'quality' => 'kualitas',
|
|
'size' => 'ukuran',
|
|
];
|
|
}
|
|
|
|
public function setFormula(Formula $formula): void
|
|
{
|
|
$this->formula = $formula;
|
|
|
|
$this->quality = $formula->quality;
|
|
$this->size = $formula->size;
|
|
$this->volume = parseRupiahToInt($formula->volume);
|
|
}
|
|
|
|
public function store(): void
|
|
{
|
|
$this->validate();
|
|
|
|
DB::transaction(function () {
|
|
Formula::create($this->prepareDataForSave());
|
|
});
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->validate();
|
|
|
|
DB::transaction(function () {
|
|
$this->formula->update($this->prepareDataForSave());
|
|
|
|
$this->formula;
|
|
});
|
|
}
|
|
|
|
private function prepareDataForSave(): array
|
|
{
|
|
return [
|
|
'quality' => $this->quality,
|
|
'size' => $this->size,
|
|
'volume' => parseRupiahToInt($this->volume),
|
|
];
|
|
}
|
|
}
|