diff --git a/src/integration-test/java/org/openlmis/referencedata/web/OrderableControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/referencedata/web/OrderableControllerIntegrationTest.java index 97ddde11..81009c78 100644 --- a/src/integration-test/java/org/openlmis/referencedata/web/OrderableControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/referencedata/web/OrderableControllerIntegrationTest.java @@ -36,7 +36,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; - import com.jayway.restassured.response.Response; import guru.nidi.ramltester.junit.RamlMatchers; import java.time.ZoneId; @@ -74,6 +73,7 @@ import org.openlmis.referencedata.domain.RightName; import org.openlmis.referencedata.dto.OrderableChildDto; import org.openlmis.referencedata.dto.OrderableDto; +import org.openlmis.referencedata.dto.PriceChangeDto; import org.openlmis.referencedata.dto.ProgramOrderableDto; import org.openlmis.referencedata.dto.VersionIdentityDto; import org.openlmis.referencedata.exception.UnauthorizedException; @@ -344,7 +344,8 @@ public void shouldCreateNewOrderableWithProgramOrderable() { .createNew(Code.code("orderableDisplayCategoryCode")); orderableDisplayCategory.setId(UUID.randomUUID()); ProgramOrderable programOrderable = new ProgramOrderable(program, orderable, 1, true, - orderableDisplayCategory, true, 1, Money.of(CurrencyUnit.USD, 10.0)); + orderableDisplayCategory, true, 1, Money.of(CurrencyUnit.USD, 10.0), + Collections.emptyList()); orderable.setProgramOrderables(Collections.singletonList(programOrderable)); orderable.export(orderableDto); @@ -929,7 +930,8 @@ public void shouldGetAuditLog() { private ProgramOrderableDto generateProgramOrderable() { return new ProgramOrderableDto(UUID.randomUUID(), UUID.randomUUID(), - null, null, true, true, 0, 1, Money.of(CurrencyUnit.USD, 10.0)); + null, null, true, true, 0, 1, Money.of(CurrencyUnit.USD, 10.0), + Collections.singletonList(new PriceChangeDto())); } private void checkIfEquals(PageDto response, List expected) { diff --git a/src/main/java/org/openlmis/referencedata/util/OrderableBuilder.java b/src/main/java/org/openlmis/referencedata/util/OrderableBuilder.java index 2efe883f..f437d1d3 100644 --- a/src/main/java/org/openlmis/referencedata/util/OrderableBuilder.java +++ b/src/main/java/org/openlmis/referencedata/util/OrderableBuilder.java @@ -17,17 +17,25 @@ import static org.springframework.util.CollectionUtils.isEmpty; +import java.time.ZonedDateTime; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; +import org.joda.money.Money; import org.openlmis.referencedata.domain.Orderable; import org.openlmis.referencedata.domain.OrderableChild; +import org.openlmis.referencedata.domain.PriceChange; import org.openlmis.referencedata.domain.Program; import org.openlmis.referencedata.domain.ProgramOrderable; +import org.openlmis.referencedata.domain.User; +import org.openlmis.referencedata.exception.NotFoundException; import org.openlmis.referencedata.repository.OrderableRepository; import org.openlmis.referencedata.repository.ProgramRepository; +import org.openlmis.referencedata.repository.UserRepository; +import org.openlmis.referencedata.service.AuthenticationHelper; +import org.openlmis.referencedata.util.messagekeys.UserMessageKeys; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -40,6 +48,12 @@ public class OrderableBuilder { @Autowired private OrderableRepository orderableRepository; + @Autowired + private UserRepository userRepository; + + @Autowired + private AuthenticationHelper authenticationHelper; + /** * Creates new instance based on data from {@link Orderable.Importer}. * @@ -67,6 +81,31 @@ public Orderable newOrderable(Orderable.Importer importer, Orderable persistedOr programOrderable.setProgram(program); programOrderable.setProduct(orderable); + List priceChanges = item.getPriceChanges().stream() + .map(priceChange -> { + User author = userRepository.findById(priceChange.getAuthor().getId()) + .orElseThrow(() -> new NotFoundException(UserMessageKeys.ERROR_NOT_FOUND)); + PriceChange priceChangeItem = PriceChange.newInstance(priceChange, author); + priceChangeItem.setProgramOrderable(programOrderable); + return priceChangeItem; + }) + .collect(Collectors.toList()); + + if (persistedOrderable != null) { + boolean priceHasChanged = true; + Money newPrice = programOrderable.getPricePerPack(); + if (persistedOrderable.getProgramOrderable(program) != null) { + Money currentPrice = persistedOrderable.getProgramOrderable(program) + .getPricePerPack(); + priceHasChanged = !currentPrice.equals(newPrice); + } + + if (priceHasChanged) { + addPriceChange(programOrderable, newPrice, priceChanges); + } + } + + programOrderable.setPriceChanges(priceChanges); return programOrderable; }) .collect(Collectors.toList()); @@ -97,4 +136,15 @@ public Orderable newOrderable(Orderable.Importer importer, Orderable persistedOr return orderable; } + private void addPriceChange(ProgramOrderable programOrderable, Money newPrice, + List priceChanges) { + PriceChange priceChange = new PriceChange(); + priceChange.setAuthor(authenticationHelper.getCurrentUser()); + priceChange.setPrice(newPrice); + priceChange.setOccurredDate(ZonedDateTime.now()); + priceChange.setProgramOrderable(programOrderable); + + priceChanges.add(priceChange); + } + } diff --git a/src/test/java/org/openlmis/referencedata/util/OrderableBuilderTest.java b/src/test/java/org/openlmis/referencedata/util/OrderableBuilderTest.java index 280e2203..205be971 100644 --- a/src/test/java/org/openlmis/referencedata/util/OrderableBuilderTest.java +++ b/src/test/java/org/openlmis/referencedata/util/OrderableBuilderTest.java @@ -18,6 +18,7 @@ import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Matchers.any; @@ -28,6 +29,8 @@ import java.util.Optional; import java.util.Set; import java.util.UUID; +import org.joda.money.CurrencyUnit; +import org.joda.money.Money; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -44,6 +47,8 @@ import org.openlmis.referencedata.dto.ProgramOrderableDto; import org.openlmis.referencedata.repository.OrderableRepository; import org.openlmis.referencedata.repository.ProgramRepository; +import org.openlmis.referencedata.service.AuthenticationHelper; +import org.openlmis.referencedata.testbuilder.UserDataBuilder; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; @@ -57,6 +62,9 @@ public class OrderableBuilderTest { @Mock private OrderableRepository orderableRepository; + @Mock + private AuthenticationHelper authenticationHelper; + @InjectMocks private OrderableBuilder orderableBuilder; @@ -123,6 +131,19 @@ public void shouldIncrementVersionNumber() { assertThat(updatedOrderable.getVersionNumber(), is(2L)); } + @Test + public void shouldAddNewPriceChange() { + Program program = createProgram("test_program"); + Orderable orderable = createOrderable(program.getId()); + orderable.getProgramOrderable(program).setPricePerPack(Money.of(CurrencyUnit.of("USD"), 10)); + OrderableDto orderableDto = createOrderableDto(program.getId()); + when(authenticationHelper.getCurrentUser()).thenReturn(new UserDataBuilder().build()); + + Orderable updatedOrderable = orderableBuilder.newOrderable(orderableDto, orderable); + + assertThat(updatedOrderable.getProgramOrderable(program).getPriceChanges(), hasSize(1)); + } + private Program createProgram(String code) { Program program = new Program(UUID.randomUUID()); program.setCode(Code.code(code));