akaunting/app/Widgets/TotalProfit.php

64 lines
2.0 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;
use App\Models\Expense\Bill;
use App\Models\Income\Invoice;
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
{
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
2019-12-29 03:01:19 +03:00
Transaction::isNotTransfer()->each(function ($transaction) use (&$current_income, &$current_expenses) {
$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
2019-12-29 03:01:19 +03:00
Invoice::accrued()->notPaid()->each(function ($invoice) use (&$open_invoice, &$overdue_invoice) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($invoice);
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
$open_invoice += $open_tmp;
$overdue_invoice += $overdue_tmp;
});
2019-11-16 10:21:14 +03:00
2019-12-29 03:01:19 +03:00
Bill::accrued()->notPaid()->each(function ($bill) use (&$open_bill, &$overdue_bill) {
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_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
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 = [
'current' => $current,
'open' => money($open, setting('default.currency'), true),
'overdue' => money($overdue, setting('default.currency'), true),
'progress' => $progress,
];
2019-11-16 10:21:14 +03:00
return view('widgets.total_profit', [
'config' => (object) $this->config,
2019-12-29 03:01:19 +03:00
'totals' => $totals,
2019-11-16 10:21:14 +03:00
]);
}
}