First Commit
This commit is contained in:
+178
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\FluxoCaixaPro\Services;
|
||||
|
||||
use Modules\FluxoCaixaPro\Models\Transaction;
|
||||
use Modules\FluxoCaixaPro\Models\Category;
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* Class CashFlowService
|
||||
*
|
||||
* Handles business logic for cash flow management.
|
||||
*
|
||||
* @package Modules\FluxoCaixaPro\Services
|
||||
*/
|
||||
class CashFlowService
|
||||
{
|
||||
/**
|
||||
* Get summary of cash flow for a specific period.
|
||||
*
|
||||
* @param string|null $startDate
|
||||
* @param string|null $endDate
|
||||
* @return array
|
||||
*/
|
||||
public function getSummary(?string $startDate = null, ?string $endDate = null): array
|
||||
{
|
||||
$query = Transaction::query();
|
||||
|
||||
if ($startDate) {
|
||||
$query->where('transaction_date', '>=', $startDate);
|
||||
}
|
||||
if ($endDate) {
|
||||
$query->where('transaction_date', '<=', $endDate);
|
||||
}
|
||||
|
||||
$transactions = $query->get();
|
||||
|
||||
// Total amounts (including personal)
|
||||
$totalIncome = $transactions->where('type', 'income')->sum('amount');
|
||||
$totalExpense = $transactions->where('type', 'expense')->sum('amount');
|
||||
$netBalance = $totalIncome - $totalExpense;
|
||||
|
||||
// Operational amounts (Business only)
|
||||
$businessTransactions = $transactions->where('is_personal', false);
|
||||
$opIncome = $businessTransactions->where('type', 'income')->sum('amount');
|
||||
$opExpense = $businessTransactions->where('type', 'expense')->sum('amount');
|
||||
$opBalance = $opIncome - $opExpense;
|
||||
|
||||
// Personal amounts
|
||||
$personalTransactions = $transactions->where('is_personal', true);
|
||||
$personalIncome = $personalTransactions->where('type', 'income')->sum('amount');
|
||||
$personalExpense = $personalTransactions->where('type', 'expense')->sum('amount');
|
||||
|
||||
// Accounts Payable/Receivable
|
||||
$pendingIncome = Transaction::where('type', 'income')->where('status', 'pending')->sum('amount');
|
||||
$pendingExpense = Transaction::where('type', 'expense')->where('status', 'pending')->sum('amount');
|
||||
|
||||
return [
|
||||
'total_income' => $totalIncome,
|
||||
'total_expense' => $totalExpense,
|
||||
'net_balance' => $netBalance,
|
||||
'operational_income' => $opIncome,
|
||||
'operational_expense' => $opExpense,
|
||||
'operational_balance' => $opBalance,
|
||||
'personal_income' => $personalIncome,
|
||||
'personal_expense' => $personalExpense,
|
||||
'pending_income' => $pendingIncome,
|
||||
'pending_expense' => $pendingExpense,
|
||||
'transaction_count' => $transactions->count()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get monthly breakdown of cash flow.
|
||||
*
|
||||
* @param int $months
|
||||
* @return array
|
||||
*/
|
||||
public function getMonthlyBreakdown(int $months = 6): array
|
||||
{
|
||||
$startDate = Carbon::now()->subMonths($months)->startOfMonth();
|
||||
|
||||
$data = Transaction::where('transaction_date', '>=', $startDate)
|
||||
->select(
|
||||
\Illuminate\Database\Capsule\Manager::raw('DATE_FORMAT(transaction_date, "%Y-%m") as month'),
|
||||
\Illuminate\Database\Capsule\Manager::raw('SUM(CASE WHEN type = "income" THEN amount ELSE 0 END) as income'),
|
||||
\Illuminate\Database\Capsule\Manager::raw('SUM(CASE WHEN type = "expense" THEN amount ELSE 0 END) as expense')
|
||||
)
|
||||
->groupBy('month')
|
||||
->orderBy('month', 'asc')
|
||||
->get();
|
||||
|
||||
return $data->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get distribution by category.
|
||||
*
|
||||
* @param string $type 'income' or 'expense'
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryDistribution(string $type = 'expense'): array
|
||||
{
|
||||
return Transaction::where('type', $type)
|
||||
->with('category')
|
||||
->select('category_id', \Illuminate\Database\Capsule\Manager::raw('SUM(amount) as total'))
|
||||
->groupBy('category_id')
|
||||
->get()
|
||||
->map(function ($item) {
|
||||
return [
|
||||
'category' => $item->category->name ?? 'Sem Categoria',
|
||||
'total' => $item->total,
|
||||
'color' => $item->category->color ?? '#6c757d'
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long term projections with scenarios.
|
||||
*
|
||||
* @param int $months
|
||||
* @return array
|
||||
*/
|
||||
public function getProjections(int $months = 12): array
|
||||
{
|
||||
$startDate = Carbon::now()->subMonths(12);
|
||||
$history = Transaction::where('transaction_date', '>=', $startDate)
|
||||
->where('is_personal', false)
|
||||
->get();
|
||||
|
||||
if ($history->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$avgMonthlyIncome = $history->where('type', 'income')->sum('amount') / 12;
|
||||
$avgMonthlyExpense = $history->where('type', 'expense')->sum('amount') / 12;
|
||||
|
||||
$projections = [];
|
||||
for ($i = 1; $i <= $months; $i++) {
|
||||
$month = Carbon::now()->addMonths($i)->format('Y-m');
|
||||
|
||||
// Realistic Scenario (Current average + slight growth)
|
||||
$realisticIncome = $avgMonthlyIncome * (1 + ($i * 0.01));
|
||||
$realisticExpense = $avgMonthlyExpense * (1 + ($i * 0.005));
|
||||
|
||||
// Optimistic Scenario (+5% income, -2% expense vs realistic)
|
||||
$optimisticIncome = $realisticIncome * 1.05;
|
||||
$optimisticExpense = $realisticExpense * 0.98;
|
||||
|
||||
// Pessimistic Scenario (-10% income, +5% expense vs realistic)
|
||||
$pessimisticIncome = $realisticIncome * 0.90;
|
||||
$pessimisticExpense = $realisticExpense * 1.05;
|
||||
|
||||
$projections[] = [
|
||||
'month' => $month,
|
||||
'realistic' => [
|
||||
'income' => $realisticIncome,
|
||||
'expense' => $realisticExpense,
|
||||
'balance' => $realisticIncome - $realisticExpense
|
||||
],
|
||||
'optimistic' => [
|
||||
'income' => $optimisticIncome,
|
||||
'expense' => $optimisticExpense,
|
||||
'balance' => $optimisticIncome - $optimisticExpense
|
||||
],
|
||||
'pessimistic' => [
|
||||
'income' => $pessimisticIncome,
|
||||
'expense' => $pessimisticExpense,
|
||||
'balance' => $pessimisticIncome - $pessimisticExpense
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
return $projections;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user