Search in sources :

Example 6 with StockEvent

use of org.openlmis.stockmanagement.domain.event.StockEvent in project openlmis-stockmanagement by OpenLMIS.

the class StockCardServiceIntegrationTest method shouldGetRefdataAndConvertOrganizationsWhenFindStockCard.

@Test
public void shouldGetRefdataAndConvertOrganizationsWhenFindStockCard() throws Exception {
    // given
    UUID userId = randomUUID();
    StockEventDto stockEventDto = createStockEventDto();
    stockEventDto.getLineItems().get(0).setLotId(randomUUID());
    // 1. mock ref data service
    FacilityDto cardFacility = new FacilityDto();
    FacilityDto sourceFacility = new FacilityDto();
    ProgramDto programDto = new ProgramDto();
    OrderableDto orderableDto = new OrderableDto();
    LotDto lotDto = new LotDto();
    when(facilityReferenceDataService.findOne(stockEventDto.getFacilityId())).thenReturn(cardFacility);
    when(facilityReferenceDataService.findOne(fromString("e6799d64-d10d-4011-b8c2-0e4d4a3f65ce"))).thenReturn(sourceFacility);
    when(programReferenceDataService.findOne(stockEventDto.getProgramId())).thenReturn(programDto);
    when(orderableReferenceDataService.findOne(stockEventDto.getLineItems().get(0).getOrderableId())).thenReturn(orderableDto);
    when(lotReferenceDataService.findOne(stockEventDto.getLineItems().get(0).getLotId())).thenReturn(lotDto);
    // 2. there is an existing stock card with line items
    StockEvent savedEvent = save(stockEventDto, userId);
    // when
    StockCard savedCard = stockCardRepository.findByOriginEvent(savedEvent);
    StockCardDto foundCardDto = stockCardService.findStockCardById(savedCard.getId());
    // then
    assertThat(foundCardDto.getFacility(), is(cardFacility));
    assertThat(foundCardDto.getProgram(), is(programDto));
    assertThat(foundCardDto.getOrderable(), is(orderableDto));
    assertThat(foundCardDto.getLot(), is(lotDto));
    StockCardLineItemDto lineItemDto = foundCardDto.getLineItems().get(0);
    assertThat(lineItemDto.getSource(), is(sourceFacility));
    assertThat(lineItemDto.getDestination().getName(), is("NGO"));
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) StockCardLineItemDto(org.openlmis.stockmanagement.dto.StockCardLineItemDto) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockEventDtoDataBuilder.createStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) BaseIntegrationTest(org.openlmis.stockmanagement.BaseIntegrationTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with StockEvent

use of org.openlmis.stockmanagement.domain.event.StockEvent 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 8 with StockEvent

use of org.openlmis.stockmanagement.domain.event.StockEvent in project openlmis-stockmanagement by OpenLMIS.

the class StockEventProcessor method saveEventAndGenerateLineItems.

private UUID saveEventAndGenerateLineItems(StockEventDto eventDto, Profiler profiler) {
    profiler.start("CONVERT_TO_EVENT");
    StockEvent stockEvent = eventDto.toEvent();
    profiler.start("DB_SAVE");
    UUID savedEventId = stockEventsRepository.save(stockEvent).getId();
    LOGGER.debug("Saved stock event with id " + savedEventId);
    if (eventDto.isPhysicalInventory()) {
        profiler.start("CREATE_PHYSICAL_INVENTORY_DTO");
        PhysicalInventoryDto inventoryDto = fromEventDto(eventDto);
        profiler.start("SUBMIT_PHYSICAL_INVENTORY");
        physicalInventoryService.submitPhysicalInventory(inventoryDto, savedEventId);
    }
    profiler.start("SAVE_FROM_EVENT");
    stockCardService.saveFromEvent(eventDto, savedEventId);
    profiler.start("CALL_NOTIFICATIONS");
    stockEventNotificationProcessor.callAllNotifications(eventDto);
    return savedEventId;
}
Also used : StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto) UUID(java.util.UUID)

Example 9 with StockEvent

use of org.openlmis.stockmanagement.domain.event.StockEvent in project openlmis-stockmanagement by OpenLMIS.

