| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Steam.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2017-02-19 07:34:39 +01:00
										 |  |  | use Crypt; | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  | use DB; | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2017-07-14 06:41:47 +02:00
										 |  |  | use Illuminate\Contracts\Encryption\DecryptException; | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class Steam. | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Steam | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      * @param \FireflyIII\Models\Account $account | 
					
						
							| 
									
										
										
										
											2015-05-05 12:57:27 +02:00
										 |  |  |      * @param \Carbon\Carbon             $date | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-02-05 12:28:05 +01:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-27 19:23:24 +02:00
										 |  |  |     public function balance(Account $account, Carbon $date): string | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  |         // abuse chart properties:
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:25:11 +02:00
										 |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty($account->id); | 
					
						
							|  |  |  |         $cache->addProperty('balance'); | 
					
						
							|  |  |  |         $cache->addProperty($date); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2017-03-04 11:19:44 +01:00
										 |  |  |             return $cache->get(); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         $currencyId = intval($account->getMeta('currency_id')); | 
					
						
							| 
									
										
										
										
											2017-06-24 13:04:41 +02:00
										 |  |  |         // use system default currency:
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (0 === $currencyId) { | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |             $currency   = app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2017-06-08 10:35:02 +02:00
										 |  |  |             $currencyId = $currency->id; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         // first part: get all balances in own currency:
 | 
					
						
							|  |  |  |         $nativeBalance = strval( | 
					
						
							|  |  |  |             $account->transactions() | 
					
						
							|  |  |  |                     ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                     ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) | 
					
						
							|  |  |  |                     ->where('transactions.transaction_currency_id', $currencyId) | 
					
						
							|  |  |  |                     ->sum('transactions.amount') | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         // get all balances in foreign currency:
 | 
					
						
							|  |  |  |         $foreignBalance = strval( | 
					
						
							|  |  |  |             $account->transactions() | 
					
						
							|  |  |  |                     ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                     ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) | 
					
						
							|  |  |  |                     ->where('transactions.foreign_currency_id', $currencyId) | 
					
						
							|  |  |  |                     ->sum('transactions.foreign_amount') | 
					
						
							| 
									
										
										
										
											2016-03-14 20:53:56 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         $balance        = bcadd($nativeBalance, $foreignBalance); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         $virtual        = null === $account->virtual_balance ? '0' : strval($account->virtual_balance); | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         $balance        = bcadd($balance, $virtual); | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  |         $cache->store($balance); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $balance; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param \FireflyIII\Models\Account $account | 
					
						
							|  |  |  |      * @param \Carbon\Carbon             $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-27 19:23:24 +02:00
										 |  |  |     public function balanceIgnoreVirtual(Account $account, Carbon $date): string | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         // abuse chart properties:
 | 
					
						
							|  |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty($account->id); | 
					
						
							|  |  |  |         $cache->addProperty('balance-no-virtual'); | 
					
						
							|  |  |  |         $cache->addProperty($date); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2017-03-04 11:19:44 +01:00
										 |  |  |             return $cache->get(); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-04-01 19:45:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         $currencyId = intval($account->getMeta('currency_id')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $nativeBalance = strval( | 
					
						
							|  |  |  |             $account->transactions() | 
					
						
							|  |  |  |                     ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                     ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) | 
					
						
							|  |  |  |                     ->where('transactions.transaction_currency_id', $currencyId) | 
					
						
							|  |  |  |                     ->sum('transactions.amount') | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         // get all balances in foreign currency:
 | 
					
						
							|  |  |  |         $foreignBalance = strval( | 
					
						
							|  |  |  |             $account->transactions() | 
					
						
							|  |  |  |                     ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                     ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) | 
					
						
							|  |  |  |                     ->where('transactions.foreign_currency_id', $currencyId) | 
					
						
							|  |  |  |                     ->sum('transactions.foreign_amount') | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         $balance        = bcadd($nativeBalance, $foreignBalance); | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 12:28:05 +01:00
										 |  |  |         $cache->store($balance); | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 12:28:05 +01:00
										 |  |  |         return $balance; | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * Gets the balance for the given account during the whole range, using this format:. | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * [yyyy-mm-dd] => 123,2 | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  |      * @param \FireflyIII\Models\Account $account | 
					
						
							|  |  |  |      * @param \Carbon\Carbon             $start | 
					
						
							|  |  |  |      * @param \Carbon\Carbon             $end | 
					
						
							| 
									
										
										
										
											2015-12-28 07:55:09 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function balanceInRange(Account $account, Carbon $start, Carbon $end): array | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // abuse chart properties:
 | 
					
						
							|  |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty($account->id); | 
					
						
							|  |  |  |         $cache->addProperty('balance-in-range'); | 
					
						
							|  |  |  |         $cache->addProperty($start); | 
					
						
							|  |  |  |         $cache->addProperty($end); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2017-06-07 07:38:58 +02:00
										 |  |  |             return $cache->get(); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $start->subDay(); | 
					
						
							|  |  |  |         $end->addDay(); | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         $balances             = []; | 
					
						
							|  |  |  |         $formatted            = $start->format('Y-m-d'); | 
					
						
							|  |  |  |         $startBalance         = $this->balance($account, $start); | 
					
						
							|  |  |  |         $balances[$formatted] = $startBalance; | 
					
						
							|  |  |  |         $currencyId           = intval($account->getMeta('currency_id')); | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |         $start->addDay(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // query!
 | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         $set = $account->transactions() | 
					
						
							|  |  |  |                        ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                        ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                        ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                        ->groupBy('transaction_journals.date') | 
					
						
							|  |  |  |                        ->groupBy('transactions.transaction_currency_id') | 
					
						
							|  |  |  |                        ->groupBy('transactions.foreign_currency_id') | 
					
						
							|  |  |  |                        ->orderBy('transaction_journals.date', 'ASC') | 
					
						
							|  |  |  |                        ->whereNull('transaction_journals.deleted_at') | 
					
						
							|  |  |  |                        ->get( | 
					
						
							|  |  |  |                            [ | 
					
						
							|  |  |  |                                'transaction_journals.date', | 
					
						
							|  |  |  |                                'transactions.transaction_currency_id', | 
					
						
							|  |  |  |                                DB::raw('SUM(transactions.amount) AS modified'), | 
					
						
							|  |  |  |                                'transactions.foreign_currency_id', | 
					
						
							|  |  |  |                                DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'), | 
					
						
							|  |  |  |                            ] | 
					
						
							|  |  |  |                        ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |         $currentBalance = $startBalance; | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |         /** @var Transaction $entry */ | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |             // normal amount and foreign amount
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             $modified        = null === $entry->modified ? '0' : strval($entry->modified); | 
					
						
							|  |  |  |             $foreignModified = null === $entry->modified_foreign ? '0' : strval($entry->modified_foreign); | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |             $amount          = '0'; | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             if ($currencyId === $entry->transaction_currency_id || 0 === $currencyId) { | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |                 // use normal amount:
 | 
					
						
							|  |  |  |                 $amount = $modified; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($currencyId === $entry->foreign_currency_id) { | 
					
						
							| 
									
										
										
										
											2017-08-10 20:48:29 +02:00
										 |  |  |                 // use foreign amount:
 | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |                 $amount = $foreignModified; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $currentBalance  = bcadd($currentBalance, $amount); | 
					
						
							| 
									
										
										
										
											2017-06-05 15:09:17 +02:00
										 |  |  |             $carbon          = new Carbon($entry->date); | 
					
						
							|  |  |  |             $date            = $carbon->format('Y-m-d'); | 
					
						
							|  |  |  |             $balances[$date] = $currentBalance; | 
					
						
							| 
									
										
										
										
											2015-12-27 08:39:29 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cache->store($balances); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $balances; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-18 10:04:53 +01:00
										 |  |  |      * This method always ignores the virtual balance. | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |      * @param \Illuminate\Support\Collection $accounts | 
					
						
							|  |  |  |      * @param \Carbon\Carbon                 $date | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-02 16:57:31 +01:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |     public function balancesByAccounts(Collection $accounts, Carbon $date): array | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         $ids = $accounts->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2017-01-19 21:54:27 +01:00
										 |  |  |         // cache this property.
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty($ids); | 
					
						
							|  |  |  |         $cache->addProperty('balances'); | 
					
						
							|  |  |  |         $cache->addProperty($date); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2017-06-07 07:38:58 +02:00
										 |  |  |             return $cache->get(); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         // need to do this per account.
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $result = []; | 
					
						
							| 
									
										
										
										
											2017-06-05 22:11:54 +02:00
										 |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							|  |  |  |             $result[$account->id] = $this->balance($account, $date); | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cache->store($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 07:34:39 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param int $isEncrypted | 
					
						
							|  |  |  |      * @param     $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function decrypt(int $isEncrypted, string $value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (1 === $isEncrypted) { | 
					
						
							| 
									
										
										
										
											2017-02-19 07:34:39 +01:00
										 |  |  |             return Crypt::decrypt($value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $accounts | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function getLastActivities(array $accounts): array | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $list = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 12:15:58 +02:00
										 |  |  |         $set = auth()->user()->transactions() | 
					
						
							|  |  |  |                      ->whereIn('transactions.account_id', $accounts) | 
					
						
							|  |  |  |                      ->groupBy(['transactions.account_id', 'transaction_journals.user_id']) | 
					
						
							| 
									
										
										
										
											2016-10-06 05:26:38 +02:00
										 |  |  |                      ->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')]); | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             $list[intval($entry->account_id)] = new Carbon($entry->max_date); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $list; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 22:49:44 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function negative(string $amount): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (1 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2017-06-17 22:49:44 +02:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 21:04:25 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function opposite(string $amount): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $string | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function phpBytes($string): int | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $string = strtolower($string); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (!(false === stripos($string, 'k'))) { | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  |             // has a K in it, remove the K and multiply by 1024.
 | 
					
						
							| 
									
										
										
										
											2016-08-05 20:53:09 +02:00
										 |  |  |             $bytes = bcmul(rtrim($string, 'kK'), '1024'); | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return intval($bytes); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (!(false === stripos($string, 'm'))) { | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  |             // has a M in it, remove the M and multiply by 1048576.
 | 
					
						
							| 
									
										
										
										
											2016-08-05 20:53:09 +02:00
										 |  |  |             $bytes = bcmul(rtrim($string, 'mM'), '1048576'); | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return intval($bytes); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (!(false === stripos($string, 'g'))) { | 
					
						
							| 
									
										
										
										
											2016-08-05 20:54:59 +02:00
										 |  |  |             // has a G in it, remove the G and multiply by (1024)^3.
 | 
					
						
							|  |  |  |             $bytes = bcmul(rtrim($string, 'gG'), '1073741824'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return intval($bytes); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:52:16 +02:00
										 |  |  |         return intval($string); | 
					
						
							| 
									
										
										
										
											2015-07-19 14:30:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 09:34:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function positive(string $amount): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-11 15:52:55 +01:00
										 |  |  |         if (bccomp($amount, '0') === -1) { | 
					
						
							| 
									
										
										
										
											2017-02-11 09:34:04 +01:00
										 |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 08:32:51 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function tryDecrypt($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $value = Crypt::decrypt($value); | 
					
						
							|  |  |  |         } catch (DecryptException $e) { | 
					
						
							|  |  |  |             // do not care.
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $value; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |