commit compras5 2911
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-29 19:49:19 +02:00
parent a935499d64
commit 96b30368bc
96 changed files with 3397 additions and 111 deletions
+3 -1
View File
@@ -4,4 +4,6 @@ Additive local development capability for integral issued-order cancellation. Do
The host must hold its company lock, authenticate an independent finance actor, validate the published policy, confirm absence of receipts/invoices/payments and open the encompassing transaction. The writer appends a reversal of the original wholly available commitment in the canonical budget_entries ledger. The host binds the reversal entry to its decision in the same transaction and audit.
MZN only. Verify source SHA-256 against config/order_cancellation.php before execution. No transport, bank operations, invoice cancellation, partial cancellation, activation or grants. The capability is disabled in the local pilot; validation uses disposable databases.
MZN only. The frozen legacy writer remains in `src`. The cycle-aware writer is versioned in `releases/0.2.0-rc.1`; `OrderCancellationRuntime` verifies its manifest, installation, schema and loaded class location before use. It reverses only the current open requisition cycle and preserves earlier cycles.
The versioned component is installed and explicitly enabled in the Compras local pilot under the owner's installation authorization of 29/11/2026. The original four-package financial installation is unchanged. No company cancellation policy, grant or business cancellation was created by installation. Native review and recovery use disposable synthetic databases. See `docs/proofs/order-cancellation-pilot-installation.json` and `order-cancellation-pilot-native-review.json`. No SGF installation, transport, bank operations or invoice cancellation is included.
@@ -0,0 +1,9 @@
{
"id": "tempo-order-cancellation",
"version": "0.2.0-rc.1",
"contract": "COMPRAS-ORDER-CANCELLATION-CYCLES-v1",
"legacy_source_sha256": "5471d9f826f47012415bf9a7086d4d7316ab8550b58e497f7432b94514a18bfa",
"files": {
"src/CommitmentReversal.php": "ecf0028a57b0280cf3a889da29a6e528c7eaa6a876e700b32ff59ac3b3eeb069"
}
}
@@ -0,0 +1,65 @@
<?php
namespace Tempo\Finance\OrderCancellation;
use Brick\Math\BigDecimal;
use Illuminate\Database\ConnectionInterface;
use LogicException;
use Tempo\Finance\Budget\BudgetContext;
/** Additive finance capability: the original commitment is never changed. */
final class CommitmentReversal
{
public function reverse(ConnectionInterface $db, BudgetContext $context, int $origin, int $order): ?int
{
if ($db->transactionLevel() < 1) {
throw new LogicException('Reversal requires the host transaction and company lock.');
}
$hasCycles = $db->getSchemaBuilder()->hasTable('budget_requisition_cycles');
if ($hasCycles !== $db->getSchemaBuilder()->hasColumn('budget_entries', 'requisition_cycle')) {
throw new LogicException('Incomplete budget cycle schema.');
}
$cycle = $context->requisitionCycles[$origin] ?? null;
if ($hasCycles) {
if ($cycle === null) {
throw new LogicException('Explicit order source cycle required.');
}
$head = $db->table('budget_requisition_cycles')->where($context->scope)->where('requisition_id', $origin)
->orderByDesc('cycle')->lockForUpdate()->first();
if (($head === null && $cycle !== 1) || ($head !== null && ((int) $head->cycle !== $cycle || $head->state !== 'open'))) {
throw new LogicException('Only the current open order source cycle can be reversed.');
}
} elseif (($cycle ?? 1) !== 1) {
throw new LogicException('Budget cycle schema required for resubmitted orders.');
}
$entries = $db->table('budget_entries')->where($context->scope)->where('requisition_id', $origin)
->when($hasCycles, fn ($query) => $query->where('requisition_cycle', $cycle))->orderBy('id')->lockForUpdate()->get();
if ($entries->isEmpty()) {
return null;
}
$commit = $entries->where('kind', 'commit');
if ($commit->count() !== 1) {
throw new LogicException('Exactly one original commitment required.');
}
$source = $commit->first();
$amount = BigDecimal::of((string) $source->committed_minor)->toInt();
$kind = 'order-cancel-'.$order;
$old = $entries->firstWhere('kind', $kind);
if ($old) {
if ((int) $old->committed_minor !== -$amount || (int) $old->reserved_minor !== 0 || $old->budget_id !== $source->budget_id) {
throw new LogicException('Invalid prior reversal.');
}
return (int) $old->id;
}
$budget = $db->table('budgets')->where($context->scope)->where('id', $source->budget_id)->lockForUpdate()->first();
if (! $budget || ! $budget->approved_by || $amount <= 0 || $entries->contains(fn ($e) => $e->budget_id !== $source->budget_id)
|| BigDecimal::of((string) $entries->sum('reserved_minor'))->toInt() !== 0
|| BigDecimal::of((string) $entries->sum('committed_minor'))->toInt() !== $amount) {
throw new LogicException('Commitment is not wholly available for reversal.');
}
return $db->table('budget_entries')->insertGetId([...$context->scope, ...($hasCycles ? ['requisition_cycle' => $cycle] : []), 'budget_id' => $source->budget_id, 'requisition_id' => $origin,
'created_by' => $context->actorId, 'kind' => $kind, 'reserved_minor' => 0, 'committed_minor' => -$amount, 'created_at' => now()]);
}
}