f = paymentFixture(); $this->workflow = app(SupplierBankWorkflow::class); $this->actingAs($this->f['issuer']); }); function independentBankData(object $test, string $currency = 'USD'): array { return [...$test->f['bank_data'], 'account_key' => (string) Str::uuid(), 'previous_id' => 0, 'currency' => $currency, 'bank_name' => 'Banco sintético '.$currency, 'account_identifier' => 'TEST-ACCOUNT-'.$currency]; } function approveIndependentBank(object $test, SupplierBankDetail $detail): void { foreach ([$test->f['author'], $test->f['validator']] as $actor) { $test->actingAs($actor); $test->workflow->review($actor, $detail, 'approved', 'Conferência independente sintética da conta'); } } test('adding an independent bank account preserves the approved legacy chain and its payment availability', function () { $original = $this->f['bank']->fresh()->getAttributes(); $detail = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); expect($detail->account_key)->not->toBeNull()->and($detail->revision)->toBe(2) ->and($this->workflow->currentAccounts($this->f['supplier']))->toHaveCount(2) ->and($this->workflow->state($this->f['supplier']))->toBe('approved') ->and($this->workflow->state($this->f['supplier'], $detail->account_key))->toBe('pending') ->and($this->f['bank']->fresh()->getAttributes())->toBe($original); $this->actingAs($this->f['author']); $payment = app(ExternalPaymentWorkflow::class)->register($this->f['author'], $this->f['invoice'], $this->f['payment_data']); expect($payment->supplier_bank_detail_id)->toBe($this->f['bank']->id)->and($payment->currency)->toBe('MZN'); }); test('a newer independent account does not supersede another account awaiting independent reviews', function () { $usd = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); $eur = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this, 'EUR')); approveIndependentBank($this, $usd); expect($this->workflow->state($this->f['supplier'], $usd->account_key))->toBe('approved') ->and($this->workflow->state($this->f['supplier'], $eur->account_key))->toBe('pending') ->and($this->workflow->state($this->f['supplier']))->toBe('approved') ->and(SupplierBankReview::query()->where('supplier_bank_detail_id', $usd->id)->count())->toBe(2); }); test('revising the legacy account keeps independent approval and refuses payment against the superseded legacy version', function () { $usd = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); approveIndependentBank($this, $usd); $this->actingAs($this->f['issuer']); $replacement = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], [...$this->f['bank_data'], 'previous_id' => $this->f['bank']->id, 'account_identifier' => 'LEGACY-REPLACEMENT']); expect($replacement->account_key)->toBeNull()->and($replacement->revision)->toBe(3) ->and($this->workflow->state($this->f['supplier']))->toBe('pending') ->and($this->workflow->state($this->f['supplier'], $usd->account_key))->toBe('approved'); $this->actingAs($this->f['author']); expect(fn () => app(ExternalPaymentWorkflow::class)->register($this->f['author'], $this->f['invoice'], $this->f['payment_data']))->toThrow(ValidationException::class); }); test('new-account response recovery preserves its identity even after a later revision without reactivating history', function () { $data = independentBankData($this); $original = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], $data); $replacement = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], [...$data, 'previous_id' => $original->id, 'account_identifier' => 'UPDATED-INDEPENDENT']); $before = AuditLog::query()->count(); $recovered = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], array_reverse($data, true)); expect($recovered->id)->toBe($original->id)->and($this->workflow->latest($this->f['supplier'], $data['account_key'])->id)->toBe($replacement->id) ->and(SupplierBankDetail::query()->count())->toBe(3)->and(AuditLog::query()->count())->toBe($before); expect(fn () => $this->workflow->propose($this->f['issuer'], $this->f['supplier'], [...$data, 'currency' => 'EUR']))->toThrow(ValidationException::class); }); test('bank consultation selects the requested account while administration retains its original redacted view', function () { $data = independentBankData($this); $detail = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], $data); $url = '/fornecedores/'.$this->f['supplier']->id.'/dados-bancarios?account='.$detail->account_key; $this->get($url)->assertOk()->assertInertia(fn (Assert $page) => $page->where('current.id', $detail->id) ->where('current.account_identifier', $data['account_identifier'])->where('accountKey', $detail->account_key)->has('accounts', 2)); $admin = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Admin]); $this->actingAs($admin)->get($url)->assertInertia(fn (Assert $page) => $page->where('current', null) ->where('accounts.1.label', 'Conta #'.$detail->id.' · v'.$detail->revision)->where('canPropose', false)->where('canReview', false)); $this->get('/fornecedores/'.$this->f['supplier']->id.'/dados-bancarios?account='.Str::uuid())->assertNotFound(); }); test('independent bank root cannot be used to read another supplier or company', function () { $detail = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); $other = paymentFixture(); $this->actingAs($other['issuer']); $this->get('/fornecedores/'.$other['supplier']->id.'/dados-bancarios?account='.$detail->account_key)->assertNotFound(); $this->get('/fornecedores/'.$this->f['supplier']->id.'/dados-bancarios?account='.$detail->account_key)->assertNotFound(); }); test('legacy bank workflow remains usable before the independent-account column is installed', function () { // The underlying fixture permits only the isolated test database. Schema::table('supplier_bank_details', function (Blueprint $table) { $table->dropIndex('supplier_bank_account_versions'); $table->dropColumn('account_key'); }); expect($this->workflow->multipleReady())->toBeFalse()->and($this->workflow->state($this->f['supplier']))->toBe('approved'); $legacy = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], [...$this->f['bank_data'], 'previous_id' => $this->f['bank']->id]); expect($legacy->fresh()->account_key)->toBeNull(); try { $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); $this->fail('An independent account was written without its schema.'); } catch (HttpException $error) { expect($error->getStatusCode())->toBe(503); } }); test('payment selector requires a deliberate choice between two approved accounts in the invoice currency', function () { $detail = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this, 'MZN')); approveIndependentBank($this, $detail); $this->actingAs($this->f['author']); $this->get('/pagamentos?invoice_id='.$this->f['invoice']->id)->assertInertia(fn (Assert $page) => $page ->where('invoices.0.bank_id', 0)->has('invoices.0.banks', 2)->missing('invoices.0.banks.0.account_identifier')); $payment = app(ExternalPaymentWorkflow::class)->register($this->f['author'], $this->f['invoice'], [...$this->f['payment_data'], 'supplier_bank_detail_id' => $detail->id]); $this->actingAs($this->f['validator']); app(ExternalPaymentWorkflow::class)->review($this->f['validator'], $payment, 'confirmed', 'Conferência sintética da conta escolhida'); expect($payment->refresh()->status)->toBe('confirmed')->and($payment->supplier_bank_detail_id)->toBe($detail->id) ->and(app(ExternalPaymentWorkflow::class)->outstanding($this->f['invoice']))->toBe(2000); }); test('bank review tasks are per current account and disappear separately after each reviewers contribution', function () { $usd = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this)); $eur = $this->workflow->propose($this->f['issuer'], $this->f['supplier'], independentBankData($this, 'EUR')); $this->actingAs($this->f['author']); $tasks = collect(app(WorkflowTaskInbox::class)->build($this->f['author'], 'procurement'))->where('task', 'supplier_bank_review'); expect($tasks)->toHaveCount(2)->and($tasks->pluck('href')->all())->toContain('/fornecedores/'.$this->f['supplier']->id.'/dados-bancarios?account='.$usd->account_key); $this->workflow->review($this->f['author'], $usd, 'approved', 'Conferência sintética individual'); $tasks = collect(app(WorkflowTaskInbox::class)->build($this->f['author'], 'procurement'))->where('task', 'supplier_bank_review'); expect($tasks)->toHaveCount(1)->and($tasks->first()['href'])->toContain($eur->account_key); $this->actingAs($this->f['validator']); expect(collect(app(WorkflowTaskInbox::class)->build($this->f['validator'], 'procurement'))->where('task', 'supplier_bank_review'))->toHaveCount(2); }); test('independent-account proposal and mandatory audit roll back together', function () { $data = independentBankData($this); $audit = Mockery::mock(RequisitionWorkflow::class)->makePartial(); $audit->shouldReceive('audit')->once()->andThrow(new RuntimeException('Synthetic audit failure')); app()->instance(RequisitionWorkflow::class, $audit); expect(fn () => $this->workflow->propose($this->f['issuer'], $this->f['supplier'], $data))->toThrow(RuntimeException::class); expect(SupplierBankDetail::query()->count())->toBe(1); }); test('native account forms normalise inputs redirect to the selected root and complete two independent reviews', function () { $data = independentBankData($this); $data['account_key'] = strtoupper($data['account_key']); $data['country'] = 'mz'; $data['currency'] = 'usd'; $data['account_identifier'] = ' test usd-123 '; $url = '/fornecedores/'.$this->f['supplier']->id.'/dados-bancarios'; $target = $url.'?account='.strtolower($data['account_key']); $this->post($url, $data)->assertSessionHasNoErrors()->assertRedirect($target); $detail = SupplierBankDetail::query()->where('account_key', strtolower($data['account_key']))->sole(); expect($detail->currency)->toBe('USD')->and($detail->country)->toBe('MZ')->and($detail->account_identifier)->toBe('TESTUSD-123'); $reviewUrl = $url.'/'.$detail->id.'/validar'; $decision = ['decision' => 'approved', 'reason' => 'Conferência sintética independente', 'independently_verified' => true]; $this->post($reviewUrl, $decision)->assertForbidden(); foreach ([$this->f['author'], $this->f['validator']] as $actor) { $this->actingAs($actor)->post($reviewUrl, $decision)->assertRedirect()->assertSessionHasNoErrors(); } $this->get($target)->assertInertia(fn (Assert $page) => $page->where('state', 'approved')->where('current.id', $detail->id)->has('history', 1)); expect($this->workflow->state($this->f['supplier']))->toBe('approved'); });