| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * TransactionFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +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-16 16:43:25 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +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-16 16:43:25 +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-16 16:43:25 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-03-08 05:47:51 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2019-03-08 05:47:51 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:52:49 +01:00
										 |  |  | use Illuminate\Database\QueryException; | 
					
						
							| 
									
										
										
										
											2018-05-10 09:10:16 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class TransactionFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private Account              $account; | 
					
						
							|  |  |  |     private TransactionCurrency  $currency; | 
					
						
							| 
									
										
										
										
											2020-11-12 20:26:41 +01:00
										 |  |  |     private ?TransactionCurrency $foreignCurrency; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private TransactionJournal   $journal; | 
					
						
							|  |  |  |     private bool                 $reconciled; | 
					
						
							|  |  |  |     private User                 $user; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |         $this->reconciled = false; | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create transaction with negative amount (for source accounts). | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |      * @param string      $amount | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |      * @param string|null $foreignAmount | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * @return Transaction | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |     public function createNegative(string $amount, ?string $foreignAmount): Transaction | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-11 06:41:02 +01:00
										 |  |  |         if ('' === $foreignAmount) { | 
					
						
							|  |  |  |             $foreignAmount = null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |         if (null !== $foreignAmount) { | 
					
						
							|  |  |  |             $foreignAmount = app('steam')->negative($foreignAmount); | 
					
						
							| 
									
										
										
										
											2019-03-08 05:47:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |         return $this->create(app('steam')->negative($amount), $foreignAmount); | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string      $amount | 
					
						
							|  |  |  |      * @param string|null $foreignAmount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Transaction | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function create(string $amount, ?string $foreignAmount): Transaction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result = null; | 
					
						
							|  |  |  |         if ('' === $foreignAmount) { | 
					
						
							|  |  |  |             $foreignAmount = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'reconciled'              => $this->reconciled, | 
					
						
							|  |  |  |             'account_id'              => $this->account->id, | 
					
						
							|  |  |  |             'transaction_journal_id'  => $this->journal->id, | 
					
						
							|  |  |  |             'description'             => null, | 
					
						
							|  |  |  |             'transaction_currency_id' => $this->currency->id, | 
					
						
							|  |  |  |             'amount'                  => $amount, | 
					
						
							|  |  |  |             'foreign_amount'          => null, | 
					
						
							|  |  |  |             'foreign_currency_id'     => null, | 
					
						
							|  |  |  |             'identifier'              => 0, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $result = Transaction::create($data); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } catch (QueryException $e) { | 
					
						
							|  |  |  |             Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data); | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |             Log::error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             throw new FireflyException('Query exception when creating transaction.', 0, $e); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (null === $result) { | 
					
						
							|  |  |  |             throw new FireflyException('Transaction is NULL.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (null !== $result) { | 
					
						
							|  |  |  |             Log::debug( | 
					
						
							|  |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'Created transaction #%d (%s %s, account %s), part of journal #%d', | 
					
						
							|  |  |  |                     $result->id, | 
					
						
							|  |  |  |                     $this->currency->code, | 
					
						
							|  |  |  |                     $amount, | 
					
						
							|  |  |  |                     $this->account->name, | 
					
						
							|  |  |  |                     $this->journal->id | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // do foreign currency thing: add foreign currency info to $one and $two if necessary.
 | 
					
						
							|  |  |  |             if (null !== $this->foreignCurrency && null !== $foreignAmount && $this->foreignCurrency->id !== $this->currency->id && '' !== $foreignAmount) { | 
					
						
							|  |  |  |                 $result->foreign_currency_id = $this->foreignCurrency->id; | 
					
						
							|  |  |  |                 $result->foreign_amount      = $foreignAmount; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $result->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |      * Create transaction with positive amount (for destination accounts). | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |      * @param string      $amount | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |      * @param string|null $foreignAmount | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * @return Transaction | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |     public function createPositive(string $amount, ?string $foreignAmount): Transaction | 
					
						
							| 
									
										
										
										
											2018-02-16 16:43:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-11 06:41:02 +01:00
										 |  |  |         if ('' === $foreignAmount) { | 
					
						
							|  |  |  |             $foreignAmount = null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |         if (null !== $foreignAmount) { | 
					
						
							|  |  |  |             $foreignAmount = app('steam')->positive($foreignAmount); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |         return $this->create(app('steam')->positive($amount), $foreignAmount); | 
					
						
							| 
									
										
										
										
											2019-03-08 05:47:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setAccount(Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->account = $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionCurrency $currency | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setCurrency(TransactionCurrency $currency): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->currency = $currency; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @param TransactionCurrency|null $foreignCurrency |null | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->foreignCurrency = $foreignCurrency; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:52:49 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2019-03-18 16:52:49 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function setJournal(TransactionJournal $journal): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->journal = $journal; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param bool $reconciled | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setReconciled(bool $reconciled): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->reconciled = $reconciled; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:52:49 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2019-03-18 16:52:49 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |