Search in sources :

Example 6 with StockCardDto

use of org.openlmis.stockmanagement.dto.StockCardDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardServiceIntegrationTest method shouldGetStockCardWithCalculatedSohWhenFindStockCard.

@Test
public void shouldGetStockCardWithCalculatedSohWhenFindStockCard() throws Exception {
    // given
    StockEventDto stockEventDto = StockEventDtoDataBuilder.createStockEventDto();
    stockEventDto.getLineItems().get(0).setSourceId(null);
    stockEventDto.getLineItems().get(0).setDestinationId(null);
    StockEvent savedEvent = save(stockEventDto, randomUUID());
    // when
    UUID cardId = stockCardRepository.findByOriginEvent(savedEvent).getId();
    StockCardDto card = stockCardService.findStockCardById(cardId);
    // then
    assertThat(card.getStockOnHand(), is(stockEventDto.getLineItems().get(0).getQuantity()));
}
Also used : StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) StockEventDtoDataBuilder.createStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) BaseIntegrationTest(org.openlmis.stockmanagement.BaseIntegrationTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with StockCardDto

use of org.openlmis.stockmanagement.dto.StockCardDto 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 8 with StockCardDto

use of org.openlmis.stockmanagement.dto.StockCardDto in project openlmis-stockmanagement by OpenLMIS.

the class JasperReportService method getStockCardReportView.

/**
 * Generate stock card report in PDF format.
 *
 * @param stockCardId stock card id
 * @return generated stock card report.
 */
public ModelAndView getStockCardReportView(UUID stockCardId) {
    StockCardDto stockCardDto = stockCardService.findStockCardById(stockCardId);
    if (stockCardDto == null) {
        throw new ResourceNotFoundException(new Message(ERROR_REPORT_ID_NOT_FOUND));
    }
    Collections.reverse(stockCardDto.getLineItems());
    Map<String, Object> params = new HashMap<>();
    params.put("datasource", singletonList(stockCardDto));
    params.put("hasLot", stockCardDto.hasLot());
    return generateReport(CARD_REPORT_URL, params);
}
Also used : Message(org.openlmis.stockmanagement.util.Message) HashMap(java.util.HashMap) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) ResourceNotFoundException(org.openlmis.stockmanagement.exception.ResourceNotFoundException)

Example 9 with StockCardDto

use of org.openlmis.stockmanagement.dto.StockCardDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardBaseService method cardToDto.

private StockCardDto cardToDto(FacilityDto facility, ProgramDto program, StockCard card) {
    card.calculateStockOnHand();
    StockCardDto cardDto = StockCardDto.createFrom(card);
    cardDto.setFacility(facility);
    cardDto.setProgram(program);
    cardDto.setOrderable(OrderableDto.builder().id(card.getOrderableId()).build());
    if (card.getLotId() != null) {
        cardDto.setLot(LotDto.builder().id(card.getLotId()).build());
    }
    List<StockCardLineItemDto> lineItems = cardDto.getLineItems();
    if (!isEmpty(lineItems)) {
        cardDto.setLastUpdate(lineItems.get(lineItems.size() - 1).getLineItem().getOccurredDate());
        cardDto.setExtraData(lineItems.get(lineItems.size() - 1).getLineItem().getExtraData());
    }
    return cardDto;
}
Also used : StockCardLineItemDto(org.openlmis.stockmanagement.dto.StockCardLineItemDto) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto)

Example 10 with StockCardDto

use of org.openlmis.stockmanagement.dto.StockCardDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardService method search.

/**
 * Find stock card page by parameters. Allowed multiple id parameters.
 *
 * @param ids      collection of ids for batch fetch
 * @param pageable pagination and sorting parameters
 * @return page of filtered stock cards.
 */
public Page<StockCardDto> search(@NotNull Collection<UUID> ids, Pageable pageable) {
    OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
    Page<StockCard> page;
    if (!authentication.isClientOnly()) {
        UserDto user = authenticationHelper.getCurrentUser();
        LOGGER.info("list of ids:" + ids);
        PermissionStrings.Handler handler = permissionService.getPermissionStrings(user.getId());
        Set<PermissionStringDto> permissionStrings = handler.get();
        LOGGER.info("list of permission strings:" + permissionStrings);
        Set<UUID> facilityIds = new HashSet<>();
        Set<UUID> programIds = new HashSet<>();
        permissionStrings.stream().filter(permissionString -> STOCK_CARDS_VIEW.equalsIgnoreCase(permissionString.getRightName())).forEach(permission -> {
            facilityIds.add(permission.getFacilityId());
            programIds.add(permission.getProgramId());
        });
        LOGGER.info("list of facility ids:" + facilityIds);
        LOGGER.info("list of program ids:" + programIds);
        if (isEmpty(ids)) {
            page = cardRepository.findByFacilityIdInAndProgramIdIn(facilityIds, programIds, pageable);
        } else {
            page = cardRepository.findByFacilityIdInAndProgramIdInAndIdIn(facilityIds, programIds, ids, pageable);
        }
    } else {
        if (isEmpty(ids)) {
            page = cardRepository.findAll(pageable);
        } else {
            page = cardRepository.findByIdIn(ids, pageable);
        }
    }
    return Pagination.getPage(createDtos(page.getContent()), pageable, page.getTotalElements());
}
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) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) PermissionStringDto(org.openlmis.stockmanagement.service.referencedata.PermissionStringDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) PermissionStrings(org.openlmis.stockmanagement.service.referencedata.PermissionStrings) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)16 UUID (java.util.UUID)10 Test (org.junit.Test)8 UUID.randomUUID (java.util.UUID.randomUUID)7 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)5 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)5 BaseIntegrationTest (org.openlmis.stockmanagement.BaseIntegrationTest)4 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)4 List (java.util.List)3 UUID.fromString (java.util.UUID.fromString)3 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)3 StockCardLineItemDto (org.openlmis.stockmanagement.dto.StockCardLineItemDto)3 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)3 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Collections.singletonList (java.util.Collections.singletonList)2 HashMap (java.util.HashMap)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)2 StockEventDtoDataBuilder.createStockEventDto (org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto)2