Akaunting/app/Abstracts/Widget.php

136 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2019-12-29 00:01:19 +00:00
<?php
namespace App\Abstracts;
2022-06-01 07:15:55 +00:00
use App\Models\Common\Report;
2019-12-29 00:01:19 +00:00
use App\Traits\Charts;
2023-10-03 08:06:08 +00:00
use App\Traits\DateTime;
2020-03-25 17:21:42 +00:00
use App\Utilities\Date;
2022-06-01 07:15:55 +00:00
use App\Utilities\Reports;
2019-12-29 00:01:19 +00:00
2019-12-30 23:20:10 +00:00
abstract class Widget
2019-12-29 00:01:19 +00:00
{
2023-10-03 08:06:08 +00:00
use Charts, DateTime;
2019-12-29 00:01:19 +00:00
2019-12-30 23:20:10 +00:00
public $model;
2020-01-15 21:42:20 +00:00
public $default_name = '';
public $default_settings = [
2023-12-05 13:20:31 +00:00
'width' => '50',
2020-01-15 21:42:20 +00:00
];
2022-06-01 07:15:55 +00:00
public $description = '';
public $report_class = '';
2020-01-15 21:42:20 +00:00
public $views = [
2022-06-01 07:15:55 +00:00
'header' => 'components.widgets.header',
2020-01-15 21:42:20 +00:00
];
2026-02-07 18:55:11 +00:00
public array $data = [];
2019-12-30 23:20:10 +00:00
public function __construct($model = null)
{
$this->model = $model;
}
2020-01-15 21:42:20 +00:00
public function getDefaultName()
{
return trans($this->default_name);
}
2019-12-30 23:20:10 +00:00
public function getDefaultSettings()
{
2020-01-15 21:42:20 +00:00
return $this->default_settings;
}
2022-06-01 07:15:55 +00: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 16:17:39 +00:00
'utm_campaign' => str_replace('-', '_', $alias),
2022-06-01 07:15:55 +00: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-15 21:42:20 +00:00
public function getViews()
{
return $this->views;
2019-12-30 23:20:10 +00:00
}
public function view($name, $data = [])
2019-12-29 00:01:19 +00:00
{
2023-11-01 20:42:27 +00:00
if (request_is_api()) {
2021-01-29 16:23:07 +00:00
return $data;
}
2020-01-15 21:42:20 +00:00
return view($name, array_merge(['class' => $this], (array) $data));
2019-12-29 00:01:19 +00:00
}
2023-10-03 08:06:08 +00:00
public function applyFilters($query, $args = ['date_field' => 'paid_at'])
{
2023-10-03 08:06:08 +00:00
return $this->scopeDateFilter($query, $args['date_field']);
}
2019-12-29 00:01:19 +00:00
public function calculateDocumentTotals($model)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
2020-01-11 13:57:32 +00:00
if ($model->status == 'paid') {
2019-12-29 00:01:19 +00:00
return [$open, $overdue];
}
$payments = 0;
2020-01-11 13:57:32 +00:00
if ($model->status == 'partial') {
2019-12-29 00:01:19 +00: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];
}
}