89 lines
3.8 KiB
PHP
89 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\PurchaseAward;
|
|
use App\Models\PurchaseOrderItem;
|
|
use App\Models\Quotation;
|
|
use App\Models\Requisition;
|
|
use App\Models\User;
|
|
use App\Services\OrderPartsWorkflow;
|
|
use App\Services\OrganizationAccess;
|
|
use App\Services\ReceiptWorkflow;
|
|
use Brick\Math\BigDecimal;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class OrderPartsController extends Controller
|
|
{
|
|
private function actor(Request $request): User
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User && $actor->hasFunctions([PurchasingRole::Procurement])
|
|
&& ! app(OrganizationAccess::class)->maintenanceOnly($actor), 403);
|
|
abort_unless(app(OrderPartsWorkflow::class)->ready(), 503);
|
|
|
|
return $actor;
|
|
}
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
return $this->page($this->actor($request));
|
|
}
|
|
|
|
public function preview(Request $request, OrderPartsWorkflow $workflow): Response
|
|
{
|
|
$actor = $this->actor($request);
|
|
|
|
return $this->page($actor, $workflow->preview($actor, $request->all()));
|
|
}
|
|
|
|
/** @param array<string,mixed>|null $preview */
|
|
private function page(User $actor, ?array $preview = null): Response
|
|
{
|
|
$candidates = PurchaseAward::query()->where('status', 'approved')
|
|
->whereIn('requisition_id', Requisition::query()->visibleTo($actor)->select('id'))
|
|
->whereExists(fn ($lines) => $lines->selectRaw('1')->from('quotation_items as qi')
|
|
->whereColumn('qi.quotation_id', 'purchase_awards.quotation_id')->whereColumn('qi.tenant_id', 'purchase_awards.tenant_id')
|
|
->whereRaw('ROUND(qi.quantity * 1000) > (select COALESCE(SUM(ROUND(oi.quantity * 1000)), 0) from purchase_order_items oi where oi.requisition_item_id = qi.requisition_item_id and oi.tenant_id = qi.tenant_id)'))
|
|
->latest('id')->paginate(20)
|
|
->through(function (PurchaseAward $award): array {
|
|
$quote = Quotation::query()->with(['items', 'supplier'])->whereKey($award->quotation_id)->firstOrFail();
|
|
$request = Requisition::query()->whereKey($award->requisition_id)->firstOrFail();
|
|
$items = [];
|
|
foreach ($quote->items as $line) {
|
|
$before = 0;
|
|
foreach (PurchaseOrderItem::query()->where('requisition_item_id', $line->requisition_item_id)->get(['quantity']) as $old) {
|
|
$before += ReceiptWorkflow::milli($old->quantity);
|
|
}
|
|
$remaining = max(0, ReceiptWorkflow::milli($line->quantity) - $before);
|
|
$items[] = ['id' => $line->id, 'description' => $line->description, 'remaining' => (string) BigDecimal::of($remaining)->dividedBy(1000, 3)];
|
|
}
|
|
|
|
return ['id' => $award->id, 'requisition_id' => $request->id, 'number' => $request->number,
|
|
'supplier' => $quote->supplier?->name, 'currency' => $quote->currency, 'items' => $items];
|
|
});
|
|
Inertia::encryptHistory();
|
|
|
|
return Inertia::render('purchases/parts', ['candidates' => $candidates, 'preview' => $preview]);
|
|
}
|
|
|
|
public function issue(Request $request, OrderPartsWorkflow $workflow): RedirectResponse
|
|
{
|
|
$order = $workflow->issue($this->actor($request), $request->all());
|
|
|
|
return to_route('orders.show', $order);
|
|
}
|
|
|
|
public function adopt(Request $request, PurchaseAward $award, OrderPartsWorkflow $workflow): RedirectResponse
|
|
{
|
|
$data = $request->validate(['operation_key' => 'required|uuid']);
|
|
$workflow->adopt($this->actor($request), $award, $data['operation_key']);
|
|
|
|
return to_route('orders.parts');
|
|
}
|
|
}
|