@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\Quotation;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\User;
|
||||
use App\Services\ProposalComparison;
|
||||
use App\Services\QuotationExchange;
|
||||
use App\Services\RequisitionWorkflow;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
|
||||
require_once __DIR__.'/../Support/QualificationFixture.php';
|
||||
|
||||
beforeEach(function () {
|
||||
$this->f = qualificationFixture();
|
||||
$this->actingAs($this->f['analyst']);
|
||||
$this->rateUrl = '/requisicoes/'.$this->f['r']->id.'/taxas-cambio';
|
||||
$this->rate = ['operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '60.1234567890', 'quoted_on' => today()->toDateString(), 'source' => 'Fonte exclusivamente sintética para teste'];
|
||||
});
|
||||
|
||||
test('finance freezes an explicit process rate once and recovers the same operation without a second audit', function () {
|
||||
$this->post($this->rateUrl, $this->rate)->assertRedirect()->assertSessionHasNoErrors();
|
||||
$before = AuditLog::query()->count();
|
||||
$this->post($this->rateUrl, $this->rate)->assertRedirect()->assertSessionHasNoErrors();
|
||||
expect(DB::table('quotation_exchange_rates')->count())->toBe(1)
|
||||
->and(DB::table('quotation_exchange_rates')->value('rate'))->toBe('60.1234567890')
|
||||
->and(AuditLog::query()->count())->toBe($before);
|
||||
$this->post($this->rateUrl, [...$this->rate, 'rate' => '61'])->assertStatus(409);
|
||||
$this->post($this->rateUrl, [...$this->rate, 'operation_id' => (string) Str::uuid(), 'rate' => '61'])->assertSessionHasErrors('currency');
|
||||
});
|
||||
|
||||
test('process exchange rates cannot be updated or deleted through native sql', function (string $command) {
|
||||
$this->post($this->rateUrl, $this->rate)->assertSessionHasNoErrors();
|
||||
expect(fn () => $command === 'update'
|
||||
? DB::table('quotation_exchange_rates')->update(['rate' => '1.0000000000'])
|
||||
: DB::table('quotation_exchange_rates')->delete())->toThrow(QueryException::class);
|
||||
})->with(['update', 'delete']);
|
||||
|
||||
test('only current finance authority can record rates in the visible process', function () {
|
||||
$this->actingAs($this->f['issuer'])->post($this->rateUrl, $this->rate)->assertForbidden();
|
||||
$this->actingAs($this->f['analyst']);
|
||||
DB::table('users')->where('id', $this->f['analyst']->id)->update(['purchasing_role' => 'requester']);
|
||||
$this->post($this->rateUrl, $this->rate)->assertForbidden();
|
||||
$other = receiptFixture();
|
||||
$finance = User::factory()->create(['tenant_id' => $other['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
$this->actingAs($finance)->post($this->rateUrl, $this->rate)->assertNotFound();
|
||||
expect(DB::table('quotation_exchange_rates')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('exchange rate refuses missing provenance zero negative and future-date assumptions', function (array $changes) {
|
||||
$this->post($this->rateUrl, [...$this->rate, ...$changes])->assertSessionHasErrors();
|
||||
expect(DB::table('quotation_exchange_rates')->count())->toBe(0);
|
||||
})->with([
|
||||
[['rate' => '0']], [['rate' => '-1']], [['rate' => '1e2']], [['source' => '']],
|
||||
[['quoted_on' => '2099-01-01']], [['currency' => 'MZN']], [['currency' => 'usd']],
|
||||
]);
|
||||
|
||||
test('foreign quotation preserves original prices and freezes finance provenance without accepting browser totals', function () {
|
||||
$this->post($this->rateUrl, $this->rate)->assertSessionHasNoErrors();
|
||||
$supplier = Supplier::query()->sole()->replicate();
|
||||
$supplier->nuit = '999999991';
|
||||
$supplier->save();
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$url = '/requisicoes/'.$this->f['r']->id.'/cotacoes';
|
||||
$data = ['currency' => 'USD', 'supplier_id' => $supplier->id, 'reference' => 'USD-FICTICIO', 'delivery_days' => 5,
|
||||
'valid_until' => today()->addWeek()->toDateString(), 'payment_terms' => '30 dias', 'no_conflict' => true,
|
||||
'items' => [['requisition_item_id' => $this->f['r']->items()->sole()->id, 'unit_price' => '1.00']],
|
||||
'exchange_basis' => ['rate' => '1'], 'total_minor' => 999];
|
||||
$this->post($url, $data)->assertRedirect()->assertSessionHasNoErrors();
|
||||
$quote = Quotation::query()->where('reference', 'USD-FICTICIO')->sole();
|
||||
expect($quote->currency)->toBe('USD')->and($quote->total_minor)->toBe(100)
|
||||
->and($quote->items()->sole()->unit_price_minor)->toBe(100)
|
||||
->and($quote->exchange_basis['rate'])->toBe('60.1234567890')
|
||||
->and(app(QuotationExchange::class)->amounts($quote)['total_minor'])->toBe(6012);
|
||||
$quote->update(['exchange_basis' => [...$quote->exchange_basis, 'rate' => '1.0000000000']]);
|
||||
$row = collect(app(ProposalComparison::class)->inspect($this->f['r'])['rows'])->firstWhere('id', $quote->id);
|
||||
expect($row['eligible'])->toBeFalse()->and($row['reference_amounts'])->toBeNull();
|
||||
});
|
||||
|
||||
test('foreign quotation without a finance rate fails atomically while legacy mzn still works', function () {
|
||||
$supplier = Supplier::query()->sole()->replicate();
|
||||
$supplier->nuit = '999999992';
|
||||
$supplier->save();
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$data = ['currency' => 'EUR', 'supplier_id' => $supplier->id, 'reference' => 'SEM-TAXA', 'delivery_days' => 5,
|
||||
'valid_until' => today()->addWeek()->toDateString(), 'payment_terms' => '30 dias', 'no_conflict' => true,
|
||||
'items' => [['requisition_item_id' => $this->f['r']->items()->sole()->id, 'unit_price' => '1.00']]];
|
||||
$this->post('/requisicoes/'.$this->f['r']->id.'/cotacoes', $data)->assertSessionHasErrors('currency');
|
||||
expect(Quotation::query()->count())->toBe(1);
|
||||
unset($data['currency']);
|
||||
$this->post('/requisicoes/'.$this->f['r']->id.'/cotacoes', $data)->assertSessionHasNoErrors();
|
||||
expect(Quotation::query()->latest('id')->first()->currency)->toBe('MZN');
|
||||
});
|
||||
|
||||
test('quotation screen provides process-scoped rates and finance authority without changing the process', function () {
|
||||
$this->post($this->rateUrl, $this->rate)->assertSessionHasNoErrors();
|
||||
$before = AuditLog::query()->count();
|
||||
$this->get('/requisicoes/'.$this->f['r']->id.'/cotacoes')->assertInertia(fn (Assert $page) => $page
|
||||
->where('exchangeAvailable', true)->where('canRecordExchange', true)->has('exchangeRates', 1)
|
||||
->where('exchangeRates.0.rate', '60.1234567890'));
|
||||
expect(AuditLog::query()->count())->toBe($before);
|
||||
});
|
||||
|
||||
test('exchange registration rolls back the frozen rate when its mandatory audit fails', function () {
|
||||
$audit = Mockery::mock(RequisitionWorkflow::class)->makePartial();
|
||||
$audit->shouldReceive('audit')->once()->andThrow(new RuntimeException('Synthetic audit failure'));
|
||||
app()->instance(RequisitionWorkflow::class, $audit);
|
||||
expect(fn () => app(QuotationExchange::class)->record($this->f['analyst'], $this->f['r'], $this->rate))->toThrow(RuntimeException::class);
|
||||
expect(DB::table('quotation_exchange_rates')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('foreign conversion overflow returns a form error and rolls back the whole quotation', function () {
|
||||
$this->post($this->rateUrl, [...$this->rate, 'rate' => '40000'])->assertSessionHasNoErrors();
|
||||
$supplier = Supplier::query()->sole()->replicate();
|
||||
$supplier->nuit = '999999993';
|
||||
$supplier->save();
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$before = AuditLog::query()->count();
|
||||
$this->post('/requisicoes/'.$this->f['r']->id.'/cotacoes', ['currency' => 'USD', 'supplier_id' => $supplier->id,
|
||||
'reference' => 'LIMITE-SINTETICO', 'delivery_days' => 5, 'valid_until' => today()->addWeek()->toDateString(),
|
||||
'payment_terms' => '30 dias', 'no_conflict' => true,
|
||||
'items' => [['requisition_item_id' => $this->f['r']->items()->sole()->id, 'unit_price' => '1000000000.00']]])
|
||||
->assertSessionHasErrors('currency');
|
||||
expect(Quotation::query()->count())->toBe(1)->and(AuditLog::query()->count())->toBe($before);
|
||||
});
|
||||
Reference in New Issue
Block a user