akaunting/app/Widgets/TotalExpenses.php

57 lines
1.6 KiB
PHP
Raw Normal View History

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