| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ReconcileController.php | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * Copyright (c) 2019 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02: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-07-12 21:32:58 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02: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-07-12 21:32:58 +02: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-07-12 21:32:58 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Json; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  | use FireflyIII\Helpers\Collector\GroupCollectorInterface; | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-08-09 17:34:11 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\UserNavigation; | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  | use Log; | 
					
						
							|  |  |  | use Throwable; | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class ReconcileController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ReconcileController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-09 17:34:11 +02:00
										 |  |  |     use UserNavigation; | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |     /** @var AccountRepositoryInterface The account repository */ | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |     private $accountRepos; | 
					
						
							|  |  |  |     /** @var CurrencyRepositoryInterface The currency repository */ | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |     private $currencyRepos; | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |     /** @var JournalRepositoryInterface Journals and transactions overview */ | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |     private $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |      * ReconcileController constructor. | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // translations:
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 app('view')->share('mainTitleIcon', 'fa-credit-card'); | 
					
						
							| 
									
										
										
										
											2018-07-15 09:38:49 +02:00
										 |  |  |                 app('view')->share('title', (string)trans('firefly.accounts')); | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |                 $this->repository    = app(JournalRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->accountRepos  = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->currencyRepos = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |      * Overview of reconciliation. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |      * @param Request $request | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return JsonResponse | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         $startBalance    = $request->get('startBalance'); | 
					
						
							|  |  |  |         $endBalance      = $request->get('endBalance'); | 
					
						
							|  |  |  |         $accountCurrency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); | 
					
						
							|  |  |  |         $amount          = '0'; | 
					
						
							|  |  |  |         $clearedAmount   = '0'; | 
					
						
							|  |  |  |         $route           = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]); | 
					
						
							|  |  |  |         $selectedIds     = $request->get('journals') ?? []; | 
					
						
							|  |  |  |         $clearedJournals = []; | 
					
						
							|  |  |  |         $clearedIds      = $request->get('cleared') ?? []; | 
					
						
							|  |  |  |         $journals        = []; | 
					
						
							|  |  |  |         /* Collect all submitted journals */ | 
					
						
							|  |  |  |         if (count($selectedIds) > 0) { | 
					
						
							|  |  |  |             /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |             $collector = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  |             $collector->setJournalIds($selectedIds); | 
					
						
							|  |  |  |             $journals = $collector->getExtractedJournals(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-31 13:35:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         /* Collect all journals already reconciled */ | 
					
						
							|  |  |  |         if (count($clearedIds) > 0) { | 
					
						
							|  |  |  |             /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |             $collector = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  |             $collector->setJournalIds($clearedIds); | 
					
						
							|  |  |  |             $clearedJournals = $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 13:53:00 +01:00
										 |  |  |         Log::debug('Start transaction loop'); | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         /** @var array $journal */ | 
					
						
							|  |  |  |         foreach ($journals as $journal) { | 
					
						
							|  |  |  |             $amount = $this->processJournal($account, $accountCurrency, $journal, $amount); | 
					
						
							| 
									
										
										
										
											2018-10-27 05:10:49 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         Log::debug(sprintf('Final amount is %s', $amount)); | 
					
						
							| 
									
										
										
										
											2018-10-28 13:53:00 +01:00
										 |  |  |         Log::debug('End transaction loop'); | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var array $journal */ | 
					
						
							|  |  |  |         foreach ($clearedJournals as $journal) { | 
					
						
							|  |  |  |             if ($journal['date'] <= $end) { | 
					
						
							|  |  |  |                 $clearedAmount = $this->processJournal($account, $accountCurrency, $journal, $clearedAmount); | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         $difference   = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount); | 
					
						
							|  |  |  |         $diffCompare  = bccomp($difference, '0'); | 
					
						
							|  |  |  |         $countCleared = count($clearedJournals); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $reconSum = bcadd(bcadd($startBalance, $amount), $clearedAmount); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $view = view( | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |                 'accounts.reconcile.overview', compact( | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |                                                  'account', 'start', 'diffCompare', 'difference', 'end', 'clearedAmount', | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |                                                  'startBalance', 'endBalance', 'amount', | 
					
						
							| 
									
										
										
										
											2019-06-23 05:53:01 +02:00
										 |  |  |                                                  'route', 'countCleared', 'reconSum', 'selectedIds' | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |                                              ) | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |             )->render(); | 
					
						
							| 
									
										
										
										
											2018-08-31 21:12:53 +02:00
										 |  |  |             // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							|  |  |  |             Log::debug(sprintf('View error: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-06-29 19:47:31 +02:00
										 |  |  |             $view = sprintf('Could not render accounts.reconcile.overview: %s', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-31 21:12:53 +02:00
										 |  |  |         // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $return = [ | 
					
						
							|  |  |  |             'post_uri' => $route, | 
					
						
							|  |  |  |             'html'     => $view, | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($return); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |      * Returns a list of transactions in a modal. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transactions(Account $account, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $startDate = clone $start; | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         $startDate->subDay(); | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         $currency     = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |         $startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places); | 
					
						
							|  |  |  |         $endBalance   = round(app('steam')->balance($account, $end), $currency->decimal_places); | 
					
						
							|  |  |  |         // get the transactions
 | 
					
						
							|  |  |  |         $selectionStart = clone $start; | 
					
						
							|  |  |  |         $selectionStart->subDays(3); | 
					
						
							|  |  |  |         $selectionEnd = clone $end; | 
					
						
							|  |  |  |         $selectionEnd->addDays(3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // grab transactions:
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  |         $collector->setAccounts(new Collection([$account])) | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |                   ->setRange($selectionStart, $selectionEnd) | 
					
						
							|  |  |  |                   ->withBudgetInformation()->withCategoryInformation()->withAccountInformation(); | 
					
						
							|  |  |  |         $array    = $collector->getExtractedJournals(); | 
					
						
							|  |  |  |         $journals = []; | 
					
						
							|  |  |  |         // "fix" amounts to make it easier on the reconciliation overview:
 | 
					
						
							|  |  |  |         /** @var array $journal */ | 
					
						
							|  |  |  |         foreach ($array as $journal) { | 
					
						
							|  |  |  |             $inverse = false; | 
					
						
							| 
									
										
										
										
											2019-06-29 19:47:31 +02:00
										 |  |  |             // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |             if (TransactionType::DEPOSIT === $journal['transaction_type_type']) { | 
					
						
							|  |  |  |                 $inverse = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // transfer to this account? then positive amount:
 | 
					
						
							| 
									
										
										
										
											2019-08-02 16:31:36 +02:00
										 |  |  |             if (TransactionType::TRANSFER === $journal['transaction_type_type'] && $account->id === $journal['destination_account_id']) { | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |                 $inverse = true; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-02 16:31:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-03 06:27:56 +02:00
										 |  |  |             // opening balance into account? then positive amount:
 | 
					
						
							|  |  |  |             if (TransactionType::OPENING_BALANCE === $journal['transaction_type_type'] | 
					
						
							|  |  |  |                 && $account->id === $journal['destination_account_id']) { | 
					
						
							| 
									
										
										
										
											2019-08-02 16:31:36 +02:00
										 |  |  |                 $inverse = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |             if (true === $inverse) { | 
					
						
							|  |  |  |                 $journal['amount'] = app('steam')->positive($journal['amount']); | 
					
						
							|  |  |  |                 if (null !== $journal['foreign_amount']) { | 
					
						
							|  |  |  |                     $journal['foreign_amount'] = app('steam')->positive($journal['foreign_amount']); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-29 19:47:31 +02:00
										 |  |  |             // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $journals[] = $journal; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |             $html = view('accounts.reconcile.transactions', | 
					
						
							|  |  |  |                          compact('account', 'journals', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd'))->render(); | 
					
						
							| 
									
										
										
										
											2018-08-31 21:12:53 +02:00
										 |  |  |             // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |             $html = sprintf('Could not render accounts.reconcile.transactions: %s', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:29:40 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-27 05:10:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-31 21:12:53 +02:00
										 |  |  |         // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2018-07-12 21:32:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param TransactionCurrency $currency | 
					
						
							|  |  |  |      * @param array $journal | 
					
						
							|  |  |  |      * @param string $amount | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function processJournal(Account $account, TransactionCurrency $currency, array $journal, string $amount): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $toAdd = '0'; | 
					
						
							|  |  |  |         Log::debug(sprintf('User submitted %s #%d: "%s"', $journal['transaction_type_type'], $journal['transaction_journal_id'], $journal['description'])); | 
					
						
							| 
									
										
										
										
											2019-06-29 19:47:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // not much magic below we need to cover using tests.
 | 
					
						
							|  |  |  |         // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         if ($account->id === $journal['source_account_id']) { | 
					
						
							|  |  |  |             if ($currency->id === $journal['currency_id']) { | 
					
						
							|  |  |  |                 $toAdd = $journal['amount']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $journal['foreign_currency_id'] && $journal['foreign_currency_id'] === $currency->id) { | 
					
						
							|  |  |  |                 $toAdd = $journal['foreign_amount']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->id === $journal['destination_account_id']) { | 
					
						
							|  |  |  |             if ($currency->id === $journal['currency_id']) { | 
					
						
							|  |  |  |                 $toAdd = bcmul($journal['amount'], '-1'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $journal['foreign_currency_id'] && $journal['foreign_currency_id'] === $currency->id) { | 
					
						
							|  |  |  |                 $toAdd = bcmul($journal['foreign_amount'], '-1'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-29 19:47:31 +02:00
										 |  |  |         // @codeCoverageIgnoreEnd
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 13:09:11 +02:00
										 |  |  |         Log::debug(sprintf('Going to add %s to %s', $toAdd, $amount)); | 
					
						
							|  |  |  |         $amount = bcadd($amount, $toAdd); | 
					
						
							|  |  |  |         Log::debug(sprintf('Result is %s', $amount)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |