Search in sources :

Example 11 with StockCard

use of org.openlmis.stockmanagement.domain.card.StockCard 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)

Example 12 with StockCard

use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.

the class StockCardService method findStockCardById.

/**
 * Find stock card by stock card id.
 *
 * @param stockCardId stock card id.
 * @return the found stock card.
 */
public StockCardDto findStockCardById(UUID stockCardId) {
    StockCard card = cardRepository.findOne(stockCardId);
    if (card == null) {
        return null;
    }
    StockCard foundCard = card.shallowCopy();
    LOGGER.debug("Stock card found");
    permissionService.canViewStockCard(foundCard.getProgramId(), foundCard.getFacilityId());
    StockCardDto cardDto = createDtos(singletonList(foundCard)).get(0);
    cardDto.setOrderable(orderableRefDataService.findOne(foundCard.getOrderableId()));
    if (cardDto.hasLot()) {
        cardDto.setLot(lotReferenceDataService.findOne(cardDto.getLot().getId()));
    }
    assignSourceDestinationReasonNameForLineItems(cardDto);
    return cardDto;
}
Also used : StockCard(org.openlmis.stockmanagement.domain.card.StockCard) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto)

Example 13 with StockCard

use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.

the class StockEventProcessContextBuilder method buildContext.

/**
 * Before processing events, put all needed ref data into context so we don't have to do frequent
 * network requests.
 *
 * @param eventDto event dto.
 * @return a context object that includes all needed ref data.
 */
public StockEventProcessContext buildContext(StockEventDto eventDto) {
    XLOGGER.entry(eventDto);
    Profiler profiler = new Profiler("BUILD_CONTEXT");
    profiler.setLogger(XLOGGER);
    LOGGER.info("build stock event process context");
    StockEventProcessContext context = new StockEventProcessContext();
    profiler.start("CREATE_LAZY_USER");
    OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
    Supplier<UUID> userIdSupplier;
    if (authentication.isClientOnly()) {
        userIdSupplier = eventDto::getUserId;
    } else {
        userIdSupplier = () -> authenticationHelper.getCurrentUser().getId();
    }
    LazyResource<UUID> userId = new LazyResource<>(userIdSupplier);
    context.setCurrentUserId(userId);
    profiler.start("CREATE_LAZY_PROGRAM");
    UUID programId = eventDto.getProgramId();
    Supplier<ProgramDto> programSupplier = new ReferenceDataSupplier<>(programService, programId);
    LazyResource<ProgramDto> program = new LazyResource<>(programSupplier);
    context.setProgram(program);
    profiler.start("CREATE_LAZY_FACILITY");
    UUID facilityId = eventDto.getFacilityId();
    Supplier<FacilityDto> facilitySupplier = new ReferenceDataSupplier<>(facilityService, facilityId);
    LazyResource<FacilityDto> facility = new LazyResource<>(facilitySupplier);
    context.setFacility(facility);
    profiler.start("CREATE_LAZY_APPROVED_PRODUCTS");
    Supplier<List<OrderableDto>> productsSupplier = () -> orderableReferenceDataService.findAll();
    LazyList<OrderableDto> products = new LazyList<>(productsSupplier);
    context.setAllApprovedProducts(products);
    profiler.start("CREATE_LAZY_LOTS");
    Supplier<List<LotDto>> lotsSupplier = () -> getLots(eventDto);
    LazyList<LotDto> lots = new LazyList<>(lotsSupplier);
    LazyGrouping<UUID, LotDto> lotsGroupedById = new LazyGrouping<>(lots, LotDto::getId);
    context.setLots(lotsGroupedById);
    profiler.start("CREATE_LAZY_EVENT_REASONS");
    Supplier<List<StockCardLineItemReason>> eventReasonsSupplier = () -> reasonRepository.findByIdIn(eventDto.getReasonIds());
    LazyList<StockCardLineItemReason> eventReasons = new LazyList<>(eventReasonsSupplier);
    LazyGrouping<UUID, StockCardLineItemReason> eventReasonsGroupedById = new LazyGrouping<>(eventReasons, StockCardLineItemReason::getId);
    context.setEventReasons(eventReasonsGroupedById);
    profiler.start("CREATE_LAZY_NODES");
    Supplier<List<Node>> nodesSupplier = () -> nodeRepository.findByIdIn(eventDto.getNodeIds());
    LazyList<Node> nodes = new LazyList<>(nodesSupplier);
    LazyGrouping<UUID, Node> nodesGroupedById = new LazyGrouping<>(nodes, Node::getId);
    context.setNodes(nodesGroupedById);
    profiler.start("CREATE_LAZY_STOCK_CARDS");
    Supplier<List<StockCard>> cardsSupplier = () -> stockCardRepository.findByProgramIdAndFacilityId(eventDto.getProgramId(), eventDto.getFacilityId());
    LazyList<StockCard> cards = new LazyList<>(cardsSupplier);
    LazyGrouping<OrderableLotIdentity, StockCard> cardsGroupedByIdentity = new LazyGrouping<>(cards, OrderableLotIdentity::identityOf);
    context.setCards(cardsGroupedByIdentity);
    profiler.start("CREATE_LAZY_CARD_REASONS");
    Supplier<List<StockCardLineItemReason>> cardReasonsSupplier = () -> getCardReasons(eventDto);
    LazyList<StockCardLineItemReason> cardReasons = new LazyList<>(cardReasonsSupplier);
    LazyGrouping<UUID, StockCardLineItemReason> cardReasonsGroupedById = new LazyGrouping<>(cardReasons, StockCardLineItemReason::getId);
    context.setCardReasons(cardReasonsGroupedById);
    profiler.start("CREATE_LAZY_SOURCES");
    Supplier<List<ValidSourceAssignment>> sourcesSupplier = () -> validSourceAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
    LazyList<ValidSourceAssignment> sources = new LazyList<>(sourcesSupplier);
    context.setSources(sources);
    profiler.start("CREATE_LAZY_DESTINATIONS");
    Supplier<List<ValidDestinationAssignment>> destinationsSupplier = () -> validDestinationAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
    LazyList<ValidDestinationAssignment> destinations = new LazyList<>(destinationsSupplier);
    context.setDestinations(destinations);
    profiler.stop().log();
    XLOGGER.exit(context);
    return context;
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LazyList(org.openlmis.stockmanagement.util.LazyList) LazyResource(org.openlmis.stockmanagement.util.LazyResource) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) Profiler(org.slf4j.profiler.Profiler) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) LazyList(org.openlmis.stockmanagement.util.LazyList) List(java.util.List) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) ReferenceDataSupplier(org.openlmis.stockmanagement.util.ReferenceDataSupplier) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) LazyGrouping(org.openlmis.stockmanagement.util.LazyGrouping) StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)

Example 14 with StockCard

use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.

the class QuantityValidatorTest method shouldRejectWhenAnyAdjustmentHasNegativeQuantity.

@Test
public void shouldRejectWhenAnyAdjustmentHasNegativeQuantity() throws Exception {
    // expect
    expectedException.expect(ValidationMessageException.class);
    expectedException.expectMessage(ERROR_EVENT_ADJUSTMENT_QUANITITY_INVALID);
    // given
    LocalDate firstDate = dateFromYear(2015);
    StockCardLineItem lineItem = createCreditLineItem(firstDate.plusDays(1), 15);
    StockCard card = new StockCard();
    card.setLineItems(singletonList(lineItem));
    StockEventDto event = spy(createPhysicalInventoryEventDto(firstDate.plusDays(2), 5, singletonList(createCreditAdjustment(-2))));
    mockCardFound(event, card);
    // when
    quantityValidator.validate(event);
}
Also used : StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) StockEventDtoDataBuilder.createStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 15 with StockCard

use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.

the class QuantityValidatorTest method shouldNotRejectWhenStockOnHandWithAdjustmentsDoesNotMatchQuantity.

@Test
public void shouldNotRejectWhenStockOnHandWithAdjustmentsDoesNotMatchQuantity() {
    // given
    LocalDate firstDate = dateFromYear(2015);
    StockCardLineItem lineItem = createCreditLineItem(firstDate.plusDays(1), 15);
    StockCard card = new StockCard();
    card.setLineItems(newArrayList(lineItem));
    StockEventDto event = spy(createPhysicalInventoryEventDto(firstDate.plusDays(2), 5, singletonList(createCreditAdjustment(5))));
    mockCardFound(event, card);
    // when
    quantityValidator.validate(event);
}
Also used : StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) StockEventDtoDataBuilder.createStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

StockCard (org.openlmis.stockmanagement.domain.card.StockCard)35 Test (org.junit.Test)20 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)12 UUID (java.util.UUID)11 StockCardLineItem (org.openlmis.stockmanagement.domain.card.StockCardLineItem)11 LocalDate (java.time.LocalDate)10 StockEventDtoDataBuilder.createStockEventDto (org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto)9 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)6 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)6 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)6 List (java.util.List)5 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)5 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)5 UUID.randomUUID (java.util.UUID.randomUUID)4 StockEventLineItemDto (org.openlmis.stockmanagement.dto.StockEventLineItemDto)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)3 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)3 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)3