id; $projectBookings = SubBooking::select('id', 'account_type_id', 'reference', 'amount', 'booking_id') ->whereHas('project', function($query) use ($id) { return $query->whereId($id); }) ->with('accountType:id,name,account_class_id', 'accountType.accountClass:id,io_type_id,name,scope_id', 'accountType.accountClass.scope:id,name', 'accountType.accountClass.ioType:id,name,factor', 'booking:id,date,person_or_company') ->get() ->sortBy('booking.date'); $incomes = $projectBookings ->where('accountType.accountClass.ioType.factor', '>', 0) ->sum('amount'); $expeditures = $projectBookings ->where('accountType.accountClass.ioType.factor', '<', 0) ->sum('amount'); return view('reports.project', compact('project', 'projectBookings', 'incomes', 'expeditures')); } /** * Display the monthly balances during a year. * * @return \Illuminate\Http\Response */ public function balances($year) { # Load monthly incomes, expeditures and the start account balance from # the database. $incomes = SubBooking::incomesInInterval(firstDayOfYear($year), lastDayOfYear($year), true)->sortBy('month'); $expeditures = SubBooking::expedituresInInterval(firstDayOfYear($year), lastDayOfYear($year), true)->sortBy('month'); $startBalance = SubBooking::balanceAt(lastDayOfYear($year - 1)); # If the data contains future months remove the data of all incomplete # months. if (date('Y') <= $year) { foreach ([$incomes, $expeditures] as $cashflows) { foreach ($cashflows as $key => $cashflow) { if ($cashflow->month >= date('n')) { $cashflows->forget($key); } } } } # Create a collection of balances, every entry holds information about # one month. $balances = collect(); for ($i = 0; $i < count($incomes); $i++) { $balance = collect(); $balance->incomes = $incomes[$i]->incomes; $balance->expeditures = $expeditures[$i]->expeditures; $balance->monthBalance = $incomes[$i]->incomes + $expeditures[$i]->expeditures; # Set date separately if the next month is in the new year. if ($incomes[$i]->month == 12) { $balance->date = firstDayOfYear($incomes[$i]->year + 1); } else { $balance->date = startOfMonth($incomes[$i]->year, $incomes[$i]->month + 1); } # Set the balance of the first month separately since the value # depends on the startBalance value and not on a value already # existing in the balances collection. if ($incomes[$i]->month == 1) { $balance->accountBalance = $startBalance + $balance->monthBalance; } else { $balance->accountBalance = $balances[$i - 1]->accountBalance + $balance->monthBalance; } # Add complete balance to the balances collection $balances->push($balance); } return view('reports.balances', compact('year', 'balances', 'startBalance')); } /** * Display the bookings sorted by scopes. * * @return \Illuminate\Http\Response */ public function scopes($type, $year) { if ($type == 'compact') { $yearFilter = function($query) use ($year) { $query->whereHas('booking', function($query) use ($year) { $query->whereYear('date', $year); }); }; $scopes = Scope::select('id', 'name') ->with([ 'accountClasses:id,io_type_id,scope_id,name', 'accountClasses.ioType:id,factor', 'accountClasses.accountTypes' => function($query) use ($year, $yearFilter) { return $query->select('id', 'account_class_id', 'name', 'number') ->whereHas('subBookings', $yearFilter) ->with(['subBookings' => $yearFilter]); } ])->get(); foreach ($scopes as $scope) { foreach ($scope->accountClasses as $accountClass) { $accountClass->amount = $accountClass->accountTypes->sum(function ($accountType) { return $accountType->subBookings->sum('amount'); }); } $scope->balance = $scope->accountClasses->sum(function ($accountClass) { return $accountClass->amount * $accountClass->ioType->factor; }); } $startBalance = SubBooking::balanceAt(lastDayOfYear($year - 1)); $endBalance = $startBalance + $scopes->sum('balance'); return view('reports.scopes.compact', compact('scopes', 'year', 'startBalance', 'endBalance')); } elseif ($type == 'detailed') { return view('reports.scopes.detailed'); } else { abort(404); } } /** * Display a list of all open targets. * * @return \Illuminate\Http\Response */ public function targets() { // } }