| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |     private readonly TransactionCurrency                   $primaryCurrency; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +02:00
										 |  |  |         $this->primaryCurrency = Amount::getPrimaryCurrency(); | 
					
						
							| 
									
										
										
										
											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-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     public function transform(PiggyBank $piggyBank): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |         // Amounts, depending on 0.0 state of target amount
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |         $percentage = null; | 
					
						
							| 
									
										
										
										
											2025-09-07 17:42:16 +02:00
										 |  |  |         if (null !== $piggyBank->meta['target_amount'] && 0 !== bccomp((string) $piggyBank->meta['current_amount'], '0')) { // target amount is not 0.00
 | 
					
						
							|  |  |  |             $percentage = (int)bcmul(bcdiv((string) $piggyBank->meta['current_amount'], $piggyBank->meta['target_amount']), '100'); | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-06 10:46:23 +02:00
										 |  |  |         $startDate  = $piggyBank->start_date?->toAtomString(); | 
					
						
							|  |  |  |         $targetDate = $piggyBank->target_date?->toAtomString(); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'id'                              => (string)$piggyBank->id, | 
					
						
							|  |  |  |             'created_at'                      => $piggyBank->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'                      => $piggyBank->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'name'                            => $piggyBank->name, | 
					
						
							|  |  |  |             'percentage'                      => $percentage, | 
					
						
							|  |  |  |             'start_date'                      => $startDate, | 
					
						
							|  |  |  |             'target_date'                     => $targetDate, | 
					
						
							|  |  |  |             'order'                           => $piggyBank->order, | 
					
						
							|  |  |  |             'active'                          => true, | 
					
						
							|  |  |  |             'notes'                           => $piggyBank->meta['notes'], | 
					
						
							|  |  |  |             'object_group_id'                 => $piggyBank->meta['object_group_id'], | 
					
						
							|  |  |  |             'object_group_order'              => $piggyBank->meta['object_group_order'], | 
					
						
							|  |  |  |             'object_group_title'              => $piggyBank->meta['object_group_title'], | 
					
						
							|  |  |  |             'accounts'                        => $piggyBank->meta['accounts'], | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // currency settings, 6.3.0.
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'object_has_currency_setting'     => true, | 
					
						
							|  |  |  |             'currency_id'                     => (string)$piggyBank->meta['currency']->id, | 
					
						
							|  |  |  |             'currency_name'                   => $piggyBank->meta['currency']->name, | 
					
						
							|  |  |  |             'currency_code'                   => $piggyBank->meta['currency']->code, | 
					
						
							|  |  |  |             'currency_symbol'                 => $piggyBank->meta['currency']->symbol, | 
					
						
							|  |  |  |             'currency_decimal_places'         => $piggyBank->meta['currency']->decimal_places, | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +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' => (int)$this->primaryCurrency->decimal_places, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'target_amount'                   => $piggyBank->meta['target_amount'], | 
					
						
							|  |  |  |             'pc_target_amount'                => $piggyBank->meta['pc_target_amount'], | 
					
						
							|  |  |  |             'current_amount'                  => $piggyBank->meta['current_amount'], | 
					
						
							|  |  |  |             'pc_current_amount'               => $piggyBank->meta['pc_current_amount'], | 
					
						
							|  |  |  |             'left_to_save'                    => $piggyBank->meta['left_to_save'], | 
					
						
							|  |  |  |             'pc_left_to_save'                 => $piggyBank->meta['pc_left_to_save'], | 
					
						
							|  |  |  |             'save_per_month'                  => $piggyBank->meta['save_per_month'], | 
					
						
							|  |  |  |             'pc_save_per_month'               => $piggyBank->meta['pc_save_per_month'], | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-06 20:39:55 +02:00
										 |  |  |             'links'                           => [ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2025-08-06 09:15:54 +02:00
										 |  |  |                     'uri' => sprintf('/piggy-banks/%d', $piggyBank->id), | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |