| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\Budget; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  | use Auth; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | use DB; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							|  |  |  | use FireflyIII\Models\LimitRepetition; | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2015-06-05 10:02:40 +02:00
										 |  |  | use FireflyIII\Repositories\Shared\ComponentRepository; | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2015-04-07 17:51:22 +02:00
										 |  |  | use Illuminate\Database\Query\Builder as QueryBuilder; | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | use Illuminate\Database\Query\JoinClause; | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-05-08 14:00:49 +02:00
										 |  |  | use Input; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BudgetRepository | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Repositories\Budget | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-06-05 10:02:40 +02:00
										 |  |  | class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-03 19:39:36 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @param Budget     $budget | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							| 
									
										
										
										
											2015-12-29 08:27:13 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-12-29 08:27:13 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2015-12-29 08:27:13 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         return $this->commonBalanceInPeriod($budget, $start, $end, $accounts); | 
					
						
							| 
									
										
										
										
											2015-12-29 08:45:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @return void | 
					
						
							| 
									
										
										
										
											2015-12-29 08:45:43 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function cleanupBudgets() | 
					
						
							| 
									
										
										
										
											2015-12-29 08:45:43 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         // delete limits with amount 0:
 | 
					
						
							|  |  |  |         BudgetLimit::where('amount', 0)->delete(); | 
					
						
							| 
									
										
										
										
											2015-12-29 08:27:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return boolean | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(Budget $budget) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $budget->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function firstActivity(Budget $budget) | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $first = $budget->transactionjournals()->orderBy('date', 'ASC')->first(); | 
					
						
							|  |  |  |         if ($first) { | 
					
						
							|  |  |  |             return $first->date; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-08 13:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         return new Carbon; | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function getActiveBudgets() | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $set = Auth::user()->budgets()->where('active', 1)->get(); | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |     public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2016-01-01 12:41:00 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |         /** @var Collection $repetitions */ | 
					
						
							|  |  |  |         return LimitRepetition:: | 
					
						
							|  |  |  |         leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') | 
					
						
							|  |  |  |                               ->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') | 
					
						
							|  |  |  |                               ->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                               ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                               ->where('budgets.user_id', Auth::user()->id) | 
					
						
							|  |  |  |                               ->get(['limit_repetitions.*', 'budget_limits.budget_id']); | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * Get the budgeted amounts for each budgets in each year. | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * @return Collection | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2015-12-31 17:46:34 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $budgetIds = $budgets->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $set = Auth::user()->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id') | 
					
						
							|  |  |  |                    ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') | 
					
						
							|  |  |  |                    ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->groupBy('budgets.id') | 
					
						
							|  |  |  |                    ->groupBy('dateFormatted') | 
					
						
							|  |  |  |                    ->whereIn('budgets.id', $budgetIds) | 
					
						
							|  |  |  |                    ->get( | 
					
						
							|  |  |  |                        [ | 
					
						
							|  |  |  |                            'budgets.*', | 
					
						
							|  |  |  |                            DB::Raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'), | 
					
						
							|  |  |  |                            DB::Raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'), | 
					
						
							|  |  |  |                        ] | 
					
						
							|  |  |  |                    ); | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBudgets() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-09 11:13:38 +02:00
										 |  |  |         /** @var Collection $set */ | 
					
						
							|  |  |  |         $set = Auth::user()->budgets()->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-05-20 06:50:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 11:13:38 +02:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns an array with every budget in it and the expenses for each budget | 
					
						
							|  |  |  |      * per month. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  |     public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-30 09:17:05 +01:00
										 |  |  |         $ids = $accounts->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  |         $set = Auth::user()->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') | 
					
						
							|  |  |  |                    ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') | 
					
						
							|  |  |  |                    ->leftJoin( | 
					
						
							|  |  |  |                        'transactions', function (JoinClause $join) { | 
					
						
							|  |  |  |                        $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); | 
					
						
							|  |  |  |                    } | 
					
						
							|  |  |  |                    ) | 
					
						
							|  |  |  |                    ->groupBy('budgets.id') | 
					
						
							|  |  |  |                    ->groupBy('dateFormatted') | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->whereIn('transactions.account_id', $ids) | 
					
						
							|  |  |  |                    ->get( | 
					
						
							|  |  |  |                        [ | 
					
						
							|  |  |  |                            'budgets.*', | 
					
						
							|  |  |  |                            DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), | 
					
						
							| 
									
										
										
										
											2016-01-15 23:12:52 +01:00
										 |  |  |                            DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'), | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  |                        ] | 
					
						
							|  |  |  |                    ); | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         foreach ($set as $budget) { | 
					
						
							|  |  |  |             $id = $budget->id; | 
					
						
							|  |  |  |             if (!isset($return[$id])) { | 
					
						
							|  |  |  |                 $return[$id] = [ | 
					
						
							|  |  |  |                     'budget'  => $budget, | 
					
						
							|  |  |  |                     'entries' => [], | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // store each entry:
 | 
					
						
							|  |  |  |             $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-28 19:56:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 17:57:03 +01:00
										 |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * Returns an array with every budget in it and the expenses for each budget | 
					
						
							|  |  |  |      * per year for. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // it's a query.
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $ids       = $accounts->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $budgetIds = $budgets->pluck('id')->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 13:11:51 +02:00
										 |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $set = Auth::user()->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') | 
					
						
							|  |  |  |                    ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') | 
					
						
							|  |  |  |                    ->leftJoin( | 
					
						
							|  |  |  |                        'transactions', function (JoinClause $join) { | 
					
						
							|  |  |  |                        $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); | 
					
						
							|  |  |  |                    } | 
					
						
							|  |  |  |                    ) | 
					
						
							|  |  |  |                    ->groupBy('budgets.id') | 
					
						
							|  |  |  |                    ->groupBy('dateFormatted') | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->whereIn('transactions.account_id', $ids) | 
					
						
							|  |  |  |                    ->whereIn('budgets.id', $budgetIds) | 
					
						
							|  |  |  |                    ->get( | 
					
						
							|  |  |  |                        [ | 
					
						
							|  |  |  |                            'budgets.*', | 
					
						
							|  |  |  |                            DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), | 
					
						
							|  |  |  |                            DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'), | 
					
						
							|  |  |  |                        ] | 
					
						
							|  |  |  |                    ); | 
					
						
							| 
									
										
										
										
											2015-07-08 13:11:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |         $return = []; | 
					
						
							|  |  |  |         foreach ($set as $budget) { | 
					
						
							|  |  |  |             $id = $budget->id; | 
					
						
							|  |  |  |             if (!isset($return[$id])) { | 
					
						
							|  |  |  |                 $return[$id] = [ | 
					
						
							|  |  |  |                     'budget'  => $budget, | 
					
						
							|  |  |  |                     'entries' => [], | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // store each entry:
 | 
					
						
							|  |  |  |             $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns a list of budgets, budget limits and limit repetitions | 
					
						
							|  |  |  |      * (doubling any of them in a left join) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							|  |  |  |         $set = Auth::user() | 
					
						
							|  |  |  |                    ->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id') | 
					
						
							|  |  |  |                    ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') | 
					
						
							|  |  |  |                    ->where( | 
					
						
							|  |  |  |                        function (Builder $query) use ($start, $end) { | 
					
						
							|  |  |  |                            $query->where( | 
					
						
							|  |  |  |                                function (Builder $query) use ($start, $end) { | 
					
						
							|  |  |  |                                    $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')); | 
					
						
							|  |  |  |                                    $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d')); | 
					
						
							|  |  |  |                                } | 
					
						
							|  |  |  |                            ); | 
					
						
							|  |  |  |                            $query->orWhere( | 
					
						
							|  |  |  |                                function (Builder $query) { | 
					
						
							|  |  |  |                                    $query->whereNull('limit_repetitions.startdate'); | 
					
						
							|  |  |  |                                    $query->whereNull('limit_repetitions.enddate'); | 
					
						
							|  |  |  |                                } | 
					
						
							|  |  |  |                            ); | 
					
						
							|  |  |  |                        } | 
					
						
							|  |  |  |                    ) | 
					
						
							|  |  |  |                    ->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return LimitRepetition|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = $budget->limitrepetitions() | 
					
						
							|  |  |  |                        ->where('limit_repetitions.startdate', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                        ->where('limit_repetitions.enddate', $end->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                        ->first(['limit_repetitions.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the expenses for this budget grouped per day, with the date | 
					
						
							|  |  |  |      * in "date" (a string, not a Carbon) and the amount in "dailyAmount". | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $set = Auth::user()->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id') | 
					
						
							|  |  |  |                    ->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                    ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->whereNull('transaction_journals.deleted_at') | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->where('budgets.id', $budget->id) | 
					
						
							|  |  |  |                    ->where('transactions.amount', '<', 0) | 
					
						
							|  |  |  |                    ->groupBy('transaction_journals.date') | 
					
						
							|  |  |  |                    ->orderBy('transaction_journals.date') | 
					
						
							|  |  |  |                    ->get(['transaction_journals.date', DB::Raw('SUM(`transactions`.`amount`) as `dailyAmount`')]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the expenses for this budget grouped per month, with the date | 
					
						
							|  |  |  |      * in "dateFormatted" (a string, not a Carbon) and the amount in "dailyAmount". | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $set = Auth::user()->budgets() | 
					
						
							|  |  |  |                    ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id') | 
					
						
							|  |  |  |                    ->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                    ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) | 
					
						
							|  |  |  |                    ->whereNull('transaction_journals.deleted_at') | 
					
						
							|  |  |  |                    ->where('budgets.id', $budget->id) | 
					
						
							|  |  |  |                    ->where('transactions.amount', '<', 0) | 
					
						
							|  |  |  |                    ->groupBy('dateFormatted') | 
					
						
							|  |  |  |                    ->orderBy('transaction_journals.date') | 
					
						
							|  |  |  |                    ->get( | 
					
						
							|  |  |  |                        [ | 
					
						
							|  |  |  |                            DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), | 
					
						
							|  |  |  |                            DB::Raw('SUM(`transactions`.`amount`) as `monthlyAmount`'), | 
					
						
							|  |  |  |                        ] | 
					
						
							|  |  |  |                    ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Carbon | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getFirstBudgetLimitDate(Budget $budget) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first(); | 
					
						
							|  |  |  |         if ($limit) { | 
					
						
							|  |  |  |             return $limit->startdate; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Carbon::now()->startOfYear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getInactiveBudgets() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							|  |  |  |         $set = Auth::user()->budgets()->where('active', 0)->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = $set->sortBy( | 
					
						
							|  |  |  |             function (Budget $budget) { | 
					
						
							|  |  |  |                 return strtolower($budget->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns all the transaction journals for a limit, possibly limited by a limit repetition. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |      * @param Budget          $budget | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      * @param LimitRepetition $repetition | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |      * @param int             $take | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-26 20:28:18 +02:00
										 |  |  |      * @return LengthAwarePaginator | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:30:06 +01:00
										 |  |  |     public function getJournals(Budget $budget, LimitRepetition $repetition = null, int $take = 50) | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |         $offset     = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0; | 
					
						
							|  |  |  |         $setQuery   = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset) | 
					
						
							|  |  |  |                              ->orderBy('transaction_journals.date', 'DESC') | 
					
						
							|  |  |  |                              ->orderBy('transaction_journals.order', 'ASC') | 
					
						
							|  |  |  |                              ->orderBy('transaction_journals.id', 'DESC'); | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |         $countQuery = $budget->transactionJournals(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!is_null($repetition->id)) { | 
					
						
							|  |  |  |             $setQuery->after($repetition->startdate)->before($repetition->enddate); | 
					
						
							|  |  |  |             $countQuery->after($repetition->startdate)->before($repetition->enddate); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |         $set   = $setQuery->get(['transaction_journals.*']); | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |         $count = $countQuery->count(); | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 19:02:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $paginator = new LengthAwarePaginator($set, $count, $take, $offset); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $paginator; | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getWithoutBudget(Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Auth::user() | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |                    ->transactionjournals() | 
					
						
							| 
									
										
										
										
											2016-01-10 12:16:25 +01:00
										 |  |  |                    ->transactionTypes([TransactionType::WITHDRAWAL]) | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |                    ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                    ->whereNull('budget_transaction_journal.id') | 
					
						
							|  |  |  |                    ->before($end) | 
					
						
							|  |  |  |                    ->after($start) | 
					
						
							|  |  |  |                    ->orderBy('transaction_journals.date', 'DESC') | 
					
						
							|  |  |  |                    ->orderBy('transaction_journals.order', 'ASC') | 
					
						
							|  |  |  |                    ->orderBy('transaction_journals.id', 'DESC') | 
					
						
							|  |  |  |                    ->get(['transaction_journals.*']); | 
					
						
							| 
									
										
										
										
											2015-04-05 10:36:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getWithoutBudgetSum(Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  |         $entry = Auth::user() | 
					
						
							|  |  |  |                      ->transactionjournals() | 
					
						
							|  |  |  |                      ->whereNotIn( | 
					
						
							|  |  |  |                          'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) { | 
					
						
							|  |  |  |                          $query | 
					
						
							|  |  |  |                              ->select('transaction_journals.id') | 
					
						
							|  |  |  |                              ->from('transaction_journals') | 
					
						
							|  |  |  |                              ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                              ->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                              ->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                              ->whereNotNull('budget_transaction_journal.budget_id'); | 
					
						
							|  |  |  |                      } | 
					
						
							|  |  |  |                      ) | 
					
						
							|  |  |  |                      ->after($start) | 
					
						
							|  |  |  |                      ->before($end) | 
					
						
							|  |  |  |                      ->leftJoin( | 
					
						
							|  |  |  |                          'transactions', function (JoinClause $join) { | 
					
						
							|  |  |  |                          $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); | 
					
						
							|  |  |  |                      } | 
					
						
							|  |  |  |                      ) | 
					
						
							|  |  |  |                      ->transactionTypes([TransactionType::WITHDRAWAL]) | 
					
						
							|  |  |  |                      ->first([DB::Raw('SUM(`transactions`.`amount`) as `journalAmount`')]); | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |         if (is_null($entry->journalAmount)) { | 
					
						
							|  |  |  |             return ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-27 21:17:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $entry->journalAmount; | 
					
						
							| 
									
										
										
										
											2015-04-06 09:15:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-01 13:54:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns an array with the following key:value pairs: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * yyyy-mm-dd:<array> | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * That array contains: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * budgetid:<amount> | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget | 
					
						
							|  |  |  |      * from the given users accounts.. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ids = $accounts->pluck('id')->toArray(); | 
					
						
							|  |  |  |         /** @var Collection $query */ | 
					
						
							|  |  |  |         $query = Auth::user()->transactionJournals() | 
					
						
							|  |  |  |                      ->transactionTypes([TransactionType::WITHDRAWAL]) | 
					
						
							|  |  |  |                      ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                      ->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') | 
					
						
							|  |  |  |                      ->whereIn('transactions.account_id', $ids) | 
					
						
							|  |  |  |                      ->where('transactions.amount', '<', 0) | 
					
						
							|  |  |  |                      ->before($end) | 
					
						
							|  |  |  |                      ->after($start) | 
					
						
							|  |  |  |                      ->groupBy('budget_id') | 
					
						
							|  |  |  |                      ->groupBy('dateFormatted') | 
					
						
							|  |  |  |                      ->get( | 
					
						
							|  |  |  |                          ['transaction_journals.date as dateFormatted', 'budget_transaction_journal.budget_id', | 
					
						
							|  |  |  |                           DB::Raw('SUM(`transactions`.`amount`) AS `sum`')] | 
					
						
							|  |  |  |                      ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         foreach ($query->toArray() as $entry) { | 
					
						
							|  |  |  |             $budgetId = $entry['budget_id']; | 
					
						
							|  |  |  |             if (!isset($return[$budgetId])) { | 
					
						
							|  |  |  |                 $return[$budgetId] = []; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $return[$budgetId][$entry['dateFormatted']] = $entry['sum']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-01 19:46:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns a list of expenses (in the field "spent", grouped per budget per account. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Collection $budgets | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * @param Carbon     $start | 
					
						
							|  |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountIds = $accounts->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $budgetIds  = $budgets->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $set        = Auth::user()->transactionjournals() | 
					
						
							|  |  |  |                           ->leftJoin( | 
					
						
							|  |  |  |                               'transactions AS t_from', function (JoinClause $join) { | 
					
						
							|  |  |  |                               $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0); | 
					
						
							|  |  |  |                           } | 
					
						
							|  |  |  |                           ) | 
					
						
							|  |  |  |                           ->leftJoin( | 
					
						
							|  |  |  |                               'transactions AS t_to', function (JoinClause $join) { | 
					
						
							|  |  |  |                               $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0); | 
					
						
							|  |  |  |                           } | 
					
						
							|  |  |  |                           ) | 
					
						
							|  |  |  |                           ->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') | 
					
						
							|  |  |  |                           ->whereIn('t_from.account_id', $accountIds) | 
					
						
							|  |  |  |                           ->whereNotIn('t_to.account_id', $accountIds) | 
					
						
							|  |  |  |                           ->where( | 
					
						
							|  |  |  |                               function (Builder $q) use ($budgetIds) { | 
					
						
							|  |  |  |                                   $q->whereIn('budget_transaction_journal.budget_id', $budgetIds); | 
					
						
							|  |  |  |                                   $q->orWhereNull('budget_transaction_journal.budget_id'); | 
					
						
							|  |  |  |                               } | 
					
						
							|  |  |  |                           ) | 
					
						
							|  |  |  |                           ->after($start) | 
					
						
							|  |  |  |                           ->before($end) | 
					
						
							|  |  |  |                           ->groupBy('t_from.account_id') | 
					
						
							|  |  |  |                           ->groupBy('budget_transaction_journal.budget_id') | 
					
						
							|  |  |  |                           ->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]) | 
					
						
							|  |  |  |                           ->get( | 
					
						
							|  |  |  |                               [ | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  |                                   't_from.account_id', 'budget_transaction_journal.budget_id', | 
					
						
							| 
									
										
										
										
											2016-01-15 23:12:52 +01:00
										 |  |  |                                   DB::Raw('SUM(`t_from`.`amount`) AS `spent`'), | 
					
						
							| 
									
										
										
										
											2016-01-01 19:46:12 +01:00
										 |  |  |                               ] | 
					
						
							|  |  |  |                           ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns an array with the following key:value pairs: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * yyyy-mm-dd:<amount> | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget | 
					
						
							|  |  |  |      * from all the users accounts. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function spentPerDay(Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $query */ | 
					
						
							|  |  |  |         $query = $budget->transactionJournals() | 
					
						
							|  |  |  |                         ->transactionTypes([TransactionType::WITHDRAWAL]) | 
					
						
							|  |  |  |                         ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                         ->where('transactions.amount', '<', 0) | 
					
						
							|  |  |  |                         ->before($end) | 
					
						
							|  |  |  |                         ->after($start) | 
					
						
							|  |  |  |                         ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         foreach ($query->toArray() as $entry) { | 
					
						
							|  |  |  |             $return[$entry['dateFormatted']] = $entry['sum']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Budget | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function store(array $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newBudget = new Budget( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'user_id' => $data['user'], | 
					
						
							|  |  |  |                 'name'    => $data['name'], | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $newBudget->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $newBudget; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param array  $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Budget | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Budget $budget, array $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // update the account:
 | 
					
						
							|  |  |  |         $budget->name   = $data['name']; | 
					
						
							|  |  |  |         $budget->active = $data['active']; | 
					
						
							|  |  |  |         $budget->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $budget; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Budget $budget | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |      * @param int    $amount | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return BudgetLimit | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |     public function updateLimitAmount(Budget $budget, Carbon $date, int $amount) | 
					
						
							| 
									
										
										
										
											2016-01-20 15:21:27 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // there should be a budget limit for this startdate:
 | 
					
						
							|  |  |  |         /** @var BudgetLimit $limit */ | 
					
						
							|  |  |  |         $limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$limit) { | 
					
						
							|  |  |  |             // if not, create one!
 | 
					
						
							|  |  |  |             $limit = new BudgetLimit; | 
					
						
							|  |  |  |             $limit->budget()->associate($budget); | 
					
						
							|  |  |  |             $limit->startdate   = $date; | 
					
						
							|  |  |  |             $limit->amount      = $amount; | 
					
						
							|  |  |  |             $limit->repeat_freq = 'monthly'; | 
					
						
							|  |  |  |             $limit->repeats     = 0; | 
					
						
							|  |  |  |             $limit->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // likewise, there should be a limit repetition to match the end date
 | 
					
						
							|  |  |  |             // (which is always the end of the month) but that is caught by an event.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if ($amount > 0) { | 
					
						
							|  |  |  |                 $limit->amount = $amount; | 
					
						
							|  |  |  |                 $limit->save(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $limit->delete(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $limit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |