Akaunting/app/Widgets/TotalExpenses.php

57 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Widgets;
2019-12-29 00:01:19 +00:00
use App\Abstracts\Widget;
use App\Models\Banking\Transaction;
2020-12-23 22:28:38 +00:00
use App\Models\Document\Document;
2019-11-16 07:21:14 +00:00
2019-12-29 00:01:19 +00:00
class TotalExpenses extends Widget
2019-11-16 07:21:14 +00:00
{
2020-01-15 21:42:20 +00:00
public $default_name = 'widgets.total_expenses';
2020-01-10 14:49:31 +00:00
2020-01-15 21:42:20 +00:00
public $views = [
'header' => 'partials.widgets.stats_header',
];
2020-01-10 14:49:31 +00:00
2019-12-29 00:01:19 +00:00
public function show()
2019-11-16 07:21:14 +00:00
{
2019-12-29 00:01:19 +00:00
$current = $open = $overdue = 0;
2019-11-16 07:21:14 +00:00
2021-01-23 20:29:22 +00:00
$this->applyFilters(Transaction::expense()->isNotTransfer())->each(function ($transaction) use (&$current) {
2019-12-29 00:01:19 +00:00
$current += $transaction->getAmountConvertedToDefault();
});
2019-11-16 07:21:14 +00:00
2020-12-23 22:28:38 +00:00
$this->applyFilters(
Document::bill()->with('transactions')->accrued()->notPaid(),
['date_field' => 'created_at']
)->each(
function ($bill) use (&$open, &$overdue) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
$open += $open_tmp;
$overdue += $overdue_tmp;
}
);
2019-11-16 07:21:14 +00:00
2020-02-09 12:23:11 +00:00
$grand = $current + $open + $overdue;
2019-12-29 00:01:19 +00:00
$progress = 100;
2019-11-16 07:21:14 +00:00
2019-12-29 00:01:19 +00:00
if (!empty($open) && !empty($overdue)) {
$progress = (int) ($open * 100) / ($open + $overdue);
2019-11-16 07:21:14 +00:00
}
2019-12-29 00:01:19 +00:00
$totals = [
2020-02-09 12:23:11 +00:00
'grand' => money($grand, setting('default.currency'), true),
2019-12-29 00:01:19 +00:00
'open' => money($open, setting('default.currency'), true),
'overdue' => money($overdue, setting('default.currency'), true),
'progress' => $progress,
];
2019-11-16 07:21:14 +00:00
2019-12-30 23:20:10 +00:00
return $this->view('widgets.total_expenses', [
2019-12-29 00:01:19 +00:00
'totals' => $totals,
2019-11-16 07:21:14 +00:00
]);
}
}