Files
pollux/app/Http/Controllers/BookingsController.php
T

314 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Booking;
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;
class BookingsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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');
});
$sums = array();
$startBalance = SubBooking::balanceAt($year - 1 . '-12-31');
$sums[0]['balance'] = $startBalance;
$expeditures = SubBooking::expedituresInInterval($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
// 'expeditures' - the sum of all expeditures in this month
// 'incomes' - the sum of all incomes in this month plus the account
// balance of the previous month
for ($month = 1; $month <= 12; $month++) {
$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'));
}
/**
* Show the form for creating a new resource.
*
* @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(StoreBooking $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) {
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));
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateBooking $request
* @param \App\Booking $booking
* @return \Illuminate\Http\Response
*/
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'));
}
/**
* Show the dialog for deleting the specified resource.
*
* @param \App\Booking $booking
* @return \Illuminate\Http\Response
*/
public function delete(Booking $booking)
{
$booking->load('subBookings.accountType.accountClass.ioType');
return view('/bookings/delete', compact('booking'));
}
/**
* Remove the specified resource from storage.
*
* @param \App\Booking $booking
* @return \Illuminate\Http\Response
*/
public function destroy(Booking $booking)
{
$booking->delete();
return redirect('/bookings/year-' . Carbon::parse(request('date'))->format('Y'));
}
/**
* 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()
->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 compact('openTargetsArray', 'projectsArray', 'accountTypeArray');
}
}