Files
dbois/application/models/eloquent/queries/QEmployeeHours.php
T

80 lines
3.5 KiB
PHP

<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 01/03/2018
* Time: 12:03
*/
class QEmployeeHours {
public static function fire( $month, $year )
{
$sql = "
SELECT monthID,
user_id,
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
ROUND(SUM(projecthours), 0) AS projecthours,
ROUND(SUM(profitablehours), 0) AS profitablehours,
ROUND(SUM(nonprofitablehours), 0) AS nonprofitablehours,
ROUND(SUM(revenues), 0) AS revenues,
ROUND(SUM(nonprojecthours), 0) AS nonprojecthours,
ROUND(SUM(projecthours + nonprojecthours), 0) AS totalhours,
targethours
FROM (
SELECT employee_id AS user_id,
MONTH(hourdate) monthID,
0 AS projecthours,
0 AS profitablehours,
0 AS nonprofitablehours,
0 AS revenues,
SUM(hours) AS nonprojecthours
FROM employeehours EMH
JOIN hourtypes HTY
ON HTY.id = EMH.hourtype_id
WHERE YEAR(hourdate) = '{$year}'
AND reportable = 'T' ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY employee_id, MONTH(hourdate)
UNION
SELECT PEM.user_id,
MONTH(hourdate) monthID,
SUM(hours) AS projecthours,
SUM(IF(hourtype_id = 1, hours, 0)) AS profitablehours,
SUM(IF(hourtype_id = 2, hours, 0)) AS nonprofitablehours,
ROUND(SUM( IF(hourtype_id = 1, (hours * PEM.tarif), 0) ), 0) AS revenues,
0 AS nonprojecthours
FROM projecthours PH
JOIN projectemployees PEM
ON PEM.id = PH.projectemployee_id
WHERE YEAR(hourdate) = '{$year}'
AND hourtype_id <= 2 ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY PEM.user_id, MONTH(hourdate)
UNION
SELECT PH.projectemployee_id AS user_id,
MONTH(hourdate) monthID,
SUM(hours) AS projecthours,
0 AS profitablehours,
SUM(IF(hourtype_id = 3, hours, 0)) AS nonprofitablehours,
0 AS revenues,
0 AS nonprojecthours
FROM projecthours PH
WHERE YEAR(hourdate) = '{$year}'
AND hourtype_id = 3 ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY PH.projectemployee_id, MONTH(hourdate)
) AS Q1
JOIN users AS USR
ON USR.id = Q1.user_id
GROUP BY user_id ";
return $sql;
}
}