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,65 @@
<?php
namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
use App\Models\Tenant\Subcontractor;
use App\Models\Tenant\SubcontractJob;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class SubcontractJobsRelationManager extends RelationManager
{
protected static string $relationship = 'subcontractJobs';
protected static ?string $title = 'Lucrări la terți';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('subcontractor_id')
->label('Subcontractor')
->options(fn () => Subcontractor::where('is_active', true)->pluck('name', 'id'))
->searchable()
->columnSpanFull(),
Forms\Components\Select::make('category')
->label('Categorie')
->options(array_combine(Subcontractor::SPECIALTIES, Subcontractor::SPECIALTIES))
->searchable(),
Forms\Components\Select::make('status')->options(SubcontractJob::STATUSES)->default('sent')->required(),
Forms\Components\Textarea::make('description')->label('Descriere')->rows(2)->columnSpanFull(),
Forms\Components\TextInput::make('cost')->label('Cost (terț)')->numeric()->default(0)->required(),
Forms\Components\TextInput::make('markup_pct')->label('Markup %')->numeric()->default(0),
Forms\Components\TextInput::make('client_price')->label('Preț client')->numeric()->default(0)
->helperText('Folosit dacă markup = 0.'),
Forms\Components\DatePicker::make('eta')->label('ETA'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('number')
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.'),
Tables\Columns\TextColumn::make('subcontractor.name')->label('Terț')->placeholder('—'),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('cost')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('client_price')->label('Preț client')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('margin')
->label('Marjă')
->state(fn (SubcontractJob $r) => $r->margin())
->money('MDL')->alignRight()
->color(fn ($s) => (float) $s > 0 ? 'success' : ((float) $s < 0 ? 'danger' : 'gray')),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => SubcontractJob::STATUSES[$s] ?? $s)
->badge()
->colors(['warning' => ['sent', 'in_progress'], 'success' => ['done', 'returned'], 'danger' => ['cancelled']]),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()]);
}
}