| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 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\Currency\CurrencyRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | use JsonException; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     private CurrencyRepositoryInterface  $currencyRepos; | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:15:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         $this->accountRepos  = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->currencyRepos = app(CurrencyRepositoryInterface::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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param PiggyBank $piggyBank | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      * @throws JsonException | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function transform(PiggyBank $piggyBank): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |         $account = $piggyBank->account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // set up repositories
 | 
					
						
							|  |  |  |         $this->accountRepos->setUser($account->user); | 
					
						
							|  |  |  |         $this->currencyRepos->setUser($account->user); | 
					
						
							|  |  |  |         $this->piggyRepos->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get currency from account, or use default.
 | 
					
						
							| 
									
										
										
										
											2020-01-08 05:48:45 +01:00
										 |  |  |         $currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user); | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // note
 | 
					
						
							|  |  |  |         $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; | 
					
						
							|  |  |  |         /** @var ObjectGroup $objectGroup */ | 
					
						
							|  |  |  |         $objectGroup = $piggyBank->objectGroups->first(); | 
					
						
							|  |  |  |         if (null !== $objectGroup) { | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             $objectGroupId    = (int)$objectGroup->id; | 
					
						
							|  |  |  |             $objectGroupOrder = (int)$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:
 | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |         $currentAmount = app('steam')->bcround($this->piggyRepos->getCurrentAmount($piggyBank), $currency->decimal_places); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2022-12-29 11:29:00 +01:00
										 |  |  |         $percentage   = null; | 
					
						
							|  |  |  |         $targetAmount = $piggyBank->targetamount; | 
					
						
							|  |  |  |         $leftToSave   = null; | 
					
						
							|  |  |  |         $savePerMonth = null; | 
					
						
							|  |  |  |         if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
 | 
					
						
							|  |  |  |             $leftToSave   = bcsub($piggyBank->targetamount, $currentAmount); | 
					
						
							|  |  |  |             $percentage   = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100'); | 
					
						
							|  |  |  |             $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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-17 07:02:08 +02:00
										 |  |  |         $startDate  = $piggyBank->startdate?->format('Y-m-d'); | 
					
						
							|  |  |  |         $targetDate = $piggyBank->targetdate?->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +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(), | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'account_id'              => (string)$piggyBank->account_id, | 
					
						
							| 
									
										
										
										
											2019-01-26 12:11:23 +01:00
										 |  |  |             'account_name'            => $piggyBank->account->name, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'name'                    => $piggyBank->name, | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'currency_id'             => (string)$currency->id, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'currency_code'           => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'         => $currency->symbol, | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'currency_decimal_places' => (int)$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, | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'order'                   => (int)$piggyBank->order, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'active'                  => true, | 
					
						
							|  |  |  |             'notes'                   => $notes, | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'object_group_id'         => $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', | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     'uri' => '/piggy_banks/' . $piggyBank->id, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |