akaunting/app/Widgets/TotalProfit.php

80 lines
2.4 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 TotalProfit extends Widget
2019-11-16 10:21:14 +03:00
{
2020-01-16 00:42:20 +03:00
public $default_name = 'widgets.total_profit';
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_income = $open_invoice = $overdue_invoice = 0;
$current_expenses = $open_bill = $overdue_bill = 0;
2019-11-16 10:21:14 +03:00
2021-01-23 23:29:22 +03:00
$this->applyFilters(Transaction::isNotTransfer())->each(function ($transaction) use (&$current_income, &$current_expenses) {
2019-12-29 03:01:19 +03:00
$amount = $transaction->getAmountConvertedToDefault();
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
if ($transaction->type == 'income') {
$current_income += $amount;
} else {
$current_expenses += $amount;
}
});
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$this->applyFilters(
Document::invoice()->with('transactions')->accrued()->notPaid(),
['date_field' => 'created_at']
)->each(
function ($invoice) use (&$open_invoice, &$overdue_invoice) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($invoice);
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$open_invoice += $open_tmp;
$overdue_invoice += $overdue_tmp;
}
);
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_bill, &$overdue_bill) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$open_bill += $open_tmp;
$overdue_bill += $overdue_tmp;
}
);
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
$current = $current_income - $current_expenses;
$open = $open_invoice - $open_bill;
$overdue = $overdue_invoice - $overdue_bill;
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_profit', [
2019-12-29 03:01:19 +03:00
'totals' => $totals,
2019-11-16 10:21:14 +03:00
]);
}
}