akaunting/app/Abstracts/Report.php

469 lines
12 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2019-11-23 21:47:20 +03:00
namespace App\Abstracts;
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
2020-01-27 22:41:08 +03:00
use App\Events\Common\ReportRowsShowing;
2019-11-16 10:21:14 +03:00
use App\Exports\Common\Reports as Export;
2020-01-18 13:11:19 +03:00
use App\Models\Banking\Transaction;
2019-11-16 10:21:14 +03:00
use App\Models\Common\Report as Model;
2020-01-18 13:11:19 +03:00
use App\Models\Sale\Invoice;
2019-12-29 03:01:19 +03:00
use App\Traits\Charts;
2019-11-16 10:21:14 +03:00
use App\Traits\DateTime;
2019-12-29 03:01:19 +03:00
use App\Utilities\Chartjs;
2019-11-16 10:21:14 +03:00
use Date;
use Illuminate\Support\Str;
abstract class Report
{
2019-12-29 03:01:19 +03:00
use Charts, DateTime;
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
public $model;
public $default_name = '';
public $category = 'reports.income_expense';
public $icon = 'fa fa-chart-pie';
2019-11-16 10:21:14 +03:00
public $year;
public $views = [];
public $tables = [];
public $dates = [];
2020-01-27 22:41:08 +03:00
public $row_names = [];
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
public $row_values = [];
public $footer_totals = [];
2019-11-16 10:21:14 +03:00
public $filters = [];
2020-01-27 22:41:08 +03:00
public $loaded = false;
2019-11-16 10:21:14 +03:00
public $indents = [
'table_header' => '0px',
2020-01-28 12:42:54 +03:00
'table_rows' => '24px',
2019-11-16 10:21:14 +03:00
];
public $chart = [
'line' => [
'width' => '0',
'height' => '300',
'options' => [
'color' => '#6da252',
2020-01-02 15:36:27 +03:00
'legend' => [
'display' => false,
],
2019-11-16 10:21:14 +03:00
],
],
'dates' => [],
'datasets' => [],
];
2020-01-27 22:41:08 +03:00
public function __construct(Model $model = null, $load_data = true)
2019-11-16 10:21:14 +03:00
{
$this->setGroups();
2020-01-16 15:39:37 +03:00
if (!$model) {
2019-11-16 10:21:14 +03:00
return;
}
2020-01-16 15:39:37 +03:00
$this->model = $model;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
if (!$load_data) {
return;
}
$this->load();
}
abstract public function getTotals();
public function load()
{
2019-11-16 10:21:14 +03:00
$this->setYear();
$this->setViews();
$this->setTables();
$this->setDates();
$this->setFilters();
$this->setRows();
2020-01-27 22:41:08 +03:00
$this->getTotals();
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
$this->loaded = true;
2019-11-16 10:21:14 +03:00
}
2020-01-04 13:42:58 +03:00
public function getDefaultName()
2019-11-16 10:21:14 +03:00
{
2020-01-16 15:39:37 +03:00
if (!empty($this->default_name)) {
return trans($this->default_name);
}
2019-11-16 10:21:14 +03:00
return Str::title(str_replace('_', ' ', Str::snake((new \ReflectionClass($this))->getShortName())));
}
public function getCategory()
{
2020-01-16 15:39:37 +03:00
return trans($this->category);
2019-11-16 10:21:14 +03:00
}
public function getIcon()
{
return $this->icon;
}
public function getTotal()
{
2020-01-27 22:41:08 +03:00
if (!$this->loaded) {
$this->load();
}
2019-11-16 10:21:14 +03:00
$sum = 0;
2020-01-27 22:41:08 +03:00
foreach ($this->footer_totals as $total) {
2019-11-16 10:21:14 +03:00
$sum += is_array($total) ? array_sum($total) : $total;
}
$total = money($sum, setting('default.currency'), true);
return $total;
}
public function getChart()
{
$chart = new Chartjs();
2020-01-28 00:51:29 +03:00
if (empty($this->model->settings->chart)) {
return $chart;
}
2020-01-16 15:39:37 +03:00
$config = $this->chart[$this->model->settings->chart];
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
$default_options = $this->getLineChartOptions();
2019-11-16 10:21:14 +03:00
$options = array_merge($default_options, (array) $config['options']);
2020-01-16 15:39:37 +03:00
$chart->type($this->model->settings->chart)
2019-11-16 10:21:14 +03:00
->width((int) $config['width'])
->height((int) $config['height'])
->options($options)
->labels(!empty($config['dates']) ? array_values($config['dates']) : array_values($this->dates));
if (!empty($config['datasets'])) {
foreach ($config['datasets'] as $dataset) {
$chart->dataset($dataset['name'], 'line', array_values($dataset['totals']))
->backgroundColor(isset($dataset['backgroundColor']) ? $dataset['backgroundColor'] : '#6da252')
->color(isset($dataset['color']) ? $dataset['color'] : '#6da252')
->options((array) $dataset['options'])
->fill(false);
}
} else {
2020-01-27 22:41:08 +03:00
foreach ($this->footer_totals as $total) {
2020-01-16 15:39:37 +03:00
$chart->dataset($this->model->name, 'line', array_values($total))
2019-11-16 10:21:14 +03:00
->backgroundColor(isset($config['backgroundColor']) ? $config['backgroundColor'] : '#6da252')
->color(isset($config['color']) ? $config['color'] : '#6da252')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
}
}
return $chart;
}
public function show()
{
return view($this->views['show'])->with('class', $this);
}
public function print()
{
return view($this->views['print'])->with('class', $this);
}
public function export()
{
2020-01-16 15:39:37 +03:00
return \Excel::download(new Export($this->views['content'], $this), $this->model->name . '.xlsx');
2019-11-16 10:21:14 +03:00
}
public function setYear()
{
$this->year = request('year', Date::now()->year);
}
public function setViews()
{
$this->views = [
'chart' => 'partials.reports.chart',
'content' => 'partials.reports.content',
'content.header' => 'partials.reports.content.header',
'content.footer' => 'partials.reports.content.footer',
'show' => 'partials.reports.show',
'header' => 'partials.reports.header',
'filter' => 'partials.reports.filter',
'print' => 'partials.reports.print',
'table' => 'partials.reports.table',
'table.footer' => 'partials.reports.table.footer',
'table.header' => 'partials.reports.table.header',
'table.rows' => 'partials.reports.table.rows',
];
}
public function setTables()
{
$this->tables = [
2020-01-27 22:41:08 +03:00
'default' => 'default',
2019-11-16 10:21:14 +03:00
];
}
public function setDates()
{
2020-01-27 22:41:08 +03:00
if (empty($this->model->settings->period)) {
return;
}
2020-01-16 15:39:37 +03:00
$function = 'sub' . ucfirst(str_replace('ly', '', $this->model->settings->period));
2019-11-16 10:21:14 +03:00
$start = $this->getFinancialStart()->copy()->$function();
for ($j = 1; $j <= 12; $j++) {
2020-01-16 15:39:37 +03:00
switch ($this->model->settings->period) {
2019-11-16 10:21:14 +03:00
case 'yearly':
$start->addYear();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
2020-01-27 22:41:08 +03:00
$this->footer_totals[$table][$date] = 0;
2019-11-16 10:21:14 +03:00
}
$j += 11;
break;
case 'quarterly':
$start->addQuarter();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
2020-01-27 22:41:08 +03:00
$this->footer_totals[$table][$date] = 0;
2019-11-16 10:21:14 +03:00
}
$j += 2;
break;
default:
$start->addMonth();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
2020-01-27 22:41:08 +03:00
$this->footer_totals[$table][$date] = 0;
2019-11-16 10:21:14 +03:00
}
break;
}
}
}
public function setFilters()
{
2020-01-16 15:39:37 +03:00
event(new ReportFilterShowing($this));
2019-11-16 10:21:14 +03:00
}
public function setGroups()
{
2020-01-16 15:39:37 +03:00
$this->groups = [];
event(new ReportGroupShowing($this));
2019-11-16 10:21:14 +03:00
}
public function setRows()
{
2020-01-27 22:41:08 +03:00
event(new ReportRowsShowing($this));
2019-11-16 10:21:14 +03:00
}
public function setTotals($items, $date_field, $check_type = false, $table = 'default')
{
foreach ($items as $item) {
// Make groups extensible
$item = $this->applyGroups($item);
$date = $this->getFormattedDate(Date::parse($item->$date_field));
2020-01-16 15:39:37 +03:00
$id_field = $this->model->settings->group . '_id';
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
if (
!isset($this->row_values[$table][$item->$id_field])
|| !isset($this->row_values[$table][$item->$id_field][$date])
|| !isset($this->footer_totals[$table][$date]))
2019-11-16 10:21:14 +03:00
{
continue;
}
$amount = $item->getAmountConvertedToDefault();
2020-01-27 22:41:08 +03:00
$type = (($item instanceof Invoice) || (($item instanceof Transaction) && ($item->type == 'income'))) ? 'income' : 'expense';
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
if (($check_type == false) || ($type == 'income')) {
$this->row_values[$table][$item->$id_field][$date] += $amount;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
$this->footer_totals[$table][$date] += $amount;
} else {
$this->row_values[$table][$item->$id_field][$date] -= $amount;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
$this->footer_totals[$table][$date] -= $amount;
2019-11-16 10:21:14 +03:00
}
}
}
public function applyFilters($model, $args = [])
{
2020-01-16 15:39:37 +03:00
event(new ReportFilterApplying($this, $model, $args));
2019-11-16 10:21:14 +03:00
return $model;
}
public function applyGroups($model, $args = [])
{
2020-01-16 15:39:37 +03:00
event(new ReportGroupApplying($this, $model, $args));
2019-11-16 10:21:14 +03:00
return $model;
}
public function getFormattedDate($date)
{
2020-01-16 15:39:37 +03:00
switch ($this->model->settings->period) {
2019-11-16 10:21:14 +03:00
case 'yearly':
$i = $date->copy()->format($this->getYearlyDateFormat());
break;
case 'quarterly':
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat());
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat());
$i = $start . '-' . $end;
break;
default:
$i = $date->copy()->format($this->getMonthlyDateFormat());
break;
}
return $i;
}
public function getUrl($action = 'print')
{
2020-01-16 15:39:37 +03:00
$print_url = 'common/reports/' . $this->model->id . '/' . $action . '?year='. $this->year;
2019-11-16 10:21:14 +03:00
collect(request('accounts'))->each(function($item) use(&$print_url) {
$print_url .= '&accounts[]=' . $item;
});
collect(request('customers'))->each(function($item) use(&$print_url) {
$print_url .= '&customers[]=' . $item;
});
collect(request('categories'))->each(function($item) use(&$print_url) {
$print_url .= '&categories[]=' . $item;
});
return $print_url;
}
2020-01-16 15:39:37 +03:00
public function getFields()
{
return [
$this->getGroupField(),
$this->getPeriodField(),
$this->getBasisField(),
$this->getChartField(),
];
}
public function getGroupField()
{
$this->setGroups();
return [
'type' => 'selectGroup',
'name' => 'group',
'title' => trans('general.group_by'),
'icon' => 'folder',
'values' => $this->groups,
'selected' => 'category',
'attributes' => [
'required' => 'required',
],
];
}
public function getPeriodField()
{
return [
'type' => 'selectGroup',
'name' => 'period',
'title' => trans('general.period'),
'icon' => 'calendar',
'values' => [
'monthly' => trans('general.monthly'),
'quarterly' => trans('general.quarterly'),
'yearly' => trans('general.yearly'),
],
'selected' => 'quarterly',
'attributes' => [
'required' => 'required',
],
];
}
public function getBasisField()
{
return [
'type' => 'selectGroup',
'name' => 'basis',
'title' => trans('general.basis'),
'icon' => 'file',
'values' => [
'accrual' => trans('general.accrual'),
'cash' => trans('general.cash'),
],
'selected' => 'accrual',
'attributes' => [
'required' => 'required',
],
];
}
public function getChartField()
{
return [
'type' => 'selectGroup',
'name' => 'chart',
'title' => trans_choice('general.charts', 1),
'icon' => 'chart-pie',
'values' => [
'0' => trans('general.disabled'),
'line' => trans('reports.charts.line'),
],
'selected' => '0',
'attributes' => [
'required' => 'required',
],
];
}
2019-11-16 10:21:14 +03:00
}