76 lines
4.4 KiB
PHP
76 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\GoodsReceipt;
|
|
use App\Models\GoodsReceiptItem;
|
|
use App\Models\StockItem;
|
|
use App\Models\SupplyRecord;
|
|
use App\Models\User;
|
|
use App\Models\Warehouse;
|
|
use App\Services\InventoryDispositions;
|
|
use App\Services\InventoryLots;
|
|
use App\Services\InventoryReversals;
|
|
use App\Services\SupplyPlanning;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class InventoryDispositionController extends Controller
|
|
{
|
|
public function index(Request $request, InventoryDispositions $workflow): Response|RedirectResponse
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
$capabilities = [];
|
|
foreach (['read', 'prepare', 'review', 'execute'] as $capability) {
|
|
$capabilities[$capability] = $workflow->allows($actor, $capability);
|
|
}
|
|
$canViewReversals = collect(['read', 'prepare', 'review', 'execute'])->contains(fn ($a) => app(InventoryReversals::class)->allows($actor, $a));
|
|
if (! collect($capabilities)->contains(fn (bool $allow) => $allow) && $canViewReversals) {
|
|
return redirect('/stock/estornos');
|
|
}
|
|
abort_unless(collect($capabilities)->contains(fn (bool $allow) => $allow), 403);
|
|
$ready = Schema::hasTable('supply_records');
|
|
$records = $ready ? app(SupplyPlanning::class)->latest('stock_event')->latest('id')->limit(100)->get() : collect();
|
|
$receipts = [];
|
|
$lots = [];
|
|
if ($ready && $capabilities['prepare']) {
|
|
foreach (GoodsReceiptItem::query()->whereNotNull('stock_item_id')->where('accepted_milli', '>', 0)->latest('id')->limit(200)->get() as $line) {
|
|
$receipt = GoodsReceipt::query()->whereKey($line->goods_receipt_id)->firstOrFail();
|
|
$receipts[] = ['id' => $line->id, 'stock_item_id' => $line->stock_item_id, 'warehouse_id' => $receipt->warehouse_id, 'accepted_milli' => $line->accepted_milli,
|
|
'returned_milli' => $workflow->returnedForReceipt($line->id), 'label' => $receipt->number.' · '.$receipt->snapshot['supplier']['name'].' · '.$line->snapshot['stock_item']['name']];
|
|
}
|
|
foreach (app(InventoryLots::class)->activeSetups()->get() as $setup) {
|
|
$context = app(InventoryLots::class)->context($setup->stock_item_id, $setup->warehouse_id);
|
|
foreach ($context['lots'] ?? [] as $lot) {
|
|
if ($lot['quantity_milli'] > 0) {
|
|
$lots[] = ['id' => $lot['id'], 'stock_item_id' => $setup->stock_item_id, 'warehouse_id' => $setup->warehouse_id, 'code' => $lot['data']['code'], 'quantity_milli' => $lot['quantity_milli'],
|
|
'receipt_item_id' => $lot['origin']['receipt_item_id'] ?? null, 'reconciled' => $context['reconciled']];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Inertia::render('stock/dispositions', ['ready' => $ready, 'actorId' => $actor->id, 'today' => today()->toDateString(), 'capabilities' => $capabilities, 'records' => $records,
|
|
'canViewOperations' => $actor->hasFunctions(['admin', 'warehouse', 'procurement', 'finance', 'inventory_adjust_review']),
|
|
'canViewReversals' => $canViewReversals,
|
|
'correctedOperations' => collect(app(InventoryReversals::class)->corrections('operation'))->only($records->pluck('data.stock_operation_id')->filter()->all())->map(fn ($r) => $r->data['reference']),
|
|
'items' => $capabilities['prepare'] ? StockItem::query()->orderBy('code')->get(['id', 'code', 'name', 'unit']) : [],
|
|
'warehouses' => $capabilities['prepare'] ? Warehouse::query()->orderBy('code')->get(['id', 'code', 'name']) : [], 'receipts' => $receipts, 'lots' => $lots,
|
|
'history' => $ready ? SupplyRecord::query()->where('kind', 'stock_event')->whereIn('record_key', $records->pluck('record_key'))->latest('id')->get()->groupBy('record_key') : []]);
|
|
}
|
|
|
|
public function save(Request $request, InventoryDispositions $workflow): RedirectResponse
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
abort_unless(Schema::hasTable('supply_records'), 503, 'Instalação do diário de stock pendente.');
|
|
$workflow->save($actor, $request->all());
|
|
|
|
return back()->with('success', 'Processo de stock registado.');
|
|
}
|
|
}
|