| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BudgetController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Report; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\Budget; | 
					
						
							| 
									
										
										
										
											2019-08-30 08:12:15 +02:00
										 |  |  | use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | use FireflyIII\Support\CacheProperties; | 
					
						
							| 
									
										
										
										
											2018-08-11 06:39:29 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\BasicDataSupport; | 
					
						
							| 
									
										
										
										
											2020-10-19 20:34:53 +02:00
										 |  |  | use FireflyIII\Support\Report\Budget\BudgetReportGenerator; | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | use JsonException; | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  | use Throwable; | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class BudgetController. | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class BudgetController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-11 06:39:29 +02:00
										 |  |  |     use BasicDataSupport; | 
					
						
							| 
									
										
										
										
											2018-07-08 12:08:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 06:48:53 +01:00
										 |  |  |     private OperationsRepositoryInterface $opsRepository; | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * ExpenseReportController constructor. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:15:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 $this->opsRepository = app(OperationsRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-10-20 04:22:57 +02:00
										 |  |  |      * Partial used in the budget report. | 
					
						
							| 
									
										
										
										
											2020-12-21 06:18:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Collection  $budgets | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2019-09-03 09:06:12 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      * @throws JsonException | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function accountPerBudget(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-20 04:22:57 +02:00
										 |  |  |         /** @var BudgetReportGenerator $generator */ | 
					
						
							|  |  |  |         $generator = app(BudgetReportGenerator::class); | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 04:22:57 +02:00
										 |  |  |         $generator->setUser(auth()->user()); | 
					
						
							|  |  |  |         $generator->setAccounts($accounts); | 
					
						
							|  |  |  |         $generator->setBudgets($budgets); | 
					
						
							|  |  |  |         $generator->setStart($start); | 
					
						
							|  |  |  |         $generator->setEnd($end); | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 04:22:57 +02:00
										 |  |  |         $generator->accountPerBudget(); | 
					
						
							|  |  |  |         $report = $generator->getReport(); | 
					
						
							| 
									
										
										
										
											2020-12-21 06:18:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('reports.budget.partials.account-per-budget', compact('report', 'budgets')); | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Collection  $budgets | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2019-09-02 21:47:20 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function accounts(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $spent  = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); | 
					
						
							|  |  |  |         $report = []; | 
					
						
							|  |  |  |         $sums   = []; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							|  |  |  |             $accountId          = $account->id; | 
					
						
							|  |  |  |             $report[$accountId] = $report[$accountId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                 'name'       => $account->name, | 
					
						
							|  |  |  |                 'id'         => $account->id, | 
					
						
							|  |  |  |                 'iban'       => $account->iban, | 
					
						
							|  |  |  |                 'currencies' => [], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // loop expenses.
 | 
					
						
							|  |  |  |         foreach ($spent as $currency) { | 
					
						
							|  |  |  |             $currencyId        = $currency['currency_id']; | 
					
						
							|  |  |  |             $sums[$currencyId] = $sums[$currencyId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                 'currency_id'             => $currency['currency_id'], | 
					
						
							|  |  |  |                 'currency_symbol'         => $currency['currency_symbol'], | 
					
						
							|  |  |  |                 'currency_name'           => $currency['currency_name'], | 
					
						
							|  |  |  |                 'currency_decimal_places' => $currency['currency_decimal_places'], | 
					
						
							|  |  |  |                 'sum'                     => '0', | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |             foreach ($currency['budgets'] as $budget) { | 
					
						
							|  |  |  |                 foreach ($budget['transaction_journals'] as $journal) { | 
					
						
							|  |  |  |                     $sourceAccountId                                            = $journal['source_account_id']; | 
					
						
							|  |  |  |                     $report[$sourceAccountId]['currencies'][$currencyId]        = $report[$sourceAccountId]['currencies'][$currencyId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                         'currency_id'             => $currency['currency_id'], | 
					
						
							|  |  |  |                         'currency_symbol'         => $currency['currency_symbol'], | 
					
						
							|  |  |  |                         'currency_name'           => $currency['currency_name'], | 
					
						
							|  |  |  |                         'currency_decimal_places' => $currency['currency_decimal_places'], | 
					
						
							|  |  |  |                         'sum'                     => '0', | 
					
						
							|  |  |  |                     ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |                     $report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd( | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |                         $report[$sourceAccountId]['currencies'][$currencyId]['sum'], | 
					
						
							|  |  |  |                         $journal['amount'] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |                     ); | 
					
						
							|  |  |  |                     $sums[$currencyId]['sum']                                   = bcadd($sums[$currencyId]['sum'], $journal['amount']); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('reports.budget.partials.accounts', compact('sums', 'report')); | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Collection  $budgets | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2019-09-03 09:06:12 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:54:58 +02:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function avgExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $spent  = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); | 
					
						
							|  |  |  |         $result = []; | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |         foreach ($spent as $currency) { | 
					
						
							|  |  |  |             foreach ($currency['budgets'] as $budget) { | 
					
						
							|  |  |  |                 foreach ($budget['transaction_journals'] as $journal) { | 
					
						
							| 
									
										
										
										
											2019-09-04 09:20:26 +02:00
										 |  |  |                     $destinationId = $journal['destination_account_id']; | 
					
						
							|  |  |  |                     $key           = sprintf('%d-%d', $destinationId, $currency['currency_id']); | 
					
						
							|  |  |  |                     $result[$key]  = $result[$key] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                         'transactions'             => 0, | 
					
						
							|  |  |  |                         'sum'                      => '0', | 
					
						
							|  |  |  |                         'avg'                      => '0', | 
					
						
							|  |  |  |                         'avg_float'                => 0, | 
					
						
							|  |  |  |                         'destination_account_name' => $journal['destination_account_name'], | 
					
						
							|  |  |  |                         'destination_account_id'   => $journal['destination_account_id'], | 
					
						
							|  |  |  |                         'currency_id'              => $currency['currency_id'], | 
					
						
							|  |  |  |                         'currency_name'            => $currency['currency_name'], | 
					
						
							|  |  |  |                         'currency_symbol'          => $currency['currency_symbol'], | 
					
						
							|  |  |  |                         'currency_decimal_places'  => $currency['currency_decimal_places'], | 
					
						
							|  |  |  |                     ]; | 
					
						
							| 
									
										
										
										
											2019-09-03 09:06:12 +02:00
										 |  |  |                     $result[$key]['transactions']++; | 
					
						
							|  |  |  |                     $result[$key]['sum']       = bcadd($journal['amount'], $result[$key]['sum']); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                     $result[$key]['avg']       = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']); | 
					
						
							|  |  |  |                     $result[$key]['avg_float'] = (float)$result[$key]['avg']; // intentional float
 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // sort by amount_float
 | 
					
						
							|  |  |  |         // sort temp array by amount.
 | 
					
						
							|  |  |  |         $amounts = array_column($result, 'avg_float'); | 
					
						
							|  |  |  |         array_multisort($amounts, SORT_ASC, $result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.budget.partials.avg-expenses', compact('result'))->render(); | 
					
						
							| 
									
										
										
										
											2022-10-31 05:53:36 +01:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |             Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); | 
					
						
							|  |  |  |             $result = sprintf('Could not render view: %s', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Collection  $budgets | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function budgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $spent  = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); | 
					
						
							|  |  |  |         $sums   = []; | 
					
						
							|  |  |  |         $report = []; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         foreach ($budgets as $budget) { | 
					
						
							|  |  |  |             $budgetId          = $budget->id; | 
					
						
							|  |  |  |             $report[$budgetId] = $report[$budgetId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                 'name'       => $budget->name, | 
					
						
							|  |  |  |                 'id'         => $budget->id, | 
					
						
							|  |  |  |                 'currencies' => [], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         foreach ($spent as $currency) { | 
					
						
							|  |  |  |             $currencyId        = $currency['currency_id']; | 
					
						
							|  |  |  |             $sums[$currencyId] = $sums[$currencyId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                 'currency_id'             => $currency['currency_id'], | 
					
						
							|  |  |  |                 'currency_symbol'         => $currency['currency_symbol'], | 
					
						
							|  |  |  |                 'currency_name'           => $currency['currency_name'], | 
					
						
							|  |  |  |                 'currency_decimal_places' => $currency['currency_decimal_places'], | 
					
						
							|  |  |  |                 'sum'                     => '0', | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |             /** @var array $budget */ | 
					
						
							|  |  |  |             foreach ($currency['budgets'] as $budget) { | 
					
						
							|  |  |  |                 $budgetId = $budget['id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 foreach ($budget['transaction_journals'] as $journal) { | 
					
						
							|  |  |  |                     // add currency info to report array:
 | 
					
						
							|  |  |  |                     $report[$budgetId]['currencies'][$currencyId]        = $report[$budgetId]['currencies'][$currencyId] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                         'sum'                     => '0', | 
					
						
							|  |  |  |                         'sum_pct'                 => '0', | 
					
						
							|  |  |  |                         'currency_id'             => $currency['currency_id'], | 
					
						
							|  |  |  |                         'currency_symbol'         => $currency['currency_symbol'], | 
					
						
							|  |  |  |                         'currency_name'           => $currency['currency_name'], | 
					
						
							|  |  |  |                         'currency_decimal_places' => $currency['currency_decimal_places'], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     ]; | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |                     $report[$budgetId]['currencies'][$currencyId]['sum'] = bcadd($report[$budgetId]['currencies'][$currencyId]['sum'], $journal['amount']); | 
					
						
							|  |  |  |                     $sums[$currencyId]['sum']                            = bcadd($sums[$currencyId]['sum'], $journal['amount']); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 09:20:26 +02:00
										 |  |  |         // loop again to get percentages.
 | 
					
						
							|  |  |  |         foreach ($report as $budgetId => $data) { | 
					
						
							| 
									
										
										
										
											2019-10-14 16:34:52 +02:00
										 |  |  |             foreach ($data['currencies'] as $currencyId => $dataX) { | 
					
						
							|  |  |  |                 $sum   = $dataX['sum'] ?? '0'; | 
					
						
							| 
									
										
										
										
											2019-09-04 09:20:26 +02:00
										 |  |  |                 $total = $sums[$currencyId]['sum'] ?? '0'; | 
					
						
							|  |  |  |                 $pct   = '0'; | 
					
						
							|  |  |  |                 if (0 !== bccomp($sum, '0') && 0 !== bccomp($total, '9')) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                     $pct = round((float)bcmul(bcdiv($sum, $total), '100')); // intentional float
 | 
					
						
							| 
									
										
										
										
											2019-09-04 09:20:26 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 $report[$budgetId]['currencies'][$currencyId]['sum_pct'] = $pct; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-21 06:18:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('reports.budget.partials.budgets', compact('sums', 'report')); | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 20:08:11 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:32 +02:00
										 |  |  |      * Show partial overview of budgets. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-11-19 07:27:54 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:54:58 +02:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      * @throws JsonException | 
					
						
							| 
									
										
										
										
											2016-11-02 20:08:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-08 12:08:53 +02:00
										 |  |  |     public function general(Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2016-11-02 20:08:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-19 20:34:53 +02:00
										 |  |  |         /** @var BudgetReportGenerator $generator */ | 
					
						
							|  |  |  |         $generator = app(BudgetReportGenerator::class); | 
					
						
							| 
									
										
										
										
											2020-04-19 06:10:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-19 20:34:53 +02:00
										 |  |  |         $generator->setUser(auth()->user()); | 
					
						
							|  |  |  |         $generator->setAccounts($accounts); | 
					
						
							|  |  |  |         $generator->setStart($start); | 
					
						
							|  |  |  |         $generator->setEnd($end); | 
					
						
							| 
									
										
										
										
											2020-02-01 15:54:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-19 20:34:53 +02:00
										 |  |  |         $generator->general(); | 
					
						
							|  |  |  |         $report = $generator->getReport(); | 
					
						
							| 
									
										
										
										
											2019-09-04 09:20:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('reports.partials.budgets', compact('report'))->render(); | 
					
						
							| 
									
										
										
										
											2016-11-02 20:08:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:32 +02:00
										 |  |  |      * Show budget overview for a period. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-12-06 07:06:20 +01:00
										 |  |  |      * @return mixed|string | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-06 07:06:20 +01:00
										 |  |  |     public function period(Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |         $cache = new CacheProperties(); | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |         $cache->addProperty($start); | 
					
						
							|  |  |  |         $cache->addProperty($end); | 
					
						
							| 
									
										
										
										
											2016-12-06 07:06:20 +01:00
										 |  |  |         $cache->addProperty('budget-period-report'); | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |         $cache->addProperty($accounts->pluck('id')->toArray()); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             return $cache->get(); | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $periods   = app('navigation')->listOfPeriods($start, $end); | 
					
						
							|  |  |  |         $keyFormat = app('navigation')->preferredCarbonFormat($start, $end); | 
					
						
							| 
									
										
										
										
											2019-09-01 18:41:57 +02:00
										 |  |  |         // list expenses for budgets in account(s)
 | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $expenses = $this->opsRepository->listExpenses($start, $end, $accounts); | 
					
						
							| 
									
										
										
										
											2019-09-01 19:08:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 16:52:35 +02:00
										 |  |  |         $report = []; | 
					
						
							| 
									
										
										
										
											2019-09-01 18:41:57 +02:00
										 |  |  |         foreach ($expenses as $currency) { | 
					
						
							|  |  |  |             foreach ($currency['budgets'] as $budget) { | 
					
						
							| 
									
										
										
										
											2019-09-28 04:47:49 +02:00
										 |  |  |                 $count = 0; | 
					
						
							| 
									
										
										
										
											2019-09-01 18:41:57 +02:00
										 |  |  |                 foreach ($budget['transaction_journals'] as $journal) { | 
					
						
							| 
									
										
										
										
											2019-09-28 04:47:49 +02:00
										 |  |  |                     $count++; | 
					
						
							|  |  |  |                     $key                               = sprintf('%d-%d', $budget['id'], $currency['currency_id']); | 
					
						
							|  |  |  |                     $dateKey                           = $journal['date']->format($keyFormat); | 
					
						
							|  |  |  |                     $report[$key]                      = $report[$key] ?? [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                         'id'                      => $budget['id'], | 
					
						
							|  |  |  |                         'name'                    => sprintf('%s (%s)', $budget['name'], $currency['currency_name']), | 
					
						
							|  |  |  |                         'sum'                     => '0', | 
					
						
							|  |  |  |                         'currency_id'             => $currency['currency_id'], | 
					
						
							|  |  |  |                         'currency_name'           => $currency['currency_name'], | 
					
						
							|  |  |  |                         'currency_symbol'         => $currency['currency_symbol'], | 
					
						
							|  |  |  |                         'currency_code'           => $currency['currency_code'], | 
					
						
							|  |  |  |                         'currency_decimal_places' => $currency['currency_decimal_places'], | 
					
						
							|  |  |  |                         'entries'                 => [], | 
					
						
							|  |  |  |                     ]; | 
					
						
							| 
									
										
										
										
											2019-09-28 04:47:49 +02:00
										 |  |  |                     $report[$key]['entries'][$dateKey] = $report[$key] ['entries'][$dateKey] ?? '0'; | 
					
						
							|  |  |  |                     $report[$key]['entries'][$dateKey] = bcadd($journal['amount'], $report[$key] ['entries'][$dateKey]); | 
					
						
							| 
									
										
										
										
											2020-10-18 16:44:34 +02:00
										 |  |  |                     $report[$key]['sum']               = bcadd($report[$key] ['sum'], $journal['amount']); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                     $report[$key]['avg']               = bcdiv($report[$key]['sum'], (string)count($periods)); | 
					
						
							| 
									
										
										
										
											2019-09-01 18:41:57 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-02 05:24:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.partials.budget-period', compact('report', 'periods'))->render(); | 
					
						
							| 
									
										
										
										
											2022-10-31 05:53:36 +01:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |             Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); | 
					
						
							|  |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 18:29:26 +01:00
										 |  |  |         $cache->store($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Collection  $accounts | 
					
						
							|  |  |  |      * @param  Collection  $budgets | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2019-09-03 09:06:12 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:54:58 +02:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function topExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-02 21:21:29 +02:00
										 |  |  |         $spent  = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); | 
					
						
							|  |  |  |         $result = []; | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |         foreach ($spent as $currency) { | 
					
						
							|  |  |  |             foreach ($currency['budgets'] as $budget) { | 
					
						
							|  |  |  |                 foreach ($budget['transaction_journals'] as $journal) { | 
					
						
							|  |  |  |                     $result[] = [ | 
					
						
							|  |  |  |                         'description'              => $journal['description'], | 
					
						
							|  |  |  |                         'transaction_group_id'     => $journal['transaction_group_id'], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                         'amount_float'             => (float)$journal['amount'], // intentional float
 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |                         'amount'                   => $journal['amount'], | 
					
						
							| 
									
										
										
										
											2022-03-27 20:24:13 +02:00
										 |  |  |                         'date'                     => $journal['date']->isoFormat($this->monthAndDayFormat), | 
					
						
							| 
									
										
										
										
											2020-12-21 06:18:59 +01:00
										 |  |  |                         'date_sort'                => $journal['date']->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |                         'destination_account_name' => $journal['destination_account_name'], | 
					
						
							|  |  |  |                         'destination_account_id'   => $journal['destination_account_id'], | 
					
						
							|  |  |  |                         'currency_id'              => $currency['currency_id'], | 
					
						
							|  |  |  |                         'currency_name'            => $currency['currency_name'], | 
					
						
							|  |  |  |                         'currency_symbol'          => $currency['currency_symbol'], | 
					
						
							|  |  |  |                         'currency_decimal_places'  => $currency['currency_decimal_places'], | 
					
						
							| 
									
										
										
										
											2019-09-02 21:47:20 +02:00
										 |  |  |                         'budget_id'                => $budget['id'], | 
					
						
							|  |  |  |                         'budget_name'              => $budget['name'], | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |                     ]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // sort by amount_float
 | 
					
						
							|  |  |  |         // sort temp array by amount.
 | 
					
						
							|  |  |  |         $amounts = array_column($result, 'amount_float'); | 
					
						
							|  |  |  |         array_multisort($amounts, SORT_ASC, $result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.budget.partials.top-expenses', compact('result'))->render(); | 
					
						
							| 
									
										
										
										
											2022-10-31 05:53:36 +01:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |             Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); | 
					
						
							|  |  |  |             $result = sprintf('Could not render view: %s', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2019-09-02 20:30:47 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-22 19:42:45 +01:00
										 |  |  | } |