parfum/app/Livewire/Forms/Studio/Stock/StockTransferForm.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

130 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Forms\Studio\Stock;
use App\Models\StockTransfer;
use App\Traits\Media\WithMediaHandler;
use App\Traits\StockTransfer\WithStockTransferItems;
use App\Traits\StockTransfer\WithStockTransferStock;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;
class StockTransferForm extends Form
{
use WithMediaHandler, WithStockTransferItems, WithStockTransferStock;
public ?StockTransfer $stockTransfer = null;
public ?string $note = null;
public string $transfer_date = '';
public string $source_outlet_id = '';
public string $destination_outlet_id = '';
public array $image = [];
public string $perfume_id = '';
public string $quantity_perfume = '';
public string $product_id = '';
public string $quantity_product = '';
public string $bottle_id = '';
public string $quantity_bottle = '';
public string $item_id = '';
public string $quantity_edit = '';
public function rules(): array
{
return [
'note' => ['nullable', 'string', 'max:100'],
'transfer_date' => ['required', 'date', 'before_or_equal:today'],
'source_outlet_id' => ['required', Rule::exists('outlets', 'id')],
'destination_outlet_id' => ['required', Rule::exists('outlets', 'id'), 'different:source_outlet_id'],
'image' => ['nullable', 'array', 'max:1'],
];
}
public function validationAttributes(): array
{
return [
'note' => 'catatan',
'transfer_date' => 'tanggal transfer',
'source_outlet_id' => 'outlet asal',
'destination_outlet_id' => 'outlet tujuan',
'image' => 'gambar',
];
}
public function setStockTransfer(StockTransfer $stockTransfer): void
{
$this->stockTransfer = $stockTransfer;
$this->note = $stockTransfer->note;
$this->transfer_date = $stockTransfer->transfer_date;
$this->source_outlet_id = $stockTransfer->source_outlet_id;
$this->destination_outlet_id = $stockTransfer->destination_outlet_id;
$this->image = $this->mapMediaCollection($stockTransfer->getMedia('image'));
}
public function store(): void
{
$this->validate();
$stockTransferItems = $this->loadStockTransferItems();
DB::transaction(function () use ($stockTransferItems) {
$data = $this->prepareSavedData();
$stockTransfer = StockTransfer::create($data);
$stockTransfer->load(['sourceOutlet', 'destinationOutlet']);
$this->uploadMedia($this->image, $stockTransfer, 'image');
foreach ($stockTransferItems as $item) {
if ($item->quantity <= 0) {
continue;
}
$item->update(['stock_transfer_id' => $stockTransfer->id]);
$this->transferStockBetweenOutlets($stockTransfer->sourceOutlet, $stockTransfer->destinationOutlet, $item);
}
});
}
public function update(): void
{
$this->validate();
DB::transaction(function () {
$data = $this->prepareSavedData();
$this->stockTransfer->update($data);
$this->syncMedia($this->image, $this->stockTransfer, 'image');
$this->uploadMedia($this->image, $this->stockTransfer, 'image');
});
}
private function prepareSavedData(): array
{
return [
'note' => $this->note,
'source_outlet_id' => $this->source_outlet_id,
'destination_outlet_id' => $this->destination_outlet_id,
'transfer_date' => $this->transfer_date,
];
}
}