@@ -2,14 +2,20 @@
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\Requisition;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\SupplyRecord;
|
||||
use App\Models\User;
|
||||
use App\Services\CommonBudget;
|
||||
use App\Services\ImportBankSupport;
|
||||
use App\Services\ImportCostDocumentHealth;
|
||||
use App\Services\ImportDossier;
|
||||
use App\Services\ImportEstimates;
|
||||
use App\Services\ImportLandedCosts;
|
||||
use App\Services\ImportLegalEstimates;
|
||||
use App\Services\ImportLegalRules;
|
||||
use App\Services\ImportParties;
|
||||
use App\Services\ImportTariffCodes;
|
||||
use App\Services\SupplyPlanning;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Database\Connection;
|
||||
@@ -34,10 +40,15 @@ if (! $app->environment('testing') || $db->getDriverName() !== 'pgsql' || $db->g
|
||||
}
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
require_once __DIR__.'/../Support/SupplyFixture.php';
|
||||
require_once __DIR__.'/../Support/ImportBankQaFixture.php';
|
||||
require_once __DIR__.'/../Support/ImportLegalQaFixture.php';
|
||||
require_once __DIR__.'/../Support/BudgetDistributionSchema.php';
|
||||
$orderPartsAmendment = ($argv[2] ?? '') === 'order-parts-amendment';
|
||||
$orderParts = $orderPartsAmendment || ($argv[2] ?? '') === 'order-parts';
|
||||
$importEstimates = ($argv[2] ?? '') === 'import-estimates';
|
||||
$legalAmounts = ($argv[2] ?? '') === 'import-legal-amount';
|
||||
$legalRules = $legalAmounts || ($argv[2] ?? '') === 'import-legal';
|
||||
$bankSupport = $legalRules || ($argv[2] ?? '') === 'import-bank-support';
|
||||
$importEstimates = $bankSupport || ($argv[2] ?? '') === 'import-estimates';
|
||||
$landedCosts = $importEstimates || ($argv[2] ?? '') === 'landed-costs';
|
||||
$f = receiptFixture();
|
||||
$author = User::factory()->create(['tenant_id' => $f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
@@ -192,6 +203,51 @@ if ($landedCosts) {
|
||||
app(ImportParties::class)->select($partyBuyer, $nativeImport, [...$partyInput, 'operation_key' => (string) Str::uuid(),
|
||||
'version' => 1, 'instruction_document_id' => $instructionsCorrection->id]);
|
||||
$nativeEvidenceRecords = [$nativeDoc, $correction, $typedFirst, $typedCorrection, $instructions, $instructionsCorrection];
|
||||
if ($bankSupport) {
|
||||
config(['import-bank-support.qa_enabled' => true]);
|
||||
$bankFixture = importBankQaFixture($partyBuyer, $nativeImport);
|
||||
$bankFirst = app(ImportBankSupport::class)->save($partyBuyer, $nativeImport, $bankFixture['input']);
|
||||
$bankFinalInput = [...$bankFixture['input'], 'operation_key' => (string) Str::uuid(), 'record_key' => $bankFirst->record_key, 'version' => 1,
|
||||
'submission_document_id' => $bankFixture['documents']['delivery']->id, 'submitted_on' => today()->toDateString(),
|
||||
'submission_basis' => 'Synthetic bank delivery receipt recorded on the explicit due date'];
|
||||
$bankFinal = app(ImportBankSupport::class)->save($partyBuyer, $nativeImport, $bankFinalInput);
|
||||
$nativeEvidenceRecords = [...$nativeEvidenceRecords, ...array_values($bankFixture['documents'])];
|
||||
}
|
||||
if ($legalRules) {
|
||||
config(['import-legal.qa_enabled' => true, 'import-tariffs.qa_enabled' => true]);
|
||||
supplyGrant($partyBuyer, ['procurement', 'import_prepare', 'import_customs']);
|
||||
$legalService = app(ImportLegalRules::class);
|
||||
$legalDraft = $legalService->save($partyBuyer, importLegalQaInput($legalAmounts ? ['charges' => [importLegalQaCharge()]] : []), importLegalQaFile());
|
||||
$legalPublishInput = ['operation_key' => (string) Str::uuid(), 'record_key' => $legalDraft->record_key, 'version' => 1,
|
||||
'action' => 'publish', 'reason' => 'Synthetic customs publication for the native recovery fixture'];
|
||||
$legalPublished = $legalService->save($partyBuyer, $legalPublishInput);
|
||||
$legalProposal = app(ImportTariffCodes::class)->save($partyBuyer, $f['r'], ['operation_key' => (string) Str::uuid(), 'version' => 0,
|
||||
'action' => 'propose', 'requisition_item_id' => $f['r']->items()->sole()->id,
|
||||
'code' => '1234.56', 'source_reference' => $legalPublished->data['official_reference'].' — '.$legalPublished->data['citation'],
|
||||
'legal_rule_id' => $legalPublished->id, 'legal_match_code' => '1234', 'legal_origin' => 'Synthetic A', 'legal_sector' => 'Synthetic sector',
|
||||
'legal_date' => '2026-11-27', 'reason' => 'Synthetic documentary suggestion preserved for recovery']);
|
||||
$nativeEvidenceRecords[] = $legalService->source($partyBuyer, $f['r'], $legalPublished->snapshot['source_id']);
|
||||
if ($legalAmounts) {
|
||||
config(['import-estimates.qa_enabled' => true]);
|
||||
$legalPlanRequest = $f['r']->replicate();
|
||||
$legalPlanRequest->fill(['number' => 'LEGAL-RECOVERY-'.Str::random(10), 'status' => 'draft']);
|
||||
$legalPlanRequest->save();
|
||||
$legalPlanItem = $f['r']->items()->sole()->replicate();
|
||||
$legalPlanItem->requisition_id = $legalPlanRequest->id;
|
||||
$legalPlanItem->save();
|
||||
$legalAmountInput = importLegalQaAmountInput($legalPlanItem->id, $legalPublished->id);
|
||||
$legalAmountInput['bases']['synthetic-duty']['value_minor'] = 10000;
|
||||
$legalAmount = app(ImportLegalEstimates::class)->save($partyBuyer, $legalPlanRequest, $legalAmountInput);
|
||||
$legalNativeInput = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'budget_id' => $budget,
|
||||
'reason' => 'Synthetic native import preparation; only the documented duty applies in this isolated scenario',
|
||||
'legal_sources' => ['duty' => ['estimate_id' => $legalAmount->id, 'charge_key' => 'synthetic-duty']],
|
||||
'components' => array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => $kind === 'duty' ? 'known' : 'not_applicable',
|
||||
'basis' => 'Explicit synthetic recovery assumptions, not a real import or legal exemption',
|
||||
'subtotal_minor' => $kind === 'duty' ? 750 : null, 'tax_minor' => $kind === 'duty' ? 0 : null,
|
||||
'total_minor' => $kind === 'duty' ? 750 : null], BudgetImportEstimate::KINDS)];
|
||||
$legalNativePlan = app(ImportEstimates::class)->save($partyBuyer, $legalPlanRequest, $legalNativeInput);
|
||||
}
|
||||
}
|
||||
$evidenceManifest = [];
|
||||
foreach ($nativeEvidenceRecords as $record) {
|
||||
$evidence = $record->snapshot['evidence'];
|
||||
@@ -294,11 +350,54 @@ try {
|
||||
if ($restoredParty->data['instruction_document_id'] !== $instructionsCorrection->id) {
|
||||
throw new RuntimeException('Restored instruction identity differs: '.get_debug_type($restoredParty->data['instruction_document_id']).'/'.get_debug_type($instructionsCorrection->id));
|
||||
}
|
||||
if ($bankSupport) {
|
||||
$bankService = app(ImportBankSupport::class);
|
||||
$bankWorkspace = $bankService->workspace($restoredBuyer, $nativeImport);
|
||||
$bankReplay = $bankService->save($restoredBuyer, $nativeImport, $bankFinalInput);
|
||||
$restoredBank = $bankWorkspace['current']->items()[0];
|
||||
if ($restoredBank['version'] !== 2 || $restoredBank['state'] !== 'documented' || $restoredBank['issues'] !== [] || $bankReplay->id !== $bankFinal->id
|
||||
|| $restoredBank['data']['commitment_document_id'] !== $bankFixture['documents']['term']->id
|
||||
|| $restoredBank['data']['declaration_document_id'] !== $bankFixture['documents']['declaration']->id
|
||||
|| $restoredBank['data']['submission_document_id'] !== $bankFixture['documents']['delivery']->id) {
|
||||
throw new RuntimeException('Bank support relationships, documentary status or original operation replay differ after restore.');
|
||||
}
|
||||
echo json_encode(['bank_support_versions_restored' => 2, 'bank_support_relationships_restored' => true,
|
||||
'bank_support_replay_without_effects' => true], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
}
|
||||
if ($legalRules) {
|
||||
$legalService = app(ImportLegalRules::class);
|
||||
$legalSource = $legalService->source($restoredBuyer, $f['r'], $legalPublished->snapshot['source_id']);
|
||||
$restoredProposal = SupplyRecord::query()->findOrFail($legalProposal->id);
|
||||
$legalReplay = $legalService->save($restoredBuyer, $legalPublishInput);
|
||||
$restoredFrozen = $legalService->freeze($legalPublished->id, '1234', 'Synthetic A', 'Synthetic sector', '2026-11-27');
|
||||
if ($legalReplay->id !== $legalPublished->id || $restoredProposal->snapshot['legal_rule'] !== $restoredFrozen
|
||||
|| $legalSource->snapshot['evidence']['sha256'] !== $restoredFrozen['source_sha256']) {
|
||||
throw new RuntimeException('Legal source, publication replay or frozen buyer context differs after restore.');
|
||||
}
|
||||
echo json_encode(['legal_source_restored' => true, 'legal_published_version_restored' => true,
|
||||
'legal_buyer_context_restored' => true, 'legal_replay_without_effects' => true], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
if ($legalAmounts) {
|
||||
$restoredRequest = Requisition::query()->findOrFail($legalPlanRequest->id);
|
||||
$amountService = app(ImportLegalEstimates::class);
|
||||
$restoredAmount = $amountService->forPlan($restoredRequest, $legalAmount->id);
|
||||
$amountReplay = $amountService->save($restoredBuyer, $restoredRequest, $legalAmountInput);
|
||||
$nativePlanReplay = app(ImportEstimates::class)->save($restoredBuyer, $restoredRequest, $legalNativeInput);
|
||||
$nativeApproval = app(ImportEstimates::class)->approval($restoredRequest);
|
||||
if ($restoredAmount->snapshot['result']['total_minor'] !== 750 || $amountReplay->id !== $legalAmount->id
|
||||
|| $nativePlanReplay->id !== $legalNativePlan->id || $nativeApproval['plan']['budget_amount_minor'] !== 10750
|
||||
|| $nativePlanReplay->snapshot['legal_sources']['duty']['estimate_id'] !== $legalAmount->id) {
|
||||
throw new RuntimeException('Calculated legal amount, native cost plan, source provenance or replay differs after restore.');
|
||||
}
|
||||
echo json_encode(['legal_amount_minor_restored' => 750, 'legal_amount_replay_without_effects' => true,
|
||||
'native_legal_plan_total_minor' => 10750, 'native_legal_plan_source_restored' => true,
|
||||
'native_legal_plan_replay_without_effects' => true], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
}
|
||||
}
|
||||
$readFingerprint = $fingerprint($restored, $afterColumns);
|
||||
if ($readFingerprint !== $after) {
|
||||
throw new RuntimeException('Native restored read changed tables: '.implode(',', array_keys(array_diff_assoc($readFingerprint, $after))));
|
||||
}
|
||||
echo json_encode(['private_original_versions_restored' => count($nativeEvidenceRecords), 'typed_dossier_versions_restored' => 4,
|
||||
echo json_encode(['private_original_versions_restored' => count($nativeEvidenceRecords), 'typed_dossier_versions_restored' => count(array_filter($nativeEvidenceRecords, fn ($record) => $record->kind === 'import_document')),
|
||||
'selected_party_versions_restored' => 2, 'source_loss_detected' => true,
|
||||
'separate_private_root_restored' => true, 'native_authorized_evidence_resolved' => true,
|
||||
'evidence_manifest_sha256' => hash('sha256', json_encode($evidenceManifest, JSON_THROW_ON_ERROR))], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
|
||||
Reference in New Issue
Block a user