engelsystem/includes/model/Stats.php

225 lines
9.3 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;
use Engelsystem\Helpers\Carbon;
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(*)
2023-01-18 13:02:11 +01:00
FROM `shift_entries`
WHERE `shift_entries`.`shift_id`=`shifts`.`id`
2020-10-27 17:13:46 +01:00
AND `freeloaded`=0
2023-01-18 13:02:11 +01:00
' . ($filter ? 'AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2020-10-27 17:13:46 +01:00
)) AS `count`
2023-01-03 22:19:03 +01:00
FROM `shifts`
WHERE (`end` >= NOW() AND `start` <= NOW())
' . ($filter ? 'AND shifts.room_id 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
2023-01-22 18:43:09 +01:00
(SELECT SUM(`count`) FROM `needed_angel_types` WHERE `needed_angel_types`.`shift_id`=`shifts`.`id`' . ($filter ? ' AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2023-01-03 22:19:03 +01:00
* TIMESTAMPDIFF(MINUTE, `shifts`.`start`, `shifts`.`end`) / 60 AS `count`
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`end` >= NOW()
AND s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id 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
2023-01-22 18:43:09 +01:00
(SELECT SUM(`count`) FROM `needed_angel_types` WHERE `needed_angel_types`.`room_id`=`shifts`.`room_id`' . ($filter ? ' AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2023-01-03 22:19:03 +01:00
* TIMESTAMPDIFF(MINUTE, `shifts`.`start`, `shifts`.`end`) / 60 AS `count`
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE shifts.`end` >= NOW()
AND NOT s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id 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
{
$in3hours = Carbon::now()->addHours(3)->toDateTimeString();
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,
(
2023-01-22 18:43:09 +01:00
SELECT SUM(needed_angel_types.`count`)
FROM `needed_angel_types`
JOIN `angel_types` ON `angel_types`.`id`=`needed_angel_types`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`shift_id`=`shifts`.`id`
' . ($filter ? 'AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2023-01-18 13:02:11 +01:00
SELECT COUNT(*) FROM `shift_entries`
JOIN `angel_types` ON `angel_types`.`id`=`shift_entries`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-18 13:02:11 +01:00
AND `shift_entries`.`shift_id`=`shifts`.`id`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2023-01-18 13:02:11 +01:00
' . ($filter ? 'AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
2023-01-03 22:19:03 +01:00
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE shifts.`end` > NOW() AND shifts.`start` < ?
AND s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id 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,
(
2023-01-22 18:43:09 +01:00
SELECT SUM(needed_angel_types.`count`)
FROM `needed_angel_types`
JOIN `angel_types` ON `angel_types`.`id`=`needed_angel_types`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`room_id`=`shifts`.`room_id`
' . ($filter ? 'AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2023-01-18 13:02:11 +01:00
SELECT COUNT(*)
FROM `shift_entries`
JOIN `angel_types` ON `angel_types`.`id`=`shift_entries`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-18 13:02:11 +01:00
AND `shift_entries`.`shift_id`=`shifts`.`id`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2023-01-18 13:02:11 +01:00
' . ($filter ? 'AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
2023-01-03 22:19:03 +01:00
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `end` > NOW() AND `start` < ?
AND NOT s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-10-27 17:13:46 +01:00
) AS `tmp`', [
$in3hours,
$in3hours,
2017-12-12 21:57:57 +01:00
]);
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;
$night_start = Carbon::createFromTimestamp($night_start)->toDateTimeString();
$night_end = Carbon::createFromTimestamp($night_end)->toDateTimeString();
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,
(
2023-01-22 18:43:09 +01:00
SELECT SUM(needed_angel_types.`count`)
FROM `needed_angel_types`
JOIN `angel_types` ON `angel_types`.`id`=`needed_angel_types`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`shift_id`=`shifts`.`id`
' . ($filter ? 'AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
) - (
2023-01-18 13:02:11 +01:00
SELECT COUNT(*) FROM `shift_entries`
JOIN `angel_types` ON `angel_types`.`id`=`shift_entries`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-18 13:02:11 +01:00
AND `shift_entries`.`shift_id`=`shifts`.`id`
AND shift_entries.`freeloaded`=0
' . ($filter ? 'AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
2023-01-03 22:19:03 +01:00
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE shifts.`end` > ? AND shifts.`start` < ?
AND s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id 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,
(
2023-01-22 18:43:09 +01:00
SELECT SUM(needed_angel_types.`count`)
FROM `needed_angel_types`
JOIN `angel_types` ON `angel_types`.`id`=`needed_angel_types`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`room_id`=`shifts`.`room_id`
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
) - (
2023-01-18 13:02:11 +01:00
SELECT COUNT(*) FROM `shift_entries`
JOIN `angel_types` ON `angel_types`.`id`=`shift_entries`.`angel_type_id`
2022-11-09 00:02:30 +01:00
WHERE `angel_types`.`show_on_dashboard`=TRUE
2023-01-18 13:02:11 +01:00
AND `shift_entries`.`shift_id`=`shifts`.`id`
2017-12-26 10:47:39 +01:00
AND `freeloaded`=0
2023-01-18 13:02:11 +01:00
' . ($filter ? 'AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
2017-12-26 10:47:39 +01:00
)
)
2017-12-25 23:12:52 +01:00
AS `count`
2023-01-03 22:19:03 +01:00
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `end` > ? AND `start` < ?
AND NOT s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id 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,
2017-12-12 21:57:57 +01:00
]);
2020-10-27 17:13:46 +01:00
return $result['count'] ?: '-';
2017-12-12 21:57:57 +01:00
}