@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
use App\Models\SupplyRecord;
|
||||
use App\Services\SupplyPlanning;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
require_once __DIR__.'/../Support/SupplyFixture.php';
|
||||
|
||||
beforeEach(function () {
|
||||
// Data/workflow checks use a private build separately; never install a new manifest in the pilot for a test.
|
||||
$this->withoutVite();
|
||||
$this->f = receiptFixture();
|
||||
supplyGrant($this->f['issuer'], ['supply_prepare']);
|
||||
supplyGrant($this->f['receiver'], ['supply_read', 'supply_review']);
|
||||
$this->actingAs($this->f['receiver']);
|
||||
});
|
||||
|
||||
function followupAgreement($test, string $from, string $until, string $reference = 'AGREEMENT-QA'): SupplyRecord
|
||||
{
|
||||
$test->actingAs($test->f['issuer']);
|
||||
$service = app(SupplyPlanning::class);
|
||||
$draft = $service->save($test->f['issuer'], 'agreement', supplyInput(['stock_item_id' => $test->f['stock']->id, 'warehouse_id' => $test->f['warehouse']->id,
|
||||
'supplier_id' => $test->f['order']->supplier_id, 'habitual' => false, 'valid_from' => $from, 'valid_until' => $until, 'origin' => 'local',
|
||||
'currency' => 'MZN', 'price_minor' => 10000, 'lead_days' => 4, 'reference' => $reference]));
|
||||
$test->actingAs($test->f['receiver']);
|
||||
|
||||
return $service->save($test->f['receiver'], 'agreement', supplyInput([], 'approve', $draft));
|
||||
}
|
||||
|
||||
function followupCounts(): array
|
||||
{
|
||||
return collect(['supply_records', 'audit_logs', 'stock_movements', 'purchase_orders', 'invoices', 'requisitions'])
|
||||
->mapWithKeys(fn ($table) => [$table => DB::table($table)->count()])->all();
|
||||
}
|
||||
|
||||
test('global contract vigency exports all filtered headers with frozen caps and links to approved articles without writes', function () {
|
||||
config(['agreement_order_limits.qa_enabled' => true]);
|
||||
$service = app(SupplyPlanning::class);
|
||||
$contracts = [];
|
||||
foreach ([null, '0', '100.00', ...array_fill(0, 24, null)] as $i => $cap) {
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$draft = $service->save($this->f['issuer'], 'contract', supplyInput(['supplier_id' => $this->f['order']->supplier_id,
|
||||
'reference' => '=QA-HEADER-'.$i, 'currency' => 'MZN', 'valid_from' => today()->toDateString(), 'valid_until' => today()->addMonth()->toDateString(),
|
||||
'periodicity_days' => 30, 'order_limit_amount' => $cap]));
|
||||
$this->actingAs($this->f['receiver']);
|
||||
$contracts[] = $service->save($this->f['receiver'], 'contract', supplyInput([], 'approve', $draft));
|
||||
}
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$draft = $service->save($this->f['issuer'], 'agreement', supplyInput(['stock_item_id' => $this->f['stock']->id, 'warehouse_id' => $this->f['warehouse']->id,
|
||||
'supplier_id' => $this->f['order']->supplier_id, 'habitual' => false, 'valid_from' => today()->toDateString(), 'valid_until' => today()->addMonth()->toDateString(),
|
||||
'origin' => 'local', 'currency' => 'MZN', 'reference' => 'QA-CHILD', 'contract_key' => $contracts[0]->record_key, 'lead_days' => 2]));
|
||||
$this->actingAs($this->f['receiver']);
|
||||
$child = $service->save($this->f['receiver'], 'agreement', supplyInput([], 'approve', $draft));
|
||||
$before = followupCounts();
|
||||
$this->get('/compras/acordos/vigencia?kind=contract&validity=current')->assertOk()->assertInertia(fn (Assert $page) => $page
|
||||
->where('kind', 'contract')->has('records.data', 25)->where('records.total', 27)->where('records.data.0.approved_articles', 1)->where('records.data.0.max_order_value_minor', null)
|
||||
->where('records.data.1.max_order_value_minor', 0)->where('records.data.0.articles_href', '/compras/fornecimento?view=agreements&linked_contract_key='.$contracts[0]->record_key));
|
||||
$response = $this->get('/compras/acordos/vigencia.csv?kind=contract&validity=current');
|
||||
$response->assertOk()->assertHeader('Content-Disposition', 'attachment; filename="vigencia-contratos-'.today()->toDateString().'.csv"');
|
||||
$rows = followupCsv($response->getContent());
|
||||
expect($rows)->toHaveCount(30)->and($rows[2])->toHaveCount(18)->and($rows[3])->toHaveCount(18)->and($rows[3][2])->toBe("'=QA-HEADER-0")
|
||||
->and($rows[3][14])->toBe('Não definido')->and($rows[4][14])->toBe('0')->and($rows[5][14])->toBe('10000')->and($rows[3][16])->toBe('1');
|
||||
expect(followupCounts())->toBe($before);
|
||||
$service->save($this->f['receiver'], 'agreement', supplyInput([], 'suspend', $child));
|
||||
$this->get('/compras/acordos/vigencia?kind=contract')->assertOk()->assertInertia(fn (Assert $page) => $page->where('records.data.0.approved_articles', 0));
|
||||
$service->save($this->f['receiver'], 'contract', supplyInput([], 'suspend', $contracts[0]));
|
||||
$this->get('/compras/acordos/vigencia?kind=contract')->assertOk()->assertInertia(fn (Assert $page) => $page->where('records.total', 26));
|
||||
config(['agreement_order_limits.qa_enabled' => false]);
|
||||
$this->get('/compras/acordos/vigencia?kind=contract')->assertStatus(503);
|
||||
$this->get('/compras/acordos/vigencia?kind=other')->assertSessionHasErrors('kind');
|
||||
});
|
||||
|
||||
function followupCsv(string $body): array
|
||||
{
|
||||
$file = fopen('php://temp', 'w+');
|
||||
fwrite($file, $body);
|
||||
rewind($file);
|
||||
$rows = [];
|
||||
while (($row = fgetcsv($file, null, ';', '"', '')) !== false) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
fclose($file);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
test('agreement vigency uses inclusive boundaries and keeps missing legacy dates indeterminate', function () {
|
||||
$today = today()->toDateString();
|
||||
$endToday = followupAgreement($this, today()->subDay()->toDateString(), $today, 'END-TODAY');
|
||||
$startToday = followupAgreement($this, $today, today()->addDay()->toDateString(), 'START-TODAY');
|
||||
followupAgreement($this, today()->subDays(3)->toDateString(), today()->subDay()->toDateString(), 'EXPIRED');
|
||||
followupAgreement($this, today()->addDay()->toDateString(), today()->addDays(2)->toDateString(), 'FUTURE');
|
||||
// Synthetic legacy source without dates: unknown is not zero, expired or current.
|
||||
SupplyRecord::query()->create(['tenant_id' => $this->f['tenant']->id, 'kind' => 'agreement', 'record_key' => (string) Str::uuid(), 'version' => 1, 'state' => 'approved',
|
||||
'stock_item_id' => $this->f['stock']->id, 'warehouse_id' => $this->f['warehouse']->id, 'supplier_id' => $this->f['order']->supplier_id,
|
||||
'created_by' => $this->f['issuer']->id, 'data' => ['reference' => 'LEGACY-NO-DATE'], 'snapshot' => [], 'reason' => 'Fonte legada sintética de teste.',
|
||||
'operation_key' => (string) Str::uuid(), 'input_hash' => str_repeat('0', 64), 'created_at' => now()]);
|
||||
$counts = followupCounts();
|
||||
$this->get('/compras/acordos/vigencia?validity=current')->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private')
|
||||
->assertInertia(fn (Assert $page) => $page->component('supply/agreement-followup')->where('asOf', $today)->where('records.total', 2)
|
||||
->where('records.data.0.id', $endToday->id)->where('records.data.1.id', $startToday->id));
|
||||
foreach (['expired' => 'EXPIRED', 'future' => 'FUTURE', 'unknown' => 'LEGACY-NO-DATE'] as $filter => $reference) {
|
||||
$this->get('/compras/acordos/vigencia?validity='.$filter)->assertInertia(fn (Assert $page) => $page->where('records.total', 1)->where('records.data.0.reference', $reference));
|
||||
}
|
||||
expect(followupCounts())->toBe($counts);
|
||||
});
|
||||
|
||||
test('expiry search and explicit cutoff use the same scope in the page and complete csv across pages', function () {
|
||||
for ($i = 0; $i < 27; $i++) {
|
||||
followupAgreement($this, today()->subDay()->toDateString(), today()->addDay()->toDateString(), 'EXPIRY-QA-'.$i);
|
||||
}
|
||||
followupAgreement($this, today()->toDateString(), today()->addMonth()->toDateString(), 'LATER-QA');
|
||||
$cutoff = today()->addDay()->toDateString();
|
||||
$counts = followupCounts();
|
||||
$this->get('/compras/acordos/vigencia?search=expiry&expires_by='.$cutoff.'&page=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 27)->has('records.data', 2)
|
||||
->where('filters.expires_by', $cutoff)->where('filters.search', 'expiry'));
|
||||
$response = $this->get('/compras/acordos/vigencia.csv?search=expiry&expires_by='.$cutoff.'&page=2')->assertOk()->assertHeader('Content-Type', 'text/csv; charset=UTF-8');
|
||||
$rows = followupCsv($response->getContent());
|
||||
expect($rows)->toHaveCount(30)->and(array_unique(array_column(array_slice($rows, 3), 0)))->toHaveCount(27)
|
||||
->and(array_column(array_slice($rows, 3), 2))->not->toContain('LATER-QA')->and(followupCounts())->toBe($counts);
|
||||
});
|
||||
|
||||
test('a later suspension excludes an old approved version and does not revive it in the report', function () {
|
||||
$record = followupAgreement($this, today()->toDateString(), today()->addWeek()->toDateString());
|
||||
app(SupplyPlanning::class)->save($this->f['receiver'], 'agreement', supplyInput([], 'suspend', $record));
|
||||
$this->get('/compras/acordos/vigencia')->assertInertia(fn (Assert $page) => $page->where('records.total', 0));
|
||||
expect(followupCsv($this->get('/compras/acordos/vigencia.csv')->assertOk()->getContent()))->toHaveCount(3);
|
||||
expect($record->fresh()->state)->toBe('approved');
|
||||
});
|
||||
|
||||
test('the report counts approved policy identities and opens only policies linked to the selected agreement', function () {
|
||||
$record = followupAgreement($this, today()->toDateString(), today()->addWeek()->toDateString());
|
||||
$other = followupAgreement($this, today()->toDateString(), today()->addWeek()->toDateString(), 'OTHER-AGREEMENT');
|
||||
$planning = app(SupplyPlanning::class);
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$draft = $planning->save($this->f['issuer'], 'policy', supplyInput([...supplyPolicy($this->f), 'agreement_key' => $record->record_key]));
|
||||
$this->actingAs($this->f['receiver']);
|
||||
$approved = $planning->save($this->f['receiver'], 'policy', supplyInput([], 'approve', $draft));
|
||||
$this->get('/compras/acordos/vigencia')->assertInertia(fn (Assert $page) => $page->where('records.data.0.approved_policies', 1)->where('records.data.1.approved_policies', 0)
|
||||
->where('records.data.0.policies_href', '/compras/fornecimento?view=calendar&linked_agreement_key='.$record->record_key));
|
||||
$this->get('/compras/fornecimento?view=calendar&linked_agreement_key='.$record->record_key)->assertInertia(fn (Assert $page) => $page->has('records', 1)->where('records.0.id', $approved->id)->where('linkedAgreementKey', $record->record_key));
|
||||
$this->get('/compras/fornecimento?view=calendar&linked_agreement_key='.$other->record_key)->assertInertia(fn (Assert $page) => $page->has('records', 0));
|
||||
});
|
||||
|
||||
test('csv neutralizes formula text and preserves unknown dates without inventing values', function () {
|
||||
$record = followupAgreement($this, today()->toDateString(), today()->addDay()->toDateString(), '=SYNTHETIC()');
|
||||
$rows = followupCsv($this->get('/compras/acordos/vigencia.csv')->assertOk()->getContent());
|
||||
expect($rows[3][2])->toBe("'=SYNTHETIC()")->and($rows[3][9])->toBe('Vigente')->and($rows[3][10])->toBe('0')
|
||||
->and($rows[3][11])->toContain('record_id='.$record->id);
|
||||
});
|
||||
|
||||
test('scope current permissions and maintenance are revalidated with no grants or cross company leakage', function () {
|
||||
followupAgreement($this, today()->toDateString(), today()->addDay()->toDateString());
|
||||
$foreign = receiptFixture();
|
||||
supplyGrant($foreign['receiver'], ['supply_read']);
|
||||
$this->actingAs($foreign['receiver'])->get('/compras/acordos/vigencia')->assertInertia(fn (Assert $page) => $page->where('records.total', 0));
|
||||
$this->actingAs($this->f['otherReceiver'])->get('/compras/acordos/vigencia')->assertForbidden();
|
||||
$this->actingAs($this->f['receiver']);
|
||||
DB::table('organization_memberships')->where('user_id', $this->f['receiver']->id)->update(['maintenance' => true]);
|
||||
$this->get('/compras/acordos/vigencia.csv')->assertForbidden();
|
||||
DB::table('organization_memberships')->where('user_id', $this->f['receiver']->id)->update(['maintenance' => false, 'expires_at' => now()->subMinute()]);
|
||||
$this->get('/compras/acordos/vigencia')->assertForbidden();
|
||||
});
|
||||
|
||||
test('malformed filters are refused rather than silently widening the report', function () {
|
||||
foreach (['validity=wrong', 'expires_by=invalid', 'page=0', 'search='.str_repeat('x', 101)] as $filter) {
|
||||
$this->getJson('/compras/acordos/vigencia?'.$filter)->assertUnprocessable();
|
||||
$this->getJson('/compras/acordos/vigencia.csv?'.$filter)->assertUnprocessable();
|
||||
}
|
||||
$this->getJson('/compras/fornecimento?view=calendar&linked_agreement_key=invalid')->assertUnprocessable();
|
||||
});
|
||||
Reference in New Issue
Block a user