Skip to content

Commit

Permalink
Merge pull request #380 from liberu-accounting/sweep/Add-Supplier-Rel…
Browse files Browse the repository at this point in the history
…ationship-to-Expenses

Add Supplier Relationship to Expenses
  • Loading branch information
curtisdelicata authored Dec 24, 2024
2 parents 76c9f43 + b7ad37d commit 55f4fb4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/Filament/Resources/ExpenseResource.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@


<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ExpenseResource\Pages;
use App\Models\Expense;
use App\Models\Supplier;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
Expand All @@ -23,6 +22,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\BelongsToSelect::make('currency_id')
->relationship('currency', 'code')
->required()
Expand Down Expand Up @@ -74,6 +92,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('currency.code')
->label('Currency')
->sortable(),
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Expense.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Expense extends Model
'cost_center_id',
'is_indirect',
'allocation_percentage',
'supplier_id'
'currency_id'
];

Expand Down Expand Up @@ -55,6 +56,10 @@ public function costCenter(): BelongsTo
return $this->belongsTo(CostCenter::class);
}

public function supplier(): BelongsTo
{
return $this->belongsTo(Supplier::class, 'supplier_id', 'supplier_id');
}
public function currency(): BelongsTo
{
return $this->belongsTo(Currency::class, 'currency_id');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('expenses', function (Blueprint $table) {
$table->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');
});
}
};

0 comments on commit 55f4fb4

Please sign in to comment.