Xu ly giao dien San Pham

This commit is contained in:
2026-04-19 23:50:21 +00:00
parent 10eac55520
commit 91ff4a5e4d
18 changed files with 744 additions and 196 deletions

View File

@@ -7,17 +7,14 @@ use App\Models\Contract;
use App\Models\Product;
use App\Models\PaymentTemplate;
use App\Enums\NavigationGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\DatePicker;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Schemas\Components\Utilities\Get;
use App\Filament\Resources\Contracts\ContractResource\RelationManagers\ScheduleItemsRelationManager;
use App\Filament\Resources\Contracts\Schemas\ContractForm;
class ContractResource extends Resource
{
protected static ?string $model = Contract::class;
@@ -30,24 +27,7 @@ class ContractResource extends Resource
public static function form(Schema $schema): Schema
{
return $schema
->components([
Section::make('Liên kết & Mẫu thanh toán')
->columns(2)
->schema([
Select::make('product_id')->label('Sản phẩm')->relationship('product', 'code')->required()->live(),
Select::make('payment_template_id')->label('Mẫu thanh toán')
->options(fn (Get $get) => PaymentTemplate::pluck('name', 'id'))
->required()->dehydrated(false),
Select::make('customers')->label('Khách hàng')->relationship('customers', 'full_name')->multiple()->required()->columnSpanFull(),
]),
Section::make('Chi tiết Hợp đồng')
->columns(2)
->schema([
TextInput::make('contract_number')->label('Số HĐ')->required(),
DatePicker::make('signing_date')->label('Ngày ký')->required(),
])
]);
return ContractForm::configure($schema);
}
public static function table(Table $table): Table

View File

@@ -2,7 +2,15 @@
namespace App\Filament\Resources\Contracts\Schemas;
use App\Models\Product;
use App\Models\PaymentTemplate;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\DatePicker;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
class ContractForm
{
@@ -10,7 +18,63 @@ class ContractForm
{
return $schema
->components([
//
Section::make('Liên kết & Mẫu thanh toán')
->columns(2)
->schema([
Select::make('product_id')
->label('Sản phẩm')
->relationship('product', 'code')
->required()
->live()
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$product = Product::find($state);
if ($product) {
$set('total_value', $product->total_price);
}
}
}),
Select::make('payment_template_id')
->label('Mẫu thanh toán')
->options(fn () => PaymentTemplate::pluck('name', 'id'))
->required()
->dehydrated(false),
Select::make('customers')
->label('Khách hàng')
->relationship('customers', 'full_name')
->multiple()
->required()
->columnSpanFull(),
]),
Section::make('Chi tiết Hợp đồng')
->columns(2)
->schema([
TextInput::make('contract_number')->label('Số HĐ')->required(),
Select::make('contract_type')
->label('Loại HĐ')
->options([
'HĐMB' => 'HĐMB',
'HĐGV' => 'HĐGV',
'HĐDC' => 'HĐDC',
])
->default('HĐMB')
->required(),
DatePicker::make('signing_date')->label('Ngày ký')->required(),
TextInput::make('total_value')
->label('Giá trị HĐ')
->numeric()
->required()
->prefix('VND'),
Select::make('status')
->label('Trạng thái')
->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(),
])
]);
}
}

View File

@@ -45,6 +45,9 @@ class PaymentTemplateResource extends Resource
TextInput::make('installment_no')->label('Đợt')->numeric()->required(),
Select::make('type')->label('Loại')->options(PaymentType::class)->required(),
TextInput::make('percentage')->label('%')->numeric()->required(),
TextInput::make('days_after_signing')->label('Ngày sau ký (F0)')->numeric(),
TextInput::make('days_after_previous')->label('Ngày sau đợt trước')->numeric(),
\Filament\Forms\Components\DatePicker::make('due_date')->label('Ngày cố định'),
])->columns(3),
])
]);

View File

@@ -4,19 +4,13 @@ namespace App\Filament\Resources\Products;
use App\Filament\Resources\Products\Pages;
use App\Models\Product;
use App\Enums\ProductType;
use App\Enums\NavigationGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Repeater;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use App\Filament\Resources\Products\ProductResource\RelationManagers\ContractsRelationManager;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use App\Filament\Resources\Products\ProductResource\RelationManagers\ContractsRelationManager;
use App\Filament\Resources\Products\Schemas\ProductForm;
class ProductResource extends Resource
{
@@ -30,43 +24,12 @@ class ProductResource extends Resource
public static function form(Schema $schema): Schema
{
return $schema
->components([
Tabs::make('ProductDetails')
->tabs([
Tabs\Tab::make('Thông tin chung')
->icon('heroicon-o-information-circle')
->columns(2)
->schema([
Select::make('project_id')->label('Dự án')->relationship('project', 'name')->required(),
Select::make('product_type')->label('Loại sản phẩm')->options(ProductType::class)->required(),
TextInput::make('code')->label('Mã SP')->required()->unique(ignoreRecord: true),
Select::make('status')
->label('Trạng thái')
->options(['Đang mở bán' => 'Đang mở bán', 'Đã cọc' => 'Đã cọc', 'Đã bán' => 'Đã bán'])
->required(),
]),
Tabs\Tab::make('Tài chính')
->icon('heroicon-o-currency-dollar')
->schema([
TextInput::make('area')->label('Diện tích (m2)')->numeric()->required(),
TextInput::make('price_per_unit')->label('Đơn giá')->numeric()->required(),
TextInput::make('total_price')->label('Tổng giá')->numeric()->required(),
]),
])->columnSpanFull()
]);
return ProductForm::configure($schema);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('project.code')->label('Dự án'),
Tables\Columns\TextColumn::make('code')->label('Mã SP')->searchable(),
Tables\Columns\TextColumn::make('product_type')->label('Loại')->badge(),
Tables\Columns\TextColumn::make('total_price')->label('Giá')->money('VND'),
Tables\Columns\TextColumn::make('status')->label('Trạng thái')->badge(),
]);
return \App\Filament\Resources\Products\Tables\ProductsTable::configure($table);
}
public static function getRelations(): array { return [ContractsRelationManager::class]; }

View File

@@ -2,6 +2,12 @@
namespace App\Filament\Resources\Products\Schemas;
use App\Enums\ProductType;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\KeyValue;
use Filament\Schemas\Schema;
class ProductForm
@@ -10,7 +16,76 @@ class ProductForm
{
return $schema
->components([
//
Tabs::make('ProductDetails')
->tabs([
Tabs\Tab::make('Thông tin chung')
->icon('heroicon-o-information-circle')
->columns(2)
->schema([
Select::make('project_id')->label('Dự án')->relationship('project', 'name')->required(),
Select::make('product_type')->label('Loại sản phẩm')->options(ProductType::class)->required(),
TextInput::make('code')->label('Mã SP')->required(),
Select::make('status')
->label('Trạng thái')
->options([
'Đang mở bán' => 'Đang mở bán',
'Đã cọc' => 'Đã cọc',
'Đã bán' => 'Đã bán'
])->required(),
]),
Tabs\Tab::make('Tài chính')
->icon('heroicon-o-currency-dollar')
->columns(2)
->schema([
TextInput::make('area')->label('Diện tích (m2)')->numeric()->required(),
TextInput::make('price_per_unit')->label('Đơn giá')->numeric()->required()->prefix('VND'),
TextInput::make('total_price')->label('Tổng giá')->numeric()->required()->prefix('VND'),
Section::make('Bổ sung')
->columns(2)
->schema([
TextInput::make('qsdd_value')->label('Giá trị QSDĐ')->numeric()->prefix('VND'),
TextInput::make('foundation_temp_value')->label('Giá trị móng (tạm tính)')->numeric()->prefix('VND'),
])
]),
Tabs\Tab::make('Kỹ thuật & Hạ tầng')
->icon('heroicon-o-cog')
->columns(2)
->schema([
TextInput::make('adjacent_road')->label('Đường tiếp giáp'),
TextInput::make('frontage_count')->label('Số mặt tiền')->numeric(),
TextInput::make('max_floors')->label('Số tầng tối đa')->numeric(),
TextInput::make('construction_status')->label('Trạng thái XD'),
KeyValue::make('infrastructure_status')
->label('Trạng thái hạ tầng')
->columnSpanFull(),
]),
Tabs\Tab::make('Tiến độ thanh toán')
->icon('heroicon-o-calendar-days')
->schema([
\Filament\Forms\Components\Repeater::make('payment_schedule_preview')
->label('Lịch trình dự kiến / Thực tế')
->relationship(function ($record) {
// Nếu sản phẩm đã có hợp đồng, lấy lịch trình từ hợp đồng đầu tiên
if ($record && $record->contracts()->exists()) {
return $record->contracts()->first()->scheduleItems();
}
// Nếu chưa có, chúng ta sẽ hiển thị mẫu từ Project (phần này cần logic state giả lập hoặc view)
return null;
})
->schema([
TextInput::make('installment_no')->label('Đợt')->disabled(),
TextInput::make('type')->label('Loại')->disabled(),
TextInput::make('percentage')->label('%')->disabled(),
TextInput::make('amount')->label('Số tiền (VNĐ)')->numeric()->disabled(),
DatePicker::make('due_date')->label('Ngày đến hạn')->disabled(),
])
->columns(5)
->addable(false)
->deletable(false)
->reorderable(false)
->placeholder('Sản phẩm chưa có hợp đồng hoặc Dự án chưa gán mẫu thanh toán mặc định.')
]),
])->columnSpanFull()
]);
}
}

