diff --git a/api-payroll/public/js/registerWorkDays.js b/api-payroll/public/js/registerWorkDays.js index e2f03b2..5579599 100644 --- a/api-payroll/public/js/registerWorkDays.js +++ b/api-payroll/public/js/registerWorkDays.js @@ -195,7 +195,7 @@ function saveNewWorkDay(){ data: parameters, success:function(data){ $('#modalServerResponseSuccess').modal('show'); - document.getElementById('serverResponseSuccess').innerHTML = 'The employee ' + data['fullName'] + ' has been updated.'; + document.getElementById('serverResponseSuccess').innerHTML = data['message']; }, error:function(x,e) { let responseText = $.parseJSON(x["responseText"]); diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 9c2c628..22dcadb 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -1,6 +1,7 @@ asserts->isNotEmpty($firstName, "The first name can't be empty."); @@ -87,6 +89,7 @@ class EmployeeApplication{ * @param $code string * @param $contractType string * @return mixed + * @throws Exception */ function savePersonAsEmployee($idEmployeeType, $idPerson, $code, $contractType){ $this->asserts->higherThanZero($idEmployeeType, "idEmployeeType must be higher than 0"); @@ -115,6 +118,7 @@ class EmployeeApplication{ /** * @param $requestData object * @return array + * @throws Exception */ function saveNewEmployee($requestData){ // Getting and validating the data @@ -578,8 +582,98 @@ class EmployeeApplication{ return $matches; } + /** + * @param $idEmployee integer + * @param $date date + * @param $baseAmount double + * @param $bonusTime double + * @param $deliveries double + * @return integer + * @throws Exception + */ + function saveWorkedDay($idEmployee, $date, $baseAmount, $bonusTime, $deliveries){ + $this->asserts->isNotEmpty($idEmployee, "The idEmployee can't be empty."); + $this->asserts->isNotEmpty($date, "The date can't be empty."); + $this->asserts->isNotEmpty($baseAmount, "The base payment per day can't be empty."); + $this->asserts->isNotEmpty($bonusTime, "The bonus per worked hours can't be empty."); + $this->asserts->isNotEmpty($deliveries, "The payment for deliveries can't be empty."); + + try { + $stmt = $this->pdo->prepare("INSERT INTO paymentsPerEmployeePerDay + (idEmployee, date, baseAmount, bonusTime, deliveries) + VALUES (:idEmployee, :date, :baseAmount, :bonusTime, :deliveries)"); + $this->pdo->beginTransaction(); + $stmt->execute(array(':idEmployee' => $idEmployee, ':date' => $date, ':baseAmount' => $baseAmount, + ':bonusTime' => $bonusTime, ':deliveries' => $deliveries)); + $id = $this->pdo->lastInsertId(); + $this->pdo->commit(); + + return $id; + + $stmt = null; + } catch( PDOExecption $e ) { + $this->pdo->rollback(); + throw new Exception('There was an error while trying to save the worked day.'); + } + } + + /** + * Takes the data from the front end for the new worked day for a + * employee and saves it + * + * @param $requestData object + * @return array + * @throws Exception + */ function SaveNewWorkDay($requestData){ - return array('status' => 'success', 'message' => 'Saved...', 'data' => $requestData); + $code = $requestData['code']; + $this->asserts->isNotEmpty($code, "The code can't be empty."); + + $idEmployee = $this->getIdEmployeeByCode($code); + $this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0"); + + $idEmployeeType = $this->getIdEmployeeTypeByCode($code); + $this->asserts->higherThanZero($idEmployeeType, "idEmployeeType must be higher than 0"); + + $idEmployeeTypePerformed = $requestData['idEmployeeTypePerformed']; + $this->asserts->isNotEmpty($idEmployeeTypePerformed, "The performed rol must be provided."); + $this->asserts->higherThanZero($idEmployeeTypePerformed, "idEmployeeTypePerformed must be higher than 0"); + + $deliveries = $requestData['deliveries']; + $this->asserts->isNotEmpty($deliveries, "The number of deliveries cannot be empty."); + + $date = $requestData['date']; + $this->asserts->isNotEmpty($date, "The worked date cannot be empty-."); + + // The emplpoyee can't take that rol + if($idEmployeeType != 3 and $idEmployeeType != $idEmployeeTypePerformed){ + throw new Exception("The performed rol can't be done by this type of employee."); + } + + // If we're working on a different month + $this->asserts->datesHaveSameMonth($date, date('Y-m-d'), "Work days can only be registered within the same month."); + + $baseAmountPaid = $this->settings['hoursPerWorkDay'] * $this->settings['paymentPerHour']; + + // Getting setting data based on employee type that was performed + switch ($idEmployeeTypePerformed) { + case 1: + $perHourBonus = $this->settings['perHourBonusDriver']; + break; + case 2: + $perHourBonus = $this->settings['perHourBonusLoader']; + break; + case 3: + $perHourBonus = $this->settings['perHourBonusAux']; + break; + } + + $bonusTime = $perHourBonus * $this->settings['hoursPerWorkDay']; + $bonusDeliveries = $deliveries * $this->settings['bonusPerDelivery']; + + $this->saveWorkedDay($idEmployee, $date, $baseAmountPaid, $bonusTime, $bonusDeliveries); + + return array('status' => 'success', 'message' => 'The worked day has been saved.', 'data' => $requestData); } } ?> \ No newline at end of file diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index d627334..ce3db51 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -81,5 +81,19 @@ class Asserts{ throw new Exception($errorMessage); } } + + /** + * Compares two dates to dertermine if they have the same month + * + * @param $firstDate date + * @param $secondDate date + * @param $errorMessage string + * @throws Exception + */ + function datesHaveSameMonth($firstDate, $secondDate, $errorMessage){ + if (date("m",strtotime($firstDate)) != date("m",strtotime($secondDate))){ + throw new Exception($errorMessage); + } + } } ?> diff --git a/api-payroll/src/settings.php b/api-payroll/src/settings.php index 23e7d09..0e84984 100644 --- a/api-payroll/src/settings.php +++ b/api-payroll/src/settings.php @@ -47,7 +47,7 @@ return [ 'codeLength' => '3', 'contractTypes' => array('INTERNO', 'EXTERNO'), 'hoursPerWorkDay' => 8, - 'paymentperHour' => 30, + 'paymentPerHour' => 30, 'bonusPerDelivery' => 5, 'perHourBonusDriver' => 10, 'perHourBonusLoader' => 5, diff --git a/database/database.sql b/database/database.sql index b6466e0..232ae18 100644 --- a/database/database.sql +++ b/database/database.sql @@ -82,12 +82,10 @@ CREATE TABLE IF NOT EXISTS `paymentsPerEmployeePerDay` ( `baseAmount` DOUBLE(10,2) NOT NULL DEFAULT 0.0 comment 'Amount paid for the hours worked', `bonusTime` DOUBLE(10,2) NOT NULL DEFAULT 0.0 comment 'Bonus paid for the hours worked', `deliveries` DOUBLE(10,2) NOT NULL DEFAULT 0.0 comment 'Bonus for the number of deliveries', - `taxes` DOUBLE(10,2) NOT NULL DEFAULT 0.0 comment 'Substracted amout for taxes', - `vouchers` DOUBLE(10,2) NOT NULL DEFAULT 0.0 comment 'Amout given in vouchers', `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'The date on which the registry was created', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'The date of the last time the row was modified', PRIMARY KEY (`id`), FOREIGN KEY (idEmployee) REFERENCES employees(id), - UNIQUE (`idEmployee`, `date`) + UNIQUE (`idEmployee`, `date`, `status`) );