diff --git a/application/controllers/beheer/EmployeeTargetController.php b/application/controllers/beheer/EmployeeTargetController.php index 63b1248..f35a620 100644 --- a/application/controllers/beheer/EmployeeTargetController.php +++ b/application/controllers/beheer/EmployeeTargetController.php @@ -5,8 +5,8 @@ * Date: 21/04/2018 * Time: 20:08 */ -//include_once('../application/models/eloquent/Employeetarget.php'); +//include_once('../application/models/eloquent/Employeetarget.php'); class EmployeeTargetController extends MY_Controller { @@ -30,5 +30,26 @@ class EmployeeTargetController extends MY_Controller { $this->json( $employeeTargets ); } + public function saveEmployeeTarget() + { + $employeeTargetValidation = new EmployeeTargetValidation(); + + if ( ! $employeeTargetValidation->getValidationResult() ) + return $this->json( [ + 'success' => FALSE, 'messages' => $employeeTargetValidation->getErrorMessages()] ); + + $target = $this->input->post(); + + $targetID = $target['id']; + unset( $target['id'] ); + + $target['endmonth'] = $target['endmonth'] ? $target['endmonth'] : NULL; + $target['endyear'] = $target['endyear'] ? $target['endyear'] : NULL; + + $targetID = Employeetarget::saveTarget( $targetID, $target ); + + $this->json( ['success' => TRUE, 'targetID' => $targetID] ); + } + } \ No newline at end of file diff --git a/application/controllers/reports/ReportsController.php b/application/controllers/reports/ReportsController.php index 49404d7..2ec3db2 100644 --- a/application/controllers/reports/ReportsController.php +++ b/application/controllers/reports/ReportsController.php @@ -51,6 +51,11 @@ class ReportsController extends MY_Controller { $result = $query->result(); + foreach($result as $user) + { + $user->targethours = Employeetarget::getTargetHoursPerEmployee( $user->user_id, $year, $month); + } + $this->json( $result ); } diff --git a/application/controllers/reports/ReservationsController.php b/application/controllers/reports/ReservationsController.php index eb96895..6d50c1b 100644 --- a/application/controllers/reports/ReservationsController.php +++ b/application/controllers/reports/ReservationsController.php @@ -27,22 +27,30 @@ class ReservationsController extends MY_Controller { { $projects[ $result->projectID ] = [ 'projectnumber' => $result->projectnumber, + 'costs' => 0, 'reservation' => 0, 'payed' => 0, 'saldo' => 0, + 'saldoToPay' => 0, + 'other_revenue' => 0, 'customers' => [] ]; } $projects[ $result->projectID ]['reservation'] += $result->reservation; + $projects[ $result->projectID ]['costs'] += $result->costs; $projects[ $result->projectID ]['payed'] += $result->payed; $projects[ $result->projectID ]['saldo'] += $result->saldo; + $projects[ $result->projectID ]['saldoToPay'] += $result->saldoToPay; + $projects[ $result->projectID ]['other_revenue'] = $result->other_revenue; $projects[ $result->projectID ]['externals'][] = [ 'external' => $result->contactperson, 'reservation' => $result->reservation, + 'costs' => $result->costs, 'payed' => $result->payed, - 'saldo' => $result->saldo + 'saldo' => $result->saldo, + 'saldoToPay' => $result->saldoToPay ]; } diff --git a/application/libraries/validation/inputValidations/EmployeeTargetValidation.php b/application/libraries/validation/inputValidations/EmployeeTargetValidation.php new file mode 100644 index 0000000..ce96662 --- /dev/null +++ b/application/libraries/validation/inputValidations/EmployeeTargetValidation.php @@ -0,0 +1,40 @@ + [ + 'validations' => 'required|integer', + 'fieldname' => 'gebruiker' + ], + 'startmonth' => [ + 'validations' => 'required|decimal', + 'fieldname' => 'startmaand' + ], + 'startyear' => [ + 'validations' => 'required|integer', + 'fieldname' => 'startjaar' + ], + 'target' =>[ + 'validations' => 'integer', + 'fieldname' => 'uren target' + ] + ]; + + public function __construct() + { + parent::__construct(); + + $this->ci =& get_instance(); + } +} \ No newline at end of file diff --git a/application/models/eloquent/Employeetarget.php b/application/models/eloquent/Employeetarget.php index 654c3e3..f958051 100644 --- a/application/models/eloquent/Employeetarget.php +++ b/application/models/eloquent/Employeetarget.php @@ -15,4 +15,48 @@ class Employeetarget extends BaseModel { return $this->belongsTo( 'User' ); } + public static function saveTarget( $targetID, $target ) + { + $target = (array) $target; + + if ( $targetID == 0 ) + { + $target = self::create( $target ); + $targetID = $target['id']; + } + else + { + self::whereId( $targetID )->update( $target ); + } + + return $targetID; + } + + public static function getTargetHoursPerEmployee( $employeeID, $year, $monthID ) + { + $months = $monthID == 0 ? [1,2,3,4,5,6,7,8,9,10,11,12] : [$monthID]; + + $targetHours = 0; + + foreach($months as $month) + { + $targetHours += self::getCurrentMonthTarget( $employeeID, $year, $month); + } + + return $targetHours; + } + + public static function getCurrentMonthTarget( $employeeID, $year, $month ) + { + $monthTarget = Self::whereUserId($employeeID) + ->where('startyear', '<=', $year) + ->where('startmonth', '<=', $month) + ->orderBy('startyear', 'DESC') + ->orderBy('startmonth', 'DESC'); + + if( $monthTarget->count() == 0 ) return 0; + + return $monthTarget->first()->target; + } + } \ No newline at end of file diff --git a/application/models/eloquent/queries/QEmployeeHours.php b/application/models/eloquent/queries/QEmployeeHours.php index 93691f0..8603af3 100644 --- a/application/models/eloquent/queries/QEmployeeHours.php +++ b/application/models/eloquent/queries/QEmployeeHours.php @@ -14,12 +14,12 @@ class QEmployeeHours { SELECT monthID, user_id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname, - SUM(projecthours) AS projecthours, - SUM(profitablehours) AS profitablehours, - SUM(nonprofitablehours) AS nonprofitablehours, - SUM(revenues) AS revenues, - SUM(nonprojecthours) AS nonprojecthours, - SUM(projecthours + nonprojecthours) AS totalhours, + 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, diff --git a/application/models/eloquent/queries/ReservationTotals.php b/application/models/eloquent/queries/ReservationTotals.php index 05ea508..7c7fd93 100644 --- a/application/models/eloquent/queries/ReservationTotals.php +++ b/application/models/eloquent/queries/ReservationTotals.php @@ -10,31 +10,47 @@ class ReservationTotals { public static function fire() { - $sql = "SELECT projectID, externalID, reservation, payed, (reservation - payed) AS saldo, PRJ.projectnumber, EXT.contactperson + // UNION ALL wordt gebruikt voor een full outer join + $sql = "SELECT Q5.project_id AS projectID, external_id AS externalID, + costs, reservation, payed, (reservation - payed) AS saldo, + (costs - payed) AS saldoToPay, PRJ.projectnumber, EXT.contactperson, + ROUND(( (other_revenue * (100 + (SELECT vatrate from vats where active='T' LIMIT 1))) / 100 ), 0) other_revenue FROM ( - SELECT COALESCE(RES.project_id, PRC.project_id) AS projectID, - COALESCE(RES.external_id, PRC.external_id) AS externalID, - SUM(COALESCE(reservationamount, 0)) AS reservation, - SUM(COALESCE(payed, 0)) AS payed - FROM reservations RES - LEFT JOIN projectcosts PRC - ON RES.project_id = PRC.project_id AND RES.external_id = PRC.external_id - GROUP BY COALESCE(RES.project_id, PRC.project_id), COALESCE(PRC.external_id, PRC.external_id) + SELECT COALESCE(reservation, 0) AS reservation, costs, payed, Q2.external_id, Q2.project_id + FROM ( + SELECT SUM(reservationamount) AS reservation, external_id, project_id + FROM reservations + GROUP BY external_id, project_id + ) Q1 + RIGHT JOIN + ( + SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id + FROM projectcosts + GROUP BY external_id, project_id + ) Q2 + ON Q1.external_id = Q2.external_id AND Q1.project_id = Q2.project_id UNION ALL - SELECT COALESCE(RES.project_id, PRC.project_id) AS projectID, - COALESCE(RES.external_id, PRC.external_id) AS externalID, - SUM(COALESCE(reservationamount, 0)) AS reservation, - SUM(COALESCE(payed, 0)) AS payed - FROM reservations RES - RIGHT JOIN projectcosts PRC - ON RES.project_id = PRC.project_id AND RES.external_id = PRC.external_id - WHERE RES.id IS NULL - GROUP BY COALESCE(RES.project_id, PRC.project_id), COALESCE(RES.external_id, PRC.external_id) - ) Q3 + SELECT reservation, COALESCE(costs, 0), COALESCE(payed, 0), Q3.external_id, Q3.project_id + FROM ( + SELECT SUM(reservationamount) AS reservation, external_id, project_id + FROM reservations + GROUP BY external_id, project_id + ) Q3 + LEFT JOIN + ( + SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, id + FROM projectcosts + GROUP BY external_id, project_id + ) Q4 + ON Q3.external_id = Q4.external_id AND Q3.project_id = Q4.project_id + WHERE Q4.id IS NULL + ) Q5 JOIN projects PRJ - ON PRJ.id = Q3.projectID + ON PRJ.id = Q5.project_id JOIN externals EXT - ON EXT.id = Q3.externalID "; + ON EXT.id = Q5.external_id + JOIN offers OFF + ON PRJ.id = OFF.project_id "; return $sql; } diff --git a/application/views/modals/employeetarget_modal.php b/application/views/modals/employeetarget_modal.php index 424a304..4de94d6 100644 --- a/application/views/modals/employeetarget_modal.php +++ b/application/views/modals/employeetarget_modal.php @@ -1,16 +1,16 @@