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

130 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Forms\Studio\Manage;
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,
];
}
}