WIP: Alternative implementation for staff payroll

This commit is contained in:
Simon Bruder 2023-12-06 17:34:12 +01:00
parent 2f221da2f4
commit c85a27fb60
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
4 changed files with 14 additions and 81 deletions

View file

@ -1,43 +0,0 @@
package catering.staff;
import java.time.LocalDateTime;
import org.springframework.validation.Errors;
import jakarta.validation.constraints.NotNull;
public class DateForm {
private @NotNull int year;
private @NotNull int month;
public DateForm() {
this.year = LocalDateTime.now().getYear();
this.month = LocalDateTime.now().getMonth().getValue();
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void validate(Errors e) {
if (year < 2023 || year > 2100) {
e.rejectValue("year", "year is not valid");
}
if (month < 1 || month > 12) {
e.rejectValue("month", "month is not valid");
}
}
}

View file

@ -2,6 +2,9 @@ package catering.staff;
import static org.salespointframework.core.Currencies.EURO; import static org.salespointframework.core.Currencies.EURO;
import java.time.YearMonth;
import java.util.Optional;
import org.javamoney.moneta.Money; import org.javamoney.moneta.Money;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -26,37 +29,19 @@ public class StaffController {
@GetMapping("/staff") @GetMapping("/staff")
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
public String getStaff(Model model) { public String getStaff(Model model, @RequestParam Optional<YearMonth> month) {
return getStaff(model, new StaffForm()); return getStaff(model, new StaffForm(), month.orElseGet(YearMonth::now));
} }
public String getStaff(Model model, @Valid StaffForm form) { public String getStaff(Model model, @Valid StaffForm form) {
return getStaff(model, form, YearMonth.now());
}
public String getStaff(Model model, @Valid StaffForm form, YearMonth month) {
model.addAttribute("staff", staffManagement.findAll()); model.addAttribute("staff", staffManagement.findAll());
model.addAttribute("form", form); model.addAttribute("form", form);
model.addAttribute("month", month);
model.addAttribute("management", staffManagement); model.addAttribute("management", staffManagement);
model.addAttribute("dateForm", new DateForm());
return "staff";
}
public String getStaff(Model model, @Valid DateForm form) {
model.addAttribute("staff", staffManagement.findAll());
model.addAttribute("form", new StaffForm());
model.addAttribute("management", staffManagement);
model.addAttribute("dateForm", form);
return "staff";
}
@PostMapping("/staff/updateYearMonth")
@PreAuthorize("hasRole('ADMIN')")
public String getStaff(Model model, @Valid DateForm form, Errors result) {
form.validate(result);
if (result.hasErrors()) {
return getStaff(model, form);
}
model.addAttribute("staff", staffManagement.findAll());
model.addAttribute("form", new StaffForm());
model.addAttribute("management", staffManagement);
model.addAttribute("dateForm", form);
return "staff"; return "staff";
} }

View file

@ -111,8 +111,4 @@ public class StaffManagement {
.map(order -> order.getDurationInSecondsDuringMonth(month)) .map(order -> order.getDurationInSecondsDuringMonth(month))
.collect(Collectors.summingDouble(seconds -> (double) seconds / 3600)); .collect(Collectors.summingDouble(seconds -> (double) seconds / 3600));
} }
public double getWorkingHoursByEmployee(Employee e, int year, int month) {
return getWorkingHoursByEmployee(e, YearMonth.of(year, month));
}
} }

View file

@ -10,16 +10,11 @@
<div layout:fragment="content"> <div layout:fragment="content">
<div> <div>
<h2>Mitarbeiterdetails</h2> <h2>Mitarbeiterdetails</h2>
<form th:object="${dateForm}" th:action="@{/staff/updateYearMonth}" method="post"> <form method="get">
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="year">Jahr:</label>
<input class="form-control" type="year" th:field="*{year}" th:errorclass="is-invalid" required>
<div th:if="${#fields.hasErrors('year')}" class="invalid-feedback">Ungültiges Jahr</div>
</div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="month">Monat:</label> <label class="form-label" for="month">Monat</label>
<input class="form-control" type="month" th:field="*{month}" th:errorclass="is-invalid" required> <input class="form-control" type="month" name="month" th:value="${month}" required>
<div th:if="${#fields.hasErrors('month')}" class="invalid-feedback">Ungültiger Monat</div>
</div> </div>
<button class="btn btn-primary" type="submit">Zeitangabe aktualisieren</button> <button class="btn btn-primary" type="submit">Zeitangabe aktualisieren</button>
</form> </form>
@ -37,7 +32,7 @@
<td th:text="${employee.name}">Max</td> <td th:text="${employee.name}">Max</td>
<td th:text="${employee.job}">Koch</td> <td th:text="${employee.job}">Koch</td>
<td th:text="${employee.wage}"></td> <td th:text="${employee.wage}"></td>
<td th:with="year=${dataForm.getYear},month=${dataForm.getMonth}" th:text="${management.getWorkingHoursByEmployee(employee, year, month)}"></td> <td th:text="${management.getWorkingHoursByEmployee(employee, month)}"></td>
<td> <td>
<a th:href="@{'/staff/edit/' + ${employee.id}}" <a th:href="@{'/staff/edit/' + ${employee.id}}"
><button class="btn btn-warning">Bearbeiten</button></a ><button class="btn btn-warning">Bearbeiten</button></a