190 lines
5.1 KiB
PHP
190 lines
5.1 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\Models\PushNotification;
|
|
use App\Traits\WithAuthorization;
|
|
use App\Traits\WithCloseModal;
|
|
use App\Traits\WithConfirmation;
|
|
use App\Traits\WithToast;
|
|
use App\Traits\WithUpdatedData;
|
|
use Flux\Flux;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
use Minishlink\WebPush\Subscription;
|
|
use Minishlink\WebPush\WebPush;
|
|
|
|
#[Title('Rumus')]
|
|
class Formula extends Component
|
|
{
|
|
use WithAuthorization, WithCloseModal, WithConfirmation, WithToast, WithUpdatedData;
|
|
|
|
public FormulaForm $form;
|
|
|
|
public array $formulas = [];
|
|
|
|
public string $method = 'create';
|
|
|
|
public string $modalTitle = '';
|
|
|
|
public array $qualities = ['Premium', 'Super Premium', 'Murni'];
|
|
|
|
public array $sizes = [];
|
|
|
|
public function mount()
|
|
{
|
|
$this->formulas = FormulaModel::orderBy('size')
|
|
->get()
|
|
->groupBy('size')
|
|
->map(function ($items) {
|
|
return $items
|
|
->map(fn ($item) => [
|
|
'hash' => $item->hash,
|
|
'quality' => $item->quality,
|
|
'volume' => $item->volume,
|
|
])
|
|
->sortBy('volume');
|
|
})
|
|
->toArray();
|
|
|
|
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
|
}
|
|
|
|
#[On('modal:open')]
|
|
public function openModal(string $method, string $modalTitle, ?string $id = null)
|
|
{
|
|
$this->resetValidation();
|
|
$this->resetErrorBag();
|
|
|
|
$this->method = $method;
|
|
$this->modalTitle = $modalTitle;
|
|
|
|
if ($id) {
|
|
$this->form->setFormula(FormulaModel::byHashOrFail($id));
|
|
}
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
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;
|
|
}
|
|
|
|
$this->canOrAbort('create formula');
|
|
|
|
$formula = $this->form->store();
|
|
|
|
// push new formula to array
|
|
$size = $formula->size;
|
|
$this->formulas[$size] ??= [];
|
|
$this->formulas[$size][] = [
|
|
'hash' => $formula->hash,
|
|
'quality' => $formula->quality,
|
|
'volume' => $formula->volume,
|
|
];
|
|
|
|
$this->toast('Rumus berhasil ditambahkan.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->pushNotification();
|
|
|
|
$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;
|
|
}
|
|
|
|
$this->canOrAbort('update formula');
|
|
|
|
$formula = $this->form->update();
|
|
|
|
// update array formulas
|
|
foreach ($this->formulas as $key => &$items) {
|
|
$index = array_search($formula->hash, array_column($items, 'hash'));
|
|
if ($index !== false) {
|
|
$this->formulas[$key][$index] = [
|
|
'hash' => $formula->hash,
|
|
'quality' => $formula->quality,
|
|
'volume' => $formula->volume,
|
|
];
|
|
break;
|
|
}
|
|
}
|
|
unset($items);
|
|
|
|
$this->toast('Rumus berhasil diperbarui.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
public function delete(FormulaModel $formula)
|
|
{
|
|
$formula->delete();
|
|
|
|
// update array formulas
|
|
$size = $formula->size;
|
|
if (isset($this->formulas[$size])) {
|
|
$this->formulas[$size] = array_values(array_filter(
|
|
$this->formulas[$size],
|
|
fn ($item) => $item['hash'] !== $formula->hash
|
|
));
|
|
|
|
if (empty($this->formulas[$size])) {
|
|
unset($this->formulas[$size]);
|
|
}
|
|
}
|
|
|
|
$this->toast('Rumus berhasil dihapus.');
|
|
|
|
Flux::modals()->close();
|
|
}
|
|
|
|
protected function pushNotification()
|
|
{
|
|
$auth = [
|
|
'VAPID' => [
|
|
'subject' => config('app.url'),
|
|
'publicKey' => config('services.vapid.public_key'),
|
|
'privateKey' => config('services.vapid.private_key'),
|
|
],
|
|
];
|
|
|
|
$webPush = new WebPush($auth);
|
|
|
|
$payload = json_encode([
|
|
'title' => 'Penggajian',
|
|
'body' => 'Penggajian baru telah ditambahkan.',
|
|
]);
|
|
|
|
foreach (PushNotification::all() as $notification) {
|
|
$webPush->sendOneNotification(
|
|
Subscription::create(json_decode($notification->subscriptions, true)),
|
|
$payload,
|
|
['TTL' => 5000],
|
|
);
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.studio.master.formulas', [
|
|
'pageTitle' => 'Rumus',
|
|
]);
|
|
}
|
|
}
|