f = proposalOpeningFixture(); $this->author = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]); $this->validator = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]); $this->actingAs($this->author); $exchange = app(QuotationExchange::class); $exchange->record($this->author, $this->f['r'], ['operation_id' => (string) Str::uuid(), 'currency' => 'USD', 'rate' => '0.5', 'quoted_on' => today()->toDateString(), 'source' => 'Taxa sintética para provar arredondamento cumulativo']); $basis = $exchange->basis($this->f['r'], 'USD'); $requested = $this->f['r']->items()->sole(); $requested->update(['quantity' => '3.000', 'unit_price_minor' => 1, 'subtotal_minor' => 3, 'total_minor' => 3]); $this->f['r']->update(['subtotal_minor' => 3, 'total_minor' => 3]); $quote = $this->f['quote']; $quote->update(['currency' => 'USD', 'subtotal_minor' => 3, 'tax_minor' => 0, 'total_minor' => 3, 'exchange_basis' => $basis]); $quote->items()->create(['tenant_id' => $quote->tenant_id, 'requisition_item_id' => $requested->id, 'description' => $requested->description, 'quantity' => '3.000', 'unit_price_minor' => 1, 'tax_rate' => '0', 'subtotal_minor' => 3, 'tax_minor' => 0, 'total_minor' => 3]); $award = PurchaseAward::query()->create([...$this->f['award_attributes'], 'subtotal_minor' => 2, 'total_minor' => 2]); $orderAttributes = $this->f['order']->getAttributes(); unset($orderAttributes['id']); $orderAttributes['purchase_award_id'] = $award->id; $orderAttributes['currency'] = 'USD'; $orderAttributes['subtotal_minor'] = $orderAttributes['total_minor'] = 3; $orderAttributes['snapshot'] = [...$this->f['order']->snapshot, 'exchange_basis' => $basis, 'reference_amounts' => ['subtotal_minor' => 2, 'tax_minor' => 0, 'total_minor' => 2]]; $this->order = PurchaseOrder::query()->create($orderAttributes); $this->line = $this->order->items()->create(['tenant_id' => $quote->tenant_id, 'requisition_item_id' => $requested->id, 'description' => $requested->description, 'category' => $requested->category, 'purchase_type' => 'consumable', 'unit' => 'cx', 'quantity' => '3.000', 'unit_price_minor' => 1, 'tax_rate' => '0', 'subtotal_minor' => 3, 'tax_minor' => 0, 'total_minor' => 3]); $this->actingAs($this->f['receiver']); app(ReceiptWorkflow::class)->confirm($this->f['receiver'], $this->order, [ 'idempotency_key' => (string) Str::uuid(), 'delivery_note' => 'GUIA-USD', 'received_on' => today()->toDateString(), 'warehouse_id' => $this->f['warehouse']->id, 'items' => [['purchase_order_item_id' => $this->line->id, 'received_quantity' => '3.000', 'accepted_quantity' => '3.000', 'quality_status' => 'compliant', 'stock_item_id' => $this->f['stock']->id]], ]); $this->actingAs($this->author); Budget::query()->create(['tenant_id' => $this->author->tenant_id, 'cost_center_id' => $this->f['r']->cost_center_id, 'year' => now()->year, 'amount_minor' => 100, 'includes_tax' => true, 'created_by' => $this->author->id, 'approved_by' => $this->validator->id, 'reason' => 'Dotação exclusivamente sintética']); DB::transaction(function () { app(ConflictGuard::class)->lockCompany($this->author); app(BudgetWorkflow::class)->record($this->author, $this->f['r'], 'reserve', 3, 3); app(BudgetWorkflow::class)->record($this->author, $this->f['r'], 'commit', 2, 2); }); $this->invoiceData = ['number' => 'USD-1', 'issued_on' => today()->toDateString(), 'subtotal' => '0.01', 'tax' => '0.00', 'total' => '0.01', 'items' => [['purchase_order_item_id' => $this->line->id, 'quantity' => '1.000', 'unit' => 'cx', 'unit_price' => '0.01', 'tax_rate' => '0']]]; }); test('foreign partial invoices consume the frozen mzn commitment cumulatively including a calculated zero delta', function () { $effects = []; for ($i = 1; $i <= 3; $i++) { $this->actingAs($this->author); $invoice = app(InvoiceWorkflow::class)->register($this->author, $this->order, [...$this->invoiceData, 'number' => 'USD-'.$i]); $snapshot = $invoice->snapshot; $this->actingAs($this->validator); app(InvoiceWorkflow::class)->validate($this->validator, $invoice); app(InvoiceWorkflow::class)->validate($this->validator, $invoice); $invoice->refresh(); $effects[] = (int) BudgetConsumption::query()->where('invoice_id', $invoice->id)->value('amount_minor'); expect($invoice->total_minor)->toBe(1)->and($invoice->snapshot['currency'])->toBe('USD'); expect($invoice->snapshot)->toBe($snapshot); } expect($effects)->toBe([1, 0, 1])->and((int) BudgetConsumption::query()->sum('amount_minor'))->toBe(2) ->and(BudgetConsumption::query()->count())->toBe(3)->and((int) BudgetEntry::query()->sum('committed_minor'))->toBe(0); }); test('foreign external payment matches the original invoice currency with independent bank and payment confirmation', function () { $invoice = app(InvoiceWorkflow::class)->register($this->author, $this->order, $this->invoiceData); $this->actingAs($this->validator); app(InvoiceWorkflow::class)->validate($this->validator, $invoice); $supplier = Supplier::query()->findOrFail($this->order->supplier_id); $this->actingAs($this->f['issuer']); $bank = app(SupplierBankWorkflow::class)->propose($this->f['issuer'], $supplier, ['previous_id' => 0, 'bank_name' => 'Banco sintético', 'country' => 'MZ', 'currency' => 'USD', 'beneficiary' => 'Titular sintético', 'account_identifier' => 'TEST-USD-ACCOUNT', 'evidence_reference' => 'Referência sintética', 'reason' => 'Somente para validação isolada']); foreach ([$this->author, $this->validator] as $reviewer) { $this->actingAs($reviewer); app(SupplierBankWorkflow::class)->review($reviewer, $bank, 'approved', 'Conferência independente sintética'); } $this->actingAs($this->author); $workflow = app(ExternalPaymentWorkflow::class); $payment = $workflow->register($this->author, $invoice, ['supplier_bank_detail_id' => $bank->id, 'amount' => '0.01', 'paid_on' => today()->toDateString(), 'external_reference' => 'USD-EXT-1', 'evidence_reference' => 'Evidência sintética de pagamento externo']); $this->actingAs($this->validator); $workflow->review($this->validator, $payment, 'confirmed', 'Conferência sintética da evidência'); expect($payment->refresh()->currency)->toBe('USD')->and($payment->amount_minor)->toBe(1) ->and($workflow->outstanding($invoice))->toBe(0)->and((int) BudgetConsumption::query()->sum('amount_minor'))->toBe(1); });