akaunting/app/Abstracts/Report.php

583 lines
15 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-07-02 01:55:45 +03:00
use App\Events\Report\DataLoaded;
use App\Events\Report\DataLoading;
2020-01-31 12:59:12 +03:00
use App\Events\Report\FilterApplying;
use App\Events\Report\FilterShowing;
use App\Events\Report\GroupApplying;
use App\Events\Report\GroupShowing;
use App\Events\Report\RowsShowing;
2019-11-16 10:21:14 +03:00
use App\Exports\Common\Reports as Export;
use App\Models\Common\Report as Model;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2019-12-29 03:01:19 +03:00
use App\Traits\Charts;
2019-11-16 10:21:14 +03:00
use App\Traits\DateTime;
2021-01-23 12:16:58 +03:00
use App\Traits\SearchString;
2019-12-29 03:01:19 +03:00
use App\Utilities\Chartjs;
use App\Utilities\Date;
use App\Utilities\Export as ExportHelper;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Str;
abstract class Report
{
2021-01-23 12:16:58 +03:00
use Charts, DateTime, SearchString;
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
2021-07-16 02:35:42 +03:00
public $has_money = true;
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 $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-03-10 14:54:20 +03:00
public $column_name_width = 'report-column-name';
public $column_value_width = 'report-column-value';
2020-03-09 17:17:42 +03:00
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();
}
2020-01-29 01:06:12 +03:00
abstract public function setData();
2020-01-27 22:41:08 +03:00
public function load()
{
2019-11-16 10:21:14 +03:00
$this->setYear();
$this->setViews();
$this->setTables();
$this->setDates();
$this->setFilters();
$this->setRows();
2020-07-02 01:55:45 +03:00
$this->loadData();
2020-03-10 14:54:20 +03:00
$this->setColumnWidth();
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-07-02 01:55:45 +03:00
public function loadData()
{
event(new DataLoading($this));
$this->setData();
event(new DataLoaded($this));
}
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;
}
2020-01-29 01:06:12 +03:00
public function getGrandTotal()
2019-11-16 10:21:14 +03:00
{
2020-01-27 22:41:08 +03:00
if (!$this->loaded) {
$this->load();
}
2020-01-29 01:06:12 +03:00
if (!empty($this->footer_totals)) {
$sum = 0;
2019-11-16 10:21:14 +03:00
2020-01-29 01:06:12 +03:00
foreach ($this->footer_totals as $total) {
$sum += is_array($total) ? array_sum($total) : $total;
}
2019-11-16 10:21:14 +03:00
2021-07-16 02:35:42 +03:00
$total = $this->has_money ? money($sum, setting('default.currency'), true)->format() : $sum;
2020-01-29 01:06:12 +03:00
} else {
$total = trans('general.na');
}
2019-11-16 10:21:14 +03:00
return $total;
}
public function getChart()
{
$chart = new Chartjs();
2020-06-12 13:38:58 +03:00
if (!$type = $this->getSetting('chart')) {
2020-01-28 00:51:29 +03:00
return $chart;
}
2020-06-12 13:38:58 +03:00
$config = $this->chart[$type];
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-06-12 13:38:58 +03:00
$chart->type($type)
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()
{
return ExportHelper::toExcel(new Export($this->views['content'], $this), $this->model->name);
2019-11-16 10:21:14 +03:00
}
2020-03-10 14:54:20 +03:00
public function setColumnWidth()
2020-03-09 17:17:42 +03:00
{
2020-06-12 13:38:58 +03:00
if (!$period = $this->getSetting('period')) {
2020-03-09 17:17:42 +03:00
return;
}
2020-03-10 14:54:20 +03:00
$width = '';
2020-03-09 17:17:42 +03:00
2020-06-12 13:38:58 +03:00
switch ($period) {
2020-03-09 17:17:42 +03:00
case 'quarterly':
2020-03-10 14:54:20 +03:00
$width = 'col-sm-2';
2020-03-09 17:17:42 +03:00
break;
case 'yearly':
2020-03-10 14:54:20 +03:00
$width = 'col-sm-4';
2020-03-09 17:17:42 +03:00
break;
}
2020-03-10 14:54:20 +03:00
if (empty($width)) {
2020-03-09 17:17:42 +03:00
return;
}
2020-03-10 14:54:20 +03:00
$this->column_name_width = $this->column_value_width = $width;
2020-03-09 17:17:42 +03:00
}
2019-11-16 10:21:14 +03:00
public function setYear()
{
2021-01-23 12:16:58 +03:00
$this->year = $this->getSearchStringValue('year', Date::now()->year);
2019-11-16 10:21:14 +03:00
}
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-06-12 13:38:58 +03:00
if (!$period = $this->getSetting('period')) {
2020-01-27 22:41:08 +03:00
return;
}
2020-06-12 13:38:58 +03:00
$function = 'sub' . ucfirst(str_replace('ly', '', $period));
2019-11-16 10:21:14 +03:00
2021-01-23 12:16:58 +03:00
$start = $this->getFinancialStart($this->year)->copy()->$function();
2019-11-16 10:21:14 +03:00
for ($j = 1; $j <= 12; $j++) {
2020-06-12 13:38:58 +03:00
switch ($period) {
2019-11-16 10:21:14 +03:00
case 'yearly':
$start->addYear();
$j += 11;
break;
case 'quarterly':
$start->addQuarter();
$j += 2;
break;
default:
$start->addMonth();
2021-02-05 21:00:15 +03:00
break;
}
2019-11-16 10:21:14 +03:00
2021-02-05 21:00:15 +03:00
$date = $this->getFormattedDate($start);
2019-11-16 10:21:14 +03:00
2021-02-05 21:00:15 +03:00
$this->dates[] = $date;
2019-11-16 10:21:14 +03:00
2021-02-05 21:00:15 +03:00
foreach ($this->tables as $table) {
$this->footer_totals[$table][$date] = 0;
2019-11-16 10:21:14 +03:00
}
}
}
public function setFilters()
{
2020-01-31 12:59:12 +03:00
event(new FilterShowing($this));
2019-11-16 10:21:14 +03:00
}
public function setGroups()
{
2020-01-16 15:39:37 +03:00
$this->groups = [];
2020-01-31 12:59:12 +03:00
event(new GroupShowing($this));
2019-11-16 10:21:14 +03:00
}
public function setRows()
{
2020-01-31 12:59:12 +03:00
event(new RowsShowing($this));
2019-11-16 10:21:14 +03:00
}
2020-05-16 11:37:56 +03:00
public function setTotals($items, $date_field, $check_type = false, $table = 'default', $with_tax = true)
2019-11-16 10:21:14 +03:00
{
2021-07-16 11:26:17 +03:00
$group_field = $this->getSetting('group') . '_id';
2019-11-16 10:21:14 +03:00
foreach ($items as $item) {
// Make groups extensible
$item = $this->applyGroups($item);
$date = $this->getFormattedDate(Date::parse($item->$date_field));
2021-07-16 11:26:17 +03:00
if (!isset($item->$group_field)) {
continue;
}
$group = $item->$group_field;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
if (
2021-07-16 11:26:17 +03:00
!isset($this->row_values[$table][$group])
|| !isset($this->row_values[$table][$group][$date])
2020-02-21 18:03:23 +03:00
|| !isset($this->footer_totals[$table][$date])
) {
2019-11-16 10:21:14 +03:00
continue;
}
2020-05-16 11:37:56 +03:00
$amount = $item->getAmountConvertedToDefault(false, $with_tax);
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$type = ($item->type === Document::INVOICE_TYPE || $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')) {
2021-07-16 11:26:17 +03:00
$this->row_values[$table][$group][$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 {
2021-07-16 11:26:17 +03:00
$this->row_values[$table][$group][$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
}
}
}
2021-07-16 11:26:17 +03:00
public function setArithmeticTotals($items, $date_field, $operator = 'add', $table = 'default', $amount_field = 'amount')
{
$group_field = $this->getSetting('group') . '_id';
$function = $operator . 'ArithmeticAmount';
foreach ($items as $item) {
// Make groups extensible
$item = $this->applyGroups($item);
$date = $this->getFormattedDate(Date::parse($item->$date_field));
if (!isset($item->$group_field)) {
continue;
}
$group = $item->$group_field;
if (
!isset($this->row_values[$table][$group])
|| !isset($this->row_values[$table][$group][$date])
|| !isset($this->footer_totals[$table][$date])
) {
continue;
}
$amount = isset($item->$amount_field) ? $item->$amount_field : 1;
$this->$function($this->row_values[$table][$group][$date], $amount);
$this->$function($this->footer_totals[$table][$date], $amount);
}
}
public function addArithmeticAmount(&$current, $amount)
{
$current = $current + $amount;
}
public function subArithmeticAmount(&$current, $amount)
{
$current = $current - $amount;
}
public function mulArithmeticAmount(&$current, $amount)
{
$current = $current * $amount;
}
public function divArithmeticAmount(&$current, $amount)
{
$current = $current / $amount;
}
public function modArithmeticAmount(&$current, $amount)
{
$current = $current % $amount;
}
public function expArithmeticAmount(&$current, $amount)
{
$current = $current ** $amount;
}
2019-11-16 10:21:14 +03:00
public function applyFilters($model, $args = [])
{
2020-01-31 12:59:12 +03:00
event(new FilterApplying($this, $model, $args));
2019-11-16 10:21:14 +03:00
return $model;
}
public function applyGroups($model, $args = [])
{
2020-01-31 12:59:12 +03:00
event(new GroupApplying($this, $model, $args));
2019-11-16 10:21:14 +03:00
return $model;
}
public function getFormattedDate($date)
{
$formatted_date = null;
2020-06-12 13:38:58 +03:00
switch ($this->getSetting('period')) {
2019-11-16 10:21:14 +03:00
case 'yearly':
$financial_year = $this->getFinancialYear($this->year);
if ($date->greaterThanOrEqualTo($financial_year->getStartDate()) && $date->lessThanOrEqualTo($financial_year->getEndDate())) {
2021-02-12 23:05:50 +03:00
if (setting('localisation.financial_denote') == 'begins') {
$formatted_date = $financial_year->getStartDate()->copy()->format($this->getYearlyDateFormat());
} else {
$formatted_date = $financial_year->getEndDate()->copy()->format($this->getYearlyDateFormat());
}
}
2019-11-16 10:21:14 +03:00
break;
case 'quarterly':
2021-02-08 12:07:22 +03:00
$quarters = $this->getFinancialQuarters($this->year);
foreach ($quarters as $quarter) {
if ($date->lessThan($quarter->getStartDate()) || $date->greaterThan($quarter->getEndDate())) {
continue;
}
$start = $quarter->getStartDate()->format($this->getQuarterlyDateFormat($this->year));
$end = $quarter->getEndDate()->format($this->getQuarterlyDateFormat($this->year));
$formatted_date = $start . '-' . $end;
}
2019-11-16 10:21:14 +03:00
break;
default:
$formatted_date = $date->copy()->format($this->getMonthlyDateFormat($this->year));
2019-11-16 10:21:14 +03:00
break;
}
return $formatted_date;
2019-11-16 10:21:14 +03:00
}
public function getUrl($action = 'print')
{
$url = company_id() . '/common/reports/' . $this->model->id . '/' . $action;
2019-11-16 10:21:14 +03:00
2021-01-23 12:16:58 +03:00
$search = request('search');
2019-11-16 10:21:14 +03:00
2021-01-23 12:16:58 +03:00
if (!empty($search)) {
$url .= '?search=' . $search;
}
2019-11-16 10:21:14 +03:00
2021-01-23 12:16:58 +03:00
return $url;
2019-11-16 10:21:14 +03:00
}
2020-01-16 15:39:37 +03:00
2020-06-12 13:38:58 +03:00
public function getSetting($name, $default = '')
{
return $this->model->settings->$name ?? $default;
}
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
}