From f4750183bb39c4da27133d3823486820cd0e0aa0 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 24 Dec 2024 10:22:51 +0000 Subject: [PATCH] Add Supplier Relationship to Expenses --- app/Filament/Resources/ExpenseResource.php | 24 ++++++++++++++++ app/Models/Expense.php | 8 +++++- ...0000_add_supplier_id_to_expenses_table.php | 28 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2024_01_15_000000_add_supplier_id_to_expenses_table.php diff --git a/app/Filament/Resources/ExpenseResource.php b/app/Filament/Resources/ExpenseResource.php index f3e5538a..57df9720 100644 --- a/app/Filament/Resources/ExpenseResource.php +++ b/app/Filament/Resources/ExpenseResource.php @@ -6,6 +6,7 @@ use App\Filament\Resources\ExpenseResource\Pages; use App\Models\Expense; +use App\Models\Supplier; use Filament\Forms; use Filament\Resources\Resource; use Filament\Tables; @@ -23,6 +24,25 @@ public static function form(Forms\Form $form): Forms\Form { return $form ->schema([ + Forms\Components\Select::make('supplier_id') + ->relationship('supplier', 'supplier_first_name', fn ($query) => $query->orderBy('supplier_first_name')) + ->searchable() + ->preload() + ->label('Supplier') + ->createOptionForm([ + Forms\Components\TextInput::make('supplier_first_name') + ->required() + ->label('First Name'), + Forms\Components\TextInput::make('supplier_last_name') + ->required() + ->label('Last Name'), + Forms\Components\TextInput::make('supplier_email') + ->email() + ->label('Email'), + Forms\Components\TextInput::make('supplier_phone_number') + ->tel() + ->label('Phone Number'), + ]), Forms\Components\TextInput::make('amount') ->required() ->numeric() @@ -54,6 +74,10 @@ public static function table(Tables\Table $table): Tables\Table { return $table ->columns([ + Tables\Columns\TextColumn::make('supplier.supplier_first_name') + ->label('Supplier') + ->searchable() + ->sortable(), Tables\Columns\TextColumn::make('amount') ->money('USD') ->sortable(), diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 6dffa7b9..cf94ff94 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -22,7 +22,8 @@ class Expense extends Model 'project_id', 'cost_center_id', 'is_indirect', - 'allocation_percentage' + 'allocation_percentage', + 'supplier_id' ]; protected $casts = [ @@ -53,6 +54,11 @@ public function costCenter(): BelongsTo return $this->belongsTo(CostCenter::class); } + public function supplier(): BelongsTo + { + return $this->belongsTo(Supplier::class, 'supplier_id', 'supplier_id'); + } + public function approve() { $this->update([ diff --git a/database/migrations/2024_01_15_000000_add_supplier_id_to_expenses_table.php b/database/migrations/2024_01_15_000000_add_supplier_id_to_expenses_table.php new file mode 100644 index 00000000..3a250e79 --- /dev/null +++ b/database/migrations/2024_01_15_000000_add_supplier_id_to_expenses_table.php @@ -0,0 +1,28 @@ + + +foreignId('supplier_id') + ->nullable() + ->constrained('suppliers', 'supplier_id') + ->nullOnDelete(); + }); + } + + public function down() + { + Schema::table('expenses', function (Blueprint $table) { + $table->dropForeign(['supplier_id']); + $table->dropColumn('supplier_id'); + }); + } +}; \ No newline at end of file