-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from liberu-accounting/sweep/Add-Time-Entry-T…
…racking-and-Invoice-Calculation-Features Add Time Entry Tracking and Invoice Calculation Features
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Filament\App\Resources; | ||
|
||
use App\Filament\App\Resources\TimeEntryResource\Pages; | ||
use App\Models\TimeEntry; | ||
use Filament\Forms; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Filament\Tables\Table; | ||
|
||
class TimeEntryResource extends Resource | ||
{ | ||
protected static ?string $model = TimeEntry::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-clock'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
Forms\Components\BelongsToSelect::make('customer_id') | ||
->relationship('customer', 'customer_name') | ||
->required(), | ||
Forms\Components\BelongsToSelect::make('invoice_id') | ||
->relationship('invoice', 'invoice_id') | ||
->nullable(), | ||
Forms\Components\DateTimePicker::make('start_time') | ||
->required(), | ||
Forms\Components\DateTimePicker::make('end_time') | ||
->required(), | ||
Forms\Components\TextInput::make('description') | ||
->required(), | ||
Forms\Components\TextInput::make('hourly_rate') | ||
->numeric() | ||
->required() | ||
->reactive() | ||
->afterStateUpdated(fn ($state, callable $set, TimeEntry $record) => | ||
$set('total_amount', $record->calculateTotalAmount()) | ||
), | ||
Forms\Components\TextInput::make('total_amount') | ||
->numeric() | ||
->disabled(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
Tables\Columns\TextColumn::make('customer.customer_name'), | ||
Tables\Columns\TextColumn::make('start_time'), | ||
Tables\Columns\TextColumn::make('end_time'), | ||
Tables\Columns\TextColumn::make('description'), | ||
Tables\Columns\TextColumn::make('hourly_rate'), | ||
Tables\Columns\TextColumn::make('total_amount'), | ||
Tables\Columns\TextColumn::make('invoice_id'), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->actions([ | ||
Tables\Actions\EditAction::make(), | ||
Tables\Actions\DeleteAction::make(), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make(), | ||
]), | ||
]); | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListTimeEntries::route('/'), | ||
'create' => Pages\CreateTimeEntry::route('/create'), | ||
'edit' => Pages\EditTimeEntry::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TimeEntry extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'customer_id', | ||
'invoice_id', | ||
'start_time', | ||
'end_time', | ||
'description', | ||
'hourly_rate', | ||
'total_amount' | ||
]; | ||
|
||
protected $casts = [ | ||
'start_time' => 'datetime', | ||
'end_time' => 'datetime', | ||
'hourly_rate' => 'decimal:2', | ||
'total_amount' => 'decimal:2' | ||
]; | ||
|
||
public function customer() | ||
{ | ||
return $this->belongsTo(Customer::class, 'customer_id'); | ||
} | ||
|
||
public function invoice() | ||
{ | ||
return $this->belongsTo(Invoice::class, 'invoice_id'); | ||
} | ||
|
||
public function calculateTotalAmount() | ||
{ | ||
$hours = $this->end_time->diffInHours($this->start_time); | ||
$this->total_amount = $hours * $this->hourly_rate; | ||
return $this->total_amount; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2024_02_24_000000_create_time_entries_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
|
||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::create('time_entries', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('customer_id'); | ||
$table->integer('invoice_id')->nullable(); | ||
$table->dateTime('start_time'); | ||
$table->dateTime('end_time'); | ||
$table->text('description'); | ||
$table->decimal('hourly_rate', 10, 2); | ||
$table->decimal('total_amount', 10, 2); | ||
$table->timestamps(); | ||
|
||
$table->foreign('customer_id')->references('customer_id')->on('customers')->onDelete('cascade'); | ||
$table->foreign('invoice_id')->references('invoice_id')->on('invoices')->onDelete('set null'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('time_entries'); | ||
} | ||
}; |