parfum/app/Livewire/Studio/Stock/Restock/Create.php
Yoga Pangestu 293dd7d1fd refactor: restructure stock management components and update routes
- Removed legacy stock management components including PurchaseForm, RestockForm, StockOpnameForm, and StockTransferForm.
- Updated routes to reflect new structure under 'studio.stock' namespace for better organization.
- Adjusted view files for stock management to align with the new routing and component structure.
- Modified action routes in the ViewServiceProvider to ensure proper access control and navigation.
- Cleaned up related test files to remove references to deleted components.
2026-01-09 21:57:06 +07:00

131 lines
3.4 KiB
PHP

<?php
namespace App\Livewire\Studio\Stock\Restock;
use App\Livewire\Forms\Studio\Stock\RestockForm;
use App\Models\Warehouse;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\Restock\WithRestockAddItem;
use App\Traits\Restock\WithRestockDeleteItem;
use App\Traits\Restock\WithRestockItems;
use App\Traits\Restock\WithRestockUpdateItem;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Tambah Restock Toko')]
class Create extends Component
{
use WithAuthorization, WithConfirmation, WithRestockAddItem, WithRestockDeleteItem, WithRestockItems, WithRestockUpdateItem, WithToast, WithUpdatedData;
public RestockForm $form;
public array $warehouses = [];
public array $outlets = [];
public array $perfumes = [];
public array $products = [];
public array $bottles = [];
public $restockItems;
public function mount(): void
{
$this->warehouses = Warehouse::orderBy('name')->pluck('name', 'id')->toArray();
$this->outlets = auth()->user()->outlets->pluck('name', 'id')->toArray();
$this->restockItems = $this->loadRestockItems();
}
public function updated($property, $value): void
{
if ($property === 'form.warehouse_id' || $property === 'form.outlet_id') {
$this->loadCreateItems();
}
}
private function loadCreateItems(): void
{
$this->reset(['perfumes', 'products', 'bottles']);
$this->form->reset(
'perfume_id',
'quantity_perfume',
'product_id',
'quantity_product',
'bottle_id',
'quantity_bottle'
);
if (! $this->form->warehouse_id || ! $this->form->outlet_id) {
return;
}
$warehouse = Warehouse::find($this->form->warehouse_id);
if (! $warehouse) {
return;
}
$this->perfumes = $warehouse->perfumes()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
$this->products = $warehouse->products()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
$this->bottles = $warehouse->bottles()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
}
public function save(): void
{
$this->canOrAbort('create restock');
if ($this->restockItems->isEmpty()) {
$this->toast('Keranjang restock tidak boleh kosong.', 'Gagal', 'danger');
return;
}
try {
$this->form->store();
} catch (ValidationException $e) {
throw $e;
} catch (\Exception $e) {
$this->toast($e->getMessage(), 'Gagal', 'danger');
return;
}
$this->redirectRoute('studio.stock.restock.index', [
'notification' => 'Restock berhasil diproses.',
], navigate: true);
}
public function render(): View
{
return view('livewire.studio.stock.restock.form', [
'pageTitle' => 'Tambah Restock Toko',
]);
}
}