54 lines
2.4 KiB
PHP
54 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\StockItem;
|
|
use App\Models\SupplyRecord;
|
|
use App\Models\User;
|
|
use App\Models\Warehouse;
|
|
use App\Services\InventoryLots;
|
|
use App\Services\SupplyPlanning;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class InventoryLotController extends Controller
|
|
{
|
|
public function index(Request $request, InventoryLots $lots): Response
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
$capabilities = [];
|
|
foreach (['read', 'prepare', 'review', 'hold', 'release'] as $capability) {
|
|
$capabilities[$capability] = $lots->allows($actor, 'inventory_lot_'.$capability);
|
|
}
|
|
abort_unless(collect($capabilities)->contains(fn (bool $allowed) => $allowed), 403);
|
|
$ready = Schema::hasTable('inventory_lot_allocations');
|
|
$records = $ready ? app(SupplyPlanning::class)->latest('lot_setup')->latest('id')->limit(100)->get() : collect();
|
|
if ($ready) {
|
|
$records = $records->concat($lots->activeSetups()->latest('id')->limit(100)->get())->unique('id')->sortByDesc('id')->values();
|
|
}
|
|
$contexts = [];
|
|
foreach ($records->where('state', 'active') as $record) {
|
|
$contexts[$record->record_key] = $lots->context($record->stock_item_id, $record->warehouse_id);
|
|
}
|
|
|
|
return Inertia::render('stock/lots', ['ready' => $ready, 'actorId' => $actor->id, 'capabilities' => $capabilities, 'records' => $records, 'contexts' => $contexts,
|
|
'items' => $capabilities['prepare'] ? StockItem::query()->orderBy('code')->get(['id', 'code', 'name', 'unit']) : [],
|
|
'warehouses' => $capabilities['prepare'] ? Warehouse::query()->orderBy('code')->get(['id', 'code', 'name']) : [],
|
|
'history' => $ready ? SupplyRecord::query()->whereIn('kind', ['lot_setup', 'lot', 'lot_state'])->latest('id')->limit(200)->get() : []]);
|
|
}
|
|
|
|
public function save(Request $request, InventoryLots $lots): RedirectResponse
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
abort_unless(Schema::hasTable('inventory_lot_allocations'), 503, 'Instalação do controlo por lotes pendente.');
|
|
$lots->save($actor, $request->all());
|
|
|
|
return back()->with('success', 'Conferência de lotes registada.');
|
|
}
|
|
}
|