81 lines
6.2 KiB
PHP
81 lines
6.2 KiB
PHP
<?php
|
|
|
|
use App\Models\ExternalPayment;
|
|
use App\Models\GoodsReceipt;
|
|
use App\Models\Invoice;
|
|
use App\Models\PurchaseAward;
|
|
use App\Models\PurchaseOrder;
|
|
use App\Models\Quotation;
|
|
use App\Models\Requisition;
|
|
use App\Models\StockItem;
|
|
use App\Models\User;
|
|
use App\Services\ExternalPaymentWorkflow;
|
|
use App\Services\ProcurementPaymentInsights;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/** Assertion-only projection; every business mutation in the office journey must use native HTTP. */
|
|
function nativeOfficePurchaseState(array $fixture): array
|
|
{
|
|
$office = $fixture['office_journey'] ?? null;
|
|
if (! app()->environment('testing') || getenv('RQ01_NATIVE_OFFICE_JOURNEY') !== '1' || ! is_array($office)
|
|
|| DB::transactionLevel() !== 1 || DB::selectOne('SHOW transaction_read_only')->transaction_read_only !== 'on') {
|
|
throw new LogicException('Owned office journey assertions require a top-level read-only transaction.');
|
|
}
|
|
$tenant = $fixture['tenant_id'];
|
|
$actor = User::query()->where('tenant_id', $tenant)->findOrFail($office['actors']['reviewer']['id']);
|
|
auth()->guard()->setUser($actor);
|
|
config(['common-payables.enabled' => true, 'common-payables.source_instance' => $office['source_instance'], 'expense-allocations.qa_enabled' => true]);
|
|
$requests = Requisition::query()->where('requester_id', $office['actors']['owner']['id'])->get();
|
|
if ($requests->count() > 1) {
|
|
throw new LogicException('The dedicated HTTP requester has more than one synthetic source.');
|
|
}
|
|
$request = $requests->first();
|
|
$budget = DB::table('budgets')->where('tenant_id', $tenant)->where('cost_center_id', $office['cost_center_id'])->where('kind', 'operating')->first();
|
|
$quotes = $request ? Quotation::query()->where('requisition_id', $request->id)->orderBy('id')->get() : collect();
|
|
$award = $request ? PurchaseAward::query()->where('requisition_id', $request->id)->first() : null;
|
|
$order = $award ? PurchaseOrder::query()->where('purchase_award_id', $award->id)->first() : null;
|
|
$invoice = $order ? Invoice::query()->where('purchase_order_id', $order->id)->first() : null;
|
|
$payments = $invoice ? ExternalPayment::query()->where('invoice_id', $invoice->id)->orderBy('id')->get() : collect();
|
|
$stock = StockItem::query()->where('code', 'HTTP-OFFICE-QA')->first();
|
|
$bank = $order ? DB::table('supplier_bank_details')->where('tenant_id', $tenant)->where('supplier_id', $order->supplier_id)->orderByDesc('id')->first(['id', 'revision']) : null;
|
|
$frames = [];
|
|
foreach (DB::select("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename") as $table) {
|
|
$name = $table->tablename;
|
|
if ($name !== 'budgets' && ! str_starts_with($name, 'budget_') && ! str_starts_with($name, 'gl_') && ! str_starts_with($name, 'pa_')
|
|
&& ! in_array($name, ['purchase_orders', 'purchase_order_items', 'purchase_order_sources', 'invoices', 'invoice_items', 'external_payments', 'stock_movements'], true)) {
|
|
continue;
|
|
}
|
|
$quoted = str_replace('"', '""', $name);
|
|
$rows = DB::select('SELECT row_to_json(t)::text AS row FROM "'.$quoted.'" t ORDER BY row_to_json(t)::text');
|
|
$frames[$name] = hash('sha256', json_encode([DB::getSchemaBuilder()->getColumnListing($name), array_column($rows, 'row')], JSON_THROW_ON_ERROR));
|
|
}
|
|
$analytics = $request && $payments->where('status', 'confirmed')->isNotEmpty()
|
|
? app(ProcurementPaymentInsights::class)->build($actor, Requisition::query()->whereKey($request->id), null, null) : null;
|
|
|
|
return [
|
|
'budget' => $budget ? ['id' => $budget->id, 'created_by' => $budget->created_by, 'approved_by' => $budget->approved_by] : null,
|
|
'request' => $request ? ['id' => $request->id, 'status' => $request->status, 'process_version' => $request->process_version,
|
|
'approval_cycle' => $request->approval_cycle, 'total_minor' => $request->total_minor,
|
|
'items' => $request->items()->orderBy('id')->get(['id', 'category'])->toArray(),
|
|
'steps' => $request->steps()->orderBy('position')->get(['id', 'role', 'decision'])->toArray()] : null,
|
|
'suppliers' => DB::table('suppliers')->where('tenant_id', $tenant)->whereIn('nuit', ['300000001', '300000002', '300000003'])
|
|
->orderBy('id')->get(['id', 'nuit', 'version'])->map(fn ($row) => (array) $row)->all(),
|
|
'quotes' => $quotes->map(fn ($quote) => ['id' => $quote->id, 'supplier_id' => $quote->supplier_id, 'total_minor' => $quote->total_minor])->all(),
|
|
'award' => $award ? ['id' => $award->id, 'status' => $award->status,
|
|
'steps' => $award->approvals()->orderBy('position')->get(['id', 'role', 'decision'])->toArray()] : null,
|
|
'order' => $order ? ['id' => $order->id, 'total_minor' => $order->total_minor,
|
|
'items' => $order->items()->orderBy('id')->get(['id', 'quantity'])->toArray(),
|
|
'account_assignments' => $order->snapshot['account_assignments'] ?? null,
|
|
'receipts' => GoodsReceipt::query()->where('purchase_order_id', $order->id)->orderBy('id')->get(['id', 'status'])->toArray()] : null,
|
|
'stock' => $stock ? ['id' => $stock->id, 'quantity_milli' => (int) DB::table('stock_movements')->where('stock_item_id', $stock->id)->sum('quantity_milli'),
|
|
'movements' => DB::table('stock_movements')->where('stock_item_id', $stock->id)->count()] : null,
|
|
'invoice' => $invoice ? ['id' => $invoice->id, 'status' => $invoice->status, 'total_minor' => $invoice->total_minor,
|
|
'outstanding_minor' => app(ExternalPaymentWorkflow::class)->outstanding($invoice)] : null,
|
|
'bank' => $bank ? ['id' => $bank->id, 'revision' => $bank->revision,
|
|
'approved_reviews' => DB::table('supplier_bank_reviews')->where('supplier_bank_detail_id', $bank->id)->where('decision', 'approved')->count()] : null,
|
|
'payments' => $payments->map(fn ($payment) => ['id' => $payment->id, 'amount_minor' => $payment->amount_minor, 'status' => $payment->status])->all(),
|
|
'analytics' => $analytics === null ? null : array_intersect_key($analytics, array_flip(['currencies', 'coverage', 'category_attribution'])),
|
|
'journals' => DB::table('gl_journals')->count(), 'financial_fingerprint' => hash('sha256', json_encode($frames, JSON_THROW_ON_ERROR)),
|
|
];
|
|
}
|