| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * TransactionGroupTwig.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +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. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +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. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +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/>. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 07:24:04 +02:00
										 |  |  | namespace FireflyIII\Support\Twig; | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2019-11-19 17:39:55 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  | use Twig\Extension\AbstractExtension; | 
					
						
							|  |  |  | use Twig\TwigFunction; | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionGroupTwig | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  | class TransactionGroupTwig extends AbstractExtension | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     public function getFunctions(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |             $this->journalArrayAmount(), | 
					
						
							|  |  |  |             $this->journalObjectAmount(), | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |             $this->journalHasMeta(), | 
					
						
							|  |  |  |             $this->journalGetMetaDate(), | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |             $this->journalGetMetaField(), | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |      * Shows the amount for a single journal array. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  |     public function journalArrayAmount(): TwigFunction | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  |         return new TwigFunction( | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |             'journalArrayAmount', | 
					
						
							| 
									
										
										
										
											2020-08-01 18:51:12 +02:00
										 |  |  |             function (array $array): string { | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |                 // if is not a withdrawal, amount positive.
 | 
					
						
							| 
									
										
										
										
											2020-08-01 18:51:12 +02:00
										 |  |  |                 $result = $this->normalJournalArrayAmount($array); | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |                 // now append foreign amount, if any.
 | 
					
						
							| 
									
										
										
										
											2020-08-01 18:51:12 +02:00
										 |  |  |                 if (null !== $array['foreign_amount']) { | 
					
						
							|  |  |  |                     $foreign = $this->foreignJournalArrayAmount($array); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |                     $result  = sprintf('%s (%s)', $result, $foreign); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             ['is_safe' => ['html']] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Generate normal amount for transaction from a transaction group. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function normalJournalArrayAmount(array $array): string | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $type       = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; | 
					
						
							|  |  |  |         $amount     = $array['amount'] ?? '0'; | 
					
						
							|  |  |  |         $colored    = true; | 
					
						
							|  |  |  |         $sourceType = $array['source_account_type'] ?? 'invalid'; | 
					
						
							|  |  |  |         $amount     = $this->signAmount($amount, $type, $sourceType); | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $colored = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $result     = app('amount')->formatFlat($array['currency_symbol'], (int) $array['currency_decimal_places'], $amount, $colored); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $result = sprintf('<span class="text-info money-transfer">%s</span>', $result); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function signAmount(string $amount, string $transactionType, string $sourceType): string | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // withdrawals stay negative
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::WITHDRAWAL !== $transactionType) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-27 10:53:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // opening balance and it comes from initial balance? its expense.
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::OPENING_BALANCE === $transactionType && AccountType::INITIAL_BALANCE !== $sourceType) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // reconciliation and it comes from reconciliation?
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::RECONCILIATION === $transactionType && AccountType::RECONCILIATION !== $sourceType) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $amount; | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * Generate foreign amount for transaction from a transaction group. | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private function foreignJournalArrayAmount(array $array): string | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $type       = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; | 
					
						
							|  |  |  |         $amount     = $array['foreign_amount'] ?? '0'; | 
					
						
							|  |  |  |         $colored    = true; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 18:53:42 +02:00
										 |  |  |         $sourceType = $array['source_account_type'] ?? 'invalid'; | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |         $amount     = $this->signAmount($amount, $type, $sourceType); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |             $colored = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $result     = app('amount')->formatFlat($array['foreign_currency_symbol'], (int) $array['foreign_currency_decimal_places'], $amount, $colored); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |             $result = sprintf('<span class="text-info money-transfer">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Shows the amount for a single journal object. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function journalObjectAmount(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'journalObjectAmount', | 
					
						
							|  |  |  |             function (TransactionJournal $journal): string { | 
					
						
							|  |  |  |                 $result = $this->normalJournalObjectAmount($journal); | 
					
						
							|  |  |  |                 // now append foreign amount, if any.
 | 
					
						
							|  |  |  |                 if ($this->journalObjectHasForeign($journal)) { | 
					
						
							|  |  |  |                     $foreign = $this->foreignJournalObjectAmount($journal); | 
					
						
							|  |  |  |                     $result  = sprintf('%s (%s)', $result, $foreign); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             ['is_safe' => ['html']] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generate normal amount for transaction from a transaction group. | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function normalJournalObjectAmount(TransactionJournal $journal): string | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $type       = $journal->transactionType->type; | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |         $first      = $journal->transactions()->where('amount', '<', 0)->first(); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $currency   = $journal->transactionCurrency; | 
					
						
							|  |  |  |         $amount     = $first->amount ?? '0'; | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |         $colored    = true; | 
					
						
							| 
									
										
										
										
											2020-08-01 14:08:03 +02:00
										 |  |  |         $sourceType = $first->account()->first()->accountType()->first()->type; | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $amount     = $this->signAmount($amount, $type, $sourceType); | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |             $colored = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $result     = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |             $result = sprintf('<span class="text-info money-transfer">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function journalObjectHasForeign(TransactionJournal $journal): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Transaction $first */ | 
					
						
							|  |  |  |         $first = $journal->transactions()->where('amount', '<', 0)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 20:49:27 +01:00
										 |  |  |         return '' !== $first->foreign_amount; | 
					
						
							| 
									
										
										
										
											2019-06-23 11:13:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-27 10:53:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Generate foreign amount for journal from a transaction group. | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function foreignJournalObjectAmount(TransactionJournal $journal): string | 
					
						
							| 
									
										
										
										
											2020-08-05 18:51:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $type       = $journal->transactionType->type; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var Transaction $first */ | 
					
						
							|  |  |  |         $first      = $journal->transactions()->where('amount', '<', 0)->first(); | 
					
						
							|  |  |  |         $currency   = $first->foreignCurrency; | 
					
						
							|  |  |  |         $amount     = '' === $first->foreign_amount ? '0' : $first->foreign_amount; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $colored    = true; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $sourceType = $first->account()->first()->accountType()->first()->type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $amount     = $this->signAmount($amount, $type, $sourceType); | 
					
						
							| 
									
										
										
										
											2020-07-27 10:53:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             $colored = false; | 
					
						
							| 
									
										
										
										
											2020-07-27 10:53:36 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $result     = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (TransactionType::TRANSFER === $type) { | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |             $result = sprintf('<span class="text-info money-transfer">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2020-07-27 10:53:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function journalHasMeta(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'journalHasMeta', | 
					
						
							|  |  |  |             static function (int $journalId, string $metaField) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $count = \DB::table('journal_meta') | 
					
						
							|  |  |  |                     ->where('name', $metaField) | 
					
						
							|  |  |  |                     ->where('transaction_journal_id', $journalId) | 
					
						
							|  |  |  |                     ->whereNull('deleted_at') | 
					
						
							|  |  |  |                     ->count() | 
					
						
							|  |  |  |                 ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return 1 === $count; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function journalGetMetaDate(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'journalGetMetaDate', | 
					
						
							|  |  |  |             static function (int $journalId, string $metaField) { | 
					
						
							|  |  |  |                 /** @var null|TransactionJournalMeta $entry */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $entry = \DB::table('journal_meta') | 
					
						
							|  |  |  |                     ->where('name', $metaField) | 
					
						
							|  |  |  |                     ->where('transaction_journal_id', $journalId) | 
					
						
							|  |  |  |                     ->whereNull('deleted_at') | 
					
						
							|  |  |  |                     ->first() | 
					
						
							|  |  |  |                 ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 if (null === $entry) { | 
					
						
							|  |  |  |                     return today(config('app.timezone')); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return new Carbon(json_decode($entry->data, false)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function journalGetMetaField(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'journalGetMetaField', | 
					
						
							|  |  |  |             static function (int $journalId, string $metaField) { | 
					
						
							|  |  |  |                 /** @var null|TransactionJournalMeta $entry */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $entry = \DB::table('journal_meta') | 
					
						
							|  |  |  |                     ->where('name', $metaField) | 
					
						
							|  |  |  |                     ->where('transaction_journal_id', $journalId) | 
					
						
							|  |  |  |                     ->whereNull('deleted_at') | 
					
						
							|  |  |  |                     ->first() | 
					
						
							|  |  |  |                 ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 if (null === $entry) { | 
					
						
							|  |  |  |                     return ''; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return json_decode($entry->data, true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |