Skip to content

Commit

Permalink
Added profit by years
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Aug 26, 2024
1 parent 286bafd commit 3c84859
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 27 deletions.
28 changes: 28 additions & 0 deletions assets/pages/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import axios from "axios";
import useAsync from "@/utils/use-async";
import {reactive} from "vue";
import PreloaderComponent from "@/components/Common/PreloaderComponent.vue";
import {useNumbers} from "@/composable/useNumbers";
const {formatPrice, formatPercent} = useNumbers()
interface SummaryCard {
name: string
Expand Down Expand Up @@ -86,6 +89,31 @@ run()
/>
</div>
</template>
<template v-if="pageData.data.statisticByYears">
<div class="text-2xl font-extrabold mt-6 mb-3">
Profit By Years
</div>
<table class="simple-table">
<thead>
<tr>
<th>Year</th>
<th>Start Year Profit (1 Jan)</th>
<th>Percent</th>
</tr>
</thead>
<tbody>
<tr
v-for="(item, index) in pageData.data.statisticByYears"
:key="index"
>
<td>{{ item.year }}</td>
<td>{{ formatPrice(item.profit) }}</td>
<td>{{ formatPercent(item.profitPercent) }}</td>
</tr>
</tbody>
</table>
</template>
</template>
</page-component>
</template>
Expand Down
9 changes: 9 additions & 0 deletions src/Controller/AnalyticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\User;
use App\Request\DTO\Deals\DealsFilterRequestDTO;
use App\Services\Deals\ClosedDealsService;
use App\Services\StatisticService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
Expand Down Expand Up @@ -36,4 +37,12 @@ public function monthlyClosedDeals(
$deals = $dealsService->getMonthlyDealsStat($user, $filter);
return $this->json($deals);
}

#[Route('/analytics/annual-stat', name: 'app_analytics_annual_statistic', methods: 'GET')]
public function annualStatistic(
StatisticService $statisticService
): JsonResponse {
$deals = $statisticService->getStatisticByYears();
return $this->json($deals);
}
}
9 changes: 6 additions & 3 deletions src/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Services\Deals\DealData;
use App\Services\DepositsService;
use App\Services\MarketData\Currencies\CurrencyService;
use App\Services\StatisticService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly AccountService $accountService,
private readonly DepositsService $depositsService,
private readonly StatisticService $statisticService
) {
}

Expand Down Expand Up @@ -61,9 +63,9 @@ public function index(#[CurrentUser] ?User $user): Response

return $this->json(
[
'usd' => $this->currencyService->getUSDRUBRate(),
'depositAccounts' => $depositAccounts,
'summary' => [
'usd' => $this->currencyService->getUSDRUBRate(),
'depositAccounts' => $depositAccounts,
'summary' => [
[
'name' => 'The Invested Amount',
'total' => $invested,
Expand Down Expand Up @@ -105,6 +107,7 @@ public function index(#[CurrentUser] ?User $user): Response
'currency' => '',
],
],
'statisticByYears' => $this->statisticService->getStatisticByYears(),
]
);
}
Expand Down
49 changes: 25 additions & 24 deletions src/Repository/StatisticRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\Statistic;
Expand All @@ -21,28 +23,27 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, Statistic::class);
}

// /**
// * @return Statistic[] Returns an array of Statistic objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('s.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }

// public function findOneBySomeField($value): ?Statistic
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
public function getLatestStatistic(): array
{
$connection = $this->getEntityManager()->getConnection();
$resultSet = $connection->executeQuery(
"SELECT * FROM statistic stat
inner join (select account_id, MAX(date) as max_date from statistic group by account_id) s_date
on stat.account_id = s_date.account_id and stat.date = s_date.max_date"
);

return $resultSet->fetchAllAssociative();
}

public function getStatisticByYears(): array
{
$connection = $this->getEntityManager()->getConnection();
$resultSet = $connection->executeQuery(
"SELECT * FROM statistic stat
inner join (select account_id, MIN(date) as max_date from statistic group by account_id, YEAR(statistic.date)) s_date
on stat.account_id = s_date.account_id and stat.date = s_date.max_date ORDER BY stat.date"
);

return $resultSet->fetchAllAssociative();
}
}
94 changes: 94 additions & 0 deletions src/Services/StatisticService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace App\Services;

use App\Entity\Statistic;
use Carbon\Carbon;
use Doctrine\ORM\EntityManagerInterface;

class StatisticService
{
public function __construct(
private readonly EntityManagerInterface $entityManager
) {
}

public function getStatisticByYears()
{
$statisticRepository = $this->entityManager->getRepository(Statistic::class);
$yearsData = $statisticRepository->getStatisticByYears();
$latestData = $statisticRepository->getLatestStatistic();

$statByYears = [];
foreach ($yearsData as $data) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $data['date']);
if (isset($statByYears[$date->year])) {
$existenceDate = $statByYears[$date->year];
} else {
$existenceDate = [
'year' => $date->year,
'date' => $date,
'balance' => 0,
'usd_balance' => 0,
'investments' => 0,
'current_value' => 0,
'profit' => 0,
];
}

$statByYears[$date->year] = [
'year' => $date->year,
'date' => $date,
'balance' => $existenceDate['balance'] + $data['balance'],
'usd_balance' => $existenceDate['usd_balance'] + $data['usd_balance'],
'investments' => $existenceDate['investments'] + $data['investments'],
'current_value' => $existenceDate['current_value'] + $data['current_value'],
'profit' => $existenceDate['profit'] + $data['profit'],
'profitPercent' => 0,
];
}

foreach ($latestData as $data) {
if (isset($statByYears['current'])) {
$existenceDate = $statByYears['current'];
} else {
$existenceDate = [
'year' => 'current',
'date' => $data['date'],
'balance' => 0,
'usd_balance' => 0,
'investments' => 0,
'current_value' => 0,
'profit' => 0,
];
}

$statByYears['current'] = [
'year' => 'current',
'date' => $data['date'],
'balance' => $existenceDate['balance'] + $data['balance'],
'usd_balance' => $existenceDate['usd_balance'] + $data['usd_balance'],
'investments' => $existenceDate['investments'] + $data['investments'],
'current_value' => $existenceDate['current_value'] + $data['current_value'],
'profit' => $existenceDate['profit'] + $data['profit'],
'profitPercent' => 0,
];
}


$prevKey = null;
// Calculate profit
foreach ($statByYears as $key => $statByYear) {
if ($prevKey) {
$statByYears[$prevKey]['profitPercent'] = round(($statByYear['profit'] * 100 / $statByYears[$prevKey]['profit']) - 100, 2);
}
$prevKey = $key;
}

unset($statByYears['current']);

return $statByYears;
}
}

0 comments on commit 3c84859

Please sign in to comment.