OnlineController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 8 1
A setBoxOfficeSales() 0 38 4
A switchTicketState() 0 11 2
1
<?php
2
3
namespace App\Http\Controllers\BoxOffice;
4
5
use App\Event;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\SetBoxOfficeSales;
8
use App\Purchase;
9
use App\Ticket;
10
use Illuminate\Support\Str;
11
12
class OnlineController extends Controller
13
{
14
    public function index(Event $event)
15
    {
16
        return view('boxoffice.online', [
17
            'event' => $event,
18
            'tickets' => $event->tickets,
19
            'occupancy' => $event->getOccupancy(),
20
            'freeTickets' => $event->freeTickets(),
21
            'turnover' => $event->getTurnover()
22
        ]);
23
    }
24
25
    public function switchTicketState(Ticket $ticket)
26
    {
27
        if($ticket->state == 'no_show') {
28
            $ticket->state = 'consumed';
29
        } else {
30
            $ticket->state = 'no_show';
31
        }
32
        $ticket->save();
33
        
34
        return response()->json([
35
            'state' => $ticket->state
36
        ]);
37
    }
38
39
    /**
40
     * This function sets the box office sales for an event. The sales are stored as a normal purchase
41
     * that is directly linked to this event object. Only this function should modify this purchase!
42
     * 
43
     * @param Event $event Is set by the route.
44
     * @param SetBoxOfficeSales $request post parameters containing the new amount of sold tickets per category
45
     */
46
    public function setBoxOfficeSales(Event $event, SetBoxOfficeSales $request)
47
    {
48
        // On the first call of this function the event has not an associated boxoffice purchase.
49
        // So we must create one.
50
        if(!$event->boxoffice) {
51
            $purchase = new Purchase();
52
            $purchase->generateSecrets();
53
            $purchase->customer_name = 'Box Office';
54
            $purchase->state = 'paid';
55
            $purchase->save();
56
            $event->boxoffice_id = $purchase->id;
57
            $event->save();
58
            $event->refresh();
59
        } else { // else fetch the existing boxoffice purchase
60
            $purchase = $event->boxoffice;
61
        }
62
63
        // For tracing: set always the latest box office submitter as responsible vendor
64
        $purchase->vendor_id = $request->user()->id;
65
        $purchase->save();
66
67
        // Delete all previously set tickets, because we recreate them by the new given set of categories
68
        Ticket::where('purchase_id', $purchase->id)->delete();
69
70
        // Freshly create the tickets sent for the box office
71
        foreach($request->tickets as $categoryId => $count) {
72
            for ($i = 0; $i < $count; $i++) {
73
                Ticket::create([
74
                    'random_id' => Str::random(32),
75
                    'seat_number' => 0, // Since it is the box office, they are responsible to sell only still available seats
76
                    'purchase_id' => $purchase->id,
77
                    'event_id' => $event->id,
78
                    'price_category_id' => $categoryId
79
                ]);
80
            }
81
        }
82
83
        return redirect()->route('boxoffice.online', $event);
84
    }
85
}
86