parfum/app/Traits/StockTransfer/WithStockTransferUpdateItem.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

85 lines
2.4 KiB
PHP

<?php
namespace App\Traits\StockTransfer;
use App\Models\Bottle;
use App\Models\Outlet;
use App\Models\Perfume;
use App\Models\Product;
use App\Models\StockTransferItem;
use Livewire\Attributes\On;
trait WithStockTransferUpdateItem
{
public string $modalTitle = '';
public function updateItem(): void
{
$this->canOrAbort('create stock transfer');
if (! $this->form->source_outlet_id) {
$this->toast('Silakan pilih outlet asal terlebih dahulu.', 'Gagal', 'warning');
return;
}
$sourceOutlet = Outlet::find($this->form->source_outlet_id);
if (! $sourceOutlet) {
$this->toast('Outlet asal tidak ditemukan.', 'Gagal', 'danger');
return;
}
$item = StockTransferItem::find($this->form->item_id);
if ($item) {
$newQuantity = parseRupiahToInt($this->form->quantity_edit);
if ($newQuantity <= 0) {
$this->toast('Jumlah harus lebih dari 0.', 'Gagal', 'danger');
return;
}
$relationName = match ($item->transferable_type) {
Perfume::class => 'perfumes',
Product::class => 'products',
Bottle::class => 'bottles',
default => null,
};
if ($relationName) {
$sourceStock = $sourceOutlet->{$relationName}()
->where($item->transferable_type::make()->getTable().'.id', $item->transferable_id)
->first()
?->pivot
?->stock ?? 0;
if ($newQuantity > $sourceStock) {
$this->toast("Stok tidak mencukupi (Update). Stok di outlet asal: {$sourceStock}.", 'Gagal', 'danger');
return;
}
}
$item->update([
'quantity' => $newQuantity,
]);
}
$this->stockTransferItems = $this->loadStockTransferItems();
$this->toast('Item berhasil diperbarui.');
$this->dispatch('modal:close', 'form-modal');
}
#[On('modal:open')]
public function openModal($item): void
{
$item = StockTransferItem::find($item['item']);
$this->form->item_id = $item->id;
$this->form->quantity_edit = formatCurrencyNumber($item->quantity, '');
$this->modalTitle = $item->transferable->name;
}
}