From 20859fe4a9d95b2071b5f52c00ebebc940b33c52 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Thu, 4 Oct 2018 22:59:41 +0200 Subject: [PATCH] Transferred the loading of the arrays for the booking forms into a own separate method. Impemented the booking.edit view and controller method. --- app/Http/Controllers/BookingsController.php | 216 +++++++++++--------- 1 file changed, 120 insertions(+), 96 deletions(-) diff --git a/app/Http/Controllers/BookingsController.php b/app/Http/Controllers/BookingsController.php index 5fcf290..ac249ab 100644 --- a/app/Http/Controllers/BookingsController.php +++ b/app/Http/Controllers/BookingsController.php @@ -20,18 +20,14 @@ class BookingsController extends Controller */ public function index($year) { - $bookings = Booking::with( - 'subBookings.accountType.accountClass.ioType', - 'subBookings.project' - ) + $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'); - } - ); + ->groupBy( function ($item) { + return Carbon::parse($item->date)->format('n'); + }); $sums = array(); @@ -65,6 +61,120 @@ class BookingsController extends Controller * @return \Illuminate\Http\Response */ public function create() + { + return view('bookings.create', BookingsController::loadFormArrays()); + } + + /** + * Store a newly created resource in storage. + * + * @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 + * @return \Illuminate\Http\Response + */ + public function show(Booking $booking) + { + // + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Booking $booking + * @return \Illuminate\Http\Response + */ + public function edit(Booking $booking) + { + $booking->load('subBookings', + 'subBookings.personTarget:id,reference,person_id', + 'subBookings.personTarget.person:id,name_or_company,surname'); + + $formArrays = BookingsController::loadFormArrays(); + + // Add the person targets of the booking to the personTargetsArray, + // 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; + + $formArrays['openTargetsArray'][$person->fullName(true)][$personTarget->id] + = $personTarget->reference; + } + + return view('bookings.edit', array_merge(compact('booking'), $formArrays)); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Booking $booking + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Booking $booking) + { + return redirect('/bookings/year-' . Carbon::parse(request('date'))->format('Y')); + } + + /** + * Show the dialog for deleting the specified resource. + * + * @param \App\Booking $booking + * @return \Illuminate\Http\Response + */ + public function delete(Booking $booking) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Booking $booking + * @return \Illuminate\Http\Response + */ + public function destroy(Booking $booking) + { + // + } + + /** + * Load the data about person targets, projects and account types from the + * database to show tis data in a form as dropdown menu. + * + * @return array + */ + private function loadFormArrays() { // Get all open targets from the database ... $openTargets = PersonTarget::open() @@ -135,92 +245,6 @@ class BookingsController extends Controller [$accountType->id] = $accountType->numberAndName(); } - return view('bookings.create', compact('projectsArray', 'openTargetsArray', 'accountTypeArray')); - } - - /** - * Store a newly created resource in storage. - * - * @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 - * @return \Illuminate\Http\Response - */ - public function show(Booking $booking) - { - // - } - - /** - * Show the form for editing the specified resource. - * - * @param \App\Booking $booking - * @return \Illuminate\Http\Response - */ - public function edit(Booking $booking) - { - // - } - - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param \App\Booking $booking - * @return \Illuminate\Http\Response - */ - public function update(Request $request, Booking $booking) - { - // - } - - /** - * Show the dialog for deleting the specified resource. - * - * @param \App\Booking $booking - * @return \Illuminate\Http\Response - */ - public function delete(Booking $booking) - { - // - } - - /** - * Remove the specified resource from storage. - * - * @param \App\Booking $booking - * @return \Illuminate\Http\Response - */ - public function destroy(Booking $booking) - { - // + return compact('openTargetsArray', 'projectsArray', 'accountTypeArray'); } }