Implemented the create method of the BookingsController and fixed some code

style issues.
This commit is contained in:
Lukas Halbritter
2018-10-04 20:30:20 +02:00
parent 9032793597
commit bfe50dec65
+120 -26
View File
@@ -4,6 +4,10 @@ namespace App\Http\Controllers;
use App\Booking; use App\Booking;
use App\SubBooking; use App\SubBooking;
use App\PersonTarget;
use App\Project;
use App\AccountType;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Carbon\Carbon; use Carbon\Carbon;
@@ -16,25 +20,27 @@ class BookingsController extends Controller
*/ */
public function index($year) public function index($year)
{ {
$bookings = Booking::with('subBookings.accountType.accountClass.ioType', $bookings = Booking::with(
'subBookings.project') 'subBookings.accountType.accountClass.ioType',
->whereYear('date', '=', $year) 'subBookings.project'
->orderBy('date') )
->get() ->whereYear('date', '=', $year)
->groupBy(function($item) { ->orderBy('date')
return Carbon::parse($item->date)->format('n'); ->get()
}); ->groupBy(
function ($item) {
return Carbon::parse($item->date)->format('n');
}
);
$sums = array(); $sums = array();
$startBalance = SubBooking::balanceAt($year - 1 . '-12-31'); $startBalance = SubBooking::balanceAt($year - 1 . '-12-31');
$sums[0]['balance'] = $startBalance; $sums[0]['balance'] = $startBalance;
$expeditures = SubBooking::expedituresInInterval( $expeditures = SubBooking::expedituresInInterval($year . '-01-01', $year . '-12-31', true);
$year . '-01-01', $year . '-12-31', true);
$incomes = SubBooking::incomesInInterval( $incomes = SubBooking::incomesInInterval($year . '-01-01', $year . '-12-31', true);
$year . '-01-01', $year . '-12-31', true);
// Create an array with the following properties: // Create an array with the following properties:
// 'balance' - The account balance at the last day of a month // 'balance' - The account balance at the last day of a month
@@ -43,11 +49,11 @@ class BookingsController extends Controller
// balance of the previous month // balance of the previous month
for ($month = 1; $month <= 12; $month++) { for ($month = 1; $month <= 12; $month++) {
$sums[$month]['expeditures'] = $expeditures->where('month', '=', $month)->first()->expeditures; $sums[$month]['expeditures'] = $expeditures->where('month', '=', $month)->first()->expeditures;
$sums[$month]['incomes'] = $expeditures->where('month', '<', $month)->sum('expeditures') + $sums[$month]['incomes'] = $expeditures->where('month', '<', $month)->sum('expeditures')
$incomes->where('month', '<=', $month)->sum('incomes') + + $incomes->where('month', '<=', $month)->sum('incomes')
$startBalance; + $startBalance;
$sums[$month]['balance'] = $sums[$month]['expeditures'] + $sums[$month]['balance'] = $sums[$month]['expeditures']
$sums[$month]['incomes']; + $sums[$month]['incomes'];
} }
return view('bookings.index', compact('bookings', 'year', 'sums')); return view('bookings.index', compact('bookings', 'year', 'sums'));
@@ -60,24 +66,112 @@ class BookingsController extends Controller
*/ */
public function create() 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. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function store(Request $request) 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. * Display the specified resource.
* *
* @param \App\Booking $booking * @param \App\Booking $booking
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function show(Booking $booking) public function show(Booking $booking)
@@ -88,7 +182,7 @@ class BookingsController extends Controller
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\Booking $booking * @param \App\Booking $booking
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function edit(Booking $booking) public function edit(Booking $booking)
@@ -99,8 +193,8 @@ class BookingsController extends Controller
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \App\Booking $booking * @param \App\Booking $booking
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(Request $request, Booking $booking) public function update(Request $request, Booking $booking)
@@ -111,7 +205,7 @@ class BookingsController extends Controller
/** /**
* Show the dialog for deleting the specified resource. * Show the dialog for deleting the specified resource.
* *
* @param \App\Booking $booking * @param \App\Booking $booking
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function delete(Booking $booking) public function delete(Booking $booking)
@@ -122,7 +216,7 @@ class BookingsController extends Controller
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\Booking $booking * @param \App\Booking $booking
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function destroy(Booking $booking) public function destroy(Booking $booking)