DashboardController::getOverview()   A
last analyzed

Complexity

Conditions 2
Paths 8

Size

Total Lines 48
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 41
nc 8
nop 2
dl 0
loc 48
rs 9.264
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\BoxOffice;
4
5
use App\Http\Controllers\Controller;
6
use App\Event;
7
use App\Project;
8
use Illuminate\Support\Facades\DB;
9
use Spipu\Html2Pdf\Html2Pdf;
10
use Spipu\Html2Pdf\Exception\Html2PdfException;
11
12
class DashboardController extends Controller
13
{
14
    public function dashboard()
15
    {
16
        $activeProjectIds = Project::where('is_archived', 0)->pluck('id');
17
        $events = Event::whereIn('project_id', $activeProjectIds)->where('state', 'open')->orderBy('start_date', 'ASC')->get();
18
19
        return view('boxoffice.dashboard', [
20
            'events' => $events
21
        ]);
22
    }
23
24
    public function downloadOverview(Event $event)
25
    {
26
        return $this->getOverview($event, 'pdfs.event-by-vendor');
27
    }
28
29
    public function downloadOverviewById(Event $event)
30
    {
31
        return $this->getOverview($event, 'pdfs.event-by-id');
32
    }
33
34
    private function getOverview(Event $event, string $view)
35
    {
36
        $tickets = $event->tickets()->orderBy('tickets.id', 'ASC')->get();
37
        $overview = DB::table('tickets')
38
            ->join('purchases', 'tickets.purchase_id', '=', 'purchases.id')
39
            ->join('users', 'purchases.vendor_id', '=', 'users.id')
40
            ->join('price_categories', 'price_categories.id', '=', 'tickets.price_category_id')
41
            ->where('tickets.event_id', $event->id)
42
            ->groupBy('users.name', 'price_categories.name', 'price_categories.price')
43
            ->select(DB::raw(
44
                "users.name AS 'vendor',
45
                price_categories.name AS 'category',
46
                price_categories.price AS 'price',
47
                COUNT(CASE purchases.state when 'paid' then 1 else null end) AS 'paid',
48
                COUNT(CASE purchases.state when 'reserved' then 1 else null end) AS 'reserved',
49
                COUNT(CASE purchases.state when 'free' then 1 else null end) AS 'free',
50
                COUNT(purchases.state) AS 'sum'"
51
            ))
52
            ->get();
53
        $sums = DB::table('tickets')
54
            ->join('purchases', 'tickets.purchase_id', '=', 'purchases.id')
55
            ->where('tickets.event_id', $event->id)
56
            ->select(DB::raw(
57
                "SUM(CASE purchases.state when 'paid' then 1 else null end) AS 'paid',
58
                SUM(CASE purchases.state when 'reserved' then 1 else null end) AS 'reserved',
59
                SUM(CASE purchases.state when 'free' then 1 else null end) AS 'free',
60
                COUNT(purchases.state) AS 'sum'"
61
            ))
62
            ->get();
63
64
        try {
65
            $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', [20, 20, 20, 20]);
66
            $html2pdf->pdf->SetDisplayMode('fullpage');
67
            $html2pdf->pdf->SetAuthor(config('app.name'));
68
            $html2pdf->pdf->SetTitle('Ticket Overview');
69
70
            $content = view($view, [
71
                'event'    => $event,
72
                'tickets'  => $tickets,
73
                'overview' => $overview,
74
                'sums'     => $sums
75
            ])->render();
76
            $html2pdf->writeHTML($content);
77
78
            $html2pdf->output('overview-event-' . $event->id . '.pdf');
79
        } catch (Html2PdfException $exc) {
80
            $html2pdf->clean();
81
            return redirect()->route('boxoffice.dashboard')->with('state', $exc);
82
        }
83
    }
84
}
85