| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers\Report; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Bill as BillCollection; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\BillLine; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Category as CategoryCollection; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Expense; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\Income; | 
					
						
							| 
									
										
										
										
											2016-01-29 17:08:06 +01:00
										 |  |  | use FireflyIII\Helpers\FiscalHelperInterface; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  | use FireflyIII\Models\Tag; | 
					
						
							| 
									
										
										
										
											2016-01-01 19:46:12 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2016-01-15 21:50:57 +01:00
										 |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Tag\TagRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-04-01 09:13:50 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							|  |  |  | use Illuminate\Database\Query\JoinClause; | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ReportHelper | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Helpers\Report | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ReportHelper implements ReportHelperInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 21:50:57 +01:00
										 |  |  |     /** @var  BudgetRepositoryInterface */ | 
					
						
							|  |  |  |     protected $budgetRepository; | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** @var ReportQueryInterface */ | 
					
						
							|  |  |  |     protected $query; | 
					
						
							| 
									
										
										
										
											2016-01-15 21:50:57 +01:00
										 |  |  |     /** @var  TagRepositoryInterface */ | 
					
						
							|  |  |  |     protected $tagRepository; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-15 22:13:38 +01:00
										 |  |  |      * ReportHelper constructor. | 
					
						
							| 
									
										
										
										
											2015-05-23 08:51:24 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-17 09:18:44 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-15 22:13:38 +01:00
										 |  |  |      * @param ReportQueryInterface      $query | 
					
						
							|  |  |  |      * @param BudgetRepositoryInterface $budgetRepository | 
					
						
							|  |  |  |      * @param TagRepositoryInterface    $tagRepository | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-15 21:50:57 +01:00
										 |  |  |     public function __construct(ReportQueryInterface $query, BudgetRepositoryInterface $budgetRepository, TagRepositoryInterface $tagRepository) | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-15 21:50:57 +01:00
										 |  |  |         $this->query            = $query; | 
					
						
							|  |  |  |         $this->budgetRepository = $budgetRepository; | 
					
						
							|  |  |  |         $this->tagRepository    = $tagRepository; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * This method generates a full report for the given period on all | 
					
						
							|  |  |  |      * the users bills and their payments. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Excludes bills which have not had a payment on the mentioned accounts. | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @return BillCollection | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     public function getBillReport(Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         /** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface'); | 
					
						
							|  |  |  |         $bills      = $repository->getBillsForAccounts($accounts); | 
					
						
							|  |  |  |         $journals   = $repository->getAllJournalsInRange($bills, $start, $end); | 
					
						
							|  |  |  |         $collection = new BillCollection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         foreach ($bills as $bill) { | 
					
						
							|  |  |  |             $billLine = new BillLine; | 
					
						
							|  |  |  |             $billLine->setBill($bill); | 
					
						
							|  |  |  |             $billLine->setActive(intval($bill->active) == 1); | 
					
						
							|  |  |  |             $billLine->setMin($bill->amount_min); | 
					
						
							|  |  |  |             $billLine->setMax($bill->amount_max); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // is hit in period?
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $entry = $journals->filter( | 
					
						
							|  |  |  |                 function (TransactionJournal $journal) use ($bill) { | 
					
						
							| 
									
										
										
										
											2016-02-06 20:17:55 +01:00
										 |  |  |                     return $journal->bill_id === $bill->id; | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2016-02-06 20:17:55 +01:00
										 |  |  |             $first = $entry->first(); | 
					
						
							|  |  |  |             if (!is_null($first)) { | 
					
						
							|  |  |  |                 $billLine->setTransactionJournalId($first->id); | 
					
						
							|  |  |  |                 $billLine->setAmount($first->journalAmount); | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |                 $billLine->setHit(true); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $billLine->setHit(false); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-04 08:56:33 +01:00
										 |  |  |             if (!(!$billLine->isHit() && !$billLine->isActive())) { | 
					
						
							| 
									
										
										
										
											2016-02-04 08:55:06 +01:00
										 |  |  |                 $collection->addBill($billLine); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return $collection; | 
					
						
							| 
									
										
										
										
											2015-12-11 16:36:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-11 17:53:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @return CategoryCollection | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     public function getCategoryReport(Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         $object = new CategoryCollection; | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * GET CATEGORIES: | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         $set = $repository->spentForAccountsPerMonth($accounts, $start, $end); | 
					
						
							|  |  |  |         foreach ($set as $category) { | 
					
						
							|  |  |  |             $object->addCategory($category); | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return $object; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a full report on the users expenses during the period for a list of accounts. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Expense | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $object = new Expense; | 
					
						
							|  |  |  |         $set    = $this->query->expense($accounts, $start, $end); | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             $object->addToTotal($entry->journalAmount); // can be positive, if it's a transfer
 | 
					
						
							|  |  |  |             $object->addOrCreateExpense($entry); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							| 
									
										
										
										
											2015-12-11 18:45:39 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * Get a full report on the users incomes during the period for the given accounts. | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @return Income | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         $object = new Income; | 
					
						
							|  |  |  |         $set    = $this->query->income($accounts, $start, $end); | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             $object->addToTotal($entry->journalAmount); | 
					
						
							|  |  |  |             $object->addOrCreateIncome($entry); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return $object; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-01 21:07:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function listOfMonths(Carbon $date) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-29 17:08:06 +01:00
										 |  |  |         /** @var FiscalHelperInterface $fiscalHelper */ | 
					
						
							|  |  |  |         $fiscalHelper = app('FireflyIII\Helpers\FiscalHelperInterface'); | 
					
						
							| 
									
										
										
										
											2016-01-29 17:09:23 +01:00
										 |  |  |         $start        = clone $date; | 
					
						
							| 
									
										
										
										
											2016-02-06 04:32:30 +01:00
										 |  |  |         $start->startOfMonth(); | 
					
						
							|  |  |  |         $end = Carbon::now(); | 
					
						
							|  |  |  |         $end->endOfMonth(); | 
					
						
							|  |  |  |         $months = []; | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         while ($start <= $end) { | 
					
						
							| 
									
										
										
										
											2016-02-06 04:32:30 +01:00
										 |  |  |             // current year:
 | 
					
						
							| 
									
										
										
										
											2016-01-27 13:38:34 +10:00
										 |  |  |             $year = $fiscalHelper->endOfFiscalYear($start)->year; | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($months[$year])) { | 
					
						
							|  |  |  |                 $months[$year] = [ | 
					
						
							| 
									
										
										
										
											2016-01-29 17:13:54 +01:00
										 |  |  |                     'fiscal_start' => $fiscalHelper->startOfFiscalYear($start)->format('Y-m-d'), | 
					
						
							|  |  |  |                     'fiscal_end'   => $fiscalHelper->endOfFiscalYear($start)->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2016-01-29 17:09:23 +01:00
										 |  |  |                     'start'        => Carbon::createFromDate($year, 1, 1)->format('Y-m-d'), | 
					
						
							|  |  |  |                     'end'          => Carbon::createFromDate($year, 12, 31)->format('Y-m-d'), | 
					
						
							|  |  |  |                     'months'       => [], | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |             $currentEnd = clone $start; | 
					
						
							|  |  |  |             $currentEnd->endOfMonth(); | 
					
						
							| 
									
										
										
										
											2016-02-06 04:32:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |             $months[$year]['months'][] = [ | 
					
						
							|  |  |  |                 'formatted' => $start->formatLocalized('%B %Y'), | 
					
						
							|  |  |  |                 'start'     => $start->format('Y-m-d'), | 
					
						
							|  |  |  |                 'end'       => $currentEnd->format('Y-m-d'), | 
					
						
							|  |  |  |                 'month'     => $start->month, | 
					
						
							|  |  |  |                 'year'      => $year, | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2016-02-06 04:32:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // to make the hop to the next month properly:
 | 
					
						
							|  |  |  |             $start = clone $currentEnd; | 
					
						
							|  |  |  |             $start->addDay(); | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return $months; | 
					
						
							| 
									
										
										
										
											2015-12-12 10:33:19 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns an array of tags and their comparitive size with amounts bla bla. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function tagReport(Carbon $start, Carbon $end, Collection $accounts): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ids        = $accounts->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $set        = Tag:: | 
					
						
							| 
									
										
										
										
											2016-04-01 09:13:50 +02:00
										 |  |  |         leftJoin('tag_transaction_journal', 'tags.id', '=', 'tag_transaction_journal.tag_id') | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |                          ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							| 
									
										
										
										
											2016-04-01 09:13:50 +02:00
										 |  |  |                          ->leftJoin( | 
					
						
							|  |  |  |                              'transactions AS source', function (JoinClause $join) { | 
					
						
							|  |  |  |                              $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', '0'); | 
					
						
							|  |  |  |                          } | 
					
						
							|  |  |  |                          ) | 
					
						
							|  |  |  |                          ->leftJoin( | 
					
						
							|  |  |  |                              'transactions AS destination', function (JoinClause $join) { | 
					
						
							|  |  |  |                              $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', '0'); | 
					
						
							|  |  |  |                          } | 
					
						
							|  |  |  |                          ) | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |                          ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                          ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							| 
									
										
										
										
											2016-04-01 09:13:50 +02:00
										 |  |  |                          ->where( | 
					
						
							|  |  |  |                              function (Builder $q) use ($ids) { | 
					
						
							|  |  |  |                                  $q->whereIn('source.account_id', $ids) | 
					
						
							|  |  |  |                                    ->whereIn('destination.account_id', $ids, 'xor'); | 
					
						
							|  |  |  |                              } | 
					
						
							|  |  |  |                          ) | 
					
						
							|  |  |  |                          ->get(['tags.id', 'tags.tag', 'transaction_journals.id as journal_id', 'destination.amount']); | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |         $collection = []; | 
					
						
							|  |  |  |         if ($set->count() === 0) { | 
					
						
							|  |  |  |             return $collection; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-01 13:23:12 +02:00
										 |  |  |         /** @var Tag $entry */ | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             // less than zero? multiply to be above zero.
 | 
					
						
							|  |  |  |             $amount = $entry->amount; | 
					
						
							| 
									
										
										
										
											2016-04-01 09:13:50 +02:00
										 |  |  |             $id     = intval($entry->id); | 
					
						
							| 
									
										
										
										
											2016-03-01 21:31:25 +01:00
										 |  |  |             if (!isset($collection[$id])) { | 
					
						
							|  |  |  |                 $collection[$id] = [ | 
					
						
							|  |  |  |                     'id'     => $id, | 
					
						
							|  |  |  |                     'tag'    => $entry->tag, | 
					
						
							|  |  |  |                     'amount' => $amount, | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $collection[$id]['amount'] = bcadd($collection[$id]['amount'], $amount); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // cleanup collection (match "fonts")
 | 
					
						
							|  |  |  |         $max = strval(max(array_column($collection, 'amount'))); | 
					
						
							|  |  |  |         foreach ($collection as $id => $entry) { | 
					
						
							|  |  |  |             $size = bcdiv($entry['amount'], $max, 4); | 
					
						
							|  |  |  |             if (bccomp($size, '0.25') === -1) { | 
					
						
							|  |  |  |                 $size = '0.5'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $collection[$id]['fontsize'] = $size; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $collection; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay | 
					
						
							|  |  |  |      * and sum up everything in the array in the given range. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * @param array  $array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getSumOfRange(Carbon $start, Carbon $end, array $array) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sum          = '0'; | 
					
						
							|  |  |  |         $currentStart = clone $start; // to not mess with the original one
 | 
					
						
							|  |  |  |         $currentEnd   = clone $end; // to not mess with the original one
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($currentStart <= $currentEnd) { | 
					
						
							|  |  |  |             $date = $currentStart->format('Y-m-d'); | 
					
						
							|  |  |  |             if (isset($array[$date])) { | 
					
						
							|  |  |  |                 $sum = bcadd($sum, $array[$date]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $currentStart->addDay(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $sum; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-09 08:20:55 +01:00
										 |  |  | } |