diff --git a/app/Http/Controllers/BookingsController.php b/app/Http/Controllers/BookingsController.php index d3b2e14..5fcf290 100644 --- a/app/Http/Controllers/BookingsController.php +++ b/app/Http/Controllers/BookingsController.php @@ -4,6 +4,10 @@ namespace App\Http\Controllers; use App\Booking; use App\SubBooking; +use App\PersonTarget; +use App\Project; +use App\AccountType; + use Illuminate\Http\Request; use Carbon\Carbon; @@ -16,25 +20,27 @@ class BookingsController extends Controller */ public function index($year) { - $bookings = Booking::with('subBookings.accountType.accountClass.ioType', - 'subBookings.project') - ->whereYear('date', '=', $year) - ->orderBy('date') - ->get() - ->groupBy(function($item) { - return Carbon::parse($item->date)->format('n'); - }); + $bookings = Booking::with( + 'subBookings.accountType.accountClass.ioType', + 'subBookings.project' + ) + ->whereYear('date', '=', $year) + ->orderBy('date') + ->get() + ->groupBy( + function ($item) { + return Carbon::parse($item->date)->format('n'); + } + ); $sums = array(); $startBalance = SubBooking::balanceAt($year - 1 . '-12-31'); $sums[0]['balance'] = $startBalance; - $expeditures = SubBooking::expedituresInInterval( - $year . '-01-01', $year . '-12-31', true); + $expeditures = SubBooking::expedituresInInterval($year . '-01-01', $year . '-12-31', true); - $incomes = SubBooking::incomesInInterval( - $year . '-01-01', $year . '-12-31', true); + $incomes = SubBooking::incomesInInterval($year . '-01-01', $year . '-12-31', true); // Create an array with the following properties: // 'balance' - The account balance at the last day of a month @@ -43,11 +49,11 @@ class BookingsController extends Controller // balance of the previous month for ($month = 1; $month <= 12; $month++) { $sums[$month]['expeditures'] = $expeditures->where('month', '=', $month)->first()->expeditures; - $sums[$month]['incomes'] = $expeditures->where('month', '<', $month)->sum('expeditures') + - $incomes->where('month', '<=', $month)->sum('incomes') + - $startBalance; - $sums[$month]['balance'] = $sums[$month]['expeditures'] + - $sums[$month]['incomes']; + $sums[$month]['incomes'] = $expeditures->where('month', '<', $month)->sum('expeditures') + + $incomes->where('month', '<=', $month)->sum('incomes') + + $startBalance; + $sums[$month]['balance'] = $sums[$month]['expeditures'] + + $sums[$month]['incomes']; } return view('bookings.index', compact('bookings', 'year', 'sums')); @@ -60,24 +66,112 @@ class BookingsController extends Controller */ public function create() { - // + // Get all open targets from the database ... + $openTargets = PersonTarget::open() + ->load('person:id,name_or_company,surname,title') + ->sortBy('reference') + ->sortBy('person.name_or_company') + ->groupBy('person.id'); + + // ... and create a list of the open targets to show it in the form as + // dropdown menu. + $openTargetsArray = []; + + foreach ($openTargets as $personTargets) { + foreach ($personTargets as $personTarget) { + $person = $personTarget->person; + + $openTargetsArray[$person->fullName(true)][$personTarget->id] + = $personTarget->reference; + } + } + + // Get all projects from the database ... + $projects = Project::select('id', 'name', 'start_date', 'end_date') + ->orderBy('name') + ->get(); + + // ... and create a list of the projects to show it in the form as + // dropdown menu. + $projectsArray = []; + + foreach ($projects as $project) { + $timeOfProject = ''; + + if ($project->start_date <= today() and $project->end_date >= today()) { + $timeOfProject = 'Laufende Projekte'; + } elseif ($project->start_date > today()) { + $timeOfProject = 'Zukünftige Projekte'; + } else { + $timeOfProject = 'Vergangene Projekte'; + } + + $projectsArray['Vergangene Projekte'][$project->id] = $project->name; + } + + // Get all account types from the database, together with their account + // class and scope ... + $accountTypes = AccountType::with( + [ + 'accountClass' => function ($query) { + $query->select('id', 'name', 'scope_id'); + }, + 'accountClass.scope' => function ($query) { + $query->select('id', 'name'); + } + ] + )->orderBy('number')->get(['id', 'name', 'account_class_id', 'number']); + + // ... and create a list of the account types to show it in the form as + // dropdown menu. + $accountTypeArray = []; + + foreach ($accountTypes as $accountType) { + $scope = $accountType->accountClass->scope; + $accountClass = $accountType->accountClass; + + $accountTypeArray[$scope->name] + [$accountClass->name] + [$accountType->id] = $accountType->numberAndName(); + } + + return view('bookings.create', compact('projectsArray', 'openTargetsArray', 'accountTypeArray')); } /** * Store a newly created resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { - // + $booking = new Booking; + + $booking->date = request('date'); + $booking->reference = request('reference'); + $booking->person_or_company = request('person_or_company'); + $booking->note = request('note'); + + $booking->save(); + + $subBooking = new SubBooking; + + $subBooking->reference = request('reference'); + $subBooking->amount = swapComma(request('amount')); + $subBooking->account_type_id = request('account_type_id'); + $subBooking->person_target_id = request('person_target_id'); + $subBooking->project_id = request('project_id'); + + $booking->subBookings()->save($subBooking); + + return redirect('/bookings/year-' . Carbon::parse(request('date'))->format('Y')); } /** * Display the specified resource. * - * @param \App\Booking $booking + * @param \App\Booking $booking * @return \Illuminate\Http\Response */ public function show(Booking $booking) @@ -88,7 +182,7 @@ class BookingsController extends Controller /** * Show the form for editing the specified resource. * - * @param \App\Booking $booking + * @param \App\Booking $booking * @return \Illuminate\Http\Response */ public function edit(Booking $booking) @@ -99,8 +193,8 @@ class BookingsController extends Controller /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request - * @param \App\Booking $booking + * @param \Illuminate\Http\Request $request + * @param \App\Booking $booking * @return \Illuminate\Http\Response */ public function update(Request $request, Booking $booking) @@ -111,7 +205,7 @@ class BookingsController extends Controller /** * Show the dialog for deleting the specified resource. * - * @param \App\Booking $booking + * @param \App\Booking $booking * @return \Illuminate\Http\Response */ public function delete(Booking $booking) @@ -122,7 +216,7 @@ class BookingsController extends Controller /** * Remove the specified resource from storage. * - * @param \App\Booking $booking + * @param \App\Booking $booking * @return \Illuminate\Http\Response */ public function destroy(Booking $booking)