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],
|
|
];
|
|
}
|
|
|
|
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 = replaceCurrency($formula->volume);
|
|
}
|
|
|
|
public function store(): Formula
|
|
{
|
|
$this->validate();
|
|
|
|
return DB::transaction(function () {
|
|
return Formula::create($this->prepareDataForSave());
|
|
});
|
|
}
|
|
|
|
public function update(): Formula
|
|
{
|
|
$this->validate();
|
|
|
|
return DB::transaction(function () {
|
|
$this->formula->update($this->prepareDataForSave());
|
|
|
|
return $this->formula;
|
|
});
|
|
}
|
|
|
|
private function prepareDataForSave(): array
|
|
{
|
|
return [
|
|
'quality' => $this->quality,
|
|
'size' => $this->size,
|
|
'volume' => replaceCurrency($this->volume),
|
|
];
|
|
}
|
|
}
|