Materials created before billing months existed have no period, so the profitability report left out what they billed. Their month is now the month they were created in, as in the export repository. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
127 lines
6.6 KiB
PHP
127 lines
6.6 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\Catalog\Application\UseCases\SetInternalHourlyCost;
|
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
|
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;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Livewire;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->travelTo('2026-11-25 12:00');
|
|
$this->manager = User::factory()->management()->create();
|
|
$this->actingAs($this->manager);
|
|
$this->editing = WorkTypeModel::factory()->create(['name' => 'Editointi', 'hourly_rate_cents' => 8000]);
|
|
$this->customer = CustomerModel::factory()->company()->create(['name' => 'Asiakas Oy']);
|
|
$this->video = ServiceUnitModel::factory()->create(['name' => 'Videotuotanto']);
|
|
$this->web = ServiceUnitModel::factory()->create(['name' => 'Verkkosivut']);
|
|
});
|
|
|
|
function profitJob(array $attributes, array $serviceUnitIds): CustomerJobModel
|
|
{
|
|
$job = CustomerJobModel::factory()->create(array_merge(['customer_id' => test()->customer->id], $attributes));
|
|
DB::table('customer_job_service_unit')->where('customer_job_id', $job->id)->delete();
|
|
DB::table('customer_job_service_unit')->insert(array_map(fn (int $id): array => ['customer_job_id' => $job->id, 'service_unit_id' => $id], $serviceUnitIds));
|
|
|
|
return $job;
|
|
}
|
|
|
|
function profitHours(CustomerJobModel $job, float $hours, string $day): void
|
|
{
|
|
$task = TaskModel::factory()->create(['customer_job_id' => $job->id, 'work_type_id' => test()->editing->id]);
|
|
TimeEntryModel::factory()->approved()->create(['task_id' => $task->id, 'hours' => $hours, 'worked_on' => $day]);
|
|
}
|
|
|
|
it('reports billing, costs and margins by job and service unit', function () {
|
|
app(SetInternalHourlyCost::class)->execute('40');
|
|
|
|
$billed = profitJob(['name' => 'Mainosvideo', 'payment_basis' => 'hourly', 'status' => JobStatus::Done], [$this->video->id]);
|
|
profitHours($billed, 10, '2026-11-03');
|
|
ExpenseModel::factory()->create(['customer_job_id' => $billed->id, 'spent_on' => '2026-11-04', 'cost_cents' => 10000, 'billed_amount_cents' => 12000]);
|
|
|
|
$exportId = app(OpenBillingDraft::class)->execute($this->manager->id);
|
|
DB::table('billing_exports')->where('id', $exportId)->update(['period' => '2026-11']);
|
|
app(AddJobToDraft::class)->execute($exportId, $billed->id);
|
|
app(FinalizeBillingExport::class)->execute($exportId, $this->manager->id);
|
|
|
|
$shared = profitJob(['name' => 'Sivusto ja video', 'payment_basis' => 'fixed_quote', 'quoted_amount_cents' => 500000], [$this->video->id, $this->web->id]);
|
|
profitHours($shared, 3, '2026-11-10');
|
|
profitHours($shared, 50, '2026-10-10');
|
|
|
|
$internal = profitJob(['customer_id' => CustomerModel::query()->where('is_internal', true)->value('id'), 'name' => 'Hallinto'], [$this->web->id]);
|
|
profitHours($internal, 2, '2026-11-10');
|
|
|
|
$page = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])
|
|
->test('reports::profitability')
|
|
->assertOk()
|
|
->assertSee('Mainosvideo')
|
|
->assertDontSee('Hallinto');
|
|
|
|
$report = $page->instance()->report();
|
|
|
|
expect(array_map(fn ($row) => $row->job->name, $report->jobs))->toBe(['Mainosvideo', 'Sivusto ja video']);
|
|
|
|
$first = $report->jobs[0]->profit;
|
|
expect($first->quarters)->toBe(40)
|
|
->and($first->billedQuarters)->toBe(40)
|
|
->and($first->workCents)->toBe(80000)
|
|
->and($first->expenseRevenueCents)->toBe(12000)
|
|
->and($first->expenseCostCents)->toBe(10000)
|
|
->and($first->hourCostCents)->toBe(40000)
|
|
->and($first->marginCents())->toBe(42000);
|
|
|
|
expect($report->jobs[1]->profit->marginCents())->toBe(-12000)
|
|
->and(array_map(fn ($unit) => [$unit->name, $unit->jobCount, $unit->profit->quarters, $unit->profit->marginCents()], $report->serviceUnits))
|
|
->toContain(['Videotuotanto', 2, 46, 36000], ['Verkkosivut', 1, 6, -6000])
|
|
->and($report->total->marginCents())->toBe(30000);
|
|
|
|
$page->set('includeInternal', true);
|
|
expect($page->instance()->report()->jobs)->toHaveCount(3);
|
|
|
|
$response = $this->get(route('reports.csv', ['report' => 'profitability', 'alku' => '2026-11-01', 'loppu' => '2026-11-30']))
|
|
->assertOk()
|
|
->assertDownload('kannattavuus-2026-11-01-2026-11-30.csv');
|
|
|
|
expect($response->streamedContent())
|
|
->toContain($billed->number.';Mainosvideo;"Asiakas Oy";Videotuotanto;Tuntityö;10;10;80000;12000;10000;40000;42000')
|
|
->toContain(';-12000');
|
|
});
|
|
|
|
it('counts a fixed price billed by a material without a billing month in the month it was created', function () {
|
|
$job = profitJob(['name' => 'Tarra', 'payment_basis' => 'fixed_quote', 'quoted_amount_cents' => 40000, 'status' => JobStatus::Done], [$this->video->id]);
|
|
profitHours($job, 1, '2026-11-20');
|
|
|
|
$exportId = app(OpenBillingDraft::class)->execute($this->manager->id);
|
|
app(AddJobToDraft::class)->execute($exportId, $job->id);
|
|
app(FinalizeBillingExport::class)->execute($exportId, $this->manager->id);
|
|
DB::table('billing_exports')->where('id', $exportId)->update(['period' => null, 'created_at' => '2026-11-24 11:03:07']);
|
|
|
|
$november = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])->test('reports::profitability')->instance()->report();
|
|
$december = Livewire::withQueryParams(['alku' => '2026-12-01', 'loppu' => '2026-12-31'])->test('reports::profitability')->instance()->report();
|
|
|
|
expect($november->jobs[0]->profit->workCents)->toBe(40000)
|
|
->and($december->jobs)->toBe([]);
|
|
});
|
|
|
|
it('asks for the internal hourly cost before showing margins', function () {
|
|
profitHours(profitJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly'], [$this->video->id]), 2, '2026-11-03');
|
|
|
|
$page = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])
|
|
->test('reports::profitability')
|
|
->assertSee(__('reports.profitability.no_cost'));
|
|
|
|
expect($page->instance()->report()->total->marginCents())->toBeNull();
|
|
});
|