Event::getTurnover()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Event extends Model
8
{
9
    //
10
    protected $fillable = ['start_date', 'end_date', 'second_name'];
11
12
    /**
13
     * Returns true, if no more tickets for this event are available
14
     */
15
    public function isSoldOut()
16
    {
17
        return $this->tickets->count() >= $this->seatMap->seats;
18
    }
19
20
    /**
21
     * Returns number of free tickets of an event
22
     */
23
    public function freeTickets()
24
    {
25
        return $this->seatMap->seats - $this->tickets->count();
26
    }
27
28
    /**
29
     * Returns the ratio of occupied seats
30
     */
31
    public function getOccupancy()
32
    {
33
        return $this->tickets->count() / $this->seatMap->seats;
34
    }
35
36
    /**
37
     * Calculate the sum of all sold tickets
38
     */
39
    public function getTurnover()
40
    {
41
        return $this->tickets->sum(function($ticket) {
42
            return $ticket->price();
43
        });
44
    }
45
46
    /**
47
     * Returns true if the given array of seat ids is still free / not already booked
48
     */
49
    public function areSeatsFree(array $requestedSeats): bool
50
    {
51
        $bookedSeats = $this->tickets()->whereIn('seat_number', $requestedSeats)->get();
52
        return $bookedSeats->isEmpty();
53
    }
54
55
    /**
56
     * Local scopes & relations
57
     */
58
    public function scopeEnded($query)
59
    {
60
        return $query->where('end_date', '<', new \DateTime());
61
    }
62
63
    public function scopeOpen($query)
64
    {
65
        return $query->where('start_date', '>', new \DateTime());
66
    }
67
68
69
70
    public function tickets()
71
    {
72
        return $this->hasMany('App\Ticket');
73
    }
74
75
    public function boxoffice()
76
    {
77
        return $this->belongsTo('App\Purchase', 'boxoffice_id', 'id');
78
    }
79
80
    public function project()
81
    {
82
        return $this->belongsTo('App\Project');
83
    }
84
85
    public function location()
86
    {
87
        return $this->belongsTo('App\Location');
88
    }
89
90
    public function seatMap()
91
    {
92
        return $this->belongsTo('App\SeatMap');
93
    }
94
95
    public function priceList()
96
    {
97
        return $this->belongsTo('App\PriceList');
98
    }
99
}
100