the class StockCard method createStockCardFrom.

/**
 * Create stock card from stock event dto and its line item.
 *
 * @param stockEventDto the origin event dto.
 * @param eventLineItem event line item.
 * @param savedEventId  the saved event id.
 * @return Created stock card.
 */
public static StockCard createStockCardFrom(StockEventDto stockEventDto, StockEventLineItemDto eventLineItem, UUID savedEventId) {
    StockCardBuilder builder = StockCard.builder();
    if (null != savedEventId) {
        StockEvent event = new StockEvent();
        event.setId(savedEventId);
        builder = builder.originEvent(event);
    }
    return builder.programId(stockEventDto.getProgramId()).facilityId(stockEventDto.getFacilityId()).orderableId(eventLineItem.getOrderableId()).lotId(eventLineItem.getLotId()).lineItems(new ArrayList<>()).stockOnHand(0).build();
}
Also used : StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent)

Example 10 with StockEvent

use of org.openlmis.stockmanagement.domain.event.StockEvent in project openlmis-stockmanagement by OpenLMIS.

the class StockCardLineItem method createLineItemFrom.

/**
 * Create line item from eventDto.
 *
 * @param eventDto     stock eventDto.
 * @param stockCard    the card that this line item belongs to.
 * @param savedEventId saved event id.
 * @return created line item.
 */
public static StockCardLineItem createLineItemFrom(StockEventDto eventDto, StockEventLineItemDto eventLineItem, StockCard stockCard, UUID savedEventId) {
    StockCardLineItemBuilder builder = StockCardLineItem.builder();
    if (null != savedEventId) {
        StockEvent event = new StockEvent();
        event.setId(savedEventId);
        builder = builder.originEvent(event);
    }
    if (null != eventLineItem.getReasonId()) {
        StockCardLineItemReason reason = new StockCardLineItemReason();
        reason.setId(eventLineItem.getReasonId());
        builder = builder.reason(reason);
    }
    if (null != eventLineItem.getSourceId()) {
        Node source = new Node();
        source.setId(eventLineItem.getSourceId());
        builder = builder.source(source);
    }
    if (null != eventLineItem.getDestinationId()) {
        Node destination = new Node();
        destination.setId(eventLineItem.getDestinationId());
        builder = builder.destination(destination);
    }
    StockCardLineItem cardLineItem = builder.stockCard(stockCard).quantity(eventLineItem.getQuantity()).stockAdjustments(eventLineItem.stockAdjustments()).occurredDate(eventLineItem.getOccurredDate()).processedDate(now()).reasonFreeText(eventLineItem.getReasonFreeText()).sourceFreeText(eventLineItem.getSourceFreeText()).destinationFreeText(eventLineItem.getDestinationFreeText()).documentNumber(eventDto.getDocumentNumber()).signature(eventDto.getSignature()).userId(eventDto.getContext().getCurrentUserId()).stockOnHand(0).extraData(eventLineItem.getExtraData()).build();
    stockCard.getLineItems().add(cardLineItem);
    return cardLineItem;
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node)

Aggregations

StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)18 UUID (java.util.UUID)10 Test (org.junit.Test)8 UUID.randomUUID (java.util.UUID.randomUUID)7 BaseIntegrationTest (org.openlmis.stockmanagement.BaseIntegrationTest)6 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)5 StockEventDtoDataBuilder.createStockEventDto (org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto)5 StockCardDataBuilder (org.openlmis.stockmanagement.testutils.StockCardDataBuilder)4 StockEventDataBuilder (org.openlmis.stockmanagement.testutils.StockEventDataBuilder)4 StockCardLineItem (org.openlmis.stockmanagement.domain.card.StockCardLineItem)3 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)3 Before (org.junit.Before)2 PhysicalInventoryDto (org.openlmis.stockmanagement.dto.PhysicalInventoryDto)2 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)2 StockCardLineItemDataBuilder (org.openlmis.stockmanagement.testutils.StockCardLineItemDataBuilder)2 Message (org.openlmis.stockmanagement.util.Message)2 ZonedDateTime (java.time.ZonedDateTime)1 Collections (java.util.Collections)1