Search in sources :

Example 1 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class StockCardSummariesServiceTest method shouldCreateDummyCards.

@Test
public void shouldCreateDummyCards() throws Exception {
    // given
    UUID orderable1Id = randomUUID();
    UUID orderable2Id = randomUUID();
    UUID orderable3Id = randomUUID();
    UUID orderable4Id = randomUUID();
    OrderableDto orderable1 = createOrderableDto(orderable1Id, "1");
    OrderableDto orderable2 = createOrderableDto(orderable2Id, "2");
    orderable2.getIdentifiers().put("tradeItem", randomUUID().toString());
    OrderableDto orderable3 = createOrderableDto(orderable3Id, "3");
    OrderableDto orderable4 = createOrderableDto(orderable4Id, "4");
    UUID programId = randomUUID();
    UUID facilityId = randomUUID();
    // 1,2,3,4 all approved
    when(orderableReferenceDataService.findAll()).thenReturn(asList(orderable1, orderable2, orderable3, orderable4));
    // but only 1, 3 have cards. 2, 4 don't have cards.
    when(cardRepository.getIdentitiesBy(programId, facilityId)).thenReturn(asList(new OrderableLotIdentity(orderable1Id, null), new OrderableLotIdentity(orderable3Id, null)));
    LotDto lotDto = new LotDto();
    lotDto.setId(randomUUID());
    // 2 has a lot
    when(lotReferenceDataService.getAllLotsOf(fromString(orderable2.getIdentifiers().get("tradeItem")))).thenReturn(singletonList(lotDto));
    // when
    List<StockCardDto> cardDtos = stockCardSummariesService.createDummyStockCards(programId, facilityId);
    // then
    assertThat(cardDtos.size(), is(3));
    String orderablePropertyName = "orderable";
    String lotPropertyName = "lot";
    String idPropertyName = "id";
    String lineItemsPropertyName = "lineItems";
    String stockOnHandPropertyName = "stockOnHand";
    String lastUpdatePropertyName = "lastUpdate";
    // 2 and lot
    assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable2)), hasProperty(lotPropertyName, is(lotDto)), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
    // 2 and no lot
    assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable2)), hasProperty(lotPropertyName, is(nullValue())), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lastUpdatePropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
    // 4 and no lot
    assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable4)), hasProperty(lotPropertyName, is(nullValue())), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lastUpdatePropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID.fromString(java.util.UUID.fromString) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) Test(org.junit.Test)

Example 2 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class StockEventNotificationProcessorTest method shouldCallStockoutNotifierForEveryCard.

@Test
public void shouldCallStockoutNotifierForEveryCard() throws Exception {
    // given
    UUID anotherStockCardId = UUID.randomUUID();
    UUID anotherOrderableId = UUID.randomUUID();
    UUID anotherLotId = UUID.randomUUID();
    StockCard anotherStockCard = new StockCard(null, facilityId, programId, orderableId, lotId, null, 0);
    anotherStockCard.setId(anotherStockCardId);
    StockEventLineItemDto secondLineItem = createStockEventLineItem();
    secondLineItem.setOrderableId(anotherOrderableId);
    secondLineItem.setLotId(anotherLotId);
    secondLineItem.setQuantity(0);
    stockEventDto.setLineItems(Arrays.asList(firstLineItem, secondLineItem));
    when(context.findCard(new OrderableLotIdentity(orderableId, lotId))).thenReturn(stockCard);
    when(context.findCard(new OrderableLotIdentity(anotherOrderableId, anotherLotId))).thenReturn(anotherStockCard);
    // when
    stockEventNotificationProcessor.callAllNotifications(stockEventDto);
    // then
    verify(stockoutNotifier, times(2)).notifyStockEditors(any(StockCard.class));
}
Also used : StockEventLineItemDto(org.openlmis.stockmanagement.dto.StockEventLineItemDto) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryService method submitPhysicalInventory.

/**
 * Persist physical inventory, with an event id.
 *
 * @param inventoryDto inventoryDto.
 * @param eventId      eventId.
 */
void submitPhysicalInventory(PhysicalInventoryDto inventoryDto, UUID eventId) {
    LOGGER.info("submit physical inventory");
    PhysicalInventory inventory = inventoryDto.toPhysicalInventoryForSubmit();
    if (null != eventId) {
        StockEvent event = new StockEvent();
        event.setId(eventId);
        inventory.setStockEvent(event);
    }
    Map<OrderableLotIdentity, StockCard> cards = stockCardRepository.findByProgramIdAndFacilityId(inventory.getProgramId(), inventory.getFacilityId()).stream().collect(toMap(OrderableLotIdentity::identityOf, card -> card));
    for (PhysicalInventoryLineItem line : inventory.getLineItems()) {
        StockCard foundCard = cards.get(OrderableLotIdentity.identityOf(line));
        // modified during recalculation, this will avoid persistence of those modified models
        if (foundCard != null) {
            StockCard stockCard = foundCard.shallowCopy();
            stockCard.calculateStockOnHand();
            line.setPreviousStockOnHandWhenSubmitted(stockCard.getStockOnHand());
        }
    }
    physicalInventoriesRepository.save(inventory);
}
Also used : StockCard(org.openlmis.stockmanagement.domain.card.StockCard) PhysicalInventoryValidator(org.openlmis.stockmanagement.validators.PhysicalInventoryValidator) Logger(org.slf4j.Logger) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) PhysicalInventoryLineItem(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem) ERROR_PHYSICAL_INVENTORY_NOT_FOUND(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_NOT_FOUND) PhysicalInventoriesRepository(org.openlmis.stockmanagement.repository.PhysicalInventoriesRepository) UUID(java.util.UUID) PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto) StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Message(org.openlmis.stockmanagement.util.Message) List(java.util.List) PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) Collectors.toMap(java.util.stream.Collectors.toMap) ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED) Service(org.springframework.stereotype.Service) ERROR_PHYSICAL_INVENTORY_DRAFT_EXISTS(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_DRAFT_EXISTS) Map(java.util.Map) ResourceNotFoundException(org.openlmis.stockmanagement.exception.ResourceNotFoundException) StockCardRepository(org.openlmis.stockmanagement.repository.StockCardRepository) Collections(java.util.Collections) PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) PhysicalInventoryLineItem(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem) StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCard(org.openlmis.stockmanagement.domain.card.StockCard)

