baks-dev/finances
BaksDev Finances — PHP 8.4+ модуль для Laravel/Symfony проектов: установка через Composer, установка конфигураций и ресурсов (baks:assets:install), поддержка миграций Doctrine и тестов PHPUnit (group=finances).
Install the Package
composer require baks-dev/finances
Ensure your project uses PHP 8.4+ and Laravel 10+ (or a compatible Symfony-based setup).
Publish Assets and Config Run the package’s installation command to set up default configurations and assets:
php artisan baks:assets:install
This typically generates:
config/finances.php)Run Migrations Generate and apply database migrations to create the required tables:
php artisan doctrine:migrations:diff
php artisan doctrine:migrations:migrate
Note: If using Laravel’s native migrations, ensure the package’s migrations are merged into your existing migration files.
First Use Case: Create a Financial Transaction Use the package’s models to create a basic transaction. For example:
use BaksDev\Finances\Models\Transaction;
$transaction = Transaction::create([
'amount' => 100.00,
'currency' => 'USD',
'type' => 'income', // or 'expense', 'fee', etc.
'description' => 'Monthly subscription revenue',
'account_id' => 1, // Reference to an Account model
]);
Verify the transaction appears in the database and can be retrieved via Eloquent:
$transactions = Transaction::all();
Verify with Tests Run the package’s test suite to ensure core functionality works:
php artisan test --group=finances
Transaction model to handle financial movements between accounts.$transaction = Transaction::create([
'amount' => 50.00,
'currency' => 'EUR',
'type' => 'expense',
'account_id' => $sourceAccount->id,
'related_account_id' => $destinationAccount->id,
'metadata' => ['invoice_id' => 'INV-123'],
]);
Transaction::validate()) or extend with custom rules:
$validator = Validator::make($data, [
'amount' => 'required|numeric|min:0',
'type' => 'required|in:income,expense,fee,refund',
]);
Transaction::where('status', 'pending')->chunk(100, function ($transactions) {
foreach ($transactions as $transaction) {
// Process and update transaction status
$transaction->update(['status' => 'completed']);
}
});
Account model.use BaksDev\Finances\Models\Account;
$account = Account::create([
'name' => 'Customer Deposits',
'type' => 'asset', // or 'liability', 'equity', 'revenue', 'expense'
'currency' => 'USD',
'balance' => 0.00,
]);
currency field and handle conversions via a service:
$convertedAmount = app(\BaksDev\Finances\Services\CurrencyConverter::class)
->convert($amount, 'USD', 'EUR');
Ledger or Report models.use BaksDev\Finances\Reports\ProfitLossReport;
$report = new ProfitLossReport();
$data = $report->generate(
startDate: now()->startOfMonth(),
endDate: now()->endOfMonth()
);
ReportInterface:
namespace App\Reports;
use BaksDev\Finances\Contracts\ReportInterface;
class CustomReport implements ReportInterface {
public function generate($startDate, $endDate) {
// Custom logic
return $data;
}
}
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => $transaction->amount * 100, // in cents
'currency' => $transaction->currency,
'metadata' => ['transaction_id' => $transaction->id],
]);
$transaction->update([
'status' => 'paid',
'payment_id' => $paymentIntent->id,
'payment_gateway' => 'stripe',
]);
FinancesServiceProvider. Override or extend bindings in your AppServiceProvider:
public function register()
{
$this->app->bind(
\BaksDev\Finances\Contracts\TransactionProcessor::class,
\App\Services\CustomTransactionProcessor::class
);
}
use BaksDev\Finances\Console\Commands\BaseCommand;
class ReconcileAccountsCommand extends BaseCommand {
protected $signature = 'finances:reconcile {account}';
protected $description = 'Reconcile transactions for a specific account';
public function handle()
{
// Custom reconciliation logic
}
}
app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\ReconcileAccountsCommand::class,
];
TransactionCreated, AccountBalanceUpdated) to trigger side effects:
use BaksDev\Finances\Events\TransactionCreated;
Event::listen(TransactionCreated::class, function ($event) {
// Send notification, log audit trail, etc.
Log::info('New transaction created', $event->transaction->toArray());
});
ApiResource:
namespace App\Http\Resources;
use BaksDev\Finances\Models\Transaction;
use Illuminate\Http\Resources\Json\JsonResource;
class TransactionResource extends JsonResource {
public function toArray($request)
{
return [
'id' => $this->id,
'amount' => $this->amount,
'currency' => $this->currency,
'type' => $this->type,
'metadata' => $this->metadata,
'created_at' => $this->created_at->toIso8601String(),
];
}
}
public function index()
{
return TransactionResource::collection(Transaction::all());
}
use BaksDev\Finances\Models\Transaction;
use Illuminate\Auth\Access\HandlesAuthorization;
class TransactionPolicy {
use HandlesAuthorization;
public function viewAny(User $user)
{
return $user->hasRole('accountant');
}
public function create(User $user)
{
return $user->hasRole(['admin', 'finance']);
}
}
AuthServiceProvider:
protected $policies = [
Transaction::class => TransactionPolicy::class,
];
database/migrations/) before running them.How can I help you explore Laravel packages today?