Add the billing material with drafts, combined invoices and billed marks

Management builds the monthly billing material (laskuaineisto) as a
draft: done jobs are added as invoices of their own, combined when the
customer, billing unit and reference allow it, and their billable hours
are set per work type. Creating the material checks the jobs again,
marks the approved hours and billable expenses billed, forms invoice
lines with price and VAT snapshots and moves the jobs to "Lasku luotu".
Fixed price jobs bill their quoted amount once. Billed hours keep their
approval and billed expenses can no longer change. The Fennoa export
and monthly fee lines come later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 13:08:24 +02:00
co-authored by Claude Opus 5.5
parent 6de5d9bef1
commit 97bd23bed5
98 changed files with 4887 additions and 22 deletions
@@ -0,0 +1,85 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Billing material (laskuaineisto): a draft of invoices made of finished
* jobs, with management's billable hours per work type, and the invoice
* lines formed when the material is created.
*/
public function up(): void
{
Schema::create('billing_exports', function (Blueprint $table) {
$table->id();
$table->string('status', 20);
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
$table->foreignId('finalized_by')->nullable()->constrained('users')->restrictOnDelete();
$table->timestamp('finalized_at')->nullable();
$table->timestamps();
$table->index('status');
});
Schema::create('billing_invoices', function (Blueprint $table) {
$table->id();
$table->foreignId('billing_export_id')->constrained()->cascadeOnDelete();
$table->foreignId('customer_id')->constrained()->restrictOnDelete();
$table->foreignId('billing_unit_id')->nullable()->constrained()->nullOnDelete();
$table->string('customer_reference')->nullable();
$table->unsignedInteger('sort_order')->default(0);
$table->timestamps();
});
Schema::create('billing_invoice_jobs', function (Blueprint $table) {
$table->id();
$table->foreignId('billing_invoice_id')->constrained()->cascadeOnDelete();
// A job is on one invoice of a material; a later material may bill it again.
$table->foreignId('customer_job_id')->constrained()->restrictOnDelete();
// The job's billing details when it was added, which decide what it may be billed with.
$table->foreignId('customer_id')->constrained()->restrictOnDelete();
$table->foreignId('billing_unit_id')->nullable()->constrained()->nullOnDelete();
$table->string('customer_reference')->nullable();
$table->unsignedInteger('sort_order')->default(0);
});
Schema::create('billing_job_hours', function (Blueprint $table) {
$table->id();
$table->foreignId('billing_invoice_job_id')->constrained()->cascadeOnDelete();
$table->foreignId('work_type_id')->constrained()->restrictOnDelete();
$table->unsignedInteger('billable_quarters');
$table->unique(['billing_invoice_job_id', 'work_type_id']);
});
Schema::create('billing_lines', function (Blueprint $table) {
$table->id();
$table->foreignId('billing_invoice_id')->constrained()->cascadeOnDelete();
$table->foreignId('customer_job_id')->constrained()->restrictOnDelete();
$table->string('kind', 20);
$table->foreignId('work_type_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('expense_id')->nullable()->constrained()->restrictOnDelete();
$table->string('description');
$table->unsignedInteger('recorded_quarters')->nullable();
$table->unsignedInteger('quantity_quarters')->nullable();
$table->integer('unit_price_cents');
$table->unsignedSmallInteger('vat_basis_points');
$table->integer('net_cents');
$table->unsignedInteger('sort_order')->default(0);
$table->index(['customer_job_id', 'kind']);
});
}
public function down(): void
{
Schema::dropIfExists('billing_lines');
Schema::dropIfExists('billing_job_hours');
Schema::dropIfExists('billing_invoice_jobs');
Schema::dropIfExists('billing_invoices');
Schema::dropIfExists('billing_exports');
}
};