Management downloads a created billing material as a semicolon CSV with decimal commas and a UTF-8 BOM for Excel: one row per invoice line with the invoice's customer, billing unit, e-invoice details and reference, the job, the line and its net, VAT and gross amounts. Consumers' full personal identity codes are included only for those who may see them, the response is not cached, and cells Excel would read as formulas are escaped. The columns follow Fennoa's import format once a sample exists. Co-Authored-By: Claude Opus 5.5 <[email protected]>
153 lines
6.1 KiB
PHP
153 lines
6.1 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Billing\Application\UseCases\AddJobToDraft;
|
|
use App\Modules\Billing\Application\UseCases\FinalizeBillingExport;
|
|
use App\Modules\Billing\Application\UseCases\OpenBillingDraft;
|
|
use App\Modules\Billing\Presentation\Http\Controllers\DownloadBillingCsv;
|
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
|
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\BillingUnitModel;
|
|
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
|
|
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
|
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
|
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->manager = User::factory()->management()->create();
|
|
$this->actingAs($this->manager);
|
|
$this->layout = WorkTypeModel::factory()->create(['name' => 'Taitto', 'hourly_rate_cents' => 8050]);
|
|
});
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
function billedJob(CustomerModel $customer, array $attributes = [], float $hours = 2.5): CustomerJobModel
|
|
{
|
|
$job = CustomerJobModel::factory()->create(array_merge([
|
|
'customer_id' => $customer->id,
|
|
'status' => JobStatus::Done,
|
|
'payment_basis' => 'hourly',
|
|
], $attributes));
|
|
|
|
$task = TaskModel::factory()->create(['customer_job_id' => $job->id, 'work_type_id' => test()->layout->id]);
|
|
TimeEntryModel::factory()->approved()->create(['task_id' => $task->id, 'hours' => $hours]);
|
|
|
|
return $job;
|
|
}
|
|
|
|
/**
|
|
* @param list<CustomerJobModel> $jobs
|
|
*/
|
|
function createdExport(array $jobs): int
|
|
{
|
|
$exportId = app(OpenBillingDraft::class)->execute(test()->manager->id);
|
|
|
|
foreach ($jobs as $job) {
|
|
app(AddJobToDraft::class)->execute($exportId, $job->id);
|
|
}
|
|
|
|
app(FinalizeBillingExport::class)->execute($exportId, test()->manager->id);
|
|
|
|
return $exportId;
|
|
}
|
|
|
|
/**
|
|
* @return list<list<string>>
|
|
*/
|
|
function csvRows(string $content): array
|
|
{
|
|
$handle = fopen('php://memory', 'r+');
|
|
fwrite($handle, $content);
|
|
rewind($handle);
|
|
$rows = [];
|
|
|
|
while (($row = fgetcsv($handle, null, ';', '"', '')) !== false) {
|
|
$rows[] = $row;
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
it('downloads a created billing material as a CSV file for Excel', function () {
|
|
$company = CustomerModel::factory()->company('0737546-2')->create(['name' => 'Asiakas Oy']);
|
|
$unit = BillingUnitModel::factory()->create([
|
|
'customer_id' => $company->id,
|
|
'name' => 'Talous',
|
|
'billing_address' => "PL 10\n40100 Jyväskylä",
|
|
'e_invoice_address' => '003707375462',
|
|
'e_invoice_operator' => 'E204503',
|
|
'reference' => 'KP-77',
|
|
]);
|
|
$first = billedJob($company, ['name' => 'Esite', 'billing_unit_id' => $unit->id, 'customer_reference' => 'PO-1']);
|
|
ExpenseModel::factory()->create(['customer_job_id' => $first->id, 'description' => 'Paperi', 'billed_amount_cents' => 1000]);
|
|
$consumer = CustomerModel::factory()->consumer()->create(['name' => 'Kalle Kuluttaja', 'personal_identity_code' => '131052-308T']);
|
|
$second = billedJob($consumer, ['name' => '=HYPERLINK("x")'], 1);
|
|
|
|
$exportId = createdExport([$first, $second]);
|
|
|
|
$response = $this->get(route('billing.csv', $exportId))
|
|
->assertOk()
|
|
->assertHeader('Content-Type', 'text/csv; charset=UTF-8')
|
|
->assertDownload("laskuaineisto-{$exportId}.csv");
|
|
|
|
expect($response->headers->get('Cache-Control'))->toContain('no-store');
|
|
|
|
$content = $response->streamedContent();
|
|
expect($content)->toStartWith("\u{FEFF}");
|
|
|
|
$rows = csvRows(substr($content, 3));
|
|
expect($rows[0])->toBe(array_values(__('billing.csv.columns')))
|
|
->and($rows)->toHaveCount(4)
|
|
->and($rows[1])->toBe([
|
|
'1', 'Asiakas Oy', 'Yritys', '0737546-2', '', 'Talous', "PL 10\n40100 Jyväskylä", '003707375462', 'E204503', 'PO-1',
|
|
$first->number, 'Esite', 'Tunnit', 'Taitto', '2,5', '2,5', 'h', '80,50', '25,5', '201,25', '51,32', '252,57',
|
|
])
|
|
->and($rows[2][12])->toBe('Kulu')
|
|
->and($rows[2][13])->toEndWith(': Paperi')
|
|
->and(array_slice($rows[2], 15))->toBe(['1', 'kpl', '10,00', '25,5', '10,00', '2,55', '12,55'])
|
|
->and(array_slice($rows[3], 0, 6))->toBe(['2', 'Kalle Kuluttaja', 'Kuluttaja', '', '131052-308T', ''])
|
|
->and($rows[3][11])->toBe("'=HYPERLINK(\"x\")")
|
|
->and($rows[3][19])->toBe('80,50');
|
|
});
|
|
|
|
it('uses the billing unit reference when the jobs have none and offers the file on the pages', function () {
|
|
$company = CustomerModel::factory()->company()->create();
|
|
$unit = BillingUnitModel::factory()->create(['customer_id' => $company->id, 'reference' => 'KP-77']);
|
|
$exportId = createdExport([billedJob($company, ['billing_unit_id' => $unit->id])]);
|
|
|
|
expect(csvRows(substr($this->get(route('billing.csv', $exportId))->streamedContent(), 3))[1][9])->toBe('KP-77');
|
|
|
|
$this->get(route('billing.export', $exportId))->assertSee('Lataa CSV')->assertSee('henkilötunnuksia');
|
|
$this->get(route('billing.index'))->assertSee(route('billing.csv', $exportId));
|
|
});
|
|
|
|
it('does not download a draft', function () {
|
|
$exportId = app(OpenBillingDraft::class)->execute($this->manager->id);
|
|
|
|
$this->get(route('billing.csv', $exportId))->assertNotFound();
|
|
$this->get(route('billing.csv', 999))->assertNotFound();
|
|
});
|
|
|
|
it('keeps everyone but management out of the file', function () {
|
|
$exportId = createdExport([billedJob(CustomerModel::factory()->company()->create())]);
|
|
$this->actingAs(User::factory()->employee()->create());
|
|
|
|
$this->get(route('billing.csv', $exportId))->assertForbidden();
|
|
});
|
|
|
|
it('guards cells against formula injection', function (string $value, string $cell) {
|
|
expect(DownloadBillingCsv::cell($value))->toBe($cell);
|
|
})->with([
|
|
['=1+1', "'=1+1"],
|
|
['+358 40', "'+358 40"],
|
|
['-5', "'-5"],
|
|
['@IMPORT', "'@IMPORT"],
|
|
['Taitto', 'Taitto'],
|
|
['12,50', '12,50'],
|
|
]);
|