Skip to content

Commit

Permalink
Merge pull request #358 from liberu-accounting/sweep/Add-Time-Entry-T…
Browse files Browse the repository at this point in the history
…racking-and-Invoice-Calculation-Features

Add Time Entry Tracking and Invoice Calculation Features
  • Loading branch information
curtisdelicata authored Dec 24, 2024
2 parents b789fc4 + 4a5bc5d commit 75e2293
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
84 changes: 84 additions & 0 deletions app/Filament/App/Resources/TimeEntryResource.php
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'),
];
}
}
11 changes: 11 additions & 0 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function taxRate()
return $this->belongsTo(TaxRate::class);
}

public function timeEntries()
{
return $this->hasMany(TimeEntry::class, 'invoice_id');
}

public function calculateTax()
{
if (!$this->taxRate) {
Expand All @@ -50,4 +55,10 @@ public function getTotalWithTax()
{
return $this->total_amount + $this->tax_amount;
}

public function calculateTotalFromTimeEntries()
{
$this->total_amount = $this->timeEntries->sum('total_amount');
return $this->total_amount;
}
}
47 changes: 47 additions & 0 deletions app/Models/TimeEntry.php
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;
}
}
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');
}
};

0 comments on commit 75e2293

Please sign in to comment.