EventController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 108
c 1
b 0
f 0
dl 0
loc 169
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testTicket() 0 54 4
A create() 0 16 1
A update() 0 15 1
A showCreate() 0 15 1
A delete() 0 10 2
A get() 0 16 1
A index() 0 22 4
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Event;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Admin\CreateUpdateEvent;
8
use App\Location;
9
use App\PriceCategory;
10
use App\PriceList;
11
use App\Project;
12
use App\Purchase;
13
use App\SeatMap;
14
use App\Ticket;
15
use Illuminate\Database\Eloquent\Builder;
16
use Illuminate\Support\Facades\Auth;
17
use Illuminate\Support\Facades\DB;
18
use Illuminate\Support\Facades\Request;
19
use Illuminate\Support\Str;
20
use Spipu\Html2Pdf\Exception\Html2PdfException;
21
use Spipu\Html2Pdf\Html2Pdf;
22
23
class EventController extends Controller
24
{
25
    public function index()
26
    {
27
        $projects = Project::where('is_archived', false)->get();
28
        $archive = Project::where('is_archived', true)->get();
29
        foreach( $archive as $project )
30
        {
31
            // Set the time range of the project by selecting the last and first dates of its events
32
            // if no events are associated with the project, set the dates to '-'
33
            $firstEvent = $project->events()->orderBy('start_date', 'ASC')->first();
34
            $project->start_date = $firstEvent === null ? '-' : $firstEvent->start_date;
35
            $lastEvent = $project->events()->orderBy('end_date', 'DESC')->first();
36
            $project->end_date = $lastEvent === null ? '-' : $lastEvent->end_date;
37
        }
38
39
        $events = Event::whereHas('project', function(Builder $query) {
40
            $query->where('is_archived', false);
41
        })->get();
42
43
        return view('admin.events.index', [
44
            'projects' => $projects,
45
            'events'   => $events,
46
            'archived' => $archive
47
        ]);
48
    }
49
50
    public function showCreate()
51
    {
52
        $projects = Project::where('is_archived', false)->orderBy('name', 'ASC')->get();
53
        $archive = Project::where('is_archived', true)->orderBy('name', 'ASC')->get();
54
        $locations = Location::orderBy('name', 'ASC')->get();
55
        $seatMaps = SeatMap::orderBy('name', 'ASC')->get();
56
        $priceLists = PriceList::orderBy('name', 'ASC')->get();
57
58
        return view('admin.events.manage-event', [
59
            'create' => true,
60
            'projects' => $projects,
61
            'archive' => $archive,
62
            'locations' => $locations,
63
            'seatmaps' => $seatMaps,
64
            'pricelists' => $priceLists
65
        ]);
66
    }
67
68
    public function create(CreateUpdateEvent $request)
69
    {
70
        $event = new Event();
71
        $event->project_id = $request->project;
72
        $event->second_name = $request->name;
73
        $event->start_date = $request->start;
74
        $event->end_date = $request->end;
75
        $event->retailer_sell_stop = $request->retailer_sell_stop;
76
        $event->customer_sell_stop = $request->customer_sell_stop;
77
        $event->location_id = $request->location;
78
        $event->seat_map_id = $request->seatmap;
79
        $event->price_list_id = $request->pricelist;
80
        $event->save();
81
82
        return redirect()->route('admin.events.get', $event)
83
                ->with('status', 'Updated Event successfully!');
84
    }
85
86
    public function get(Event $event)
87
    {
88
        $projects = Project::where('is_archived', false)->orderBy('name', 'ASC')->get();
89
        $archive = Project::where('is_archived', true)->orderBy('name', 'ASC')->get();
90
        $locations = Location::orderBy('name', 'ASC')->get();
91
        $seatMaps = SeatMap::orderBy('name', 'ASC')->get();
92
        $priceLists = PriceList::orderBy('name', 'ASC')->get();
93
94
        return view('admin.events.manage-event', [
95
            'create' => false,
96
            'event' => $event,
97
            'projects' => $projects,
98
            'archive' => $archive,
99
            'locations' => $locations,
100
            'seatmaps' => $seatMaps,
101
            'pricelists' => $priceLists
102
        ]);
103
    }
104
105
    public function update(Event $event, CreateUpdateEvent $request)
106
    {
107
        $event->project_id = $request->project;
108
        $event->second_name = $request->name;
109
        $event->start_date = $request->start;
110
        $event->end_date = $request->end;
111
        $event->retailer_sell_stop = $request->retailer_sell_stop;
112
        $event->customer_sell_stop = $request->customer_sell_stop;
113
        $event->location_id = $request->location;
114
        $event->seat_map_id = $request->seatmap;
115
        $event->price_list_id = $request->pricelist;
116
        $event->save();
117
118
        return redirect()->route('admin.events.get', $event)
119
                ->with('status', 'Updated Event successfully!');
120
    }
121
122
    public function delete(Event $event)
123
    {
124
        if($event->tickets()->exists())
125
        {
126
            return redirect()->route('admin.events.get', $event)
127
                    ->with('status', 'Error on deletion: Event has tickets!');
128
        }
129
        $event->delete();
130
        return redirect()->route('admin.events.dashboard')
131
                        ->with('status', 'Deleted Event successfully!');
132
    }
133
134
    /**
135
     * Returns a ticket filled with dummy data to check the
136
     * correct processing of the logo in the layout
137
     */
138
    public function testTicket(Event $event, Request $request)
139
    {
140
        // Wrap dummy data creation in a transaction in order
141
        // to not actually store it in the production database.
142
        //
143
        // We have to use eloquent models and cannot use factories,
144
        // because factories are not available on prod installations.
145
        DB::beginTransaction();
146
        $now = now();
147
148
        $purchase = new Purchase();
149
        $purchase->state = 'paid';
150
        $purchase->state_updated = $now;
151
        $purchase->random_id = Str::random(20);
152
        $purchase->payment_secret = Str::random(20);
153
        $purchase->customer_id = Auth::user()->id;
154
        $purchase->vendor_id = Auth::user()->id;
155
        $purchase->payment_id = 'dummy-reference';
156
        $purchase->save();
157
158
        $backupPriceCategory = PriceCategory::create([
159
            'name'        => 'StandardPrice',
160
            'price'       => 40,
161
            'description' => 'Default Standard pricing'
162
        ]);
163
        
164
        for($i = 0; $i < 8; $i++) {
165
            $ticket = new Ticket();
166
            $ticket->random_id         = Str::random(20);
167
            $ticket->seat_number       = $i + 1000;
168
            $ticket->event_id          = $event->id;
169
            $ticket->purchase_id       = $purchase->id;
170
            // If the event already has a pricelist attached use the first item of it. Else use the backup price category
171
            $ticket->price_category_id = $event->priceList->categories ? $event->priceList->categories[0]->id : $backupPriceCategory->id;
172
            $ticket->state             = 'consumed';
173
            $ticket->save();
174
        }
175
        try {
176
            $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', 0);
177
            $html2pdf->pdf->SetDisplayMode('fullpage');
178
            $html2pdf->pdf->SetAuthor(config('app.name'));
179
            $html2pdf->pdf->SetTitle('Purchase #' . $purchase->id);
180
181
            // Generate pdf-content by passing the tickets to the view
182
            $content = view('pdfs.ticket-v2', ['tickets' => $purchase->tickets])->render();
183
            $html2pdf->writeHTML($content);
184
185
            $html2pdf->output('tickets-' . $purchase->id . '.pdf');
186
        } catch (Html2PdfException $e) {
187
            $html2pdf->clean();
188
            DB::rollBack();
189
            return redirect()->route('ticket.purchase', ['purchase' => $purchase])->with('state', $e->getMessage());
190
        }
191
        DB::rollBack();
192
    }
193
}
194