mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	Refactor code to traits.
This commit is contained in:
		| @@ -340,294 +340,4 @@ class ExpenseController extends Controller | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     /** @noinspection MoreThanThreeArgumentsInspection */ | ||||
|     /** | ||||
|      * Group by category (earnings). | ||||
|      * | ||||
|      * @param Collection $assets | ||||
|      * @param Collection $opposing | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      * | ||||
|      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||||
|      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||||
|      */ | ||||
|     protected function earnedByCategory(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info | ||||
|     { | ||||
|         /** @var TransactionCollectorInterface $collector */ | ||||
|         $collector = app(TransactionCollectorInterface::class); | ||||
|         $collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($assets); | ||||
|         $collector->setOpposingAccounts($opposing)->withCategoryInformation(); | ||||
|         $set = $collector->getTransactions(); | ||||
|         $sum = []; | ||||
|         // loop to support multi currency | ||||
|         foreach ($set as $transaction) { | ||||
|             $currencyId   = $transaction->transaction_currency_id; | ||||
|             $categoryName = $transaction->transaction_category_name; | ||||
|             $categoryId   = (int)$transaction->transaction_category_id; | ||||
|             // if null, grab from journal: | ||||
|             if (0 === $categoryId) { | ||||
|                 $categoryName = $transaction->transaction_journal_category_name; | ||||
|                 $categoryId   = (int)$transaction->transaction_journal_category_id; | ||||
|             } | ||||
|             if (0 !== $categoryId) { | ||||
|                 $categoryName = app('steam')->tryDecrypt($categoryName); | ||||
|             } | ||||
|  | ||||
|             // if not set, set to zero: | ||||
|             if (!isset($sum[$categoryId][$currencyId])) { | ||||
|                 $sum[$categoryId] = [ | ||||
|                     'grand_total'  => '0', | ||||
|                     'name'         => $categoryName, | ||||
|                     'per_currency' => [ | ||||
|                         $currencyId => [ | ||||
|                             'sum'      => '0', | ||||
|                             'category' => [ | ||||
|                                 'id'   => $categoryId, | ||||
|                                 'name' => $categoryName, | ||||
|                             ], | ||||
|                             'currency' => [ | ||||
|                                 'symbol' => $transaction->transaction_currency_symbol, | ||||
|                                 'dp'     => $transaction->transaction_currency_dp, | ||||
|                             ], | ||||
|                         ], | ||||
|                     ], | ||||
|                 ]; | ||||
|             } | ||||
|  | ||||
|             // add amount | ||||
|             $sum[$categoryId]['per_currency'][$currencyId]['sum'] = bcadd( | ||||
|                 $sum[$categoryId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount | ||||
|             ); | ||||
|             $sum[$categoryId]['grand_total']                      = bcadd($sum[$categoryId]['grand_total'], $transaction->transaction_amount); | ||||
|         } | ||||
|  | ||||
|         return $sum; | ||||
|     } | ||||
|  | ||||
|     /** @noinspection MoreThanThreeArgumentsInspection */ | ||||
|     /** | ||||
|      * Earned in period for accounts. | ||||
|      * | ||||
|      * @param Collection $assets | ||||
|      * @param Collection $opposing | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function earnedInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info | ||||
|     { | ||||
|         /** @var TransactionCollectorInterface $collector */ | ||||
|         $collector = app(TransactionCollectorInterface::class); | ||||
|         $collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($assets); | ||||
|         $collector->setOpposingAccounts($opposing); | ||||
|         $set = $collector->getTransactions(); | ||||
|         $sum = [ | ||||
|             'grand_sum'    => '0', | ||||
|             'per_currency' => [], | ||||
|         ]; | ||||
|         // loop to support multi currency | ||||
|         foreach ($set as $transaction) { | ||||
|             $currencyId = $transaction->transaction_currency_id; | ||||
|  | ||||
|             // if not set, set to zero: | ||||
|             if (!isset($sum['per_currency'][$currencyId])) { | ||||
|                 $sum['per_currency'][$currencyId] = [ | ||||
|                     'sum'      => '0', | ||||
|                     'currency' => [ | ||||
|                         'symbol' => $transaction->transaction_currency_symbol, | ||||
|                         'dp'     => $transaction->transaction_currency_dp, | ||||
|                     ], | ||||
|                 ]; | ||||
|             } | ||||
|  | ||||
|             // add amount | ||||
|             $sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $transaction->transaction_amount); | ||||
|             $sum['grand_sum']                        = bcadd($sum['grand_sum'], $transaction->transaction_amount); | ||||
|         } | ||||
|  | ||||
|         return $sum; | ||||
|     } | ||||
|  | ||||
|     /** @noinspection MoreThanThreeArgumentsInspection */ | ||||
|     /** | ||||
|      * Spent by budget. | ||||
|      * | ||||
|      * @param Collection $assets | ||||
|      * @param Collection $opposing | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||||
|      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||||
|      */ | ||||
|     protected function spentByBudget(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info | ||||
|     { | ||||
|         /** @var TransactionCollectorInterface $collector */ | ||||
|         $collector = app(TransactionCollectorInterface::class); | ||||
|         $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets); | ||||
|         $collector->setOpposingAccounts($opposing)->withBudgetInformation(); | ||||
|         $set = $collector->getTransactions(); | ||||
|         $sum = []; | ||||
|         // loop to support multi currency | ||||
|         foreach ($set as $transaction) { | ||||
|             $currencyId = $transaction->transaction_currency_id; | ||||
|             $budgetName = $transaction->transaction_budget_name; | ||||
|             $budgetId   = (int)$transaction->transaction_budget_id; | ||||
|             // if null, grab from journal: | ||||
|             if (0 === $budgetId) { | ||||
|                 $budgetName = $transaction->transaction_journal_budget_name; | ||||
|                 $budgetId   = (int)$transaction->transaction_journal_budget_id; | ||||
|             } | ||||
|             if (0 !== $budgetId) { | ||||
|                 $budgetName = app('steam')->tryDecrypt($budgetName); | ||||
|             } | ||||
|  | ||||
|             // if not set, set to zero: | ||||
|             if (!isset($sum[$budgetId][$currencyId])) { | ||||
|                 $sum[$budgetId] = [ | ||||
|                     'grand_total'  => '0', | ||||
|                     'name'         => $budgetName, | ||||
|                     'per_currency' => [ | ||||
|                         $currencyId => [ | ||||
|                             'sum'      => '0', | ||||
|                             'budget'   => [ | ||||
|                                 'id'   => $budgetId, | ||||
|                                 'name' => $budgetName, | ||||
|                             ], | ||||
|                             'currency' => [ | ||||
|                                 'symbol' => $transaction->transaction_currency_symbol, | ||||
|                                 'dp'     => $transaction->transaction_currency_dp, | ||||
|                             ], | ||||
|                         ], | ||||
|                     ], | ||||
|                 ]; | ||||
|             } | ||||
|  | ||||
|             // add amount | ||||
|             $sum[$budgetId]['per_currency'][$currencyId]['sum'] = bcadd( | ||||
|                 $sum[$budgetId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount | ||||
|             ); | ||||
|             $sum[$budgetId]['grand_total']                      = bcadd($sum[$budgetId]['grand_total'], $transaction->transaction_amount); | ||||
|         } | ||||
|  | ||||
|         return $sum; | ||||
|     } | ||||
|  | ||||
|     /** @noinspection MoreThanThreeArgumentsInspection */ | ||||
|     /** | ||||
|      * Spent by category. | ||||
|      * | ||||
|      * @param Collection $assets | ||||
|      * @param Collection $opposing | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      * | ||||
|      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||||
|      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||||
|      */ | ||||
|     protected function spentByCategory(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info | ||||
|     { | ||||
|         /** @var TransactionCollectorInterface $collector */ | ||||
|         $collector = app(TransactionCollectorInterface::class); | ||||
|         $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets); | ||||
|         $collector->setOpposingAccounts($opposing)->withCategoryInformation(); | ||||
|         $set = $collector->getTransactions(); | ||||
|         $sum = []; | ||||
|         // loop to support multi currency | ||||
|         foreach ($set as $transaction) { | ||||
|             $currencyId   = $transaction->transaction_currency_id; | ||||
|             $categoryName = $transaction->transaction_category_name; | ||||
|             $categoryId   = (int)$transaction->transaction_category_id; | ||||
|             // if null, grab from journal: | ||||
|             if (0 === $categoryId) { | ||||
|                 $categoryName = $transaction->transaction_journal_category_name; | ||||
|                 $categoryId   = (int)$transaction->transaction_journal_category_id; | ||||
|             } | ||||
|             if (0 !== $categoryId) { | ||||
|                 $categoryName = app('steam')->tryDecrypt($categoryName); | ||||
|             } | ||||
|  | ||||
|             // if not set, set to zero: | ||||
|             if (!isset($sum[$categoryId][$currencyId])) { | ||||
|                 $sum[$categoryId] = [ | ||||
|                     'grand_total'  => '0', | ||||
|                     'name'         => $categoryName, | ||||
|                     'per_currency' => [ | ||||
|                         $currencyId => [ | ||||
|                             'sum'      => '0', | ||||
|                             'category' => [ | ||||
|                                 'id'   => $categoryId, | ||||
|                                 'name' => $categoryName, | ||||
|                             ], | ||||
|                             'currency' => [ | ||||
|                                 'symbol' => $transaction->transaction_currency_symbol, | ||||
|                                 'dp'     => $transaction->transaction_currency_dp, | ||||
|                             ], | ||||
|                         ], | ||||
|                     ], | ||||
|                 ]; | ||||
|             } | ||||
|  | ||||
|             // add amount | ||||
|             $sum[$categoryId]['per_currency'][$currencyId]['sum'] = bcadd( | ||||
|                 $sum[$categoryId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount | ||||
|             ); | ||||
|             $sum[$categoryId]['grand_total']                      = bcadd($sum[$categoryId]['grand_total'], $transaction->transaction_amount); | ||||
|         } | ||||
|  | ||||
|         return $sum; | ||||
|     } | ||||
|  | ||||
|     /** @noinspection MoreThanThreeArgumentsInspection */ | ||||
|     /** | ||||
|      * Spent in a period. | ||||
|      * | ||||
|      * @param Collection $assets | ||||
|      * @param Collection $opposing | ||||
|      * @param Carbon     $start | ||||
|      * @param Carbon     $end | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function spentInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info | ||||
|     { | ||||
|         /** @var TransactionCollectorInterface $collector */ | ||||
|         $collector = app(TransactionCollectorInterface::class); | ||||
|         $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets); | ||||
|         $collector->setOpposingAccounts($opposing); | ||||
|         $set = $collector->getTransactions(); | ||||
|         $sum = [ | ||||
|             'grand_sum'    => '0', | ||||
|             'per_currency' => [], | ||||
|         ]; | ||||
|         // loop to support multi currency | ||||
|         foreach ($set as $transaction) { | ||||
|             $currencyId = (int)$transaction->transaction_currency_id; | ||||
|  | ||||
|             // if not set, set to zero: | ||||
|             if (!isset($sum['per_currency'][$currencyId])) { | ||||
|                 $sum['per_currency'][$currencyId] = [ | ||||
|                     'sum'      => '0', | ||||
|                     'currency' => [ | ||||
|                         'symbol' => $transaction->transaction_currency_symbol, | ||||
|                         'dp'     => $transaction->transaction_currency_dp, | ||||
|                     ], | ||||
|                 ]; | ||||
|             } | ||||
|  | ||||
|             // add amount | ||||
|             $sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $transaction->transaction_amount); | ||||
|             $sum['grand_sum']                        = bcadd($sum['grand_sum'], $transaction->transaction_amount); | ||||
|         } | ||||
|  | ||||
|         return $sum; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user