| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2025-08-05 13:40:46 +02:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							|  |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-27 05:57:58 +02:00
										 |  |  |     private readonly TransactionCurrency $primaryCurrency; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |     private bool                         $convertToPrimary; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  |         $this->primaryCurrency  = Amount::getPrimaryCurrency(); | 
					
						
							|  |  |  |         $this->convertToPrimary = Amount::convertToPrimary(); | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  |         $currency      = $event->meta['currency'] ?? $this->primaryCurrency; | 
					
						
							|  |  |  |         $amount        = Steam::bcround($event->amount, $currency->decimal_places); | 
					
						
							|  |  |  |         $primaryAmount = null; | 
					
						
							|  |  |  |         if ($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) { | 
					
						
							|  |  |  |             $primaryAmount = $amount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) { | 
					
						
							|  |  |  |             $primaryAmount = Steam::bcround($event->native_amount, $this->primaryCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											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 [ | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'id'                              => (string)$event->id, | 
					
						
							|  |  |  |             'created_at'                      => $event->created_at?->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'                      => $event->updated_at?->toAtomString(), | 
					
						
							|  |  |  |             'amount'                          => $amount, | 
					
						
							|  |  |  |             'pc_amount'                       => $primaryAmount, | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // currencies according to 6.3.0
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'has_currency_setting'            => true, | 
					
						
							|  |  |  |             'currency_id'                     => (string)$currency->id, | 
					
						
							|  |  |  |             'currency_name'                   => $currency->name, | 
					
						
							|  |  |  |             'currency_code'                   => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'                 => $currency->symbol, | 
					
						
							|  |  |  |             'currency_decimal_places'         => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             'primary_currency_id'             => (string)$this->primaryCurrency->id, | 
					
						
							|  |  |  |             'primary_currency_name'           => $this->primaryCurrency->name, | 
					
						
							|  |  |  |             'primary_currency_code'           => $this->primaryCurrency->code, | 
					
						
							|  |  |  |             'primary_currency_symbol'         => $this->primaryCurrency->symbol, | 
					
						
							|  |  |  |             'primary_currency_decimal_places' => $this->primaryCurrency->decimal_places, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'transaction_journal_id'          => null !== $event->transaction_journal_id ? (string)$event->transaction_journal_id : null, | 
					
						
							|  |  |  |             'transaction_group_id'            => $event->meta['transaction_group_id'], | 
					
						
							|  |  |  |             'links'                           => [ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2025-08-05 19:49:13 +02:00
										 |  |  |                     'uri' => sprintf('/piggy-banks/%d/events/%s', $event->piggy_bank_id, $event->id), | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |