Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Bank Statement and Reconciliation Functionality #151

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions app/Filament/App/Resources/BankStatementResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\BankStatementResource\Pages;
use App\Models\BankStatement;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;

class BankStatementResource extends Resource
{
protected static ?string $model = BankStatement::class;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\DatePicker::make('statement_date')
->required(),
Forms\Components\Select::make('account_id')
->relationship('account', 'name')
->required(),
Forms\Components\TextInput::make('total_credits')
->required()
->numeric(),
Forms\Components\TextInput::make('total_debits')
->required()
->numeric(),
Forms\Components\TextInput::make('ending_balance')
->required()
->numeric(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('statement_date'),
Tables\Columns\TextColumn::make('account.name'),
Tables\Columns\TextColumn::make('total_credits'),
Tables\Columns\TextColumn::make('total_debits'),
Tables\Columns\TextColumn::make('ending_balance'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListBankStatements::route('/'),
'create' => Pages\CreateBankStatement::route('/create'),
'edit' => Pages\EditBankStatement::route('/{record}/edit'),
];
}
}
17 changes: 15 additions & 2 deletions app/Filament/App/Resources/TransactionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ public static function form(Form $form): Form
BelongsToSelect::make('credit_account_id')
->relationship('creditAccount', 'name')
->label('Credit Account'),
Forms\Components\Toggle::make('reconciled')
->label('Reconciled'),
Forms\Components\Textarea::make('discrepancy_notes')
->label('Discrepancy Notes'),
]);
}

public static function table(Table $table): Table
{
return $table
Expand All @@ -69,9 +73,18 @@ public static function table(Table $table): Table
->label('Credit Account')
->searchable()
->sortable(),
Tables\Columns\IconColumn::make('reconciled')
->boolean(),
TextColumn::make('discrepancy_notes')
->label('Discrepancy Notes')
->searchable(),
])
->filters([
//
Tables\Filters\SelectFilter::make('reconciled')
->options([
true => 'Reconciled',
false => 'Not Reconciled',
]),
])
->actions([
Tables\Actions\EditAction::make(),
Expand Down
29 changes: 29 additions & 0 deletions app/Models/BankStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BankStatement extends Model
{
use HasFactory;

protected $fillable = [
'statement_date',
'account_id',
'total_credits',
'total_debits',
'ending_balance',
];

public function account()
{
return $this->belongsTo(Account::class);
}

public function transactions()
{
return $this->hasMany(Transaction::class);
}
}
7 changes: 6 additions & 1 deletion app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Transaction extends Model
'amount',
'debit_account_id',
'credit_account_id',
// Add any other necessary fields for double-entry accounting here
'reconciled',
'discrepancy_notes',
];

protected $casts = [
'reconciled' => 'boolean',
];

public function debitAccount()
Expand Down
49 changes: 49 additions & 0 deletions app/Services/ReconciliationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Services;

use App\Models\BankStatement;
use App\Models\Transaction;

class ReconciliationService
{
public function reconcile(BankStatement $bankStatement)
{
$transactions = Transaction::where('account_id', $bankStatement->account_id)
->whereBetween('transaction_date', [$bankStatement->statement_date->startOfMonth(), $bankStatement->statement_date->endOfMonth()])
->get();

$totalCredits = 0;
$totalDebits = 0;

foreach ($transactions as $transaction) {
if ($transaction->amount > 0) {
$totalCredits += $transaction->amount;
} else {
$totalDebits += abs($transaction->amount);
}

$this->matchTransaction($transaction, $bankStatement);
}

$discrepancy = ($totalCredits - $totalDebits) - ($bankStatement->total_credits - $bankStatement->total_debits);

return [
'matched_transactions' => $transactions->where('reconciled', true)->count(),
'unmatched_transactions' => $transactions->where('reconciled', false)->count(),
'discrepancy' => $discrepancy,
];
}

private function matchTransaction(Transaction $transaction, BankStatement $bankStatement)
{
// Implement matching logic here
// For example, match by date and amount
$matched = $bankStatement->transactions()
->where('transaction_date', $transaction->transaction_date)
->where('amount', $transaction->amount)
->exists();

$transaction->update(['reconciled' => $matched]);
}
}
46 changes: 46 additions & 0 deletions tests/Feature/Services/ReconciliationServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Tests\Feature\Services;

use App\Models\Account;
use App\Models\BankStatement;
use App\Models\Transaction;
use App\Services\ReconciliationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ReconciliationServiceTest extends TestCase
{
use RefreshDatabase;

public function test_reconciliation_service_matches_transactions()
{
$account = Account::factory()->create();
$bankStatement = BankStatement::factory()->create([
'account_id' => $account->id,
'statement_date' => now(),
'total_credits' => 1000,
'total_debits' => 500,
'ending_balance' => 500,
]);

Transaction::factory()->count(3)->create([
'account_id' => $account->id,
'transaction_date' => now(),
'amount' => 300,
]);

Transaction::factory()->count(2)->create([
'account_id' => $account->id,
'transaction_date' => now(),
'amount' => -200,
]);

$reconciliationService = new ReconciliationService();
$result = $reconciliationService->reconcile($bankStatement);

$this->assertEquals(5, $result['matched_transactions']);
$this->assertEquals(0, $result['unmatched_transactions']);
$this->assertEquals(0, $result['discrepancy']);
}
}
Loading