Skip to content

Commit

Permalink
Added renvenue and profit to Analyses area
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
MasterZydra committed Mar 11, 2024
1 parent 88c91ae commit a44668d
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Types of changes: `Added`, `Changed`, `Deprecate`, `Removed`, `Fixed`, `Secruity

## [Unreleased]

## v2.4.0 - 11.03.2024 - Added revenue and profit statistic

### Added
- Added renvenue and profit to Analyses area

## v2.3.1 - 10.03.2024 - Changed sort order for open volume distributions table

### Changed
Expand Down
67 changes: 67 additions & 0 deletions app/Http/Controllers/FinancialRevenueProfitStatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Http\Controllers;

use App\Models\DeliveryNote;
use App\Models\Invoice;
use Framework\Authentication\Auth;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Database\QueryBuilder;
use Framework\Routing\BaseController;
use Framework\Routing\ControllerInterface;

class FinancialRevenueProfitStatsController extends BaseController implements ControllerInterface
{
public function execute(): void
{
Auth::checkRole('Maintainer');

$dataSet = Database::executeBuilder(QueryBuilder::new('invoices')->select('DISTINCT year'));
$years = [];
if ($dataSet !== false) {
while ($row = $dataSet->fetch_assoc()) {
$years[] = $row['year'];
}
}

$data = [];
foreach ($years as $year) {
$data[$year] = $this->calculate($year);
}

view('statistics.revenueAndProfit', ['data' => $data]);
}

private function calculate(int $year): array
{
$invoices = Invoice::all(Invoice::getQueryBuilder()
->where(ColType::Int, 'year', Condition::Equal, $year)
->where(ColType::Int, 'isPaid', Condition::Equal, 1)
);

$invoiceIds = array_map(fn(Invoice $invoice): int => $invoice->getId(), $invoices);

$deliveryNotes = DeliveryNote::all(DeliveryNote::getQueryBuilder()
->where(ColType::Int, 'invoiceId', Condition::In, $invoiceIds)
);

$revenue = 0.0;
$payouts = 0.0;
/** @var \App\Models\DeliveryNote $deliveryNote */
foreach ($deliveryNotes as $deliveryNote) {
$revenue += $deliveryNote->getPrice()->getPrice() * $deliveryNote->getAmount();

if ($deliveryNote->getSupplier()->getHasFullPayout()) {
$payouts += $deliveryNote->getPrice()->getPrice() * $deliveryNote->getAmount();
} elseif (!$deliveryNote->getSupplier()->getHasNoPayout()) {
$payouts += $deliveryNote->getPrice()->getPricePayout() * $deliveryNote->getAmount();
}
}

$profit = $revenue - $payouts;

return ['revenue' => $revenue, 'payouts' => $payouts, 'profit' => $profit];
}
}
2 changes: 2 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Http\Controllers\EditImprintSettingsController;
use App\Http\Controllers\EditInvoiceSettingsController;
use App\Http\Controllers\EditVolumeDistributionController;
use App\Http\Controllers\FinancialRevenueProfitStatsController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\OpenVolumeDistributionsController;
use App\Http\Controllers\PlotController;
Expand Down Expand Up @@ -67,6 +68,7 @@
Router::addController('openVolumeDistributions', new OpenVolumeDistributionsController());
Router::addController('showVolumeDistribution', new VolumeDistributionPdfController());
Router::addController('showVolumeDistribution', new VolumeDistributionPdfController(), 'POST');
Router::addController('financialRevenueProfitStats', new FinancialRevenueProfitStatsController());
}

if (Auth::hasRole('Administrator')) {
Expand Down
14 changes: 14 additions & 0 deletions public/js/chart.umd.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'Email' => 'E-Mail',
'EntityCannotBeEdited' => 'Entität kann nicht bearbeitet werden',
'English' => 'Englisch',
'Finances' => 'Finanzen',
'Firstname' => 'Vorname',
'ForcePasswordChange' => 'Passwortänderung erzwingen',
'FullPayout' => 'Volle Auszahlung',
Expand Down Expand Up @@ -91,17 +92,21 @@
'PasswordChangedSuccessfully' => 'Passwort erfolgreich geändert',
'PasswordIsIncorrect' => 'Passwort ist falsch',
'Payout' => 'Auszahlung',
'Payouts' => 'Auszahlungen',
'Permissions' => 'Berechtigungen',
'PleaseSelect' => 'Bitte auswählen',
'Plot' => 'Flurstück',
'PostalCode' => 'Postleitzahl',
'Price' => 'Preis',
'Product' => 'Produkt',
'Profit' => 'Gewinn',
'Provider' => 'Anbieter',
'ReadyForInvoice' => 'Bereit für Rechnung',
'Recipient' => 'Empfänger',
'RepeatNewPassword' => 'Neues Passwort wiederholen',
'Responsible' => 'Verantwortlicher',
'Revenue' => 'Umsatz',
'RevenueAndProfit' => 'Umsatz und Gewinn',
'Save' => 'Speichern',
'Send' => 'Senden',
'Sender' => 'Absender',
Expand Down
5 changes: 5 additions & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'Email' => 'E-mail',
'EntityCannotBeEdited' => 'Entity cannot be edited',
'English' => 'English',
'Finances' => 'Finances',
'Firstname' => 'Firstname',
'ForcePasswordChange' => 'Force password change',
'FullPayout' => 'Full payout',
Expand Down Expand Up @@ -91,17 +92,21 @@
'PasswordChangedSuccessfully' => 'Password changed successfully',
'PasswordIsIncorrect' => 'Password is incorrect',
'Payout' => 'Payout',
'Payouts' => 'Payouts',
'Permissions' => 'Permissions',
'PleaseSelect' => 'Please select',
'Plot' => 'Plot',
'PostalCode' => 'Postal code',
'Price' => 'Price',
'Product' => 'Product',
'Profit' => 'Profit',
'Provider' => 'Provider',
'ReadyForInvoice' => 'Ready for invoice',
'Recipient' => 'Recipient',
'RepeatNewPassword' => 'Repeat new password',
'Responsible' => 'Responsible',
'Revenue' => 'Revenue',
'RevenueAndProfit' => 'Revenue and profit',
'Save' => 'Save',
'Send' => 'Send',
'Sender' => 'Sender',
Expand Down
2 changes: 1 addition & 1 deletion resources/Views/about.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="scrollable">
<tr>
<td>Bio-Manager Version</td>
<td class="right">2.3.1</td>
<td class="right">2.4.0</td>
</tr>
<tr>
<td><?= __('Developer') ?></td>
Expand Down
5 changes: 5 additions & 0 deletions resources/Views/auth/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<a href="showVolumeDistribution"><?= __('ShowVolumeDistribution') ?></a>
</div>

<div class="box">
<strong><?= __('Finances') ?></strong><br>
<a href="financialRevenueProfitStats"><?= __('RevenueAndProfit') ?></a>
</div>

<?php }
// Links for administration
if (Auth::hasRole('Administrator')) { ?>
Expand Down
81 changes: 81 additions & 0 deletions resources/Views/statistics/revenueAndProfit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php use Framework\Facades\Format; ?>
<?= component('layout.header') ?>

<h1><?= __('RevenueAndProfit') ?></h1>

<table class="scrollable">
<tr>
<th class="center"><?= __('Year') ?></th>
<th class="center"><?= __('Revenue') ?></th>
<th class="center"><?= __('Payouts') ?></th>
<th class="center"><?= __('Profit') ?></th>
</tr>
<?php foreach ($data as $year => $line): ?>
<tr>
<td class="center"><?= $year ?></td>
<td class="center"><?= Format::currency($line['revenue']) ?></td>
<td class="center"><?= Format::currency($line['payouts']) ?></td>
<td class="center"><?= Format::currency($line['profit']) ?></td>
</tr>
<?php endforeach ?>
</table>


<div style="margin-top: 20px; background-color: white;max-width: 1200px;">
<canvas id="myChart"></canvas>
</div>

<script src="js/chart.umd.js"></script>

<script>
const ctx = document.getElementById('myChart');

const handleResize = (chart) => {
chart.resize();
}

new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: '<?= __('Revenue') ?>',
data: <?= json_encode(array_map(fn(array $line): float => $line['revenue'], $data)) ?>,
borderWidth: 1
},
{
label: '<?= __('Payouts') ?>',
data: <?= json_encode(array_map(fn(array $line): float => $line['payouts'], $data)) ?>,
borderWidth: 1
},
{
label: '<?= __('Profit') ?>',
data: <?= json_encode(array_map(fn(array $line): float => $line['profit'], $data)) ?>,
borderWidth: 1
},
]
},
options: {
responsive: true,
onResize: handleResize,
maintainAspectRatio: true,
scales: {
x: {
title: {
display: true,
text: '<?= __('Year') ?>',
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: '<?= __('inX', setting('currencyUnit')) ?>'
}
}
}
}
});
</script>

<?= component('layout.footer') ?>

0 comments on commit a44668d

Please sign in to comment.