| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * MetaPieChart.php | 
					
						
							|  |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers\Chart; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collector\JournalCollectorInterface; | 
					
						
							| 
									
										
										
										
											2017-04-29 08:22:56 +02:00
										 |  |  | use FireflyIII\Helpers\Filter\NegativeAmountFilter; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Filter\OpposingAccountFilter; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Filter\PositiveAmountFilter; | 
					
						
							| 
									
										
										
										
											2017-04-28 20:08:25 +02:00
										 |  |  | use FireflyIII\Helpers\Filter\TransferFilter; | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  | use FireflyIII\Models\Tag; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  | use FireflyIII\Repositories\Tag\TagRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2017-02-11 15:52:55 +01:00
										 |  |  | use Steam; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class MetaPieChart | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Helpers\Chart | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class MetaPieChart implements MetaPieChartInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $accounts; | 
					
						
							|  |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $budgets; | 
					
						
							|  |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $categories; | 
					
						
							|  |  |  |     /** @var bool */ | 
					
						
							|  |  |  |     protected $collectOtherObjects = false; | 
					
						
							|  |  |  |     /** @var  Carbon */ | 
					
						
							|  |  |  |     protected $end; | 
					
						
							|  |  |  |     /** @var array */ | 
					
						
							|  |  |  |     protected $grouping | 
					
						
							|  |  |  |         = [ | 
					
						
							|  |  |  |             'account'  => ['opposing_account_id'], | 
					
						
							|  |  |  |             'budget'   => ['transaction_journal_budget_id', 'transaction_budget_id'], | 
					
						
							|  |  |  |             'category' => ['transaction_journal_category_id', 'transaction_category_id'], | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |             'tag'      => [], | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     /** @var array */ | 
					
						
							|  |  |  |     protected $repositories | 
					
						
							|  |  |  |         = [ | 
					
						
							|  |  |  |             'account'  => AccountRepositoryInterface::class, | 
					
						
							|  |  |  |             'budget'   => BudgetRepositoryInterface::class, | 
					
						
							|  |  |  |             'category' => CategoryRepositoryInterface::class, | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |             'tag'      => TagRepositoryInterface::class, | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     /** @var  Carbon */ | 
					
						
							|  |  |  |     protected $start; | 
					
						
							| 
									
										
										
										
											2017-02-24 20:27:26 +01:00
										 |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $tags; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     /** @var  string */ | 
					
						
							|  |  |  |     protected $total = '0'; | 
					
						
							|  |  |  |     /** @var  User */ | 
					
						
							|  |  |  |     protected $user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->accounts   = new Collection; | 
					
						
							|  |  |  |         $this->budgets    = new Collection; | 
					
						
							|  |  |  |         $this->categories = new Collection; | 
					
						
							| 
									
										
										
										
											2017-02-24 21:01:33 +01:00
										 |  |  |         $this->tags       = new Collection; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $direction | 
					
						
							|  |  |  |      * @param string $group | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function generate(string $direction, string $group): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $transactions = $this->getTransactions($direction); | 
					
						
							|  |  |  |         $grouped      = $this->groupByFields($transactions, $this->grouping[$group]); | 
					
						
							|  |  |  |         $chartData    = $this->organizeByType($group, $grouped); | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |         $key          = strval(trans('firefly.everything_else')); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // also collect all other transactions
 | 
					
						
							|  |  |  |         if ($this->collectOtherObjects && $direction === 'expense') { | 
					
						
							|  |  |  |             /** @var JournalCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |             $collector = app(JournalCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |             $collector->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |             $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]); | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $journals        = $collector->getJournals(); | 
					
						
							|  |  |  |             $sum             = strval($journals->sum('transaction_amount')); | 
					
						
							|  |  |  |             $sum             = bcmul($sum, '-1'); | 
					
						
							|  |  |  |             $sum             = bcsub($sum, $this->total); | 
					
						
							|  |  |  |             $chartData[$key] = $sum; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->collectOtherObjects && $direction === 'income') { | 
					
						
							|  |  |  |             /** @var JournalCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |             $collector = app(JournalCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |             $collector->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |             $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]); | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |             $journals        = $collector->getJournals(); | 
					
						
							|  |  |  |             $sum             = strval($journals->sum('transaction_amount')); | 
					
						
							|  |  |  |             $sum             = bcsub($sum, $this->total); | 
					
						
							|  |  |  |             $chartData[$key] = $sum; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $chartData; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setAccounts(Collection $accounts): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->accounts = $accounts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setBudgets(Collection $budgets): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->budgets = $budgets; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param Collection $categories | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setCategories(Collection $categories): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->categories = $categories; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param bool $collectOtherObjects | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setCollectOtherObjects(bool $collectOtherObjects): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->collectOtherObjects = $collectOtherObjects; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setEnd(Carbon $end): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->end = $end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setStart(Carbon $start): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->start = $start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 20:27:26 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-02-24 20:27:26 +01:00
										 |  |  |      * @param Collection $tags | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setTags(Collection $tags): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->tags = $tags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      * @param User $user | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return MetaPieChartInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user): MetaPieChartInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $direction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTransactions(string $direction): Collection | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var JournalCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |         $collector = app(JournalCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-04-29 08:22:56 +02:00
										 |  |  |         $types     = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; | 
					
						
							|  |  |  |         $collector->addFilter(NegativeAmountFilter::class); | 
					
						
							|  |  |  |         if ($direction === 'expense') { | 
					
						
							|  |  |  |             $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; | 
					
						
							|  |  |  |             $collector->addFilter(PositiveAmountFilter::class); | 
					
						
							|  |  |  |             $collector->removeFilter(NegativeAmountFilter::class); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |         $collector->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         $collector->setAccounts($this->accounts); | 
					
						
							|  |  |  |         $collector->setRange($this->start, $this->end); | 
					
						
							|  |  |  |         $collector->setTypes($types); | 
					
						
							|  |  |  |         $collector->withOpposingAccount(); | 
					
						
							| 
									
										
										
										
											2017-04-29 08:22:56 +02:00
										 |  |  |         $collector->addFilter(OpposingAccountFilter::class); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($direction === 'income') { | 
					
						
							| 
									
										
										
										
											2017-04-28 20:08:25 +02:00
										 |  |  |             $collector->removeFilter(TransferFilter::class); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |         $collector->setBudgets($this->budgets); | 
					
						
							|  |  |  |         $collector->setCategories($this->categories); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 20:27:26 +01:00
										 |  |  |         if ($this->tags->count() > 0) { | 
					
						
							|  |  |  |             $collector->setTags($this->tags); | 
					
						
							|  |  |  |             $collector->withCategoryInformation(); | 
					
						
							|  |  |  |             $collector->withBudgetInformation(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 08:22:56 +02:00
										 |  |  |         return $collector->getJournals(); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $set | 
					
						
							|  |  |  |      * @param array      $fields | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |     protected function groupByFields(Collection $set, array $fields): array | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |         if (count($fields) === 0 && $this->tags->count() > 0) { | 
					
						
							|  |  |  |             // do a special group on tags:
 | 
					
						
							|  |  |  |             return $this->groupByTag($set); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         $grouped = []; | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         foreach ($set as $transaction) { | 
					
						
							|  |  |  |             $values = []; | 
					
						
							|  |  |  |             foreach ($fields as $field) { | 
					
						
							|  |  |  |                 $values[] = intval($transaction->$field); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $value           = max($values); | 
					
						
							|  |  |  |             $grouped[$value] = $grouped[$value] ?? '0'; | 
					
						
							|  |  |  |             $grouped[$value] = bcadd($transaction->transaction_amount, $grouped[$value]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $grouped; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							|  |  |  |      * @param array  $array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function organizeByType(string $type, array $array): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chartData  = []; | 
					
						
							|  |  |  |         $names      = []; | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |         $repository = app($this->repositories[$type]); | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |         $repository->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |         foreach ($array as $objectId => $amount) { | 
					
						
							|  |  |  |             if (!isset($names[$objectId])) { | 
					
						
							|  |  |  |                 $object           = $repository->find(intval($objectId)); | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |                 $names[$objectId] = $object->name ?? $object->tag; | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-02-11 15:52:55 +01:00
										 |  |  |             $amount                       = Steam::positive($amount); | 
					
						
							| 
									
										
										
										
											2017-01-03 17:02:17 +01:00
										 |  |  |             $this->total                  = bcadd($this->total, $amount); | 
					
						
							|  |  |  |             $chartData[$names[$objectId]] = $amount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $chartData; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:04:57 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection $set | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-02-24 21:09:20 +01:00
										 |  |  |     private function groupByTag(Collection $set): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $grouped = []; | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         foreach ($set as $transaction) { | 
					
						
							|  |  |  |             $journal = $transaction->transactionJournal; | 
					
						
							|  |  |  |             $tags    = $journal->tags; | 
					
						
							|  |  |  |             /** @var Tag $tag */ | 
					
						
							|  |  |  |             foreach ($tags as $tag) { | 
					
						
							|  |  |  |                 $tagId           = $tag->id; | 
					
						
							|  |  |  |                 $grouped[$tagId] = $grouped[$tagId] ?? '0'; | 
					
						
							|  |  |  |                 $grouped[$tagId] = bcadd($transaction->transaction_amount, $grouped[$tagId]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $grouped; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-05 19:51:58 +01:00
										 |  |  | } |