parfum/app/Livewire/Forms/Studio/Master/Warehouse/AdjustmentStockForm.php

98 lines
2.5 KiB
PHP

<?php
namespace App\Livewire\Forms\Studio\Master\Warehouse;
use App\Models\Warehouse;
use App\Services\StockActivityLogService;
use Livewire\Form;
class AdjustmentStockForm extends Form
{
public string $itemId = '';
public string $type = '';
public string $name = '';
public string $oldStock = '0';
public string $newStock = '0';
public ?string $note = null;
public function rules(): array
{
return [
'newStock' => 'required',
'note' => 'nullable',
];
}
public function validationAttributes(): array
{
return [
'newStock' => 'stok baru',
'note' => 'keterangan',
];
}
public function setAdjustment(string $itemId, string $type, string $name, int $stock): void
{
$this->itemId = $itemId;
$this->type = $type;
$this->name = $name;
$this->oldStock = formatCurrencyNumber($stock);
$this->newStock = formatCurrencyNumber($stock);
$this->note = null;
}
public function save(Warehouse $warehouse): void
{
$this->validate();
$newStockValue = (int) str_replace(['.', ','], '', (string) $this->newStock);
$oldStockValue = (int) str_replace(['.', ','], '', (string) $this->oldStock);
$typeId = explode('_', $this->itemId);
$type = $typeId[0];
$id = $typeId[1];
$relation = match ($type) {
'perfume' => 'perfumes',
'product' => 'products',
'bottle' => 'bottles',
default => null,
};
if (! $relation) {
return;
}
$item = $warehouse->{$relation}()->find($id);
if (! $item) {
return;
}
$warehouse->{$relation}()->updateExistingPivot($id, [
'stock' => $newStockValue,
]);
// Log the activity
StockActivityLogService::logWarehouse(
logName: 'adjustment',
event: 'update',
description: "Penyesuaian manual untuk {$this->name}: berubah dari {$oldStockValue} menjadi {$newStockValue}. Catatan: ".($this->note ?? '-'),
performedOn: $item,
warehouse: $warehouse,
quantityChange: $newStockValue - $oldStockValue,
previousStock: $oldStockValue,
newStock: $newStockValue,
extraProperties: [
'note' => $this->note,
'adjustment_type' => 'manual',
]
);
}
}