akaunting/app/Abstracts/Widget.php

141 lines
3.2 KiB
PHP
Raw Normal View History

2019-12-29 03:01:19 +03:00
<?php
namespace App\Abstracts;
2022-06-01 10:15:55 +03:00
use App\Models\Common\Report;
2019-12-29 03:01:19 +03:00
use App\Traits\Charts;
2020-03-25 20:21:42 +03:00
use App\Utilities\Date;
2022-06-01 10:15:55 +03:00
use App\Utilities\Reports;
use Illuminate\Support\Str;
2019-12-29 03:01:19 +03:00
2019-12-31 02:20:10 +03:00
abstract class Widget
2019-12-29 03:01:19 +03:00
{
use Charts;
2019-12-31 02:20:10 +03:00
public $model;
2020-01-16 00:42:20 +03:00
public $default_name = '';
public $default_settings = [
2022-10-17 15:14:55 +03:00
'width' => 'w-full lg:w-2/4 lg:px-12 my-8',
2020-01-16 00:42:20 +03:00
];
2022-06-01 10:15:55 +03:00
public $description = '';
public $report_class = '';
2020-01-16 00:42:20 +03:00
public $views = [
2022-06-01 10:15:55 +03:00
'header' => 'components.widgets.header',
2020-01-16 00:42:20 +03:00
];
2019-12-31 02:20:10 +03:00
public function __construct($model = null)
{
$this->model = $model;
}
2020-01-16 00:42:20 +03:00
public function getDefaultName()
{
return trans($this->default_name);
}
2019-12-31 02:20:10 +03:00
public function getDefaultSettings()
{
2020-01-16 00:42:20 +03:00
return $this->default_settings;
}
2022-06-01 10:15:55 +03:00
public function getDescription()
{
return trans($this->description);
}
public function getReportUrl(): string
{
$empty_url = '';
if (empty($this->report_class)) {
return $empty_url;
}
if (Reports::isModule($this->report_class) && Reports::isModuleDisabled($this->report_class)) {
$alias = Reports::getModuleAlias($this->report_class);
return route('apps.app.show', [
'alias' => $alias,
'utm_source' => 'widget',
'utm_medium' => 'app',
2022-06-30 19:17:39 +03:00
'utm_campaign' => str_replace('-', '_', $alias),
2022-06-01 10:15:55 +03:00
]);
}
if (! class_exists($this->report_class)) {
return $empty_url;
}
if (Reports::cannotRead($this->report_class)) {
return $empty_url;
}
$model = Report::where('class', $this->report_class)->first();
if (! $model instanceof Report) {
return route('reports.create');
}
return route('reports.show', $model->id);
}
2020-01-16 00:42:20 +03:00
public function getViews()
{
return $this->views;
2019-12-31 02:20:10 +03:00
}
public function view($name, $data = [])
2019-12-29 03:01:19 +03:00
{
2021-01-29 19:23:07 +03:00
if (request()->isApi()) {
return $data;
}
2020-01-16 00:42:20 +03:00
return view($name, array_merge(['class' => $this], (array) $data));
2019-12-29 03:01:19 +03:00
}
public function applyFilters($model, $args = ['date_field' => 'paid_at'])
{
if (empty(request()->get('start_date', null))) {
return $model;
}
$start_date = request()->get('start_date') . ' 00:00:00';
$end_date = request()->get('end_date') . ' 23:59:59';
2019-12-31 02:20:10 +03:00
return $model->whereBetween($args['date_field'], [$start_date, $end_date]);
}
2019-12-29 03:01:19 +03:00
public function calculateDocumentTotals($model)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
2020-01-11 16:57:32 +03:00
if ($model->status == 'paid') {
2019-12-29 03:01:19 +03:00
return [$open, $overdue];
}
$payments = 0;
2020-01-11 16:57:32 +03:00
if ($model->status == 'partial') {
2019-12-29 03:01:19 +03:00
foreach ($model->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if the invoice/bill is open or overdue
if ($model->due_at > $today) {
$open += $model->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $model->getAmountConvertedToDefault() - $payments;
}
return [$open, $overdue];
}
}