BP-826, fix calculation for ending phase between start and end day
diff --git a/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/controller/planning/PlanningController.java b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/controller/planning/PlanningController.java
index c6c86b5..e1d5362 100644
--- a/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/controller/planning/PlanningController.java
+++ b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/controller/planning/PlanningController.java
@@ -126,10 +126,11 @@
 				// delete existing entries

 				StandbyGroup stbyGroup = standbyGroupRepository.findOne(standbyBlueprintDto.getStandbyGroupId());

 

-				this.changeOrDeleteExistingBodies(

-						stbyGroup, new DateTime(DateHelper.getStartOfDay(standbyBlueprintDto.getValidFrom()))

-								.plusSeconds(1).toDate(),

-						DateHelper.getEndOfDay(standbyBlueprintDto.getValidTo()), 1L, username);

+				this.changeOrDeleteExistingBodies(stbyGroup,

+						new DateTime(DateHelper.getStartOfDay(standbyBlueprintDto.getValidFrom())).plusSeconds(1)

+								.toDate(),

+						DateHelper.getEndOfDay(new DateTime(standbyBlueprintDto.getValidTo()).minusSeconds(1).toDate()),

+						1L, username);

 

 				for (Entry<Integer, PlanningPhaseDto> entry : planningPhaseMap.entrySet()) {

 					LOGGER.debug("Start Plannig for phase " + entry.getKey());

@@ -179,7 +180,10 @@
 		Map<Integer, PlanningPhaseDto> resultMap = new HashMap<>();

 		try {

 			Date validFrom = standbyBlueprintDto.getValidFrom();

-			Date validTo = DateHelper.getEndOfDay(standbyBlueprintDto.getValidTo());

+

+			DateTime dt = new DateTime(standbyBlueprintDto.getValidTo());

+			dt = dt.minusSeconds(1);

+			Date validTo = DateHelper.getEndOfDay(dt.toDate());

 			Date tmpDate = validFrom;

 			int phase = 1;

 

@@ -267,8 +271,10 @@
 				if (tmpDate.getTime() == endDate.getTime()) {

 					this.changeOrDeleteAtIntervalEnd(body, duration, tmpDate, username);

 				} else {

-					if (body.getValidFrom().getTime() >= startDate.getTime())

+					if (body.getValidFrom().getTime() >= startDate.getTime()

+							&& body.getValidTo().getTime() <= endDate.getTime()) {

 						standbyScheduleBodyRepository.delete(body);

+					}

 				}

 			}

 			tmpDate = DateHelper.addDaysToDate(tmpDate, 1);

@@ -290,7 +296,7 @@
 			}

 			Date tmpDate = dto.getStartDate();

 

-			while (tmpDate.getTime() <= dto.getEndDate().getTime()) {

+			while (tmpDate.getTime() < dto.getEndDate().getTime()) {

 

 				// reset position if end of list has been reached

 				PlanningBodyResultDto planningBodyResultDto = this.calculateScheduleBodyEntries(lsUserInGroup,

@@ -513,6 +519,7 @@
 				int coveredDays = DateHelper.calculateDifferenceOfDays(startWeekDayInt, endWeekDayInt,

 						fittingDuration.getValidFrom(),

 						new DateTime(fittingDuration.getValidTo()).minusSeconds(1).toDate());

+

 				if (coveredDays == 0) {

 					// single entry with no over night duration

 					LOGGER.debug("A day was found that just cover a single day.");

@@ -556,11 +563,16 @@
 				StandbyScheduleBody stbyBody = this.createScheduleBodyObject(lsUserInGroup.get(0).getStandbyGroup(),

 						username, currentDate, lsUserInGroup.get(currentPosition).getUser(), fittingDuration,

 						TXT_AUTOMATIC_PLANNING);

+				int dayOfCurrentWeek = DateHelper.getDayOfWeek(currentDate);

+				if (dayOfCurrentWeek != DateHelper.getDayOfWeek(new DateTime(lastDate).minusSeconds(1).toDate())

+						|| fittingDuration.getValidDayFrom() != dayOfCurrentWeek) {

 

-				if (DateHelper.getDayOfWeek(currentDate) != DateHelper.getDayOfWeek(fittingDuration.getValidFrom())) {

-					// set start of day when it is not the start of the duration

+					// set start of day when it is not the start of the duration and it is not the

+					// end of the planning phase

 					stbyBody.setValidFrom(DateHelper.getStartOfDay(currentDate));

+

 				}

+

 				// reset ending date to beginning of next day.

 				stbyBody.setValidTo(DateHelper.getEndOfDay(currentDate));

 				standbyScheduleBodyRepository.save(stbyBody);