mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Refactor code to traits.
This commit is contained in:
		| @@ -33,6 +33,7 @@ use FireflyIII\Models\Transaction; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | ||||
| use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | ||||
| use FireflyIII\Support\CacheProperties; | ||||
| use Illuminate\Support\Collection; | ||||
|  | ||||
| /** | ||||
| @@ -68,6 +69,41 @@ trait AugumentData | ||||
|         return $combined; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Small helper function for the revenue and expense account charts. | ||||
|      * | ||||
|      * @param array $names | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function expandNames(array $names): array | ||||
|     { | ||||
|         $result = []; | ||||
|         foreach ($names as $entry) { | ||||
|             $result[$entry['name']] = 0; | ||||
|         } | ||||
|  | ||||
|         return $result; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Small helper function for the revenue and expense account charts. | ||||
|      * | ||||
|      * @param Collection $accounts | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function extractNames(Collection $accounts): array | ||||
|     { | ||||
|         $return = []; | ||||
|         /** @var Account $account */ | ||||
|         foreach ($accounts as $account) { | ||||
|             $return[$account->id] = $account->name; | ||||
|         } | ||||
|  | ||||
|         return $return; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Returns the budget limits belonging to the given budget and valid on the given day. | ||||
|      * | ||||
| @@ -168,6 +204,43 @@ trait AugumentData | ||||
|         return $return; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets all budget limits for a budget. | ||||
|      * | ||||
|      * @param Budget $budget | ||||
|      * @param Carbon $start | ||||
|      * @param Carbon $end | ||||
|      * | ||||
|      * @return Collection | ||||
|      */ | ||||
|     protected function getLimits(Budget $budget, Carbon $start, Carbon $end): Collection // get data + augment with info | ||||
|     { | ||||
|         /** @var BudgetRepositoryInterface $repository */ | ||||
|         $repository = app(BudgetRepositoryInterface::class); | ||||
|         // properties for cache | ||||
|         $cache = new CacheProperties; | ||||
|         $cache->addProperty($start); | ||||
|         $cache->addProperty($end); | ||||
|         $cache->addProperty($budget->id); | ||||
|         $cache->addProperty('get-limits'); | ||||
|  | ||||
|         if ($cache->has()) { | ||||
|             return $cache->get(); // @codeCoverageIgnore | ||||
|         } | ||||
|  | ||||
|         $set    = $repository->getBudgetLimits($budget, $start, $end); | ||||
|         $limits = new Collection(); | ||||
|  | ||||
|         /** @var BudgetLimit $entry */ | ||||
|         foreach ($set as $entry) { | ||||
|             $entry->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date); | ||||
|             $limits->push($entry); | ||||
|         } | ||||
|         $cache->store($limits); | ||||
|  | ||||
|         return $set; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Helper function that groups expenses. | ||||
|      * | ||||
| @@ -260,4 +333,4 @@ trait AugumentData | ||||
|  | ||||
|         return $grouped; | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
							
								
								
									
										155
									
								
								app/Support/Http/Controllers/AutoCompleteCollector.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								app/Support/Http/Controllers/AutoCompleteCollector.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,155 @@ | ||||
