@@ -38,6 +38,7 @@ use App\Services\ProposalComparison;
|
||||
use App\Services\ProposalOpeningWorkflow;
|
||||
use App\Services\PurchaseControlPolicyWorkflow;
|
||||
use App\Services\PurchaseRevisionWorkflow;
|
||||
use App\Services\QuotationExchange;
|
||||
use App\Services\RequisitionCatalogue;
|
||||
use App\Services\RequisitionWorkflow;
|
||||
use Illuminate\Database\QueryException;
|
||||
@@ -47,6 +48,94 @@ use Inertia\Testing\AssertableInertia as Assert;
|
||||
|
||||
require_once __DIR__.'/../Support/SupplyFixture.php';
|
||||
|
||||
test('mixed currency comparison ranks the frozen mzn reference rather than raw foreign amounts', function () {
|
||||
$this->actingAs($this->finance);
|
||||
app(QuotationExchange::class)->record($this->finance, $this->r, [
|
||||
'operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '60',
|
||||
'quoted_on' => today()->toDateString(), 'source' => 'Fonte sintética para comparação multimoeda',
|
||||
]);
|
||||
$quote = $this->quotes[0];
|
||||
$quote->update(['currency' => 'USD', 'exchange_basis' => app(QuotationExchange::class)->basis($this->r, 'USD')]);
|
||||
$result = app(ProposalComparison::class)->inspect($this->r);
|
||||
expect($result['ready'])->toBeTrue()->and($result['top_ids'])->toBe([$this->quotes[1]->id])
|
||||
->and($result['rows'][0]['total_minor'])->toBe(10000)
|
||||
->and($result['rows'][0]['reference_amounts']['total_minor'])->toBe(600000);
|
||||
});
|
||||
|
||||
test('foreign award approves and commits mzn while the immutable order keeps original currency and rate', function () {
|
||||
$this->actingAs($this->finance);
|
||||
$exchange = app(QuotationExchange::class);
|
||||
$exchange->record($this->finance, $this->r, ['operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '60',
|
||||
'quoted_on' => today()->toDateString(), 'source' => 'Fonte sintética congelada antes da proposta']);
|
||||
$quote = $this->quotes[0];
|
||||
$quote->items()->update(['unit_price_minor' => 100, 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100]);
|
||||
$quote->update(['currency' => 'USD', 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100, 'exchange_basis' => $exchange->basis($this->r, 'USD')]);
|
||||
$award = selectPurchaseAward($this);
|
||||
expect($award->total_minor)->toBe(6000);
|
||||
$order = issuePurchaseOrder($this, $award);
|
||||
expect($order->currency)->toBe('USD')->and($order->total_minor)->toBe(100)
|
||||
->and($order->items()->sole()->unit_price_minor)->toBe(100)
|
||||
->and($order->snapshot['reference_amounts']['total_minor'])->toBe(6000)
|
||||
->and($order->snapshot['exchange_basis'])->toBe($quote->exchange_basis);
|
||||
$this->get('/ordens/'.$order->id.'/imprimir')->assertOk()->assertSee('USD')->assertSee('60.0000000000')->assertSee('Referência orçamental');
|
||||
});
|
||||
|
||||
test('foreign currency conversion determines final approval thresholds without changing estimate approvals', function () {
|
||||
$this->actingAs($this->finance);
|
||||
$exchange = app(QuotationExchange::class);
|
||||
$exchange->record($this->finance, $this->r, ['operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '40000',
|
||||
'quoted_on' => today()->toDateString(), 'source' => 'Taxa exclusivamente sintética para testar alçadas']);
|
||||
awardPrices($this, 5000000);
|
||||
$quote = $this->quotes[0];
|
||||
$quote->items()->update(['unit_price_minor' => 100, 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100]);
|
||||
$quote->update(['currency' => 'USD', 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100, 'exchange_basis' => $exchange->basis($this->r, 'USD')]);
|
||||
$before = $this->r->steps()->get()->toArray();
|
||||
$award = selectPurchaseAward($this);
|
||||
expect($award->total_minor)->toBe(4000000)->and($award->approvals()->count())->toBe(2)
|
||||
->and($this->r->steps()->get()->toArray())->toBe($before);
|
||||
});
|
||||
|
||||
test('foreign order cancellation reverses the mzn commitment while preserving the original foreign document', function () {
|
||||
$this->actingAs($this->finance);
|
||||
$exchange = app(\App\Services\QuotationExchange::class);
|
||||
$exchange->record($this->finance, $this->r, ['operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '60',
|
||||
'quoted_on' => today()->toDateString(), 'source' => 'Taxa sintética para testar reversão do compromisso']);
|
||||
$quote = $this->quotes[0];
|
||||
$quote->items()->update(['unit_price_minor' => 100, 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100]);
|
||||
$quote->update(['currency' => 'USD', 'subtotal_minor' => 100, 'tax_minor' => 0, 'total_minor' => 100, 'exchange_basis' => $exchange->basis($this->r, 'USD')]);
|
||||
Budget::query()->create(['tenant_id' => $this->tenant->id, 'cost_center_id' => $this->r->cost_center_id, 'year' => now()->year,
|
||||
'amount_minor' => 20000, 'includes_tax' => true, 'created_by' => $this->finance->id, 'approved_by' => $this->admin->id, 'reason' => 'Dotação sintética para cancelamento multimoeda']);
|
||||
DB::transaction(function () {
|
||||
app(ConflictGuard::class)->lockCompany($this->finance);
|
||||
app(BudgetWorkflow::class)->record($this->finance, $this->r, 'reserve', 10000, 10000);
|
||||
});
|
||||
$order = issuePurchaseOrder($this, selectPurchaseAward($this));
|
||||
expect((int) DB::table('budget_entries')->where('requisition_id', $this->r->id)->sum('committed_minor'))->toBe(6000);
|
||||
$original = $order->getAttributes();
|
||||
config(['order_cancellation.enabled' => true]);
|
||||
$admins = [$this->admin];
|
||||
foreach (range(1, 2) as $index) {
|
||||
$admins[] = User::factory()->create(['tenant_id' => $this->tenant->id, 'purchasing_role' => PurchasingRole::Admin]);
|
||||
}
|
||||
$this->actingAs($admins[0]);
|
||||
$policyWorkflow = app(\App\Services\OrderCancellationPolicyWorkflow::class);
|
||||
$policy = $policyWorkflow->propose($admins[0], ['previous_id' => 0, 'mode' => 'issued_order', 'enabled' => true,
|
||||
'valid_from' => today()->toDateString(), 'valid_until' => today()->addWeek()->toDateString(), 'reason' => 'Política exclusivamente sintética para cancelamento']);
|
||||
foreach (array_slice($admins, 1) as $reviewer) {
|
||||
$this->actingAs($reviewer);
|
||||
$policyWorkflow->review($reviewer, $policy, 'approved', 'Conferência sintética independente');
|
||||
}
|
||||
$this->actingAs($this->buyer);
|
||||
$flow = app(\App\Services\OrderCancellationWorkflow::class);
|
||||
$cancellation = $flow->request($this->buyer, $order, ['operation_key' => (string) Str::uuid(),
|
||||
'reason' => 'Cancelamento sintético antes de qualquer execução', 'evidence' => 'Evidência documental exclusivamente sintética']);
|
||||
$this->actingAs($this->finance);
|
||||
$flow->decide($this->finance, $cancellation, 'approved', 'Conferência financeira sintética independente');
|
||||
expect((int) DB::table('budget_entries')->where('requisition_id', $this->r->id)->sum('committed_minor'))->toBe(0)
|
||||
->and((int) DB::table('budget_entries')->where('requisition_id', $this->r->id)->where('kind', 'order-cancel-'.$order->id)->value('committed_minor'))->toBe(-6000)
|
||||
->and($order->fresh()->getAttributes())->toBe($original);
|
||||
});
|
||||
|
||||
function awardPrices(object $test, int $firstPrice, int $taxRate = 0): void
|
||||
{
|
||||
$test->r->items()->update(['tax_rate' => $taxRate]);
|
||||
|
||||
Reference in New Issue
Block a user