Akaunting/app/Widgets/CashFlow.php

210 lines
5.8 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Widgets;
2022-06-16 14:57:43 +00:00
use Akaunting\Apexcharts\Chart;
2019-12-29 00:01:19 +00:00
use App\Abstracts\Widget;
2019-11-16 07:21:14 +00:00
use App\Models\Banking\Transaction;
2022-06-24 12:56:29 +00:00
use App\Traits\Charts;
2019-11-16 07:21:14 +00:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2022-06-01 07:15:55 +00:00
use App\Utilities\Date;
2019-11-16 07:21:14 +00:00
2019-12-29 00:01:19 +00:00
class CashFlow extends Widget
2019-11-16 07:21:14 +00:00
{
2022-06-24 12:56:29 +00:00
use Charts, Currencies, DateTime;
2019-11-16 07:21:14 +00:00
2020-01-15 21:42:20 +00:00
public $default_name = 'widgets.cash_flow';
2020-01-10 14:49:31 +00:00
2020-01-15 21:42:20 +00:00
public $default_settings = [
2023-12-05 13:20:31 +00:00
'width' => '100',
2020-01-15 21:42:20 +00:00
];
2020-01-10 14:49:31 +00:00
2022-06-01 07:15:55 +00: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 00:01:19 +00:00
public function show()
2026-02-07 18:55:11 +00:00
{
$this->setData();
return $this->view('widgets.cash_flow', $this->data);
}
public function setData(): void
2022-06-01 07:15:55 +00: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 14:57:43 +00:00
$chart = new Chart();
2022-06-01 07:15:55 +00:00
$chart->setType('line')
2022-08-13 09:37:30 +00:00
->setDefaultLocale($this->getDefaultLocaleOfChart())
->setLocales($this->getLocaleTranslationOfChart())
2022-08-13 12:13:25 +00:00
->setStacked(true)
->setBar(['columnWidth' => '40%'])
->setLegendPosition('top')
->setYaxisLabels(['formatter' => $this->getChartLabelFormatter()])
->setLabels(array_values($this->getLabels()))
->setColors($this->getColors())
2022-06-01 07:15:55 +00: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 15:16:14 +00:00
2022-06-01 07:15:55 +00:00
$totals = [
2022-08-31 15:16:14 +00: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 07:15:55 +00:00
];
2026-02-07 18:55:11 +00:00
$this->data = [
2022-06-01 07:15:55 +00:00
'chart' => $chart,
'totals' => $totals,
2026-02-07 18:55:11 +00:00
];
2022-06-01 07:15:55 +00:00
}
public function setFilter(): void
2019-11-16 07:21:14 +00:00
{
2023-10-03 08:06:08 +00:00
$financial_year = $this->getFinancialYear();
2019-11-16 07:21:14 +00:00
$this->start_date = Date::parse(request('start_date', $financial_year->copy()->getStartDate()->toDateString()))->startOfDay();
$this->end_date = Date::parse(request('end_date', $financial_year->copy()->getEndDate()->toDateString()))->endOfDay();
2022-06-01 07:15:55 +00:00
$this->period = request('period', 'month');
}
public function getLabels(): array
{
2019-12-29 00:01:19 +00:00
$labels = [];
2019-11-16 07:21:14 +00:00
2023-10-03 08:06:08 +00:00
$start_date = $this->start_date->copy();
2019-11-16 07:21:14 +00:00
2023-10-03 08:06:08 +00:00
$counter = $this->end_date->diffInMonths($this->start_date);
2019-11-16 07:21:14 +00:00
2023-10-03 08:06:08 +00:00
for ($j = 0; $j <= $counter; $j++) {
$labels[$j] = $start_date->format($this->getMonthlyDateFormat());
2019-11-16 07:21:14 +00:00
2022-06-01 07:15:55 +00:00
if ($this->period == 'month') {
2023-10-03 08:06:08 +00:00
$start_date->addMonth();
2019-11-16 07:21:14 +00:00
} else {
2023-10-03 08:06:08 +00:00
$start_date->addMonths(3);
$j += 2;
2019-11-16 07:21:14 +00:00
}
}
2022-06-01 07:15:55 +00:00
return $labels;
}
public function getColors(): array
{
return [
'#8bb475',
'#fb7185',
'#7779A2',
];
2019-11-16 07:21:14 +00:00
}
2022-06-01 07:15:55 +00:00
private function calculateTotals($type): array
2019-11-16 07:21:14 +00:00
{
2019-12-29 00:01:19 +00:00
$totals = [];
2019-11-16 07:21:14 +00:00
$date_format = 'Y-m';
2022-06-01 07:15:55 +00:00
if ($this->period == 'month') {
2019-11-16 07:21:14 +00:00
$n = 1;
2022-06-01 07:15:55 +00:00
$start_date = $this->start_date->format($date_format);
$end_date = $this->end_date->format($date_format);
2019-11-16 07:21:14 +00:00
$next_date = $start_date;
} else {
$n = 3;
2022-06-01 07:15:55 +00:00
$start_date = $this->start_date->quarter;
$end_date = $this->end_date->quarter;
2019-11-16 07:21:14 +00:00
$next_date = $start_date;
}
2022-06-01 07:15:55 +00:00
$s = clone $this->start_date;
2019-11-16 07:21:14 +00:00
//$totals[$start_date] = 0;
while ($next_date <= $end_date) {
$totals[$next_date] = 0;
2022-06-01 07:15:55 +00:00
if ($this->period == 'month') {
2019-11-16 07:21:14 +00:00
$next_date = $s->addMonths($n)->format($date_format);
} else {
if (isset($totals[4])) {
break;
}
$next_date = $s->addMonths($n)->quarter;
}
}
2022-06-01 07:15:55 +00:00
$items = $this->applyFilters(Transaction::$type()->whereBetween('paid_at', [$this->start_date, $this->end_date])->isNotTransfer())->get();
2019-11-16 07:21:14 +00:00
2022-06-01 07:15:55 +00:00
$this->setTotals($totals, $items, $date_format);
2019-11-16 07:21:14 +00:00
return $totals;
}
2022-06-01 07:15:55 +00:00
private function setTotals(&$totals, $items, $date_format): void
2019-11-16 07:21:14 +00:00
{
2022-06-01 07:15:55 +00:00
$type = 'income';
2019-11-16 07:21:14 +00:00
foreach ($items as $item) {
2022-06-01 07:15:55 +00:00
$type = $item->type;
if ($this->period == 'month') {
2019-11-16 07:21:14 +00: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 06:54:18 +00:00
2023-10-03 08:06:08 +00:00
$precision = currency()->getPrecision();
2020-07-06 06:54:18 +00:00
foreach ($totals as $key => $value) {
2022-06-01 07:15:55 +00:00
if ($type == 'expense') {
$value = -1 * $value;
}
2020-07-06 06:54:18 +00:00
$totals[$key] = round($value, $precision);
}
2019-11-16 07:21:14 +00:00
}
2022-06-01 07:15:55 +00:00
private function calculateProfit($incomes, $expenses): array
2019-11-16 07:21:14 +00:00
{
$profit = [];
2023-10-03 08:06:08 +00:00
$precision = currency()->getPrecision();
2022-06-01 07:15:55 +00:00
2019-11-16 07:21:14 +00:00
foreach ($incomes as $key => $income) {
2022-06-01 07:15:55 +00:00
$value = $income - abs($expenses[$key]);
$profit[$key] = round($value, $precision);
2019-11-16 07:21:14 +00:00
}
return $profit;
}
}