akaunting/app/Widgets/CashFlow.php

220 lines
6.3 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Widgets;
2022-06-16 17:57:43 +03:00
use Akaunting\Apexcharts\Chart;
2019-12-29 03:01:19 +03:00
use App\Abstracts\Widget;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
2022-06-24 15:56:29 +03:00
use App\Traits\Charts;
2019-11-16 10:21:14 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2022-06-01 10:15:55 +03:00
use App\Utilities\Date;
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
class CashFlow extends Widget
2019-11-16 10:21:14 +03:00
{
2022-06-24 15:56:29 +03:00
use Charts, Currencies, DateTime;
2019-11-16 10:21:14 +03:00
2020-01-16 00:42:20 +03:00
public $default_name = 'widgets.cash_flow';
2020-01-10 17:49:31 +03:00
2020-01-16 00:42:20 +03:00
public $default_settings = [
2022-10-18 17:45:13 +03:00
'width' => 'w-full my-8 lg:px-12',
2020-01-16 00:42:20 +03:00
];
2020-01-10 17:49:31 +03:00
2022-06-01 10:15:55 +03:00
public $description = 'widgets.description.cash_flow';
public $report_class = 'Modules\CashFlowStatement\Reports\CashFlowStatement';
public $start_date;
public $end_date;
public $period;
2019-12-29 03:01:19 +03:00
public function show()
2022-06-01 10:15:55 +03:00
{
$this->setFilter();
$income = array_values($this->calculateTotals('income'));
$expense = array_values($this->calculateTotals('expense'));
$profit = array_values($this->calculateProfit($income, $expense));
2022-06-16 17:57:43 +03:00
$chart = new Chart();
2022-06-01 10:15:55 +03:00
$chart->setType('line')
2022-08-13 12:37:30 +03:00
->setDefaultLocale($this->getDefaultLocaleOfChart())
->setLocales($this->getLocaleTranslationOfChart())
2022-08-13 15:13:25 +03:00
->setStacked(true)
->setBar(['columnWidth' => '40%'])
->setLegendPosition('top')
->setYaxisLabels(['formatter' => $this->getChartLabelFormatter()])
->setLabels(array_values($this->getLabels()))
->setColors($this->getColors())
2022-06-01 10:15:55 +03:00
->setDataset(trans('general.incoming'), 'column', $income)
->setDataset(trans('general.outgoing'), 'column', $expense)
->setDataset(trans_choice('general.profits', 1), 'line', $profit);
$incoming_amount = money(array_sum($income));
$outgoing_amount = money(abs(array_sum($expense)));
$profit_amount = money(array_sum($profit));
2022-08-31 18:16:14 +03:00
2022-06-01 10:15:55 +03:00
$totals = [
2022-08-31 18:16:14 +03:00
'incoming_exact' => $incoming_amount->format(),
'incoming_for_humans' => $incoming_amount->formatForHumans(),
'outgoing_exact' => $outgoing_amount->format(),
'outgoing_for_humans' => $outgoing_amount->formatForHumans(),
'profit_exact' => $profit_amount->format(),
'profit_for_humans' => $profit_amount->formatForHumans(),
2022-06-01 10:15:55 +03:00
];
return $this->view('widgets.cash_flow', [
'chart' => $chart,
'totals' => $totals,
]);
}
public function setFilter(): void
2019-11-16 10:21:14 +03:00
{
2019-12-28 22:52:37 +03:00
$financial_start = $this->getFinancialStart()->format('Y-m-d');
2019-11-16 10:21:14 +03:00
// check and assign year start
2019-12-28 22:52:37 +03:00
if (($year_start = Date::today()->startOfYear()->format('Y-m-d')) !== $financial_start) {
$year_start = $financial_start;
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
$this->start_date = Date::parse(request('start_date', $year_start));
$this->end_date = Date::parse(request('end_date', Date::parse($year_start)->addYear(1)->subDays(1)->format('Y-m-d')));
$this->period = request('period', 'month');
}
public function getLabels(): array
{
2019-11-16 10:21:14 +03:00
$range = request('range', 'custom');
2022-06-01 10:15:55 +03:00
$start_month = $this->start_date->month;
$end_month = $this->end_date->month;
2019-11-16 10:21:14 +03:00
// Monthly
2019-12-29 03:01:19 +03:00
$labels = [];
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
$s = clone $this->start_date;
2019-11-16 10:21:14 +03:00
if ($range == 'last_12_months') {
$end_month = 12;
$start_month = 0;
} elseif ($range == 'custom') {
2022-06-01 10:15:55 +03:00
$end_month = $this->end_date->diffInMonths($this->start_date);
2019-11-16 10:21:14 +03:00
$start_month = 0;
}
for ($j = $end_month; $j >= $start_month; $j--) {
$labels[$end_month - $j] = $s->format('M Y');
2022-06-01 10:15:55 +03:00
if ($this->period == 'month') {
2019-11-16 10:21:14 +03:00
$s->addMonth();
} else {
$s->addMonths(3);
$j -= 2;
}
}
2022-06-01 10:15:55 +03:00
return $labels;
}
public function getColors(): array
{
return [
'#8bb475',
'#fb7185',
'#7779A2',
];
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
private function calculateTotals($type): array
2019-11-16 10:21:14 +03:00
{
2019-12-29 03:01:19 +03:00
$totals = [];
2019-11-16 10:21:14 +03:00
$date_format = 'Y-m';
2022-06-01 10:15:55 +03:00
if ($this->period == 'month') {
2019-11-16 10:21:14 +03:00
$n = 1;
2022-06-01 10:15:55 +03:00
$start_date = $this->start_date->format($date_format);
$end_date = $this->end_date->format($date_format);
2019-11-16 10:21:14 +03:00
$next_date = $start_date;
} else {
$n = 3;
2022-06-01 10:15:55 +03:00
$start_date = $this->start_date->quarter;
$end_date = $this->end_date->quarter;
2019-11-16 10:21:14 +03:00
$next_date = $start_date;
}
2022-06-01 10:15:55 +03:00
$s = clone $this->start_date;
2019-11-16 10:21:14 +03:00
//$totals[$start_date] = 0;
while ($next_date <= $end_date) {
$totals[$next_date] = 0;
2022-06-01 10:15:55 +03:00
if ($this->period == 'month') {
2019-11-16 10:21:14 +03:00
$next_date = $s->addMonths($n)->format($date_format);
} else {
if (isset($totals[4])) {
break;
}
$next_date = $s->addMonths($n)->quarter;
}
}
2022-06-01 10:15:55 +03:00
$items = $this->applyFilters(Transaction::$type()->whereBetween('paid_at', [$this->start_date, $this->end_date])->isNotTransfer())->get();
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
$this->setTotals($totals, $items, $date_format);
2019-11-16 10:21:14 +03:00
return $totals;
}
2022-06-01 10:15:55 +03:00
private function setTotals(&$totals, $items, $date_format): void
2019-11-16 10:21:14 +03:00
{
2022-06-01 10:15:55 +03:00
$type = 'income';
2019-11-16 10:21:14 +03:00
foreach ($items as $item) {
2022-06-01 10:15:55 +03:00
$type = $item->type;
if ($this->period == 'month') {
2019-11-16 10:21:14 +03:00
$i = Date::parse($item->paid_at)->format($date_format);
} else {
$i = Date::parse($item->paid_at)->quarter;
}
if (!isset($totals[$i])) {
continue;
}
$totals[$i] += $item->getAmountConvertedToDefault();
}
2020-07-06 09:54:18 +03:00
2023-07-10 12:53:43 +03:00
$precision = config('money.currencies.' . default_currency() . '.precision');
2020-07-06 09:54:18 +03:00
foreach ($totals as $key => $value) {
2022-06-01 10:15:55 +03:00
if ($type == 'expense') {
$value = -1 * $value;
}
2020-07-06 09:54:18 +03:00
$totals[$key] = round($value, $precision);
}
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
private function calculateProfit($incomes, $expenses): array
2019-11-16 10:21:14 +03:00
{
$profit = [];
2023-07-10 12:53:43 +03:00
$precision = config('money.currencies.' . default_currency() . '.precision');
2022-06-01 10:15:55 +03:00
2019-11-16 10:21:14 +03:00
foreach ($incomes as $key => $income) {
2022-06-01 10:15:55 +03:00
$value = $income - abs($expenses[$key]);
$profit[$key] = round($value, $precision);
2019-11-16 10:21:14 +03:00
}
return $profit;
}
}