179 lines
4.8 KiB
PHP
179 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Studio\Master;
|
|
|
|
use App\Livewire\Forms\Studio\Master\FormulaForm;
|
|
use App\Models\Bottle;
|
|
use App\Models\Formula as FormulaModel;
|
|
use App\Traits\Notification\WithSubscribeNotification;
|
|
use App\Traits\WithAuthorization;
|
|
use App\Traits\WithCloseModal;
|
|
use App\Traits\WithConfirmation;
|
|
use App\Traits\WithToast;
|
|
use App\Traits\WithUpdatedData;
|
|
use Flux\Flux;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Collection;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
|
|
#[Title('Rumus')]
|
|
class Formula extends Component
|
|
{
|
|
use WithAuthorization, WithCloseModal, WithConfirmation, WithSubscribeNotification, WithToast, WithUpdatedData;
|
|
|
|
public FormulaForm $form;
|
|
|
|
public Collection $formulas;
|
|
|
|
public string $method = 'create';
|
|
|
|
public string $modalTitle = '';
|
|
|
|
public array $qualities = ['Premium', 'Super Premium', 'Murni'];
|
|
|
|
public array $sizes = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->formulas = FormulaModel::orderBy('size')
|
|
->get()
|
|
->groupBy('size')
|
|
->map(
|
|
fn ($items) => $items
|
|
->map(fn ($item) => [
|
|
'hash' => $item->hash,
|
|
'quality' => $item->quality,
|
|
'volume' => $item->volume,
|
|
])
|
|
->sortBy('volume')
|
|
->values()
|
|
->all()
|
|
);
|
|
|
|
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$this->canOrAbort('create formula');
|
|
|
|
if (FormulaModel::where('quality', $this->form->quality)->where('size', $this->form->size)->exists()) {
|
|
$this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning');
|
|
|
|
return;
|
|
}
|
|
|
|
$formula = $this->form->store();
|
|
|
|
$size = $formula->size;
|
|
|
|
$newFormula = [
|
|
'hash' => $formula->hash,
|
|
'quality' => $formula->quality,
|
|
'volume' => $formula->volume,
|
|
];
|
|
|
|
// Get the group for this size, or an empty Collection
|
|
$group = $this->formulas->get($size, collect());
|
|
|
|
// Add, then re-sort
|
|
$group = $group->push($newFormula)
|
|
->sortBy('volume')
|
|
->values();
|
|
|
|
// Put it back
|
|
$this->formulas->put($size, $group);
|
|
|
|
$this->toast('Rumus berhasil ditambahkan.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->canOrAbort('update formula');
|
|
|
|
$exists = FormulaModel::where('quality', $this->form->quality)
|
|
->where('size', $this->form->size)
|
|
->where('id', '!=', $this->form->formula->id)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning');
|
|
|
|
return;
|
|
}
|
|
|
|
$formula = $this->form->update();
|
|
|
|
// update array formulas
|
|
$this->formulas = $this->formulas->map(function ($group) use ($formula) {
|
|
return collect($group)
|
|
->map(function ($item) use ($formula) {
|
|
if ($item['hash'] === $formula->hash) {
|
|
$item['quality'] = $formula->quality;
|
|
$item['volume'] = $formula->volume;
|
|
}
|
|
|
|
return $item;
|
|
})
|
|
->sortBy('volume')
|
|
->values();
|
|
});
|
|
|
|
$this->toast('Rumus berhasil diperbarui.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
public function delete(FormulaModel $formula): void
|
|
{
|
|
$this->canOrAbort('delete formula');
|
|
|
|
$formula->delete();
|
|
|
|
// update array formulas
|
|
$size = $formula->size;
|
|
|
|
if ($this->formulas->has($size)) {
|
|
$group = collect($this->formulas->get($size))
|
|
->reject(fn ($item) => $item['hash'] === $formula->hash)
|
|
->values()
|
|
->all();
|
|
|
|
if (empty($group)) {
|
|
$this->formulas->forget($size);
|
|
} else {
|
|
$this->formulas->put($size, $group);
|
|
}
|
|
}
|
|
|
|
$this->toast('Rumus berhasil dihapus.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
#[On('modal:open')]
|
|
public function openModal(string $method, string $modalTitle, ?string $id = null): void
|
|
{
|
|
$this->resetValidation();
|
|
$this->resetErrorBag();
|
|
|
|
$this->method = $method;
|
|
$this->modalTitle = $modalTitle;
|
|
|
|
if ($id) {
|
|
$this->form->setFormula(FormulaModel::byHashOrFail($id));
|
|
}
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.studio.master.formulas', [
|
|
'pageTitle' => 'Rumus',
|
|
]);
|
|
}
|
|
}
|