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'); } };