Refactor category repositories.

This commit is contained in:
James Cole
2019-08-28 12:28:23 +02:00
parent e11af70f99
commit fa706d27d8
12 changed files with 713 additions and 919 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use Illuminate\Support\Collection;
/**
* Class WholePeriodChartGenerator
@@ -43,6 +44,7 @@ class WholePeriodChartGenerator
*/
public function generate(Category $category, Carbon $start, Carbon $end): array
{
$collection = new Collection([$category]);
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
@@ -65,10 +67,11 @@ class WholePeriodChartGenerator
while ($current <= $end) {
$key = $current->format('Y-m-d');
$currentEnd = app('navigation')->endOfPeriod($current, $step);
$spent[$key] = $opsRepository->spentInPeriod($category, $accounts, $current, $currentEnd);
$earned[$key] = $opsRepository->earnedInPeriod($category, $accounts, $current, $currentEnd);
$spent[$key] = $opsRepository->sumExpenses($current, $currentEnd, $accounts, $collection);
$earned[$key] = $opsRepository->sumIncome($current, $currentEnd, $accounts, $collection);
$current = app('navigation')->addPeriod($current, $step, 0);
}
$currencies = $this->extractCurrencies($spent) + $this->extractCurrencies($earned);
// generate chart data (for each currency)
@@ -95,21 +98,23 @@ class WholePeriodChartGenerator
$current = clone $start;
while ($current <= $end) {
$key = $current->format('Y-m-d');
$label = app('navigation')->periodShow($current, $step);
$key = $current->format('Y-m-d');
$label = app('navigation')->periodShow($current, $step);
/** @var array $currency */
foreach ($currencies as $currency) {
$code = $currency['currency_code'];
$currencyId = $currency['currency_id'];
$spentInfoKey = sprintf('spent-in-%s', $code);
$earnedInfoKey = sprintf('earned-in-%s', $code);
$spentAmount = $spent[$key][$code]['spent'] ?? '0';
$earnedAmount = $earned[$key][$code]['earned'] ?? '0';
$spentAmount = $spent[$key][$currencyId]['sum'] ?? '0';
$earnedAmount = $earned[$key][$currencyId]['sum'] ?? '0';
$chartData[$spentInfoKey]['entries'][$label] = round($spentAmount, $currency['currency_decimal_places']);
$chartData[$earnedInfoKey]['entries'][$label] = round($earnedAmount, $currency['currency_decimal_places']);
}
$current = app('navigation')->addPeriod($current, $step, 0);
}
return $chartData;
}
@@ -150,18 +155,15 @@ class WholePeriodChartGenerator
private function extractCurrencies(array $array): array
{
$return = [];
foreach ($array as $info) {
foreach ($info as $block) {
$currencyId = $block['currency_id'];
if (!isset($return[$currencyId])) {
$return[$currencyId] = [
'currency_id' => $block['currency_id'],
'currency_code' => $block['currency_code'],
'currency_name' => $block['currency_name'],
'currency_symbol' => $block['currency_symbol'],
'currency_decimal_places' => $block['currency_decimal_places'],
foreach ($array as $block) {
foreach ($block as $currencyId => $currencyRow) {
$return[$currencyId] = $return[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_name' => $currencyRow['currency_name'],
'currency_symbol' => $currencyRow['currency_symbol'],
'currency_code' => $currencyRow['currency_code'],
'currency_decimal_places' => $currencyRow['currency_decimal_places'],
];
}
}
}