engelsystem/includes/model/Stats.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

221 lines
8.7 KiB
PHP
Raw Normal View History

2017-12-12 21:57:57 +01:00
<?php
2017-12-25 23:12:52 +01:00
2017-12-12 21:57:57 +01:00
use Engelsystem\Database\Db;
2020-10-27 17:13:46 +01:00
use Engelsystem\ShiftsFilter;
2017-12-12 21:57:57 +01:00
/**
* Returns the number of angels currently working.
2017-12-25 23:12:52 +01:00
*
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return int|string
2017-12-12 21:57:57 +01:00
*/
2020-10-27 17:13:46 +01:00
function stats_currently_working(ShiftsFilter $filter = null)
2017-12-12 21:57:57 +01:00
{
2020-10-27 17:13:46 +01:00
$result = Db::selectOne(
'
SELECT SUM((
SELECT COUNT(*)
FROM `ShiftEntry`
WHERE `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
)) AS `count`
2017-12-12 21:57:57 +01:00
FROM `Shifts`
2020-10-27 17:13:46 +01:00
WHERE (`end` >= UNIX_TIMESTAMP() AND `start` <= UNIX_TIMESTAMP())
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '')
2020-10-27 17:13:46 +01:00
);
2017-12-25 23:12:52 +01:00
2020-10-27 17:13:46 +01:00
return $result['count'] ?: '-';
2017-12-12 21:57:57 +01:00
}
/**
* Return the number of hours still to work.
2017-12-25 23:12:52 +01:00
*
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return int|string
2017-12-12 21:57:57 +01:00
*/
2020-10-27 17:13:46 +01:00
function stats_hours_to_work(ShiftsFilter $filter = null)
2017-12-12 21:57:57 +01:00
{
2020-10-27 17:13:46 +01:00
$result = Db::selectOne(
'
2017-12-25 23:12:52 +01:00
SELECT ROUND(SUM(`count`)) AS `count` FROM (
2020-05-13 18:26:32 +02:00
SELECT
2020-10-27 17:13:46 +01:00
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`' . ($filter ? ' AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2017-12-25 23:12:52 +01:00
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
2020-10-27 17:13:46 +01:00
WHERE `end` >= UNIX_TIMESTAMP()
AND s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-05-13 18:26:32 +02:00
2017-12-14 19:39:47 +01:00
UNION ALL
2020-05-13 18:26:32 +02:00
SELECT
2020-10-27 17:13:46 +01:00
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`' . ($filter ? ' AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2017-12-25 23:12:52 +01:00
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
2020-10-27 17:13:46 +01:00
WHERE `end` >= UNIX_TIMESTAMP()
AND NOT s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2017-12-25 23:12:52 +01:00
) AS `tmp`
2020-10-27 17:13:46 +01:00
'
);
return $result['count'] ?: '-';
2017-12-12 21:57:57 +01:00
}
/**
* Returns the number of needed angels in the next 3 hours
2017-12-25 23:12:52 +01:00
*
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return int|string
2017-12-12 21:57:57 +01:00
*/
2020-10-27 17:13:46 +01:00
function stats_angels_needed_three_hours(ShiftsFilter $filter = null)
2017-12-12 21:57:57 +01:00
{
2020-10-27 17:13:46 +01:00
$in3hours = time() + 3 * 60 * 60;
$result = Db::selectOne('
2017-12-25 23:12:52 +01:00
SELECT SUM(`count`) AS `count` FROM (
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
2020-05-13 18:26:32 +02:00
SELECT SUM(`count`)
FROM `NeededAngelTypes`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2020-05-13 18:26:32 +02:00
SELECT COUNT(*) FROM `ShiftEntry`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2020-05-13 18:26:32 +02:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
2020-10-27 17:13:46 +01:00
WHERE `end` > UNIX_TIMESTAMP() AND `start` < ?
AND s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-05-13 18:26:32 +02:00
2017-12-14 19:39:47 +01:00
UNION ALL
2020-05-13 18:26:32 +02:00
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
2020-05-13 18:26:32 +02:00
SELECT SUM(`count`)
FROM `NeededAngelTypes`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2020-05-13 18:26:32 +02:00
SELECT COUNT(*) FROM `ShiftEntry`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2020-05-13 18:26:32 +02:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
2020-10-27 17:13:46 +01:00
WHERE `end` > UNIX_TIMESTAMP() AND `start` < ?
AND NOT s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-10-27 17:13:46 +01:00
) AS `tmp`', [
$in3hours,
2017-12-12 21:57:57 +01:00
$in3hours
]);
2020-10-27 17:13:46 +01:00
return $result['count'] ?: '-';
2017-12-12 21:57:57 +01:00
}
/**
2018-09-17 12:33:15 +02:00
* Returns the number of needed angels for nightshifts (see config)
2017-12-25 23:12:52 +01:00
*
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return int|string
2017-12-12 21:57:57 +01:00
*/
2020-10-27 17:13:46 +01:00
function stats_angels_needed_for_nightshifts(ShiftsFilter $filter = null)
2017-12-12 21:57:57 +01:00
{
2018-09-17 12:33:15 +02:00
$nightShiftsConfig = config('night_shifts');
$nightStartTime = $nightShiftsConfig['start'];
$nightEndTime = $nightShiftsConfig['end'];
2017-12-25 23:12:52 +01:00
$night_start = parse_date(
'Y-m-d H:i',
2018-09-17 12:33:15 +02:00
date('Y-m-d', time() + 12 * 60 * 60) . ' ' . $nightStartTime . ':00'
2017-12-25 23:12:52 +01:00
);
2018-09-17 12:33:15 +02:00
$night_end = $night_start + ($nightEndTime - $nightStartTime) * 60 * 60;
2020-10-27 17:13:46 +01:00
$result = Db::selectOne('
2017-12-25 23:12:52 +01:00
SELECT SUM(`count`) AS `count` FROM (
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
2020-05-13 18:26:32 +02:00
SELECT SUM(`count`)
FROM `NeededAngelTypes`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2020-05-13 18:26:32 +02:00
SELECT COUNT(*) FROM `ShiftEntry`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2020-05-13 18:26:32 +02:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-05-13 18:26:32 +02:00
2017-12-14 19:39:47 +01:00
UNION ALL
2020-05-13 18:26:32 +02:00
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
2020-05-13 18:26:32 +02:00
SELECT SUM(`count`)
FROM `NeededAngelTypes`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
2022-11-09 00:02:30 +01:00
' . ($filter ? 'AND angel_types.id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2020-05-13 18:26:32 +02:00
SELECT COUNT(*) FROM `ShiftEntry`
2022-11-09 00:02:30 +01:00
JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `angel_types`.`show_on_dashboard`=TRUE
2020-05-13 18:26:32 +02:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2020-10-27 17:13:46 +01:00
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND NOT s.shift_id IS NULL
2022-10-18 19:15:22 +02:00
' . ($filter ? 'AND Shifts.RID IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-10-27 17:13:46 +01:00
) AS `tmp`', [
$night_start,
$night_end,
2017-12-12 21:57:57 +01:00
$night_start,
$night_end
]);
2020-10-27 17:13:46 +01:00
return $result['count'] ?: '-';
2017-12-12 21:57:57 +01:00
}