first()?->value ?? 100; $margin2Coeff = \App\Models\Setting::where('key', 'margin2_coefficient')->first()?->value ?? 100; $leiRate = \App\Models\Setting::where('key', 'bgn_to_lei')->first()?->value ?? 4.95; $denarRate = \App\Models\Setting::where('key', 'bgn_to_denars')->first()?->value ?? 61.5; return $schema ->columns(1) ->extraAttributes([ 'x-data' => 'pricingCalculator()', 'x-init' => 'initPricing()', 'data-margin1-coeff' => $margin1Coeff, 'data-margin2-coeff' => $margin2Coeff, 'data-lei-rate' => $leiRate, 'data-denar-rate' => $denarRate ]) ->components([ // Section: Basic Attributes (Role 1 or 4) Schemas\Components\Section::make('Модел') ->description('Основни атрибути на модела') ->columns(1) ->schema([ Schemas\Components\Grid::make(3) ->schema([ Forms\Components\Select::make('collection_id') ->label('Колекция') ->relationship('collection', 'name') ->required() ->searchable() ->preload() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), Forms\Components\Select::make('theme_id') ->label('Тема') ->relationship('theme', 'theme_name') ->required() ->searchable() ->preload() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), Forms\Components\Select::make('month_id') ->label('Месец на пазара') ->options(\App\Models\Month::orderBy('order')->pluck('name', 'id')) ->required() ->searchable() ->preload() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), ]), Schemas\Components\Grid::make(2) ->schema([ Forms\Components\Textarea::make('description') ->label('Описание') ->required() ->rows(3) ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), Forms\Components\TextInput::make('order') ->label('Пореден номер') ->numeric() ->default(fn($record) => $record ? $record->month->garmentModels()->max('order') + 1 : 1), ]), Schemas\Components\Grid::make(3) ->schema([ Forms\Components\TextInput::make('article_number') ->label('Артикулен номер') ->required() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), Forms\Components\TextInput::make('constructor') ->label('Конструктор') ->nullable() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), Forms\Components\TextInput::make('cut_number') ->label('Номер на кройка') ->nullable() ->disabled(fn() => !auth()->user()->can('viewBasicAttributes', \App\Models\GarmentModel::class)), ]), ]), // Section: Technology and Fabric Costs (Role 2 or 4) Schemas\Components\Section::make('ТЕХНОЛОГ И РАЗХОДИ НА ПЛАТ') ->collapsible() ->columns(1) ->schema([ Forms\Components\Repeater::make('tech_fabric_expenses') ->label('Разходи за технология и плат') ->relationship('expenses', fn($query) => $query->whereHas('expenseCategory', fn($q) => $q->where('group', 'tech_fabric'))->orderByRaw('(SELECT "order" FROM expense_categories WHERE expense_categories.id = expenses.category_id) ASC')) ->table([ TableColumn::make('Категория'), TableColumn::make('Количество'), TableColumn::make('Единична цена'), TableColumn::make('Общо'), ]) ->schema([ Forms\Components\Select::make('category_id') ->label('Категория') ->relationship('expenseCategory', 'name', fn($query) => $query->where('group', 'tech_fabric')->orderBy('order', 'asc')->active()) ->required() ->searchable() ->preload() ->disableOptionsWhenSelectedInSiblingRepeaterItems(), Forms\Components\TextInput::make('quantity') ->label('Количество') ->numeric() ->required() ->extraAttributes([ 'x-on:input' => 'calculateSubtotal($event)', 'class' => 'tech-fabric-quantity' ]), Forms\Components\TextInput::make('unit_price') ->label('Единична цена') ->numeric() ->required() ->extraAttributes([ 'x-on:input' => 'calculateSubtotal($event)', 'class' => 'tech-fabric-unit-price' ]), Forms\Components\TextInput::make('subtotal') ->label('Общо') ->numeric() ->disabled() ->dehydrated(false) ->extraAttributes(['class' => 'tech-fabric-subtotal']), ]) ->compact() ->columns(4) ->addActionLabel('Добави разход'), ]) ->visible(fn() => auth()->user()->can('viewTechnologyCosts', \App\Models\GarmentModel::class)), // Section: Warehouse Materials (Role 3 or 4) Schemas\Components\Section::make('РАЗХОДИ НА МАТЕРИАЛИ - СКЛАД') ->collapsible() ->columns(1) ->schema([ Forms\Components\Repeater::make('warehouse_expenses') ->label('Разходи за складови материали') ->relationship('expenses', fn($query) => $query->whereHas('expenseCategory', fn($q) => $q->where('group', 'warehouse'))->orderByRaw('(SELECT "order" FROM expense_categories WHERE expense_categories.id = expenses.category_id) ASC')) ->table([ TableColumn::make('Категория'), TableColumn::make('Количество'), TableColumn::make('Единична цена'), TableColumn::make('Общо'), ]) ->schema([ Forms\Components\Select::make('category_id') ->label('Категория') ->relationship('expenseCategory', 'name', fn($query) => $query->where('group', 'warehouse')->orderBy('order', 'asc')->active()) ->required() ->searchable() ->preload() ->disableOptionsWhenSelectedInSiblingRepeaterItems(), Forms\Components\TextInput::make('unit_price') ->label('Единична цена') ->numeric() ->required() ->extraAttributes([ 'x-on:input' => 'calculateSubtotal($event)', 'class' => 'warehouse-unit-price' ]), Forms\Components\TextInput::make('quantity') ->label('Количество') ->numeric() ->required() ->extraAttributes([ 'x-on:input' => 'calculateSubtotal($event)', 'class' => 'warehouse-quantity' ]), Forms\Components\TextInput::make('subtotal') ->label('Общо') ->numeric() ->disabled() ->dehydrated(false) ->extraAttributes(['class' => 'warehouse-subtotal']), ]) ->compact() ->columns(4) ->addActionLabel('Добави разход'), ]) ->visible(fn() => auth()->user()->can('viewWarehouseExpenses', \App\Models\GarmentModel::class)), // Section: Pricing (Role 4 only) Schemas\Components\Section::make('ЦЕНООБРАЗУВАНЕ') ->columns(6) ->collapsible() ->schema([ Forms\Components\TextInput::make('profit') ->label('Печалба') ->numeric() ->default(0) ->extraAttributes([ 'x-on:input' => 'calculatePricing()', 'class' => 'profit-field' ]), Forms\Components\TextInput::make('total_cost') ->label('Обща цена') ->numeric() ->disabled() ->dehydrated(false) ->extraAttributes(['class' => 'total-cost-field']), Forms\Components\TextInput::make('total_cost_plus_profit') ->label('Обща цена + печалба') ->numeric() ->disabled() ->dehydrated(false) ->extraAttributes(['class' => 'total-cost-plus-profit-field']), Forms\Components\TextInput::make('profit_percentage') ->label('Процент печалба') ->numeric() ->disabled() ->dehydrated(false) ->suffix('%') ->extraAttributes(['class' => 'profit-percentage-field']), Forms\Components\TextInput::make('wholesale_vat') ->label('Търговска цена с ДДС') ->numeric() ->disabled() ->dehydrated() ->extraAttributes(['class' => 'wholesale-vat-field']), Forms\Components\TextInput::make('selling_price') ->label('Продажна цена') ->numeric() ->disabled() ->dehydrated() ->extraAttributes(['class' => 'selling-price-field']), Forms\Components\TextInput::make('margin1') ->label('Марж 1 (%)') ->numeric() ->default(0) ->disabled() ->dehydrated() ->suffix('%') ->extraAttributes(['class' => 'margin1-field']), Forms\Components\TextInput::make('margin2') ->label('Марж 2 (x)') ->numeric() ->default(2.1) ->extraAttributes([ 'x-on:input' => 'calculatePricing()', 'class' => 'margin2-field' ]) ->suffix('x'), Forms\Components\TextInput::make('selling_price_lei') ->label('Продажна цена (LEI)') ->numeric() ->disabled() ->dehydrated() ->extraAttributes(['class' => 'selling-price-lei-field']), Forms\Components\TextInput::make('selling_price_denars') ->label('Продажна цена (MKD)') ->numeric() ->disabled() ->dehydrated() ->extraAttributes(['class' => 'selling-price-denars-field']), ]) ->visible(fn() => auth()->user()->can('viewPricing', \App\Models\GarmentModel::class)), ]); } }