Apply some changes according to comments on the pullrequest

This commit is contained in:
Lukas Fürderer 2020-06-10 11:48:38 +02:00
parent 8fff0e2dca
commit 691916894f
Signed by: Lukas
GPG Key ID: B0AFA46F94103349
4 changed files with 32 additions and 22 deletions

View File

@ -4,14 +4,13 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.hso.ecommerce.entities.booking.Booking; import org.hso.ecommerce.entities.booking.Booking;
import org.hso.ecommerce.entities.booking.BookingAccountEntry; import org.hso.ecommerce.entities.booking.BookingAccountEntry;
import org.hso.ecommerce.entities.booking.BookingReason; import org.hso.ecommerce.entities.booking.BookingReason;
import org.hso.ecommerce.repos.booking.BookingRepository; import org.hso.ecommerce.repos.booking.BookingRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@Controller @Controller
@ -169,7 +168,7 @@ public class AccountingController {
} }
@GetMapping("/intern/accounting/") @GetMapping("/intern/accounting/")
public String accounting(HttpServletRequest request) { public String accounting(Model model) {
List<Booking> bookings = bookingRepository.allBookingsReverseChronologically(); List<Booking> bookings = bookingRepository.allBookingsReverseChronologically();
List<TemplateBooking> templateBookings = new ArrayList<>(); List<TemplateBooking> templateBookings = new ArrayList<>();
for (Booking booking : bookings) { for (Booking booking : bookings) {
@ -200,25 +199,25 @@ public class AccountingController {
templateBookings.add(tb); templateBookings.add(tb);
} }
request.setAttribute("bookings", templateBookings); model.addAttribute("bookings", templateBookings);
return "intern/accounting/index"; return "intern/accounting/index";
} }
@GetMapping("/intern/accounting/vat") @GetMapping("/intern/accounting/vat")
public String accountingVat(HttpServletRequest request) { public String accountingVat(Model model) {
List<Booking> bookings = bookingRepository.vatBookingsReverseChronologically(); List<Booking> bookings = bookingRepository.vatBookingsReverseChronologically();
ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isVATAccount); ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isVATAccount);
request.setAttribute("balance", result.balance); model.addAttribute("balance", result.balance);
request.setAttribute("bookings", result.bookings); model.addAttribute("bookings", result.bookings);
return "intern/accounting/vat"; return "intern/accounting/vat";
} }
@GetMapping("/intern/accounting/main") @GetMapping("/intern/accounting/main")
public String accountingIntern(HttpServletRequest request) { public String accountingIntern(Model model) {
List<Booking> bookings = bookingRepository.mainBookingsReverseChronologically(); List<Booking> bookings = bookingRepository.mainBookingsReverseChronologically();
ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isMainAccount); ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isMainAccount);
request.setAttribute("balance", result.balance); model.addAttribute("balance", result.balance);
request.setAttribute("bookings", result.bookings); model.addAttribute("bookings", result.bookings);
return "intern/accounting/main"; return "intern/accounting/main";
} }
} }

View File

@ -1,5 +1,7 @@
package org.hso.ecommerce.controller.intern.accounting; package org.hso.ecommerce.controller.intern.accounting;
import java.util.Optional;
import org.hso.ecommerce.action.booking.CreateBookingAction; import org.hso.ecommerce.action.booking.CreateBookingAction;
import org.hso.ecommerce.entities.booking.Booking; import org.hso.ecommerce.entities.booking.Booking;
import org.hso.ecommerce.entities.booking.BookingAccountEntry; import org.hso.ecommerce.entities.booking.BookingAccountEntry;
@ -212,11 +214,15 @@ public class ManualAccountingController {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new InvalidFormDataException("Die angegebene Kunden-Nr. ist ungültig."); throw new InvalidFormDataException("Die angegebene Kunden-Nr. ist ungültig.");
} }
return bookingAccountEntryRepository.getByUser(userId).or(() -> { Optional<BookingAccountEntry> bookingAccount = bookingAccountEntryRepository.getByUser(userId);
return userRepository.findById(userId).map((user) -> BookingAccountEntry.newUser(user)); if (!bookingAccount.isPresent()) {
}).orElseThrow(() -> { bookingAccount = userRepository.findById(userId).map((user) -> BookingAccountEntry.newUser(user));
return new InvalidFormDataException("Der Kunde Nr. " + userId + " konnte nicht gefunden werden."); }
}); if (bookingAccount.isPresent()) {
return bookingAccount.get();
} else {
throw new InvalidFormDataException("Der Kunde Nr. " + userId + " konnte nicht gefunden werden.");
}
} else if (account.equals("Sup")) { } else if (account.equals("Sup")) {
long supplierId; long supplierId;
try { try {
@ -224,12 +230,17 @@ public class ManualAccountingController {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new InvalidFormDataException("Die angegebene Lieferanten-Nr. ist ungültig."); throw new InvalidFormDataException("Die angegebene Lieferanten-Nr. ist ungültig.");
} }
return bookingAccountEntryRepository.getBySupplier(supplierId).or(() -> { Optional<BookingAccountEntry> bookingAccount = bookingAccountEntryRepository.getBySupplier(supplierId);
return supplierRepository.findById(supplierId).map((sup) -> BookingAccountEntry.newSupplier(sup)); if (!bookingAccount.isPresent()) {
}).orElseThrow(() -> { bookingAccount = supplierRepository.findById(supplierId)
return new InvalidFormDataException( .map((sup) -> BookingAccountEntry.newSupplier(sup));
}
if (bookingAccount.isPresent()) {
return bookingAccount.get();
} else {
throw new InvalidFormDataException(
"Der Lieferant Nr. " + supplierId + " konnte nicht gefunden werden."); "Der Lieferant Nr. " + supplierId + " konnte nicht gefunden werden.");
}); }
} else { } else {
throw new RuntimeException("Invalid form value for an account: " + account); throw new RuntimeException("Invalid form value for an account: " + account);
} }

View File

@ -35,7 +35,7 @@ public class SupplierIndexController {
private final BookingRepository bookingRepository = null; private final BookingRepository bookingRepository = null;
@Autowired @Autowired
private AccountingController accountingController = null; private final AccountingController accountingController = null;
@GetMapping("suppliers") @GetMapping("suppliers")
public String listSuppliers(Model model) { public String listSuppliers(Model model) {

View File

@ -23,7 +23,7 @@
<form th:action="@{/intern/accounting/addManual}" th:object="${form_vals}" method="post" class="detailgrid"> <form th:action="@{/intern/accounting/addManual}" th:object="${form_vals}" method="post" class="detailgrid">
<div class="s"> <div class="s">
<label for="amount">Betrag</label> <label for="amount">Betrag</label>
<input type="number" step="0.01" th:field="*{amount}" />&nbsp;EUR <input type="number" step="0.01" id="amount" th:field="*{amount}" />&nbsp;EUR
</div> </div>
<div class="spacer"></div> <div class="spacer"></div>