| 
									
										
										
										
											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
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankEventTransformer.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; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBankEvent; | 
					
						
							| 
									
										
										
										
											2018-07-22 16:35:46 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PiggyBankEventTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-15 22:03:42 +01:00
										 |  |  | class PiggyBankEventTransformer extends AbstractTransformer | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  |     private PiggyBankRepositoryInterface $piggyRepos; | 
					
						
							|  |  |  |     private AccountRepositoryInterface   $repository; | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * PiggyBankEventTransformer constructor. | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-15 22:03:42 +01:00
										 |  |  |     public function __construct() | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  |         $this->repository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->piggyRepos = app(PiggyBankRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Convert piggy bank event. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											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(PiggyBankEvent $event): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // get account linked to piggy bank
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $account   = $event->piggyBank->account; | 
					
						
							| 
									
										
										
										
											2018-07-22 16:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         // set up repositories.
 | 
					
						
							|  |  |  |         $this->repository->setUser($account->user); | 
					
						
							|  |  |  |         $this->piggyRepos->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get associated currency or fall back to the default:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $currency  = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup); | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get associated journal and transaction, if any:
 | 
					
						
							| 
									
										
										
										
											2020-07-12 17:54:51 +02:00
										 |  |  |         $journalId = $event->transaction_journal_id; | 
					
						
							| 
									
										
										
										
											2019-09-22 22:10:39 +02:00
										 |  |  |         $groupId   = null; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (0 !== (int) $journalId) { | 
					
						
							|  |  |  |             $groupId   = (int) $event->transactionJournal->transaction_group_id; | 
					
						
							|  |  |  |             $journalId = (int) $journalId; | 
					
						
							| 
									
										
										
										
											2019-09-22 22:10:39 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-06 12:45:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'id'                      => (string) $event->id, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'created_at'              => $event->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'              => $event->updated_at->toAtomString(), | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |             'amount'                  => app('steam')->bcround($event->amount, $currency->decimal_places), | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +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, | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'transaction_journal_id'  => null !== $journalId ? (string) $journalId : null, | 
					
						
							|  |  |  |             'transaction_group_id'    => null !== $groupId ? (string) $groupId : null, | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |             'links'                   => [ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                     'uri' => '/piggy_bank_events/'.$event->id, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |