summaryrefslogtreecommitdiff
path: root/src/Controllers/Metrics/Stats.php
blob: 891f8c803aa3346b1c017ec3daf17ff20f746191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

namespace Engelsystem\Controllers\Metrics;

use Engelsystem\Database\Database;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression as QueryExpression;

class Stats
{
    /** @var Database */
    protected $db;

    /**
     * @param Database $db
     */
    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    /**
     * The number of not arrived users
     *
     * @param null $working
     * @return int
     */
    public function arrivedUsers($working = null): int
    {
        $query = $this
            ->getQuery('users')
            ->join('users_state', 'user_id', '=', 'id')
            ->where('arrived', '=', 1);

        if (!is_null($working)) {
            // @codeCoverageIgnoreStart
            $query
                ->leftJoin('UserWorkLog', 'UserWorkLog.user_id', '=', 'users.id')
                ->leftJoin('ShiftEntry', 'ShiftEntry.UID', '=', 'users.id')
                ->groupBy('users.id');

            $query->where(function ($query) use ($working) {
                /** @var QueryBuilder $query */
                if ($working) {
                    $query
                        ->whereNotNull('ShiftEntry.SID')
                        ->orWhereNotNull('UserWorkLog.work_hours');

                    return;
                }
                $query
                    ->whereNull('ShiftEntry.SID')
                    ->whereNull('UserWorkLog.work_hours');
            });
            // @codeCoverageIgnoreEnd
        }

        return $query
            ->count();
    }

    /**
     * The number of not arrived users
     *
     * @return int
     */
    public function newUsers(): int
    {
        return $this
            ->getQuery('users')
            ->join('users_state', 'user_id', '=', 'id')
            ->where('arrived', '=', 0)
            ->count();
    }

    /**
     * The number of currently working users
     *
     * @param null $freeloaded
     * @return int
     * @codeCoverageIgnore
     */
    public function currentlyWorkingUsers($freeloaded = null): int
    {
        $query = $this
            ->getQuery('users')
            ->join('ShiftEntry', 'ShiftEntry.UID', '=', 'users.id')
            ->join('Shifts', 'Shifts.SID', '=', 'ShiftEntry.SID')
            ->where('Shifts.start', '<=', time())
            ->where('Shifts.end', '>', time());

        if (!is_null($freeloaded)) {
            $query->where('ShiftEntry.freeloaded', '=', $freeloaded);
        }

        return $query->count();
    }

    /**
     * The number of worked shifts
     *
     * @param bool|null $done
     * @param bool|null $freeloaded
     * @return int
     * @codeCoverageIgnore
     */
    public function workSeconds($done = null, $freeloaded = null): int
    {
        $query = $this
            ->getQuery('ShiftEntry')
            ->join('Shifts', 'Shifts.SID', '=', 'ShiftEntry.SID');

        if (!is_null($freeloaded)) {
            $query->where('freeloaded', '=', $freeloaded);
        }

        if (!is_null($done)) {
            $query->where('end', ($done == true ? '<' : '>='), time());
        }

        return $query->sum($this->raw('end - start'));
    }

    /**
     * @param string $table
     * @return QueryBuilder
     */
    protected function getQuery(string $table): QueryBuilder
    {
        return $this->db
            ->getConnection()
            ->table($table);
    }

    /**
     * @param mixed $value
     * @return QueryExpression
     * @codeCoverageIgnore
     */
    protected function raw($value)
    {
        return $this->db->getConnection()->raw($value);
    }
}