View File

@@ -5,7 +5,10 @@ namespace App\Filament\Resources\Products\Tables;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use App\Models\Project;
class ProductsTable
{
@@ -13,10 +16,45 @@ class ProductsTable
{
return $table
->columns([
//
TextColumn::make('project.code')
->label('Dự án')
->sortable()
->searchable(),
TextColumn::make('code')
->label('Mã SP')
->sortable()
->searchable(),
TextColumn::make('product_type')
->label('Loại')
->badge(),
TextColumn::make('total_price')
->label('Giá')
->money('VND')
->sortable(),
TextColumn::make('status')
->label('Trạng thái')
->badge()
->color(fn (string $state): string => match ($state) {
'Đang mở bán' => 'success',
'Đã cọc' => 'warning',
'Đã bán' => 'danger',
default => 'gray',
}),
])
->filters([
//
SelectFilter::make('project_id')
->label('Dự án')
->relationship('project', 'name'),
SelectFilter::make('status')
->label('Trạng thái')
->options([
'Đang mở bán' => 'Đang mở bán',
'Đã cọc' => 'Đã cọc',
'Đã bán' => 'Đã bán',
]),
SelectFilter::make('product_type')
->label('Loại sản phẩm')
->options(\App\Enums\ProductType::class),
])
->recordActions([
EditAction::make(),

View File

@@ -32,6 +32,11 @@ class ProjectResource extends Resource
->schema([
TextInput::make('code')->label('Mã Dự án')->required()->unique(ignoreRecord: true),
TextInput::make('name')->label('Tên Dự án')->required(),
Select::make('payment_template_id')
->label('Mẫu thanh toán mặc định')
->relationship('paymentTemplate', 'name')
->placeholder('Chọn mẫu thanh toán cho toàn dự án')
->columnSpanFull(),
Select::make('type')
->label('Loại hình')
->options([

View File

@@ -30,7 +30,8 @@ class Contract extends Model
public function customers()
{
return $this->belongsToMany(Customer::class, 'contract_customers')
->withPivot('role', 'transfer_order')
->using(ContractCustomer::class)
->withPivot('id', 'role', 'transfer_order')
->withTimestamps();
}
@@ -68,4 +69,20 @@ class Contract extends Model
{
return $this->hasMany(PaymentFine::class);
}
protected static function booted()
{
static::creating(function ($contract) {
// Tự động lấy giá trị từ sản phẩm nếu chưa có
if (empty($contract->total_value) && !empty($contract->product_id)) {
$product = Product::find($contract->product_id);
if ($product) {
$contract->total_value = $product->total_price;
}
}
// Tính toán số tiền còn lại
$contract->remaining_amount = ($contract->total_value ?? 0) - ($contract->paid_amount ?? 0);
});
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\Pivot;
class ContractCustomer extends Pivot
{
use HasUuids;
protected $table = 'contract_customers';
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
}

View File

@@ -21,7 +21,8 @@ class Customer extends Model
public function contracts()
{
return $this->belongsToMany(Contract::class, 'contract_customers')
->withPivot('role', 'transfer_order')
->using(ContractCustomer::class)
->withPivot('id', 'role', 'transfer_order')
->withTimestamps();
}
}

View File

@@ -11,12 +11,17 @@ class Project extends Model
use HasUuids, HasFactory;
protected $guarded = [];
public function products()
{
return $this->hasMany(Product::class);
}
public function paymentTemplate()
{
return $this->belongsTo(PaymentTemplate::class);
}
public function paymentTemplates()
{
return $this->hasMany(PaymentTemplate::class);