127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Studio\Manage\Order;
|
|
|
|
use App\Jobs\StorePushNotification;
|
|
use App\Livewire\Forms\Studio\Manage\OrderForm;
|
|
use App\Models\Bottle;
|
|
use App\Models\Perfume;
|
|
use App\Models\Product;
|
|
use App\Models\PushNotification;
|
|
use App\Models\User;
|
|
use App\Traits\Authorization\WithAuthorization;
|
|
use App\Traits\Components\WithConfirmation;
|
|
use App\Traits\Components\WithToast;
|
|
use App\Traits\Order\WithAddItem;
|
|
use App\Traits\Order\WithCalculateTotal;
|
|
use App\Traits\Order\WithDiscount;
|
|
use App\Traits\Order\WithManageItem;
|
|
use App\Traits\Order\WithMember;
|
|
use App\Traits\Order\WithOrderItem;
|
|
use App\Traits\Order\WithUpdateItem;
|
|
use App\Traits\Utilities\WithUpdatedData;
|
|
use Illuminate\Contracts\View\View;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
|
|
#[Title('Tambah Order')]
|
|
class Create extends Component
|
|
{
|
|
use WithAddItem, WithAuthorization, WithCalculateTotal, WithConfirmation, WithDiscount, WithManageItem, WithMember, WithOrderItem, WithToast, WithUpdatedData, WithUpdateItem;
|
|
|
|
public OrderForm $form;
|
|
|
|
public array $outlets = [];
|
|
|
|
public array $perfumes = [];
|
|
|
|
public array $bottles = [];
|
|
|
|
public array $qualities = [];
|
|
|
|
public array $products = [];
|
|
|
|
public string $subtotal = '';
|
|
|
|
public int $voucherDiscount = 0;
|
|
|
|
public string $total = '';
|
|
|
|
public $items;
|
|
|
|
public array $vouchers = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->outlets = auth()->user()->outlets->pluck('name', 'id')->toArray();
|
|
|
|
// Set default outlet if only one available
|
|
if (count($this->outlets) === 1) {
|
|
$this->form->outlet_id = key($this->outlets);
|
|
}
|
|
|
|
$this->perfumes = Perfume::whereHas('outlets', fn ($query) => $query->where('outlets.id', $this->form->outlet_id))->orderBy('name')->pluck('name', 'id')->toArray();
|
|
|
|
$this->bottles = Bottle::whereHas('outlets', fn ($query) => $query->where('outlets.id', $this->form->outlet_id))->get()->pluck('display_name', 'id')->toArray();
|
|
|
|
$this->products = Product::whereHas('outlets', fn ($query) => $query->where('outlets.id', $this->form->outlet_id))->orderBy('name')->pluck('name', 'id')->toArray();
|
|
|
|
$this->items = $this->loadOrderItems();
|
|
|
|
$this->subtotal = $this->getSubTotal();
|
|
|
|
$this->total = $this->getTotal();
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$this->canOrAbort('create order');
|
|
|
|
if ($this->items->isEmpty()) {
|
|
$this->toast('Keranjang tidak boleh kosong.', 'Gagal', 'danger');
|
|
|
|
return;
|
|
}
|
|
|
|
$result = $this->form->store();
|
|
|
|
if (! $result['status']) {
|
|
$this->toast($result['message'], 'Gagal', 'danger');
|
|
|
|
return;
|
|
}
|
|
|
|
$userIds = User::role(['Developer', 'Owner', 'Leader'])->pluck('id')->toArray();
|
|
StorePushNotification::dispatch(
|
|
PushNotification::whereIn('user_id', $userIds)->get(),
|
|
[
|
|
'title' => '🛒 Order Baru!',
|
|
'body' => 'Ada order baru masuk dengan nominal '.formatCurrencyNumber($result['data']['total'], 'Rp').' ✨',
|
|
'url' => route('studio.manage.order.index'),
|
|
],
|
|
);
|
|
|
|
$this->dispatch('refreshDatatable');
|
|
|
|
$this->toast('Order berhasil ditambahkan.');
|
|
|
|
$this->redirectRoute('studio.manage.order.index', [
|
|
'order' => $result['data']->id,
|
|
]);
|
|
}
|
|
|
|
public function updatedFormOutletId($value): void
|
|
{
|
|
$this->perfumes = Perfume::whereHas('outlets', fn ($query) => $query->where('outlets.id', $value))->orderBy('name')->pluck('name', 'id')->toArray();
|
|
$this->bottles = Bottle::whereHas('outlets', fn ($query) => $query->where('outlets.id', $value))->get()->pluck('display_name', 'id')->toArray();
|
|
$this->products = Product::whereHas('outlets', fn ($query) => $query->where('outlets.id', $value))->orderBy('name')->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.studio.manage.order.form', [
|
|
'pageTitle' => 'Tambah Order',
|
|
]);
|
|
}
|
|
}
|