. */ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Report; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Category; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Http\Controllers\BasicDataSupport; use Illuminate\Support\Collection; use Log; use Throwable; /** * Class CategoryController. */ class CategoryController extends Controller { use BasicDataSupport; /** * Show overview of expenses in category. * * @param Collection $accounts * @param Carbon $start * @param Carbon $end * * @return mixed|string */ public function expenses(Collection $accounts, Carbon $start, Carbon $end) { $cache = new CacheProperties; $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category-period-expenses-report'); $cache->addProperty($accounts->pluck('id')->toArray()); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } /** @var CategoryRepositoryInterface $repository */ $repository = app(CategoryRepositoryInterface::class); $categories = $repository->getCategories(); $data = $repository->periodExpenses($categories, $accounts, $start, $end); $data[0] = $repository->periodExpensesNoCategory($accounts, $start, $end); $report = $this->filterPeriodReport($data); // depending on the carbon format (a reliable way to determine the general date difference) // change the "listOfPeriods" call so the entire period gets included correctly. $range = app('navigation')->preferredCarbonFormat($start, $end); if ('Y' === $range) { $start->startOfYear(); } if ('Y-m' === $range) { $start->startOfMonth(); } $periods = app('navigation')->listOfPeriods($start, $end); try { $result = view('reports.partials.category-period', compact('report', 'periods'))->render(); // @codeCoverageIgnoreStart } catch (Throwable $e) { Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage())); $result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage()); } // @codeCoverageIgnoreEnd $cache->store($result); return $result; } /** * Show overview of income in category. * * @param Collection $accounts * * @param Carbon $start * @param Carbon $end * * @return string */ public function income(Collection $accounts, Carbon $start, Carbon $end): string { $cache = new CacheProperties; $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category-period-income-report'); $cache->addProperty($accounts->pluck('id')->toArray()); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } /** @var CategoryRepositoryInterface $repository */ $repository = app(CategoryRepositoryInterface::class); $categories = $repository->getCategories(); $data = $repository->periodIncome($categories, $accounts, $start, $end); $data[0] = $repository->periodIncomeNoCategory($accounts, $start, $end); $report = $this->filterPeriodReport($data); // depending on the carbon format (a reliable way to determine the general date difference) // change the "listOfPeriods" call so the entire period gets included correctly. $range = app('navigation')->preferredCarbonFormat($start, $end); if ('Y' === $range) { $start->startOfYear(); } if ('Y-m' === $range) { $start->startOfMonth(); } $periods = app('navigation')->listOfPeriods($start, $end); try { $result = view('reports.partials.category-period', compact('report', 'periods'))->render(); // @codeCoverageIgnoreStart } catch (Throwable $e) { Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage())); $result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage()); } // @codeCoverageIgnoreEnd $cache->store($result); return $result; } /** * Show overview of operations. * * @param Collection $accounts * @param Carbon $start * @param Carbon $end * * @return mixed|string * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function operations(Collection $accounts, Carbon $start, Carbon $end) { // chart properties for cache: $cache = new CacheProperties; $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category-report'); $cache->addProperty($accounts->pluck('id')->toArray()); if ($cache->has()) { //return $cache->get(); // @codeCoverageIgnore } /** @var CategoryRepositoryInterface $repository */ $repository = app(CategoryRepositoryInterface::class); $categories = $repository->getCategories(); $report = [ 'categories' => [], 'sums' => [], ]; /** @var Category $category */ foreach ($categories as $category) { $spent = $repository->spentInPeriod($category, $accounts, $start, $end); $earned = $repository->earnedInPeriod($category, $accounts, $start, $end); if (0 === count($spent) && 0 === count($earned)) { continue; } $currencies = array_unique(array_merge(array_keys($spent), array_keys($earned))); foreach ($currencies as $code) { $currencyInfo = $spent[$code] ?? $earned[$code]; $key = sprintf('%s-%s', $category->id, $code); $report['categories'][$key] = [ 'name' => $category->name, 'spent' => $spent[$code]['spent'] ?? '0', 'earned' => $earned[$code]['earned'] ?? '0', 'id' => $category->id, 'currency_id' => $currencyInfo['currency_id'], 'currency_code' => $currencyInfo['currency_code'], 'currency_symbol' => $currencyInfo['currency_symbol'], 'currency_name' => $currencyInfo['currency_name'], 'currency_decimal_places' => $currencyInfo['currency_decimal_places'], ]; } } $sum = []; /** * @var string $categoryId * @var array $row */ foreach ($report['categories'] as $categoryId => $row) { $sum[$categoryId] = (float)$row['spent']; } array_multisort($sum, SORT_ASC, $report['categories']); // get sums: foreach ($report['categories'] as $entry) { $currencyId = $entry['currency_id']; $report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [ 'spent' => '0', 'earned' => '0', 'currency_id' => $entry['currency_id'], 'currency_code' => $entry['currency_code'], 'currency_symbol' => $entry['currency_symbol'], 'currency_name' => $entry['currency_name'], 'cyrrency_decimal_places' => $entry['currency_decimal_places'], ]; $report['sums'][$currencyId]['spent'] = bcadd($report['sums'][$currencyId]['spent'], $entry['spent']); $report['sums'][$currencyId]['earned'] = bcadd($report['sums'][$currencyId]['earned'], $entry['earned']); } // @codeCoverageIgnoreStart try { $result = view('reports.partials.categories', compact('report'))->render(); $cache->store($result); } catch (Throwable $e) { Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage())); $result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage()); } // @codeCoverageIgnoreEnd return $result; } /** * @param array $array * * @return bool */ private function noAmountInArray(array $array): bool { if (0 === count($array)) { return true; } return false; } }