diff --git a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java index 52f7e8da..28766233 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java @@ -57,8 +57,6 @@ public class TripRequirementServiceImpl extends RequirementBaseServiceImpl billList = getBills(entity); + boolean hasOwned = false; + + for (TripBill tb : billList) { + if (tb.getOwnedBy().equals(entity.getOwnedBy())) { + hasOwned = true; + break; + } + } + + if (!hasOwned) { TripBill bill = tripBillService.createTripBill(entity); tripBillService.add(bill); bill.setOwnedBy(entity.getOwnedBy()); tripBillService.update(bill); + } - if (entity.getBillForPassengers() != null && entity.getBillForPassengers()) { - for (User u : entity.getPassengers()) { - if (!u.equals(entity.getOwnedBy())) { - TripBill passBill = tripBillService.createPassengersBill(entity); - tripBillService.add(passBill); - passBill.setOwnedBy(u); - tripBillService.update(passBill); - } + if (entity.getBillForPassengers() != null && entity.getBillForPassengers()) { + for (User u : entity.getPassengers()) { + if (canCreateBill(entity, u, billList)) { + TripBill passBill = tripBillService.createPassengersBill(entity); + tripBillService.add(passBill); + passBill.setOwnedBy(u); + tripBillService.update(passBill); } + } - sendMailToPassengers(entity, settingsService.getSettings().getConfReqTripPassenger()); + sendMailToPassengers(entity, settingsService.getSettings().getConfReqTripPassenger()); + } + } + + private boolean canCreateBill(TripRequirement req, User owner, List bills) { + if (owner.equals(req.getOwnedBy())) { + return false; + } + + for (TripBill tb : bills) { + if (tb.getOwnedBy().equals(owner)) { + return false; } } + + return true; } @Override