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;
|
2019-12-31 15:49:09 +03:00
|
|
|
use App\Models\Purchase\Bill;
|
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
|
|
|
|
2019-12-30 12:30:30 +03:00
|
|
|
$this->applyFilters(Transaction::type('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
|
|
|
|
2019-12-30 12:30:30 +03:00
|
|
|
$this->applyFilters(Bill::accrued()->notPaid(), ['date_field' => 'created_at'])->each(function ($bill) use (&$open, &$overdue) {
|
2019-12-29 03:01:19 +03:00
|
|
|
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2019-12-29 03:01:19 +03:00
|
|
|
$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
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|