| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankTransformer.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:18 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +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. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01: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/>. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  | use FireflyIII\Models\ObjectGroup; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PiggyBankTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  | class PiggyBankTransformer extends AbstractTransformer | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private AccountRepositoryInterface   $accountRepos; | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  |     private PiggyBankRepositoryInterface $piggyRepos; | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * PiggyBankTransformer constructor. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |     public function __construct() | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  |         $this->accountRepos = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->piggyRepos   = app(PiggyBankRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Transform the piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     public function transform(PiggyBank $piggyBank): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $user             = $piggyBank->accounts()->first()->user; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // set up repositories
 | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         $this->accountRepos->setUser($user); | 
					
						
							|  |  |  |         $this->piggyRepos->setUser($user); | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // note
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $notes            = $this->piggyRepos->getNoteText($piggyBank); | 
					
						
							|  |  |  |         $notes            = '' === $notes ? null : $notes; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  |         $objectGroupId    = null; | 
					
						
							| 
									
										
										
										
											2020-06-20 16:28:23 +02:00
										 |  |  |         $objectGroupOrder = null; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  |         $objectGroupTitle = null; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|ObjectGroup $objectGroup */ | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $objectGroup      = $piggyBank->objectGroups->first(); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  |         if (null !== $objectGroup) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             $objectGroupId    = $objectGroup->id; | 
					
						
							|  |  |  |             $objectGroupOrder = $objectGroup->order; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  |             $objectGroupTitle = $objectGroup->title; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |         // get currently saved amount:
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $currency         = $piggyBank->transactionCurrency; | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  |         $currentAmount    = $this->piggyRepos->getCurrentAmount($piggyBank); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |         // Amounts, depending on 0.0 state of target amount
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $percentage       = null; | 
					
						
							|  |  |  |         $targetAmount     = $piggyBank->target_amount; | 
					
						
							|  |  |  |         $leftToSave       = null; | 
					
						
							|  |  |  |         $savePerMonth     = null; | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |         if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
 | 
					
						
							| 
									
										
										
										
											2024-11-30 16:02:30 +01:00
										 |  |  |             $leftToSave   = bcsub($piggyBank->target_amount, $currentAmount); | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             $percentage   = (int) bcmul(bcdiv($currentAmount, $targetAmount), '100'); | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |             $targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places); | 
					
						
							|  |  |  |             $leftToSave   = app('steam')->bcround($leftToSave, $currency->decimal_places); | 
					
						
							|  |  |  |             $savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places); | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $startDate        = $piggyBank->start_date?->format('Y-m-d'); | 
					
						
							|  |  |  |         $targetDate       = $piggyBank->target_date?->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             'id'                      => (string) $piggyBank->id, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'created_at'              => $piggyBank->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'              => $piggyBank->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'name'                    => $piggyBank->name, | 
					
						
							| 
									
										
										
										
											2025-02-01 18:59:55 +01:00
										 |  |  |             'accounts'                => $this->renderAccounts($piggyBank), | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             'currency_id'             => (string) $currency->id, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'currency_code'           => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'         => $currency->symbol, | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |             'currency_decimal_places' => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |             'target_amount'           => $targetAmount, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'percentage'              => $percentage, | 
					
						
							|  |  |  |             'current_amount'          => $currentAmount, | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |             'left_to_save'            => $leftToSave, | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |             'save_per_month'          => $savePerMonth, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'start_date'              => $startDate, | 
					
						
							|  |  |  |             'target_date'             => $targetDate, | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             'order'                   => $piggyBank->order, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'active'                  => true, | 
					
						
							|  |  |  |             'notes'                   => $notes, | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             'object_group_id'         => null !== $objectGroupId ? (string) $objectGroupId : null, | 
					
						
							| 
									
										
										
										
											2020-06-07 11:31:01 +02:00
										 |  |  |             'object_group_order'      => $objectGroupOrder, | 
					
						
							|  |  |  |             'object_group_title'      => $objectGroupTitle, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'links'                   => [ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |                     'uri' => '/piggy_banks/'.$piggyBank->id, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function renderAccounts(PiggyBank $piggyBank): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = []; | 
					
						
							| 
									
										
										
										
											2024-12-04 06:38:47 +01:00
										 |  |  |         foreach ($piggyBank->accounts()->get() as $account) { | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             $return[] = [ | 
					
						
							| 
									
										
										
										
											2025-02-01 20:52:41 +01:00
										 |  |  |                 'id'                    => (string) $account->id, | 
					
						
							| 
									
										
										
										
											2025-02-01 19:06:02 +01:00
										 |  |  |                 'name'                  => $account->name, | 
					
						
							|  |  |  |                 'current_amount'        => (string) $account->pivot->current_amount, | 
					
						
							|  |  |  |                 'native_current_amount' => (string) $account->pivot->native_current_amount, | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |                 // TODO add balance, add left to save.
 | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |