2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Widgets;
|
|
|
|
|
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;
|
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
|
|
|
use App\Utilities\Chartjs;
|
|
|
|
use Date;
|
|
|
|
|
2019-12-29 03:01:19 +03:00
|
|
|
class CashFlow extends Widget
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
use Currencies, DateTime;
|
|
|
|
|
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 = [
|
|
|
|
'width' => 'col-md-12',
|
|
|
|
];
|
2020-01-10 17:49:31 +03:00
|
|
|
|
2019-12-29 03:01:19 +03:00
|
|
|
public function show()
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 15:08:55 +03:00
|
|
|
$start = Date::parse(request('start_date', $year_start));
|
|
|
|
$end = Date::parse(request('end_date', Date::parse($year_start)->addYear(1)->subDays(1)->format('Y-m-d')));
|
2019-11-16 10:21:14 +03:00
|
|
|
$period = request('period', 'month');
|
|
|
|
$range = request('range', 'custom');
|
|
|
|
|
|
|
|
$start_month = $start->month;
|
|
|
|
$end_month = $end->month;
|
|
|
|
|
|
|
|
// Monthly
|
2019-12-29 03:01:19 +03:00
|
|
|
$labels = [];
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$s = clone $start;
|
|
|
|
|
|
|
|
if ($range == 'last_12_months') {
|
|
|
|
$end_month = 12;
|
|
|
|
$start_month = 0;
|
|
|
|
} elseif ($range == 'custom') {
|
|
|
|
$end_month = $end->diffInMonths($start);
|
|
|
|
$start_month = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($j = $end_month; $j >= $start_month; $j--) {
|
|
|
|
$labels[$end_month - $j] = $s->format('M Y');
|
|
|
|
|
|
|
|
if ($period == 'month') {
|
|
|
|
$s->addMonth();
|
|
|
|
} else {
|
|
|
|
$s->addMonths(3);
|
|
|
|
$j -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 22:52:37 +03:00
|
|
|
$income = $this->calculateTotals('income', $start, $end, $period);
|
|
|
|
$expense = $this->calculateTotals('expense', $start, $end, $period);
|
|
|
|
$profit = $this->calculateProfit($income, $expense);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$chart = new Chartjs();
|
|
|
|
$chart->type('line')
|
|
|
|
->width(0)
|
|
|
|
->height(300)
|
2019-12-29 03:01:19 +03:00
|
|
|
->options($this->getLineChartOptions())
|
2019-11-16 10:21:14 +03:00
|
|
|
->labels(array_values($labels));
|
|
|
|
|
|
|
|
$chart->dataset(trans_choice('general.incomes', 1), 'line', array_values($income))
|
|
|
|
->backgroundColor('#328aef')
|
|
|
|
->color('#328aef')
|
|
|
|
->options([
|
|
|
|
'borderWidth' => 4,
|
|
|
|
'pointStyle' => 'line',
|
|
|
|
])
|
|
|
|
->fill(false);
|
|
|
|
|
2019-12-29 03:01:19 +03:00
|
|
|
$chart->dataset(trans_choice('general.expenses', 2), 'line', array_values($expense))
|
2019-11-16 10:21:14 +03:00
|
|
|
->backgroundColor('#ef3232')
|
|
|
|
->color('#ef3232')
|
|
|
|
->options([
|
|
|
|
'borderWidth' => 4,
|
|
|
|
'pointStyle' => 'line',
|
|
|
|
])
|
|
|
|
->fill(false);
|
|
|
|
|
2019-12-29 03:01:19 +03:00
|
|
|
$chart->dataset(trans_choice('general.profits', 1), 'line', array_values($profit))
|
|
|
|
->backgroundColor('#6da252')
|
|
|
|
->color('#6da252')
|
|
|
|
->options([
|
|
|
|
'borderWidth' => 4,
|
|
|
|
'pointStyle' => 'line',
|
|
|
|
])
|
|
|
|
->fill(false);
|
|
|
|
|
2020-01-03 19:00:38 +03:00
|
|
|
return $this->view('widgets.line_chart', [
|
2019-12-28 22:52:37 +03:00
|
|
|
'chart' => $chart,
|
|
|
|
]);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-12-28 22:52:37 +03:00
|
|
|
private function calculateTotals($type, $start, $end, $period)
|
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';
|
|
|
|
|
|
|
|
if ($period == 'month') {
|
|
|
|
$n = 1;
|
|
|
|
$start_date = $start->format($date_format);
|
|
|
|
$end_date = $end->format($date_format);
|
|
|
|
$next_date = $start_date;
|
|
|
|
} else {
|
|
|
|
$n = 3;
|
|
|
|
$start_date = $start->quarter;
|
|
|
|
$end_date = $end->quarter;
|
|
|
|
$next_date = $start_date;
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = clone $start;
|
|
|
|
|
|
|
|
//$totals[$start_date] = 0;
|
|
|
|
while ($next_date <= $end_date) {
|
|
|
|
$totals[$next_date] = 0;
|
|
|
|
|
|
|
|
if ($period == 'month') {
|
|
|
|
$next_date = $s->addMonths($n)->format($date_format);
|
|
|
|
} else {
|
|
|
|
if (isset($totals[4])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$next_date = $s->addMonths($n)->quarter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 15:14:16 +03:00
|
|
|
$items = $this->applyFilters(Transaction::$type()->whereBetween('paid_at', [$start, $end])->isNotTransfer())->get();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2019-12-28 22:52:37 +03:00
|
|
|
$this->setTotals($totals, $items, $date_format, $period);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return $totals;
|
|
|
|
}
|
|
|
|
|
2019-12-28 22:52:37 +03:00
|
|
|
private function setTotals(&$totals, $items, $date_format, $period)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if ($period == 'month') {
|
|
|
|
$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
|
|
|
|
|
|
|
$precision = config('money.' . setting('default.currency') . '.precision');
|
|
|
|
|
|
|
|
foreach ($totals as $key => $value) {
|
|
|
|
$totals[$key] = round($value, $precision);
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-12-28 22:52:37 +03:00
|
|
|
private function calculateProfit($incomes, $expenses)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$profit = [];
|
|
|
|
|
|
|
|
foreach ($incomes as $key => $income) {
|
|
|
|
if ($income > 0 && $income > $expenses[$key]) {
|
|
|
|
$profit[$key] = $income - $expenses[$key];
|
|
|
|
} else {
|
|
|
|
$profit[$key] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $profit;
|
|
|
|
}
|
|
|
|
}
|