Example 4 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class StockCardService method findOrCreateCard.

private StockCard findOrCreateCard(StockEventDto eventDto, StockEventLineItemDto eventLineItem, UUID savedEventId, List<StockCard> cardsToUpdate) {
    OrderableLotIdentity identity = identityOf(eventLineItem);
    StockCard card = eventDto.getContext().findCard(identity);
    if (null == card) {
        card = cardsToUpdate.stream().filter(elem -> identityOf(elem).equals(identity)).findFirst().orElse(null);
    }
    if (null == card) {
        card = createStockCardFrom(eventDto, eventLineItem, savedEventId);
    }
    if (cardsToUpdate.stream().noneMatch(elem -> identityOf(elem).equals(identity))) {
        cardsToUpdate.add(card);
    }
    return card;
}
Also used : FacilityReferenceDataService(org.openlmis.stockmanagement.service.referencedata.FacilityReferenceDataService) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) OrganizationRepository(org.openlmis.stockmanagement.repository.OrganizationRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils.isEmpty(org.apache.commons.collections4.CollectionUtils.isEmpty) OrderableLotIdentity.identityOf(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity.identityOf) Collections.singletonList(java.util.Collections.singletonList) StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) HashSet(java.util.HashSet) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Lists(com.google.common.collect.Lists) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) LotReferenceDataService(org.openlmis.stockmanagement.service.referencedata.LotReferenceDataService) Service(org.springframework.stereotype.Service) PHYSICAL_INVENTORY(org.openlmis.stockmanagement.domain.reason.ReasonCategory.PHYSICAL_INVENTORY) Pageable(org.springframework.data.domain.Pageable) StockCardRepository(org.openlmis.stockmanagement.repository.StockCardRepository) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) STOCK_CARDS_VIEW(org.openlmis.stockmanagement.service.PermissionService.STOCK_CARDS_VIEW) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) StockEventLineItemDto(org.openlmis.stockmanagement.dto.StockEventLineItemDto) Logger(org.slf4j.Logger) PermissionStrings(org.openlmis.stockmanagement.service.referencedata.PermissionStrings) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) Collection(java.util.Collection) Set(java.util.Set) Pagination(org.openlmis.stockmanagement.web.Pagination) UUID(java.util.UUID) NotNull(javax.validation.constraints.NotNull) Page(org.springframework.data.domain.Page) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) PermissionStringDto(org.openlmis.stockmanagement.service.referencedata.PermissionStringDto) List(java.util.List) MessageService(org.openlmis.stockmanagement.i18n.MessageService) AuthenticationHelper(org.openlmis.stockmanagement.util.AuthenticationHelper) Message(org.openlmis.stockmanagement.util.Message) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) StockCardLineItem.createLineItemFrom(org.openlmis.stockmanagement.domain.card.StockCardLineItem.createLineItemFrom) StockCard.createStockCardFrom(org.openlmis.stockmanagement.domain.card.StockCard.createStockCardFrom) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCard(org.openlmis.stockmanagement.domain.card.StockCard)

Example 5 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class StockCardSummariesService method createOrderableLots.

private Map<OrderableLotIdentity, OrderableLot> createOrderableLots(List<OrderableDto> orderableDtos) {
    Stream<OrderableLot> orderableLots = orderableDtos.stream().flatMap(this::lotsOfOrderable);
    Stream<OrderableLot> orderablesOnly = orderableDtos.stream().map(orderableDto -> new OrderableLot(orderableDto, null));
    return concat(orderableLots, orderablesOnly).collect(toMap(OrderableLotIdentity::identityOf, orderableLot -> orderableLot));
}
Also used : Stream.empty(java.util.stream.Stream.empty) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) Getter(lombok.Getter) OrderableFulfillReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableFulfillReferenceDataService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApprovedProductReferenceDataService(org.openlmis.stockmanagement.service.referencedata.ApprovedProductReferenceDataService) OrderableLotIdentity.identityOf(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity.identityOf) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Collectors.toMap(java.util.stream.Collectors.toMap) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LotReferenceDataService(org.openlmis.stockmanagement.service.referencedata.LotReferenceDataService) Service(org.springframework.stereotype.Service) Map(java.util.Map) Stream.concat(java.util.stream.Stream.concat) Pageable(org.springframework.data.domain.Pageable) StockCardRepository(org.openlmis.stockmanagement.repository.StockCardRepository) OrderableFulfillDto(org.openlmis.stockmanagement.dto.referencedata.OrderableFulfillDto) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) IdentifiableByOrderableLot(org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot) PageImpl(org.springframework.data.domain.PageImpl) IdentifiableByOrderableLot(org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot)

Aggregations

OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)12 UUID (java.util.UUID)6 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)6 Test (org.junit.Test)5 List (java.util.List)4 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)4 Message (org.openlmis.stockmanagement.util.Message)4 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)3 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)3 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)3 StockCardRepository (org.openlmis.stockmanagement.repository.StockCardRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Service (org.springframework.stereotype.Service)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors.toMap (java.util.stream.Collectors.toMap)2