From ca80abc38993fbbd83a8e2c8c55a6bc9612cd53c Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Wed, 15 Aug 2018 01:40:36 -0600 Subject: [PATCH] [add] Validations for work day abstracted to their own method --- .../src/application/EmployeeApplication.php | 50 +++++++++++++++---- api-payroll/src/routes.php | 2 +- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 617048a..b44aaee 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -671,14 +671,17 @@ class EmployeeApplication{ } /** - * Takes the data from the front end for the new worked day for a - * employee and saves it + * Takes the data from the front end for the work day, this coulld be + * for an update or a creation of a new registry + * + * The function will take the request body, validate it and pass the + * processed data back to the wrapper method * * @param $requestData object * @return array * @throws Exception */ - function SaveNewWorkDay($requestData){ + function validateDataForStorageWorkDay($requestData){ $code = $requestData['code']; $this->asserts->isNotEmpty($code, "The code can't be empty."); @@ -729,14 +732,43 @@ class EmployeeApplication{ $bonusTime = $perHourBonus * $this->settings['hoursPerWorkDay']; $bonusDeliveries = $deliveries * $this->settings['bonusPerDelivery']; - $idPaymentPerEmployeePerDay = $this->saveWorkedDay($idEmployee, $date, $baseAmountPaid, - $bonusTime, $bonusDeliveries); - $contractType = $this->getContractTypeByEmployee($idEmployee); - $this->storeWorkDayDetails($idPaymentPerEmployeePerDay, $idEmployeeType, $idEmployeeTypePerformed, - $contractType, $this->settings['hoursPerWorkDay'], $this->settings['paymentPerHour'], - $perHourBonus, $deliveries, $this->settings['bonusPerDelivery']); + $result = array( + 'idEmployee' => (int)$idEmployee, + 'date' => $date, + 'baseAmountPaid' => $baseAmountPaid, + 'bonusTime' => $bonusTime, + 'bonusDeliveries' => $bonusDeliveries, + 'contractType' => $contractType, + 'idEmployeeType' => (int)$idEmployeeType, + 'idEmployeeTypePerformed' => (int)$idEmployeeTypePerformed, + 'hoursPerWorkDay' => $this->settings['hoursPerWorkDay'], + 'paymentPerHour' => $this->settings['paymentPerHour'], + 'perHourBonus' => $perHourBonus, + 'deliveries' => $deliveries, + 'bonusPerDelivery' => $this->settings['bonusPerDelivery'] + ); + + return $result; + } + + /** + * Wrapper function to store a new day that has been worked by an employee + * + * @param $requestData object + * @return array + * @throws Exception + */ + function newWorkedDay($requestData){ + $data = $this->validateDataForStorageWorkDay($requestData); + + $idPaymentPerEmployeePerDay = $this->saveWorkedDay($data['idEmployee'], $data['date'], + $data['baseAmountPaid'], $data['bonusTime'], $data['bonusDeliveries']); + + $this->storeWorkDayDetails($idPaymentPerEmployeePerDay, $data['idEmployeeType'], + $data['idEmployeeTypePerformed'], $data['contractType'], $data['hoursPerWorkDay'], + $data['paymentPerHour'], $data['perHourBonus'], $data['deliveries'], $data['bonusPerDelivery']); return array('status' => 'success', 'message' => 'The worked day has been saved.', 'data' => $requestData); } diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index e27788a..086ab9a 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -102,7 +102,7 @@ $app->post('/api/employee/workday', function ($request, $response) { return $response->withStatus(200) ->withHeader('Content-Type', 'application/json') - ->write(json_encode($this->employeeApplication->SaveNewWorkDay($requestData))); + ->write(json_encode($this->employeeApplication->newWorkedDay($requestData))); }); $app->get('/api/employee/salary/{code}', function (Request $request, Response $response, array $args) {