Implemented bookings update method.

Fixed some code stlye issues as well as some issues with referencing null.
This commit is contained in:
Lukas Halbritter
2018-10-05 17:13:30 +02:00
parent cde98630a1
commit 75d6568dac
+66 -7
View File
@@ -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')
$currExpeditures = $expeditures->where('month', '=', $month)->first()->expeditures;
$currIncomes = $expeditures->where('month', '<', $month)->sum('expeditures')
+ $incomes->where('month', '<=', $month)->sum('incomes')
+ $startBalance;
$sums[$month]['balance'] = $sums[$month]['expeditures']
+ $sums[$month]['incomes'];
$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,12 +131,14 @@ 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) {
if (isset($subBooking->personTarget)) {
$personTarget = $subBooking->personTarget;
$person = $personTarget->person;
$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'));
}