-
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.
- Loading branch information
Showing
5 changed files
with
162 additions
and
27 deletions.
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
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,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; | ||
} | ||
} |