-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added renvenue and profit to Analyses area
Fixes #25
- Loading branch information
1 parent
88c91ae
commit a44668d
Showing
9 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/Http/Controllers/FinancialRevenueProfitStatsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') ?> |