From 75d6568dacb3d3acdd2e2ef4eb03cc1fe4a9b777 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Fri, 5 Oct 2018 17:13:30 +0200 Subject: [PATCH] Implemented bookings update method. Fixed some code stlye issues as well as some issues with referencing null. --- app/Http/Controllers/BookingsController.php | 85 +++++++++++++++++---- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/BookingsController.php b/app/Http/Controllers/BookingsController.php index ac249ab..e445c41 100644 --- a/app/Http/Controllers/BookingsController.php +++ b/app/Http/Controllers/BookingsController.php @@ -7,6 +7,8 @@ use App\SubBooking; use App\PersonTarget; use App\Project; use App\AccountType; +use App\Http\Requests\StoreBooking; +use App\Http\Requests\UpdateBooking; use Illuminate\Http\Request; use Carbon\Carbon; @@ -44,12 +46,17 @@ class BookingsController extends Controller // 'incomes' - the sum of all incomes in this month plus the account // 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']; + $currExpeditures = $expeditures->where('month', '=', $month)->first()->expeditures; + + $currIncomes = $expeditures->where('month', '<', $month)->sum('expeditures') + + $incomes->where('month', '<=', $month)->sum('incomes') + + $startBalance; + + $currBalance = $currExpeditures + $currIncomes; + + $sums[$month]['expeditures'] = $currExpeditures; + $sums[$month]['incomes'] = $currIncomes; + $sums[$month]['balance'] = $currBalance; } return view('bookings.index', compact('bookings', 'year', 'sums')); @@ -71,7 +78,7 @@ class BookingsController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(StoreBooking $request) { $booking = new Booking; @@ -124,11 +131,13 @@ class BookingsController extends Controller // since it is possible that the person target of the sub-booking is no // longer open and therefor not loaded into the list of open targets. foreach ($booking->subBookings as $subBooking) { - $personTarget = $subBooking->personTarget; - $person = $personTarget->person; + if (isset($subBooking->personTarget)) { + $personTarget = $subBooking->personTarget; + $person = $personTarget->person; - $formArrays['openTargetsArray'][$person->fullName(true)][$personTarget->id] - = $personTarget->reference; + $formArrays['openTargetsArray'][$person->fullName(true)][$personTarget->id] + = $personTarget->reference; + } } return view('bookings.edit', array_merge(compact('booking'), $formArrays)); @@ -137,12 +146,62 @@ class BookingsController extends Controller /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\UpdateBooking $request * @param \App\Booking $booking * @return \Illuminate\Http\Response */ - public function update(Request $request, Booking $booking) + public function update(UpdateBooking $request, Booking $booking) { + // Fill the booking with the edited values + foreach (request('booking') as $colName => $value) { + $booking->$colName = $value; + } + + // Safe the edited booking in the database. + $booking->save(); + + // Check, if all sub-booking rows are empty. If so, redirect to the + // delete view of the booking. + $noMoreSubBookings = true; + + foreach (request('sub_booking') as $rSubBooking) { + if (!allEmpty($rSubBooking)) { + $noMoreSubBookings = false; + break; + } + } + + if ($noMoreSubBookings) { + return redirect('/bookings/' . $booking->id . '/delete'); + } + + // Fill a existent or new SubBooking with the values from the form and + // safe it in the database. + foreach (request('sub_booking') as $id => $rSubBooking) { + // If a sub-booking row is completely empty, check whether there was + // a sub-booking and all data was removed. In this case, remove the + // respective SubBooking from the database. + // If the line is the blank line for new entries, skip it. + if (allEmpty($rSubBooking)) { + if ($id != 0) { + SubBooking::find($id)->delete(); + } + + continue; + } + + // Create new SubBooking or load an existing one. + $subBooking = $id === 0 ? + new SubBooking() : + SubBooking::find($id); + + foreach ($rSubBooking as $colName => $value) { + $subBooking->$colName = $value; + } + + $booking->subBookings()->save($subBooking); + } + return redirect('/bookings/year-' . Carbon::parse(request('date'))->format('Y')); }