components([ Grid::make(3) ->schema([ Section::make('Thông tin định danh') ->columnSpan(2) ->columns(2) ->schema([ Select::make('product_id') ->label('Sản phẩm (Lô đất)') ->relationship('product', 'code') ->searchable() ->preload() ->required() ->live() ->afterStateUpdated(function (Set $set, $state) { if ($state) { $product = Product::find($state); if ($product) { $set('total_value', $product->total_price); $set('land_value', $product->qsdd_value); $set('foundation_value', $product->foundation_temp_value); $set('sales_phase_id', null); } } }), Select::make('sales_phase_id') ->label('Đợt mở bán') ->options(function (Get $get) { $productId = $get('product_id'); if (! $productId) return []; $product = Product::find($productId); if (! $product) return []; return SalesPhase::where('project_id', $product->project_id) ->whereIn('status', ['Chuẩn bị', 'Đang mở bán']) ->pluck('name', 'id'); }) ->searchable() ->preload() ->live() ->helperText('Chọn đợt mở bán để áp dụng giá và chính sách của đợt') ->afterStateUpdated(function (Set $set, Get $get, $state) { $productId = $get('product_id'); if ($state && $productId) { $phaseProduct = \App\Models\SalesPhaseProduct::where('sales_phase_id', $state) ->where('product_id', $productId) ->first(); if ($phaseProduct) { $set('land_value', $phaseProduct->land_value ?? $get('land_value')); $set('foundation_value', $phaseProduct->foundation_value ?? $get('foundation_value')); $total = (float) ($phaseProduct->land_value ?? $get('land_value')) + (float) ($phaseProduct->foundation_value ?? $get('foundation_value')); if ($phaseProduct->sale_price > 0) { $total = (float) $phaseProduct->sale_price; } $set('total_value', $total); if ($phaseProduct->discount_details) { $set('discount_details', $phaseProduct->discount_details); } } } }), TextInput::make('contract_number') ->label('Số HĐMB') ->required() ->unique(ignoreRecord: true), Select::make('contract_type') ->label('Loại hợp đồng') ->options([ 'HĐMB' => 'Hợp đồng mua bán', 'HĐGV' => 'Hợp đồng góp vốn', 'HĐDC' => 'Hợp đồng đặt cọc', ]) ->default('HĐMB') ->required(), TextInput::make('transfer_order') ->label('Thứ tự chuyển nhượng') ->numeric() ->default(0) ->helperText('0 là chủ hiện tại, 1 là F0, 2 là F1...'), ]), Section::make('Trạng thái') ->columnSpan(1) ->schema([ Select::make('status') ->label('Trạng thái pháp lý') ->options([ 'Đang hiệu lực' => 'Đang hiệu lực', 'Đã hoàn thành' => 'Đã hoàn thành', 'Đã hủy' => 'Đã hủy', ]) ->default('Đang hiệu lực') ->required(), DatePicker::make('signing_date') ->label('Ngày ký HĐ') ->required(), DatePicker::make('sale_date') ->label('Ngày bán thực tế'), ]), ]), Section::make('Chi tiết Tài chính & Chiết khấu') ->columns(3) ->schema([ TextInput::make('land_value') ->label('Giá trị QSDĐ') ->numeric() ->prefix('VND') ->live(onBlur: true) ->afterStateUpdated(fn ($state, $get, $set) => $set('total_value', (float)$state + (float)$get('foundation_value'))), TextInput::make('foundation_value') ->label('Giá trị Móng') ->numeric() ->prefix('VND') ->live(onBlur: true) ->afterStateUpdated(fn ($state, $get, $set) => $set('total_value', (float)$get('land_value') + (float)$state)), TextInput::make('total_value') ->label('Tổng giá trị niêm yết') ->numeric() ->prefix('VND') ->readOnly(), Placeholder::make('discount_overview') ->label('Tổng quan chiết khấu (Dữ liệu từ Excel)') ->columnSpanFull() ->content(function ($record) { if (!$record || !$record->discount_details) return 'Không có chiết khấu'; $details = $record->discount_details; $html = '
| Diễn giải | Giá trị (VNĐ) |
|---|---|
| {$desc}{$isOverride} | {$value} |