parfum/app/Livewire/Studio/Manage/StockTransfer/Create.php
Yoga Pangestu dffc9f9e72 feat: add stock transfer management functionality
- Implemented stock transfer creation and management with Livewire components.
- Created StockTransfer and StockTransferItem models with necessary relationships.
- Added migration files for stock_transfers and stock_transfer_items tables.
- Developed traits for handling stock transfer items (add, delete, update).
- Integrated stock transfer logging in StockActivityLogService.
- Updated ViewServiceProvider to include stock transfer permissions and routes.
- Created frontend views for stock transfer form and index with appropriate UI components.
- Added role permissions for stock transfer actions in RolePermissionSeeder.
2026-01-09 21:41:49 +07:00

131 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Studio\Manage\StockTransfer;
use App\Livewire\Forms\Studio\Manage\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.manage.stock_transfer.index', [
'notification' => 'Transfer stok berhasil diproses.',
], navigate: true);
}
public function render(): View
{
return view('livewire.studio.manage.stock-transfer.form', [
'pageTitle' => 'Tambah Transfer Stok Antar Outlet',
]);
}
}