Added print version of the cashbook.

This commit is contained in:
Lukas Halbritter
2019-07-02 12:18:24 +02:00
parent a6ec45f685
commit 1bd4ffa0f7
5 changed files with 183 additions and 55 deletions
+54 -42
View File
@@ -24,48 +24,15 @@ class BookingsController extends Controller
*/
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');
});
$cashbook = $this->getCashbook($year);
$sums = array();
$startBalance = SubBooking::balanceAt($year - 1 . '-12-31');
$sums[0]['balance'] = $startBalance;
return view('bookings.index', $cashbook);
}
$expeditures = SubBooking::expedituresInInterval($year . '-01-01', $year . '-12-31', true);
$incomes = SubBooking::incomesInInterval($year . '-01-01', $year . '-12-31', true);
public function report($year) {
$cashbook = $this->getCashbook($year);
// 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;
}
$firstDateString = Carbon::parse(Booking::firstDate());
$firstDate['Y'] = $firstDateString->format('Y');
$firstDate['m'] = $firstDateString->format('m');
return view('bookings.index', compact('bookings', 'year', 'sums', 'firstDate'));
return view('bookings.print', $cashbook);
}
/**
@@ -285,7 +252,7 @@ class BookingsController extends Controller
// ... 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;
@@ -306,7 +273,7 @@ class BookingsController extends Controller
foreach ($projects as $project) {
$timeOfProject = '';
if ($project->start_date <= today() and $project->end_date >= today()) {
$timeOfProject = 'Laufende Projekte';
} elseif ($project->start_date > today()) {
@@ -314,7 +281,7 @@ class BookingsController extends Controller
} else {
$timeOfProject = 'Vergangene Projekte';
}
$projectsArray['Vergangene Projekte'][$project->id] = $project->name;
}
@@ -346,4 +313,49 @@ class BookingsController extends Controller
return compact('openTargetsArray', 'projectsArray', 'accountTypeArray');
}
private function getCashbook($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;
}
$firstDateString = Carbon::parse(Booking::firstDate());
$firstDate['Y'] = $firstDateString->format('Y');
$firstDate['m'] = $firstDateString->format('m');
return compact('bookings', 'year', 'sums', 'firstDate');
}
}