Stage 9 — Subcontractor System: outsourced work with cost+markup

Schema:
- subcontractors (specialty, rating, contact)
- subcontract_jobs (work_order link, cost, markup_pct, client_price, status
  workflow, sent_at/eta/returned_at, paid_to_sub)

Models:
- SubcontractJob: auto number (SC-YY-NNNN), client_price = cost×(1+markup/100)
  when markup>0 (else manual), margin() helper, recalcs parent WO on save/delete
- WorkOrder.recalcTotal now includes non-cancelled subcontract job client_price

Filament (new "Subcontractare" nav group):
- SubcontractorResource (specialty/rating CRUD)
- SubcontractJobResource board with cost/client/margin columns + status filters,
  nav badge = open jobs
- SubcontractJobsRelationManager on WorkOrder

Tests (7 new):
- client_price from markup; manual price without markup; auto number;
  WO total includes jobs; cancelled excluded; delete recalcs; tenant isolation

Closes roadmap to 16/18 stages (only Stage 10 Bodyshop remains).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-28 06:43:15 +00:00
parent 94938f24d7
commit e8078f157a
15 changed files with 680 additions and 1 deletions
@@ -0,0 +1,62 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('subcontractors', function (Blueprint $t) {
$t->id();
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
$t->string('name');
$t->string('specialty', 48)->nullable(); // Turbo / Cutie viteze / ...
$t->string('phone', 40)->nullable();
$t->string('email')->nullable();
$t->unsignedTinyInteger('rating')->default(3);
$t->boolean('is_active')->default(true);
$t->text('notes')->nullable();
$t->timestamps();
$t->softDeletes();
$t->index(['company_id', 'is_active']);
});
Schema::create('subcontract_jobs', function (Blueprint $t) {
$t->id();
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
$t->foreignId('work_order_id')->nullable()->constrained()->nullOnDelete();
$t->foreignId('subcontractor_id')->nullable()->constrained()->nullOnDelete();
$t->string('number', 32);
$t->string('category', 48)->nullable();
$t->text('description')->nullable();
$t->decimal('cost', 12, 2)->default(0); // what the sub charges us
$t->decimal('markup_pct', 5, 2)->default(0);
$t->decimal('client_price', 12, 2)->default(0); // what we bill the client
$t->string('status', 16)->default('sent'); // sent / in_progress / done / returned / cancelled
$t->date('sent_at')->nullable();
$t->date('eta')->nullable();
$t->date('returned_at')->nullable();
$t->boolean('paid_to_sub')->default(false);
$t->text('notes')->nullable();
$t->timestamps();
$t->softDeletes();
$t->unique(['company_id', 'number']);
$t->index(['company_id', 'status']);
$t->index(['company_id', 'work_order_id']);
});
}
public function down(): void
{
Schema::dropIfExists('subcontract_jobs');
Schema::dropIfExists('subcontractors');
}
};