parfum/app/Livewire/Studio/Stock/StockTransfer/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.7 KiB
PHP

<?php
namespace App\Livewire\Studio\Stock\StockTransfer;
use App\Livewire\Forms\Studio\Stock\StockTransferForm;
use App\Models\Outlet;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\StockTransfer\WithStockTransferAddItem;
use App\Traits\StockTransfer\WithStockTransferDeleteItem;
use App\Traits\StockTransfer\WithStockTransferItems;
use App\Traits\StockTransfer\WithStockTransferUpdateItem;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Tambah Transfer Stok Antar Outlet')]
class Create extends Component
{
use WithAuthorization, WithConfirmation, WithStockTransferAddItem, WithStockTransferDeleteItem, WithStockTransferItems, WithStockTransferUpdateItem, WithToast, WithUpdatedData;
public StockTransferForm $form;
public array $sourceOutlets = [];
public array $destinationOutlets = [];
public array $perfumes = [];
public array $products = [];
public array $bottles = [];
public $stockTransferItems;
public function mount(): void
{
$this->sourceOutlets = auth()->user()->outlets->pluck('name', 'id')->toArray();
$this->destinationOutlets = Outlet::orderBy('name')->pluck('name', 'id')->toArray();
$this->stockTransferItems = $this->loadStockTransferItems();
}
public function updated($property, $value): void
{
if ($property === 'form.source_outlet_id' || $property === 'form.destination_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->source_outlet_id || ! $this->form->destination_outlet_id) {
return;
}
$sourceOutlet = Outlet::find($this->form->source_outlet_id);
if (! $sourceOutlet) {
return;
}
$this->perfumes = $sourceOutlet->perfumes()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
$this->products = $sourceOutlet->products()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
$this->bottles = $sourceOutlet->bottles()
->wherePivot('stock', '>', 0)
->orderBy('name')
->get()
->pluck('name', 'id')
->toArray();
}
public function save(): void
{
$this->canOrAbort('create stock transfer');
if ($this->stockTransferItems->isEmpty()) {
$this->toast('Keranjang transfer stok 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.stock_transfer.index', [
'notification' => 'Transfer stok berhasil diproses.',
], navigate: true);
}
public function render(): View
{
return view('livewire.studio.stock.stock-transfer.form', [
'pageTitle' => 'Tambah Transfer Stok Antar Outlet',
]);
}
}