mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 10:47:00 +00:00 
			
		
		
		
	Make sure sorting doesnt break opening balance.
This commit is contained in:
		| @@ -70,6 +70,7 @@ class CorrectDatabase extends Command | ||||
|             'firefly-iii:delete-empty-journals', | ||||
|             'firefly-iii:delete-empty-groups', | ||||
|             'firefly-iii:fix-account-types', | ||||
|             'firefly-iii:fix-account-order', | ||||
|             'firefly-iii:rename-meta-fields', | ||||
|             'firefly-iii:fix-ob-currencies', | ||||
|             'firefly-iii:fix-long-descriptions', | ||||
|   | ||||
							
								
								
									
										94
									
								
								app/Console/Commands/Correction/FixAccountOrder.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								app/Console/Commands/Correction/FixAccountOrder.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | ||||
| <?php | ||||
| /** | ||||
|  * FixAccountTypes.php | ||||
|  * Copyright (c) 2020 james@firefly-iii.org | ||||
|  * | ||||
|  * This file is part of Firefly III (https://github.com/firefly-iii). | ||||
|  * | ||||
|  * 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. | ||||
|  * | ||||
|  * This program 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 Affero General Public License for more details. | ||||
|  * | ||||
|  * 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/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Console\Commands\Correction; | ||||
|  | ||||
| use FireflyIII\Exceptions\FireflyException; | ||||
| use FireflyIII\Models\AccountType; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Console\Command; | ||||
|  | ||||
| /** | ||||
|  * Class FixAccountOrder | ||||
|  */ | ||||
| class FixAccountOrder extends Command | ||||
| { | ||||
|     /** | ||||
|      * The console command description. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $description = 'Make sure account order is correct.'; | ||||
|     /** | ||||
|      * The name and signature of the console command. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $signature = 'firefly-iii:fix-account-order'; | ||||
|  | ||||
|     private AccountRepositoryInterface $repository; | ||||
|  | ||||
|     /** | ||||
|      * Execute the console command. | ||||
|      * | ||||
|      * @throws FireflyException | ||||
|      * @return int | ||||
|      */ | ||||
|     public function handle(): int | ||||
|     { | ||||
|         $this->stupidLaravel(); | ||||
|         $start = microtime(true); | ||||
|  | ||||
|         $users = User::get(); | ||||
|         foreach ($users as $user) { | ||||
|             $this->repository->setUser($user); | ||||
|             $sets = [ | ||||
|                 [AccountType::DEFAULT, AccountType::ASSET], | ||||
|                 [AccountType::EXPENSE, AccountType::BENEFICIARY], | ||||
|                 [AccountType::REVENUE], | ||||
|                 [AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE], | ||||
|             ]; | ||||
|             foreach ($sets as $set) { | ||||
|                 $this->repository->resetAccountOrder($set); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $end = round(microtime(true) - $start, 2); | ||||
|         $this->info(sprintf('Verifying account order took %s seconds', $end)); | ||||
|  | ||||
|         return 0; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is | ||||
|      * executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should | ||||
|      * be called from the handle method instead of using the constructor to initialize the command. | ||||
|      * | ||||
|      * @codeCoverageIgnore | ||||
|      */ | ||||
|     private function stupidLaravel(): void | ||||
|     { | ||||
|         $this->repository = app(AccountRepositoryInterface::class); | ||||
|     } | ||||
| } | ||||
| @@ -89,6 +89,7 @@ class UpgradeDatabase extends Command | ||||
|             'firefly-iii:delete-empty-journals', | ||||
|             'firefly-iii:delete-empty-groups', | ||||
|             'firefly-iii:fix-account-types', | ||||
|             'firefly-iii:fix-account-order', | ||||
|             'firefly-iii:rename-meta-fields', | ||||
|             'firefly-iii:fix-ob-currencies', | ||||
|             'firefly-iii:fix-long-descriptions', | ||||
|   | ||||
| @@ -32,6 +32,7 @@ use Illuminate\Contracts\View\Factory; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Pagination\LengthAwarePaginator; | ||||
| use Illuminate\View\View; | ||||
| use Exception; | ||||
|  | ||||
| /** | ||||
|  * | ||||
| @@ -129,6 +130,7 @@ class IndexController extends Controller | ||||
|      * @param Request $request | ||||
|      * @param string  $objectType | ||||
|      * | ||||
|      * @throws Exception | ||||
|      * @return Factory|View | ||||
|      */ | ||||
|     public function index(Request $request, string $objectType) | ||||
| @@ -138,10 +140,17 @@ class IndexController extends Controller | ||||
|             return $this->emptyIndex($objectType); | ||||
|         } | ||||
|  | ||||
|         // reset account order: | ||||
|  | ||||
|         $objectType   = $objectType ?? 'asset'; | ||||
|         $subTitle     = (string) trans(sprintf('firefly.%s_accounts', $objectType)); | ||||
|         $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); | ||||
|         $types        = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); | ||||
|  | ||||
|         if (1 === random_int(0, 20)) { | ||||
|             $this->repository->resetAccountOrder($types); | ||||
|         } | ||||
|  | ||||
|         $collection    = $this->repository->getActiveAccountsByType($types); | ||||
|         $total         = $collection->count(); | ||||
|         $page          = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); | ||||
|   | ||||
| @@ -71,7 +71,7 @@ class InstallController extends Controller | ||||
|             'firefly-iii:restore-oauth-keys'           => [], | ||||
|             'generate-keys'                            => [], // an exception :( | ||||
|  | ||||
|             // there are 14 upgrade commands. | ||||
|             // upgrade commands | ||||
|             'firefly-iii:transaction-identifiers'      => [], | ||||
|             'firefly-iii:migrate-to-groups'            => [], | ||||
|             'firefly-iii:account-currencies'           => [], | ||||
| @@ -87,7 +87,7 @@ class InstallController extends Controller | ||||
|             'firefly-iii:migrate-recurrence-meta'      => [], | ||||
|             'firefly-iii:migrate-tag-locations'        => [], | ||||
|  | ||||
|             // there are 16 verify commands. | ||||
|             // verify commands | ||||
|             'firefly-iii:fix-piggies'                  => [], | ||||
|             'firefly-iii:create-link-types'            => [], | ||||
|             'firefly-iii:create-access-tokens'         => [], | ||||
| @@ -100,6 +100,7 @@ class InstallController extends Controller | ||||
|             'firefly-iii:delete-empty-journals'        => [], | ||||
|             'firefly-iii:delete-empty-groups'          => [], | ||||
|             'firefly-iii:fix-account-types'            => [], | ||||
|             'firefly-iii:fix-account-order'            => [], | ||||
|             'firefly-iii:rename-meta-fields'           => [], | ||||
|             'firefly-iii:fix-ob-currencies'            => [], | ||||
|             'firefly-iii:fix-long-descriptions'        => [], | ||||
|   | ||||
| @@ -697,4 +697,20 @@ class AccountRepository implements AccountRepositoryInterface | ||||
|  | ||||
|         return TransactionCurrency::whereIn('id', $currencyIds)->get(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function resetAccountOrder(array $types): void | ||||
|     { | ||||
|         $list = $this->getAccountsByType($types); | ||||
|         /** | ||||
|          * @var int     $index | ||||
|          * @var Account $account | ||||
|          */ | ||||
|         foreach ($list as $index => $account) { | ||||
|             $account->order = $index + 1; | ||||
|             $account->save(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -47,6 +47,13 @@ interface AccountRepositoryInterface | ||||
|      */ | ||||
|     public function count(array $types): int; | ||||
|  | ||||
|     /** | ||||
|      * Reset order types of the mentioned accounts. | ||||
|      * | ||||
|      * @param array $types | ||||
|      */ | ||||
|     public function resetAccountOrder(array $types): void; | ||||
|  | ||||
|     /** | ||||
|      * @param Account $account | ||||
|      * | ||||
|   | ||||
| @@ -162,6 +162,27 @@ trait AccountServiceTrait | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Returns true if the data in the array is submitted but empty. | ||||
|      * | ||||
|      * @param array $data | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function isEmptyOBData(array $data): bool | ||||
|     { | ||||
|         if (!isset($data['opening_balance']) && !isset($data['opening_balance_date'])) { | ||||
|             // not set, so false. | ||||
|             return false; | ||||
|         } | ||||
|         // if isset, but is empty: | ||||
|         if ('' === $data['opening_balance'] && '' === $data['opening_balance_date']) { | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Account $account | ||||
|      * @param array   $data | ||||
|   | ||||
| @@ -115,12 +115,14 @@ class AccountUpdateService | ||||
|         // if it can have a virtual balance, it can also have an opening balance. | ||||
|  | ||||
|         if (in_array($type->type, $this->canHaveVirtual, true)) { | ||||
|             // check if is submitted as empty, that makes it valid: | ||||
|  | ||||
|             if ($this->validOBData($data)) { | ||||
|  | ||||
|             if ($this->validOBData($data) && !$this->isEmptyOBData($data)) { | ||||
|                 $this->updateOBGroup($account, $data); | ||||
|             } | ||||
|  | ||||
|             if (!$this->validOBData($data)) { | ||||
|             if (!$this->validOBData($data) && $this->isEmptyOBData($data)) { | ||||
|                 $this->deleteOBGroup($account); | ||||
|             } | ||||
|         } | ||||
|   | ||||
| @@ -91,7 +91,7 @@ trait ChartGeneration | ||||
|             while ($currentStart <= $end) { | ||||
|                 $format   = $currentStart->format('Y-m-d'); | ||||
|                 $label    = trim($currentStart->formatLocalized((string)trans('config.month_and_day', [], $locale))); | ||||
|                 $balance  = isset($range[$format]) ? round($range[$format], 12) : $previous; | ||||
|                 $balance  = $range[$format] ?? $previous; | ||||
|                 $previous = $balance; | ||||
|                 $currentStart->addDay(); | ||||
|                 $currentSet['entries'][$label] = $balance; | ||||
|   | ||||
| @@ -180,6 +180,7 @@ | ||||
|       "@php artisan firefly-iii:delete-empty-journals", | ||||
|       "@php artisan firefly-iii:delete-empty-groups", | ||||
|       "@php artisan firefly-iii:fix-account-types", | ||||
|       "@php artisan firefly-iii:fix-account-order", | ||||
|       "@php artisan firefly-iii:rename-meta-fields", | ||||
|       "@php artisan firefly-iii:fix-ob-currencies", | ||||
|       "@php artisan firefly-iii:fix-long-descriptions", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user