@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\StockMovement;
|
||||
use App\Models\User;
|
||||
use App\Services\BusinessClients;
|
||||
use App\Services\CustomerDeliveries;
|
||||
use App\Services\CustomerDeliverySchema;
|
||||
use App\Services\CustomerSales;
|
||||
use App\Services\ReceiptWorkflow;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Tempo\Finance\GeneralLedger\LedgerSchema;
|
||||
use Tempo\Finance\GeneralLedger\ReceivablesSchema;
|
||||
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
require_once __DIR__.'/../Support/SupplyFixture.php';
|
||||
|
||||
beforeEach(function () {
|
||||
if (DB::getDriverName() !== 'pgsql' || ! getenv('RQ01_LEDGER_QA_SOURCE')) {
|
||||
$this->markTestSkipped('Requires the owned PostgreSQL common-core regression.');
|
||||
}
|
||||
require_once getenv('RQ01_LEDGER_QA_SOURCE').'/bootstrap.php';
|
||||
LedgerSchema::assertSynthetic(DB::connection());
|
||||
$this->f = receiptFixture('10.000');
|
||||
$this->actingAs($this->f['receiver']);
|
||||
app(ReceiptWorkflow::class)->confirm($this->f['receiver'], $this->f['order'], receiptPayload($this->f, '10.000'));
|
||||
ReceivablesSchema::installForHost(DB::connection(), ReceivablesSchema::CONTRACT, fn ($db) => LedgerSchema::assertSynthetic($db));
|
||||
CustomerDeliverySchema::install(DB::connection(), fn ($db) => LedgerSchema::assertSynthetic($db));
|
||||
config(['third_party.sales_enabled' => true, 'customer_sales.qa_enabled' => true]);
|
||||
$this->salesAdmin = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::Admin]);
|
||||
$this->salesFinance = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
$this->salesReviewer = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
$this->actingAs($this->salesAdmin);
|
||||
$this->salesClient = app(BusinessClients::class)->save($this->salesAdmin, null, ['client_key' => (string) Str::uuid(), 'code' => 'CLIENTE-NATIVO',
|
||||
'name' => 'Cliente inteiramente sintético', 'active' => true, 'reason' => 'Cliente sintético para conferir a ligação real entre módulos.']);
|
||||
app(CustomerSales::class)->command($this->salesAdmin, 'series', ['operation_key' => (string) Str::uuid(), 'series_key' => 'VENDA-2026',
|
||||
'prefix' => 'V/2026', 'starts_on' => '2026-01-01', 'ends_on' => '2026-12-31', 'first_number' => '1', 'reason' => 'Configuração explícita da série de teste.']);
|
||||
$this->saleInput = ['operation_key' => (string) Str::uuid(), 'client_id' => (string) $this->salesClient->id, 'client_version' => '1',
|
||||
'series_key' => 'VENDA-2026', 'issued_on' => '2026-11-29', 'due_on' => '2026-12-29', 'reason' => 'Preparação documental da venda inteiramente sintética.',
|
||||
'lines' => [['stock_item_id' => (string) $this->f['stock']->id, 'quantity' => '2.000', 'unit_price' => '100.00',
|
||||
'tax_rate' => '16', 'tax_basis' => 'Taxa ilustrativa explicitamente indicada para este teste.']]];
|
||||
});
|
||||
|
||||
function nativeCustomerCommand(object $test, User $actor, string $action, array $data): array
|
||||
{
|
||||
$test->actingAs($actor);
|
||||
|
||||
return app(CustomerSales::class)->command($actor, $action, ['operation_key' => (string) Str::uuid(),
|
||||
'reason' => 'Intervenção explícita no percurso nativo de teste.', ...$data]);
|
||||
}
|
||||
|
||||
function nativeCustomerInvoice(object $test): array
|
||||
{
|
||||
$proposal = nativeCustomerCommand($test, $test->f['issuer'], 'invoice_prepare', $test->saleInput);
|
||||
nativeCustomerCommand($test, $test->salesFinance, 'invoice_review', ['proposal_id' => $proposal['proposal_id'], 'revision' => $proposal['revision'], 'decision' => 'approved']);
|
||||
|
||||
return nativeCustomerCommand($test, $test->f['issuer'], 'invoice_issue', ['proposal_id' => $proposal['proposal_id'], 'revision' => $proposal['revision']]);
|
||||
}
|
||||
|
||||
test('native customer invoice physical deliveries and partial receipt use the original common writers once', function () {
|
||||
$invoice = nativeCustomerInvoice($this);
|
||||
expect((int) StockMovement::query()->sum('quantity_milli'))->toBe(10000);
|
||||
$this->actingAs($this->f['receiver']);
|
||||
$data = ['operation_key' => (string) Str::uuid(), 'invoice_id' => $invoice['invoice_id'], 'line_no' => '1', 'warehouse_id' => (string) $this->f['warehouse']->id,
|
||||
'quantity' => '1.000', 'reference' => 'ENT-CLIENTE-01', 'reason' => 'Entrega física parcial ao cliente indicado na factura.'];
|
||||
$delivery = app(CustomerDeliveries::class)->record($this->f['receiver'], $data);
|
||||
expect(app(CustomerDeliveries::class)->record($this->f['receiver'], $data)['replayed'])->toBeTrue()
|
||||
->and((int) StockMovement::query()->sum('quantity_milli'))->toBe(9000);
|
||||
$movement = StockMovement::query()->where('stock_operation_id', $delivery['stock_operation_id'])->sole();
|
||||
expect($movement->snapshot['inventory_operation_hash'])->toMatch('/^[a-f0-9]{64}$/D')
|
||||
->and($movement->snapshot['sales_invoice']['invoice_id'])->toBe($invoice['invoice_id']);
|
||||
app(CustomerDeliveries::class)->record($this->f['receiver'], [...$data, 'operation_key' => (string) Str::uuid(), 'reference' => 'ENT-CLIENTE-02']);
|
||||
expect(fn () => app(CustomerDeliveries::class)->record($this->f['receiver'], [...$data, 'operation_key' => (string) Str::uuid(),
|
||||
'reference' => 'ENT-CLIENTE-03', 'quantity' => '0.001']))->toThrow(HttpException::class);
|
||||
expect((int) StockMovement::query()->sum('quantity_milli'))->toBe(8000)->and(DB::table('customer_deliveries')->count())->toBe(2);
|
||||
$warehouse = app(CustomerDeliveries::class)->workspace($this->f['receiver']);
|
||||
expect($warehouse['invoices'][0]['lines'][0]['remaining_milli'])->toBe(0)
|
||||
->and($warehouse['invoices'][0]['lines'][0])->not->toHaveKeys(['unit_price_minor', 'tax_minor', 'total_minor']);
|
||||
$receipt = nativeCustomerCommand($this, $this->salesFinance, 'receipt_prepare', ['client_id' => $this->salesClient->id, 'client_version' => 1,
|
||||
'amount' => '100.00', 'received_on' => '2026-11-29', 'reference' => 'REC-CLIENTE-01']);
|
||||
nativeCustomerCommand($this, $this->salesReviewer, 'receipt_decide', ['proposal_id' => $receipt['proposal_id'], 'revision' => $receipt['revision'], 'decision' => 'confirmed']);
|
||||
$allocation = nativeCustomerCommand($this, $this->salesFinance, 'allocation_prepare', ['invoice_id' => $invoice['invoice_id'], 'receipt_id' => $receipt['proposal_id'], 'amount' => '100.00']);
|
||||
nativeCustomerCommand($this, $this->salesReviewer, 'allocation_decide', ['proposal_id' => $allocation['proposal_id'], 'revision' => $allocation['revision'], 'decision' => 'confirmed']);
|
||||
expect(app(CustomerSales::class)->engine($this->salesReviewer)->invoice($invoice['invoice_id'])['outstanding_minor'])->toBe(13200)
|
||||
->and(DB::table('cr_invoices')->count())->toBe(1)->and(DB::table('cr_allocation_decisions')->count())->toBe(1);
|
||||
});
|
||||
|
||||
test('an invoice does not fabricate stock when the linked physical delivery exceeds available quantity', function () {
|
||||
$this->saleInput['lines'][0]['quantity'] = '12.000';
|
||||
$invoice = nativeCustomerInvoice($this);
|
||||
$this->actingAs($this->f['receiver']);
|
||||
expect(fn () => app(CustomerDeliveries::class)->record($this->f['receiver'], ['operation_key' => (string) Str::uuid(),
|
||||
'invoice_id' => $invoice['invoice_id'], 'line_no' => 1, 'warehouse_id' => $this->f['warehouse']->id, 'quantity' => '11.000',
|
||||
'reference' => 'ENT-SEM-SALDO', 'reason' => 'A factura não substitui a existência física de material.']))->toThrow(ValidationException::class);
|
||||
expect(DB::table('customer_deliveries')->count())->toBe(0)->and(DB::table('stock_operations')->count())->toBe(0)
|
||||
->and((int) StockMovement::query()->sum('quantity_milli'))->toBe(10000);
|
||||
});
|
||||
|
||||
test('native customer roles do not confer warehouse financial access or allow procurement to confirm receipts', function () {
|
||||
$invoice = nativeCustomerInvoice($this);
|
||||
$this->actingAs($this->f['receiver']);
|
||||
expect(fn () => app(CustomerSales::class)->engine($this->f['receiver'])->invoice($invoice['invoice_id']))->toThrow(HttpException::class);
|
||||
expect(app(CustomerSales::class)->engine($this->f['receiver'])->deliveryInvoice($invoice['invoice_id'])['lines'][0]['quantity_milli'])->toBe(2000);
|
||||
$this->actingAs($this->f['issuer']);
|
||||
expect(fn () => app(CustomerSales::class)->command($this->f['issuer'], 'receipt_prepare', []))->toThrow(HttpException::class);
|
||||
$this->f['receiver']->forceFill(['purchasing_role' => PurchasingRole::Requester])->save();
|
||||
expect($this->f['receiver']->fresh()->purchasing_role)->toBe(PurchasingRole::Requester);
|
||||
$this->actingAs($this->f['receiver']);
|
||||
expect(fn () => app(CustomerDeliveries::class)->workspace($this->f['receiver']))->toThrow(HttpException::class);
|
||||
});
|
||||
Reference in New Issue
Block a user