| <?php | ||||
| /** | ||||
|  * AutoCompleteCollector.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Support\Http\Controllers; | ||||
|  | ||||
| use FireflyIII\Models\Account; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use FireflyIII\Repositories\Bill\BillRepositoryInterface; | ||||
| use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | ||||
| use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | ||||
| use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | ||||
| use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | ||||
| use FireflyIII\Repositories\Tag\TagRepositoryInterface; | ||||
| use Illuminate\Support\Collection; | ||||
|  | ||||
| /** | ||||
|  * Trait AutoCompleteCollector | ||||
|  */ | ||||
| trait AutoCompleteCollector | ||||
| { | ||||
|  | ||||
|     /** | ||||
|      * @param array  $unfiltered | ||||
|      * @param string $query | ||||
|      * | ||||
|      * @return array|null | ||||
|      */ | ||||
|     protected function filterResult(?array $unfiltered, string $query): ?array | ||||
|     { | ||||
|         if (null === $unfiltered) { | ||||
|             return null; // @codeCoverageIgnore | ||||
|         } | ||||
|         if ('' === $query) { | ||||
|             sort($unfiltered); | ||||
|  | ||||
|             return $unfiltered; | ||||
|         } | ||||
|         $return = []; | ||||
|         if ('' !== $query) { | ||||
|             $return = array_values( | ||||
|                 array_filter( | ||||
|                     $unfiltered, function (string $value) use ($query) { | ||||
|                     return !(false === stripos($value, $query)); | ||||
|                 }, ARRAY_FILTER_USE_BOTH | ||||
|                 ) | ||||
|             ); | ||||
|         } | ||||
|         sort($return); | ||||
|  | ||||
|  | ||||
|         return $return; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param array $types | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getAccounts(array $types): array | ||||
|     { | ||||
|         $repository = app(AccountRepositoryInterface::class); | ||||
|         // find everything: | ||||
|         /** @var Collection $collection */ | ||||
|         $collection = $repository->getAccountsByType($types); | ||||
|         $filtered   = $collection->filter( | ||||
|             function (Account $account) { | ||||
|                 return true === $account->active; | ||||
|             } | ||||
|         ); | ||||
|  | ||||
|         return array_values(array_unique($filtered->pluck('name')->toArray())); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getBills(): array | ||||
|     { | ||||
|         $repository = app(BillRepositoryInterface::class); | ||||
|  | ||||
|         return array_unique($repository->getActiveBills()->pluck('name')->toArray()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getBudgets(): array | ||||
|     { | ||||
|         $repository = app(BudgetRepositoryInterface::class); | ||||
|  | ||||
|         return array_unique($repository->getBudgets()->pluck('name')->toArray()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getCategories(): array | ||||
|     { | ||||
|         $repository = app(CategoryRepositoryInterface::class); | ||||
|  | ||||
|         return array_unique($repository->getCategories()->pluck('name')->toArray()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getCurrencyNames(): array | ||||
|     { | ||||
|         /** @var CurrencyRepositoryInterface $repository */ | ||||
|         $repository = app(CurrencyRepositoryInterface::class); | ||||
|  | ||||
|         return $repository->get()->pluck('name')->toArray(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getTags(): array | ||||
|     { | ||||
|         /** @var TagRepositoryInterface $repository */ | ||||
|         $repository = app(TagRepositoryInterface::class); | ||||
|  | ||||
|         return array_unique($repository->get()->pluck('tag')->toArray()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function getTransactionTypes(): array | ||||
|     { | ||||
|         $repository = app(JournalRepositoryInterface::class); | ||||
|  | ||||
|         return array_unique($repository->getTransactionTypes()->pluck('type')->toArray()); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										106
									
								
								app/Support/Http/Controllers/ChartGeneration.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								app/Support/Http/Controllers/ChartGeneration.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,106 @@ | ||||
| <?php | ||||
| /** | ||||
|  * ChartGeneration.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Support\Http\Controllers; | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use FireflyIII\Generator\Chart\Basic\GeneratorInterface; | ||||
| use FireflyIII\Models\Account; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | ||||
| use FireflyIII\Support\CacheProperties; | ||||
| use Illuminate\Support\Collection; | ||||
| use Log; | ||||
|  | ||||
| /** | ||||
|  * Trait ChartGeneration | ||||
|  */ | ||||
| trait ChartGeneration | ||||
| { | ||||
|     /** | ||||
|      * Shows an overview of the account balances for a set of accounts. | ||||
|      * | ||||
|      * @param Collection $accounts | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      * | ||||
|      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||||
|      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||||
|      */ | ||||
|     protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method. | ||||
|     { | ||||
|  | ||||
|         // chart properties for cache: | ||||
|         $cache = new CacheProperties(); | ||||
|         $cache->addProperty($start); | ||||
|         $cache->addProperty($end); | ||||
|         $cache->addProperty('chart.account.account-balance-chart'); | ||||
|         $cache->addProperty($accounts); | ||||
|         if ($cache->has()) { | ||||
|             return $cache->get(); // @codeCoverageIgnore | ||||
|         } | ||||
|         Log::debug('Regenerate chart.account.account-balance-chart from scratch.'); | ||||
|         /** @var GeneratorInterface $generator */ | ||||
|         $generator = app(GeneratorInterface::class); | ||||
|  | ||||
|         /** @var CurrencyRepositoryInterface $repository */ | ||||
|         $repository = app(CurrencyRepositoryInterface::class); | ||||
|         /** @var AccountRepositoryInterface $accountRepos */ | ||||
|         $accountRepos = app(AccountRepositoryInterface::class); | ||||
|  | ||||
|         $default   = app('amount')->getDefaultCurrency(); | ||||
|         $chartData = []; | ||||
|         /** @var Account $account */ | ||||
|         foreach ($accounts as $account) { | ||||
|             $currency = $repository->findNull((int)$accountRepos->getMetaValue($account, 'currency_id')); | ||||
|             if (null === $currency) { | ||||
|                 $currency = $default; | ||||
|             } | ||||
|             $currentSet = [ | ||||
|                 'label'           => $account->name, | ||||
|                 'currency_symbol' => $currency->symbol, | ||||
|                 'entries'         => [], | ||||
|             ]; | ||||
|  | ||||
|             $currentStart = clone $start; | ||||
|             $range        = app('steam')->balanceInRange($account, $start, clone $end); | ||||
|             $previous     = array_values($range)[0]; | ||||
|             while ($currentStart <= $end) { | ||||
|                 $format   = $currentStart->format('Y-m-d'); | ||||
|                 $label    = $currentStart->formatLocalized((string)trans('config.month_and_day')); | ||||
|                 $balance  = isset($range[$format]) ? round($range[$format], 12) : $previous; | ||||
|                 $previous = $balance; | ||||
|                 $currentStart->addDay(); | ||||
|                 $currentSet['entries'][$label] = $balance; | ||||
|             } | ||||
|             $chartData[] = $currentSet; | ||||
|         } | ||||
|         $data = $generator->multiSet($chartData); | ||||
|         $cache->store($data); | ||||
|  | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										53
									
								
								app/Support/Http/Controllers/CronRunner.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								app/Support/Http/Controllers/CronRunner.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| <?php | ||||
| /** | ||||
|  * CronRunner.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Support\Http\Controllers; | ||||
|  | ||||
| use FireflyIII\Exceptions\FireflyException; | ||||
| use FireflyIII\Support\Cronjobs\RecurringCronjob; | ||||
|  | ||||
| /** | ||||
|  * Trait CronRunner | ||||
|  */ | ||||
| trait CronRunner | ||||
| { | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|     protected function runRecurring(): string | ||||
|     { | ||||
|         /** @var RecurringCronjob $recurring */ | ||||
|         $recurring = app(RecurringCronjob::class); | ||||
|         try { | ||||
|             $result = $recurring->fire(); | ||||
|         } catch (FireflyException $e) { | ||||
|             return $e->getMessage(); | ||||
|         } | ||||
|         if (false === $result) { | ||||
|             return 'The recurring transaction cron job did not fire.'; | ||||
|         } | ||||
|  | ||||
|         return 'The recurring transaction cron job fired successfully.'; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -38,10 +38,13 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | ||||
| use FireflyIII\Repositories\Tag\TagRepositoryInterface; | ||||
| use FireflyIII\Support\CacheProperties; | ||||
| use Illuminate\Support\Collection; | ||||
| use Log; | ||||
|  | ||||
| /** | ||||
|  * Trait PeriodOverview. | ||||
|  * | ||||
|  * TODO verify this all works as expected. | ||||
|  * | ||||
|  * - Group expenses, income, etc. under this period. | ||||
|  * - Returns collection of arrays. Possible fields are: | ||||
|  * -    start (string), | ||||
| @@ -55,6 +58,7 @@ use Illuminate\Support\Collection; | ||||
|  */ | ||||
| trait PeriodOverview | ||||
| { | ||||
|  | ||||
|     /** | ||||
|      * This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range) | ||||
|      * and for each period, the amount of money spent and earned. This is a complex operation which is cached for | ||||
| @@ -255,6 +259,95 @@ trait PeriodOverview | ||||
|         return $entries; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * TODO has to be synced with the others. | ||||
|      * | ||||
|      * Show period overview for no category view. | ||||
|      * | ||||
|      * @param Carbon $theDate | ||||
|      * | ||||
|      * @return Collection | ||||
|      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||||
|      */ | ||||
|     protected function getNoCategoryPeriodOverview(Carbon $theDate): Collection // period overview method. | ||||
|     { | ||||
|         Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d'))); | ||||
|         $range = app('preferences')->get('viewRange', '1M')->data; | ||||
|         $first = $this->journalRepos->firstNull(); | ||||
|         $start = null === $first ? new Carbon : $first->date; | ||||
|         $end   = $theDate ?? new Carbon; | ||||
|  | ||||
|         Log::debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d'))); | ||||
|         Log::debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d'))); | ||||
|  | ||||
|         // properties for cache | ||||
|         $cache = new CacheProperties; | ||||
|         $cache->addProperty($start); | ||||
|         $cache->addProperty($end); | ||||
|         $cache->addProperty('no-category-period-entries'); | ||||
|  | ||||
|         if ($cache->has()) { | ||||
|             return $cache->get(); // @codeCoverageIgnore | ||||
|         } | ||||
|  | ||||
|         $dates   = app('navigation')->blockPeriods($start, $end, $range); | ||||
|         $entries = new Collection; | ||||
|  | ||||
|         foreach ($dates as $date) { | ||||
|  | ||||
|             // count journals without category in this period: | ||||
|             /** @var TransactionCollectorInterface $collector */ | ||||
|             $collector = app(TransactionCollectorInterface::class); | ||||
|             $collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory() | ||||
|                       ->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]); | ||||
|             $collector->removeFilter(InternalTransferFilter::class); | ||||
|             $count = $collector->getTransactions()->count(); | ||||
|  | ||||
|             // amount transferred | ||||
|             /** @var TransactionCollectorInterface $collector */ | ||||
|             $collector = app(TransactionCollectorInterface::class); | ||||
|             $collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory() | ||||
|                       ->withOpposingAccount()->setTypes([TransactionType::TRANSFER]); | ||||
|             $collector->removeFilter(InternalTransferFilter::class); | ||||
|             $transferred = app('steam')->positive((string)$collector->getTransactions()->sum('transaction_amount')); | ||||
|  | ||||
|             // amount spent | ||||
|             /** @var TransactionCollectorInterface $collector */ | ||||
|             $collector = app(TransactionCollectorInterface::class); | ||||
|             $collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes( | ||||
|                 [TransactionType::WITHDRAWAL] | ||||
|             ); | ||||
|             $spent = $collector->getTransactions()->sum('transaction_amount'); | ||||
|  | ||||
|             // amount earned | ||||
|             /** @var TransactionCollectorInterface $collector */ | ||||
|             $collector = app(TransactionCollectorInterface::class); | ||||
|             $collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes( | ||||
|                 [TransactionType::DEPOSIT] | ||||
|             ); | ||||
|             $earned = $collector->getTransactions()->sum('transaction_amount'); | ||||
|             /** @noinspection PhpUndefinedMethodInspection */ | ||||
|             $dateStr  = $date['end']->format('Y-m-d'); | ||||
|             $dateName = app('navigation')->periodShow($date['end'], $date['period']); | ||||
|             $entries->push( | ||||
|                 [ | ||||
|                     'string'      => $dateStr, | ||||
|                     'name'        => $dateName, | ||||
|                     'count'       => $count, | ||||
|                     'spent'       => $spent, | ||||
|                     'earned'      => $earned, | ||||
|                     'transferred' => $transferred, | ||||
|                     'start'       => clone $date['start'], | ||||
|                     'end'         => clone $date['end'], | ||||
|                 ] | ||||
|             ); | ||||
|         } | ||||
|         Log::debug('End of loops'); | ||||
|         $cache->store($entries); | ||||
|  | ||||
|         return $entries; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * This shows a period overview for a tag. It goes back in time and lists all relevant transactions and sums. | ||||
|      * | ||||
|   | ||||
		Reference in New Issue
	
	Block a user