@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Requisition;
|
||||
use App\Services\ExternalPaymentWorkflow;
|
||||
use App\Services\ProcurementPayableInsights;
|
||||
use App\Services\SupplierPayables;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
require_once __DIR__.'/../Support/PaymentFixture.php';
|
||||
|
||||
beforeEach(function () {
|
||||
$this->f = paymentFixture();
|
||||
config(['common-payables.enabled' => true, 'common-payables.source_instance' => 'insights-fixture-instance']);
|
||||
foreach ([$this->f['author'], $this->f['validator']] as $user) {
|
||||
foreach (['view', 'prepare', 'validate'] as $action) {
|
||||
DB::table('pa_access')->insert(['tenant_id' => $user->tenant_id, 'company_id' => 0, 'unit_id' => 0,
|
||||
'user_id' => $user->id, 'person' => 'insights-person-'.$user->id, 'action' => $action]);
|
||||
}
|
||||
}
|
||||
$this->actingAs($this->f['author']);
|
||||
$prepared = app(SupplierPayables::class)->engine($this->f['author'])->command('prepare', $this->f['invoice']->id, 'Preparação sintética para consulta', (string) Str::uuid());
|
||||
$this->actingAs($this->f['validator']);
|
||||
app(SupplierPayables::class)->engine($this->f['validator'])->command('validate', $prepared['proposal_id'], 'Conferência sintética independente', (string) Str::uuid());
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
if ($this->committedInsightsFixture ?? false) {
|
||||
if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName()) || DB::transactionLevel() !== 0) {
|
||||
throw new LogicException('Read report fixture cleanup requires the disposable database.');
|
||||
}
|
||||
DB::statement('DROP SCHEMA public CASCADE');
|
||||
DB::statement('CREATE SCHEMA public');
|
||||
}
|
||||
});
|
||||
|
||||
test('financial insights use native positions across authorised filtered requests without modifying sources', function () {
|
||||
$copy = $this->f['invoice']->replicate();
|
||||
$copy->number = 'UNRECOGNISED-INSIGHTS';
|
||||
$copy->save();
|
||||
receiptFixture(); // Another company is never included.
|
||||
$this->actingAs($this->f['validator']);
|
||||
if (DB::getDriverName() === 'pgsql') {
|
||||
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName()) || DB::transactionLevel() !== 1) {
|
||||
throw new LogicException('Commit only the disposable report test fixture.');
|
||||
}
|
||||
DB::commit();
|
||||
$this->committedInsightsFixture = true;
|
||||
}
|
||||
$tables = ['pa_obligations', 'pa_sources', 'pa_proposals', 'pa_events', 'pa_commands', 'invoices', 'external_payments', 'audit_logs'];
|
||||
$before = [];
|
||||
foreach ($tables as $table) {
|
||||
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
|
||||
}
|
||||
$this->get('/relatorios')->assertOk()->assertInertia(fn (Assert $page) => $page
|
||||
->where('payableInsights.coverage', ['invoices' => 2, 'recognised' => 1, 'unrecognised' => 1, 'indeterminate' => 0])
|
||||
->where('payableInsights.currencies.0.currency', 'MZN')->where('payableInsights.currencies.0.principal_minor', '6000')
|
||||
->where('payableInsights.currencies.0.balance_minor', '6000')->where('payableInsights.currencies.0.confirmed_payments_minor', '0')
|
||||
->where('payableInsights.currencies.0.allocated_payments_minor', '0')->missing('payableInsights.bank_snapshot'));
|
||||
$this->get('/relatorios?search=NO-MATCH-INSIGHTS')->assertOk()->assertInertia(fn (Assert $page) => $page
|
||||
->where('payableInsights.coverage.invoices', 0)->where('payableInsights.currencies', []));
|
||||
$csv = $this->get('/relatorios/obrigacoes.csv')->assertOk()->assertDownload();
|
||||
expect($csv->streamedContent())->toContain('MZN;1;6000;0;0;6000')->toContain('2;1;1;0')->not->toContain('bank_snapshot');
|
||||
$this->actingAs($this->f['issuer'])->get('/relatorios/obrigacoes.csv')->assertForbidden();
|
||||
foreach ($tables as $table) {
|
||||
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($before[$table]);
|
||||
}
|
||||
});
|
||||
|
||||
test('a divergent native position is counted as indeterminate and never replaced by zero', function () {
|
||||
DB::table('invoices')->where('id', $this->f['invoice']->id)->update(['total_minor' => 6001]);
|
||||
$result = app(ProcurementPayableInsights::class)->build($this->f['validator'], Requisition::query()->visibleTo($this->f['validator']));
|
||||
expect($result['coverage']['indeterminate'])->toBe(1)->and($result['coverage']['recognised'])->toBe(0)->and($result['currencies'])->toBe([]);
|
||||
});
|
||||
|
||||
test('native imported history is counted once and pending payments do not reduce the reported balance', function () {
|
||||
$f = paymentFixture();
|
||||
config(['common-payables.history_enabled' => true]);
|
||||
foreach ([$f['author'], $f['validator']] as $user) {
|
||||
foreach (['view', 'prepare', 'validate'] as $action) {
|
||||
DB::table('pa_access')->insert(['tenant_id' => $user->tenant_id, 'company_id' => 0, 'unit_id' => 0, 'user_id' => $user->id,
|
||||
'person' => 'history-insights-'.$user->id, 'action' => $action]);
|
||||
}
|
||||
}
|
||||
$this->actingAs($f['author']);
|
||||
$payments = app(ExternalPaymentWorkflow::class);
|
||||
$payment = $payments->register($f['author'], $f['invoice'], [...$f['payment_data'], 'amount' => '30.00']);
|
||||
$this->actingAs($f['validator']);
|
||||
$payments->review($f['validator'], $payment, 'confirmed', 'Conferência histórica sintética');
|
||||
$this->actingAs($f['author']);
|
||||
$payments->register($f['author'], $f['invoice'], [...$f['payment_data'], 'amount' => '10.00', 'external_reference' => 'INSIGHTS-PENDING']);
|
||||
$adapter = app(SupplierPayables::class);
|
||||
$proposal = $adapter->engine($f['author'])->command('prepare_history', $f['invoice']->id, 'Importação histórica sintética', (string) Str::uuid());
|
||||
$this->actingAs($f['validator']);
|
||||
$adapter->engine($f['validator'])->command('validate_history', $proposal['proposal_id'], 'Conferência independente do histórico', (string) Str::uuid());
|
||||
$result = app(ProcurementPayableInsights::class)->build($f['validator'], Requisition::query()->visibleTo($f['validator']));
|
||||
expect($result['coverage']['recognised'])->toBe(1)->and($result['currencies'][0]['principal_minor'])->toBe('6000')
|
||||
->and($result['currencies'][0]['confirmed_payments_minor'])->toBe('3000')->and($result['currencies'][0]['allocated_payments_minor'])->toBe('0')
|
||||
->and($result['currencies'][0]['balance_minor'])->toBe('3000');
|
||||
});
|
||||
|
||||
test('read composition rejects commands and revalidates withdrawn native financial access', function () {
|
||||
$actor = $this->f['validator'];
|
||||
$engine = app(SupplierPayables::class)->readEngine($actor);
|
||||
$commands = DB::table('pa_commands')->count();
|
||||
expect(fn () => $engine->command('prepare', $this->f['invoice']->id, 'Proibido na composição de leitura', (string) Str::uuid()))->toThrow(LogicException::class);
|
||||
expect(DB::table('pa_commands')->count())->toBe($commands);
|
||||
DB::table('pa_access')->where('user_id', $actor->id)->where('action', 'view')->delete();
|
||||
expect(fn () => $engine->positionForSource($this->f['invoice']->id))->toThrow(HttpException::class);
|
||||
expect(app(ProcurementPayableInsights::class)->build($actor, Requisition::query()->visibleTo($actor)))->toBeNull();
|
||||
$this->actingAs($this->f['issuer']);
|
||||
expect(app(ProcurementPayableInsights::class)->build($this->f['issuer'], Requisition::query()->visibleTo($this->f['issuer'])))->toBeNull();
|
||||
});
|
||||
Reference in New Issue
Block a user