143 lines
4.1 KiB
PHP
143 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Dashboard;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
class DashboardController extends BaseController
|
|
{
|
|
private array $invoiceOverviews = [];
|
|
|
|
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
|
|
if (!defined('AUTHENTICATED') || AUTHENTICATED === false) {
|
|
die('NO ACCESS');
|
|
}
|
|
}
|
|
|
|
public function getOwhp(): ResponseInterface
|
|
{
|
|
$recapprojects = new \Recapprojects('F', false, false);
|
|
|
|
$projects = $recapprojects->getProjects();
|
|
|
|
$currentFinancialYear = 0;
|
|
$totalRevenues = 0;
|
|
$totalOwp = 0;
|
|
$ownRevenue = 0;
|
|
$available = 0;
|
|
|
|
foreach ($projects as $project) {
|
|
$revenues = 0;
|
|
foreach ($project['employees'] as $employee) {
|
|
$revenues += $employee['revenues'];
|
|
}
|
|
$currentFinancialYear += $project['current_financial_year'];
|
|
|
|
$owp = $project['own_revenue'] - $revenues;
|
|
$owp = $owp > 0 ? $owp : 0;
|
|
$totalOwp += $owp;
|
|
|
|
$ownRevenue += $project['own_revenue'];
|
|
$totalRevenues += $revenues;
|
|
$available += $project['budgetAvailable'];
|
|
}
|
|
|
|
$outstandings = \Invoice::getTotalsOutstandingPerProject(false);
|
|
|
|
$this->setInvoiceOverview();
|
|
|
|
return $this->json([
|
|
'totalOwp' => round($totalOwp, 0),
|
|
'revenuesThisYear' => $this->getRevenuesThisYear(),
|
|
'currentFinancialYear' => round($currentFinancialYear),
|
|
'owpNextyear' => round($totalOwp - $currentFinancialYear),
|
|
'revenuesNextyear' => round($totalOwp / 0.4),
|
|
'totalRevenues' => round($totalRevenues),
|
|
'outstandings' => $this->invoiceOverviews,
|
|
'costsToPay' => $this->reservations(),
|
|
'invoiced' => $this->getInvoiced(),
|
|
'totalChance' => $this->getChanceRevenues(),
|
|
'totalPayment' => round(\Payment::getPaymentInYear(date('Y')))
|
|
]);
|
|
}
|
|
|
|
private function getRevenuesThisYear(): float
|
|
{
|
|
$year = date('Y');
|
|
$sql = \QEmployeeHours::fire(false, $year);
|
|
|
|
$db = \Config\Database::connect();
|
|
$query = $db->query($sql);
|
|
|
|
$result = $query->getResult();
|
|
|
|
$totalRevenue = 0;
|
|
foreach ($result as $revenue) {
|
|
$totalRevenue += $revenue->revenues;
|
|
}
|
|
|
|
return $totalRevenue;
|
|
}
|
|
|
|
private function setInvoiceOverview(): void
|
|
{
|
|
$projects = \Project::whereArchive('F')->get();
|
|
|
|
foreach ($projects as $project) {
|
|
$invoiceOverview = new \InvoiceOverview($project, date('Y'));
|
|
|
|
$this->invoiceOverviews[] = $invoiceOverview->getProjectDetails();
|
|
}
|
|
}
|
|
|
|
private function reservations(): float
|
|
{
|
|
$sql = \ReservationTotals::fire();
|
|
|
|
$db = \Config\Database::connect();
|
|
$query = $db->query($sql);
|
|
|
|
$results = $query->getResult();
|
|
|
|
$costsToPay = 0;
|
|
foreach ($results as $result) {
|
|
$toPay = $result->other_revenue - $result->costs + $result->saldoToPay;
|
|
$costsToPay += $toPay;
|
|
}
|
|
|
|
return $costsToPay;
|
|
}
|
|
|
|
private function getInvoiced(): float
|
|
{
|
|
$invoiced = \Invoice::getInvoiced(date('Y'));
|
|
|
|
$totalInvoiced = 0;
|
|
foreach ($invoiced as $invoice) {
|
|
$totalInvoiced += round(($invoice->invoiceamount * 1.21), 0);
|
|
}
|
|
|
|
return $totalInvoiced;
|
|
}
|
|
|
|
private function getChanceRevenues(): float
|
|
{
|
|
$offers = \Offer::getAllOffers(false, 'F', '', 'id', 'desc');
|
|
|
|
$totalChance = 0;
|
|
foreach ($offers as $offer) {
|
|
$totalChance += round(
|
|
($offer->own_revenue) * // + $offer->other_revenue mail 17-01-2022
|
|
($offer->acceptance_chance / 100),
|
|
0
|
|
);
|
|
}
|
|
|
|
return $totalChance;
|
|
}
|
|
}
|