feat: per-row and global discount with margin validation
Parts (wo_parts) already had discount_pct; now also added to wo_works. Labor catalog gets a cost_price field as the hard minimum sell floor. - WorkOrderWork: discount_pct + cost_price fields; total calculation now includes discount: hours × price_per_hour × (1 − disc/100) - Labor model: cost_price field (used to pre-fill work rows and validate) - WorksRelationManager: discount_pct field with live helper text showing effective price + margin %; validation blocks save if below buy cost or below company min_discount_margin_pct; "Reducere globală" header action applies % to all work rows (skips rows that would breach limits) - PartsRelationManager: same validation on existing discount_pct field; helper text (🟢/🟠/🔴) shows effective price + margin in real time; "Reducere globală" header action for all parts - Settings page: new "Reduceri & limite preț" section with min_discount_margin_pct — 0 = no limit, >0 = hard block below that % - LaborResource form: cost_price field Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+121
-6
@@ -5,9 +5,11 @@ namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
|
||||
use App\Models\Tenant\Labor;
|
||||
use App\Models\Tenant\User;
|
||||
use App\Models\Tenant\WorkOrderWork;
|
||||
use App\Tenancy\TenantManager;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
@@ -24,6 +26,12 @@ class WorksRelationManager extends RelationManager
|
||||
return __('Manopere');
|
||||
}
|
||||
|
||||
private static function minMargin(): float
|
||||
{
|
||||
$company = app(TenantManager::class)->current();
|
||||
return (float) data_get($company?->settings, 'min_discount_margin_pct', 0);
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
@@ -40,6 +48,8 @@ class WorksRelationManager extends RelationManager
|
||||
$set('name', $labor->label());
|
||||
$set('hours', $labor->hours);
|
||||
$set('price_per_hour', $labor->hours > 0 ? round($labor->price / max((float) $labor->hours, 1), 2) : 0);
|
||||
$set('cost_price', $labor->cost_price);
|
||||
$set('discount_pct', 0);
|
||||
}
|
||||
})
|
||||
->createOptionForm([
|
||||
@@ -49,15 +59,77 @@ class WorksRelationManager extends RelationManager
|
||||
Forms\Components\TextInput::make('name_ro')->label(__('Denumire'))->required(),
|
||||
Forms\Components\TextInput::make('hours')->label(__('Ore normate'))->numeric()->default(1)->required(),
|
||||
Forms\Components\TextInput::make('price')->label(__('Preț total (MDL)'))->numeric()->default(0)->required(),
|
||||
Forms\Components\TextInput::make('cost_price')->label(__('Preț cost (MDL)'))->numeric()->default(0),
|
||||
])
|
||||
->createOptionUsing(function (array $data) {
|
||||
$data['is_active'] = true;
|
||||
return Labor::create($data)->id;
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
Forms\Components\TextInput::make('name')->label(__('Nume'))->required()->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('hours')->label(__('Ore'))->numeric()->default(1)->required(),
|
||||
Forms\Components\TextInput::make('price_per_hour')->label(__('Preț/h'))->numeric()->required(),
|
||||
Forms\Components\TextInput::make('hours')->label(__('Ore'))->numeric()->default(1)->required()->live(onBlur: true),
|
||||
Forms\Components\TextInput::make('price_per_hour')->label(__('Preț/h'))->numeric()->required()->live(onBlur: true),
|
||||
Forms\Components\TextInput::make('cost_price')
|
||||
->label(__('Preț cost (MDL)'))
|
||||
->numeric()
|
||||
->default(0)
|
||||
->helperText(__('Costul intern al manoperei — limita minimă de vânzare')),
|
||||
|
||||
Forms\Components\TextInput::make('discount_pct')
|
||||
->label(__('Reducere %'))
|
||||
->numeric()
|
||||
->default(0)
|
||||
->minValue(0)
|
||||
->maxValue(100)
|
||||
->step(0.01)
|
||||
->live(onBlur: true)
|
||||
->rules([
|
||||
fn (Get $get): \Closure => function (string $attribute, $value, \Closure $fail) use ($get) {
|
||||
$hours = max(0, (float) $get('hours'));
|
||||
$rate = max(0, (float) $get('price_per_hour'));
|
||||
$costPrice = max(0, (float) $get('cost_price'));
|
||||
$disc = min(100, max(0, (float) $value));
|
||||
$gross = $hours * $rate;
|
||||
$effective = round($gross * (1 - $disc / 100), 2);
|
||||
|
||||
if ($costPrice > 0 && $effective < $costPrice) {
|
||||
$fail(__('Prețul efectiv (:eff MDL) este sub prețul de cost (:cost MDL).', [
|
||||
'eff' => number_format($effective, 2),
|
||||
'cost' => number_format($costPrice, 2),
|
||||
]));
|
||||
return;
|
||||
}
|
||||
|
||||
$minMargin = self::minMargin();
|
||||
if ($minMargin > 0 && $gross > 0 && $costPrice > 0) {
|
||||
$margin = ($effective - $costPrice) / $effective * 100;
|
||||
if ($margin < $minMargin) {
|
||||
$fail(__('Marja (:m%) este sub limita minimă (:min%). Necesită permisiune specială.', [
|
||||
'm' => number_format($margin, 1),
|
||||
'min' => number_format($minMargin, 1),
|
||||
]));
|
||||
}
|
||||
}
|
||||
},
|
||||
])
|
||||
->helperText(function (Get $get, $state) {
|
||||
$hours = max(0, (float) $get('hours'));
|
||||
$rate = max(0, (float) $get('price_per_hour'));
|
||||
$cost = max(0, (float) $get('cost_price'));
|
||||
$disc = min(100, max(0, (float) $state));
|
||||
$gross = $hours * $rate;
|
||||
$effective = round($gross * (1 - $disc / 100), 2);
|
||||
if ($gross <= 0) return null;
|
||||
$minMargin = self::minMargin();
|
||||
if ($cost > 0 && $gross > 0) {
|
||||
$margin = $effective > 0 ? ($effective - $cost) / $effective * 100 : 0;
|
||||
$color = ($margin < $minMargin && $minMargin > 0) ? '🔴' : '🟢';
|
||||
return "{$color} " . __('Efectiv:') . ' ' . number_format($effective, 2) . ' MDL · ' . __('Marjă:') . ' ' . number_format($margin, 1) . '%';
|
||||
}
|
||||
return __('Efectiv:') . ' ' . number_format($effective, 2) . ' MDL';
|
||||
}),
|
||||
|
||||
Forms\Components\Select::make('master_id')
|
||||
->label(__('Maistru'))
|
||||
->options(fn () => User::pluck('name', 'id'))
|
||||
@@ -70,13 +142,12 @@ class WorksRelationManager extends RelationManager
|
||||
]);
|
||||
}
|
||||
|
||||
/** Global visibility flag from tenant Settings — user can hide margin details in-session. */
|
||||
private static function marginDetailsVisible(): bool
|
||||
{
|
||||
if (! auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_VIEW_INTERNAL_MARGIN)) {
|
||||
return false;
|
||||
}
|
||||
$tenant = app(\App\Tenancy\TenantManager::class)->current();
|
||||
$tenant = app(TenantManager::class)->current();
|
||||
return (bool) data_get($tenant?->settings, 'show_internal_margin_details', true);
|
||||
}
|
||||
|
||||
@@ -89,13 +160,15 @@ class WorksRelationManager extends RelationManager
|
||||
->getStateUsing(fn ($record) => $record->labor?->label() ?: $record->name),
|
||||
Tables\Columns\TextColumn::make('hours')->label(__('Ore'))->numeric(decimalPlaces: 2)->alignRight(),
|
||||
Tables\Columns\TextColumn::make('price_per_hour')->label(__('Preț/h'))->money('MDL')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('discount_pct')->label(__('Discount'))->alignRight()
|
||||
->formatStateUsing(fn ($state) => $state > 0 ? number_format((float)$state, 1) . '%' : '—')
|
||||
->color(fn ($state) => $state > 0 ? 'warning' : null),
|
||||
Tables\Columns\TextColumn::make('total')
|
||||
->label(__('Total'))
|
||||
->money('MDL')
|
||||
->alignRight()
|
||||
->description(function ($record) {
|
||||
if (! self::marginDetailsVisible() || $record->salary_base === null) return null;
|
||||
// WO-level toggle off → hide margin text complet
|
||||
if ($record->workOrder && $record->workOrder->apply_margin === false) return null;
|
||||
if ((float) $record->applied_margin_pct === 0.0) return null;
|
||||
return 'Bază salariu: ' . number_format((float) $record->salary_base, 2) . ' MDL · marjă ' . rtrim(rtrim(number_format((float) $record->applied_margin_pct, 2), '0'), '.') . '%';
|
||||
@@ -111,7 +184,6 @@ class WorksRelationManager extends RelationManager
|
||||
->label(__('Adaugă manoperă'))
|
||||
->modalHeading(__('Adaugă manoperă în fișă'))
|
||||
->after(function (WorkOrderWork $record) {
|
||||
// Auto-add the labor's default parts to the parent WO.
|
||||
if (! $record->labor_id) return;
|
||||
$labor = Labor::with('laborParts.part')->find($record->labor_id);
|
||||
$wo = $record->workOrder;
|
||||
@@ -126,6 +198,49 @@ class WorksRelationManager extends RelationManager
|
||||
->title(__('Piese implicite adăugate (:n)', ['n' => $labor->laborParts->count()]))
|
||||
->success()->send();
|
||||
}),
|
||||
|
||||
Actions\Action::make('bulk_discount_works')
|
||||
->label(__('Reducere globală'))
|
||||
->icon('heroicon-m-tag')
|
||||
->color('warning')
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('discount_pct')
|
||||
->label(__('Reducere % (toate manoperele)'))
|
||||
->numeric()
|
||||
->minValue(0)
|
||||
->maxValue(100)
|
||||
->step(0.01)
|
||||
->required()
|
||||
->helperText(__('Aplică același % de reducere la toate liniile de manoperă din această fișă.')),
|
||||
])
|
||||
->action(function (array $data) {
|
||||
$disc = min(100, max(0, (float) $data['discount_pct']));
|
||||
$minMargin = self::minMargin();
|
||||
$blocked = 0;
|
||||
|
||||
foreach ($this->getOwnerRecord()->works as $work) {
|
||||
$gross = (float) $work->hours * (float) $work->price_per_hour;
|
||||
$effective = round($gross * (1 - $disc / 100), 2);
|
||||
$cost = (float) $work->cost_price;
|
||||
|
||||
if ($cost > 0 && $effective < $cost) { $blocked++; continue; }
|
||||
if ($minMargin > 0 && $effective > 0 && $cost > 0) {
|
||||
$margin = ($effective - $cost) / $effective * 100;
|
||||
if ($margin < $minMargin) { $blocked++; continue; }
|
||||
}
|
||||
$work->update(['discount_pct' => $disc]);
|
||||
}
|
||||
|
||||
$msg = $blocked > 0
|
||||
? __(':n linii blocate (sub limita de marjă sau preț cost).', ['n' => $blocked])
|
||||
: null;
|
||||
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title(__('Reducere aplicată'))
|
||||
->body($msg)
|
||||
->{$blocked > 0 ? 'warning' : 'success'}()
|
||||
->send();
|
||||
}),
|
||||
])
|
||||
->actions([
|
||||
Actions\EditAction::make(),
|
||||
|
||||
Reference in New Issue
Block a user