Search in sources :

Example 11 with StockCardLineItemReason

use of org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason in project openlmis-stockmanagement by OpenLMIS.

the class AdjustmentReasonValidatorIntegrationTest method incorrectReasonCategoryShouldNotPassWhenEventHasNoSourceAndDestination.

@Test
public void incorrectReasonCategoryShouldNotPassWhenEventHasNoSourceAndDestination() throws Exception {
    // given
    StockCardLineItemReason reason = StockCardLineItemReason.builder().reasonType(ReasonType.CREDIT).reasonCategory(ReasonCategory.TRANSFER).name("Credit Ad_hoc").isFreeTextAllowed(false).build();
    StockEventDto stockEventDto = StockEventDtoDataBuilder.createNoSourceDestinationStockEventDto();
    stockEventDto.getLineItems().get(0).setReasonId(reasonRepository.save(reason).getId());
    setContext(stockEventDto);
    expectedEx.expect(ValidationMessageException.class);
    expectedEx.expectMessage(ERROR_EVENT_ADJUSTMENT_REASON_CATEGORY_INVALID + ": " + reason.getReasonCategory());
    // when
    adjustmentReasonValidator.validate(stockEventDto);
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) BaseIntegrationTest(org.openlmis.stockmanagement.BaseIntegrationTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with StockCardLineItemReason

use of org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason 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 13 with StockCardLineItemReason

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

Example 14 with StockCardLineItemReason

use of org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason in project openlmis-stockmanagement by OpenLMIS.

the class StockCardDtoDataBuilder method createStockCardDto.

/**
 * Create stock card dto.
 *
 * @return stock card dto
 */
public static StockCardDto createStockCardDto() {
    StockCardLineItemReason reason = StockCardLineItemReason.builder().name("Transfer In").reasonCategory(ReasonCategory.ADJUSTMENT).reasonType(ReasonType.CREDIT).build();
    StockCardLineItem lineItem = StockCardLineItem.builder().stockOnHand(1).quantity(1).occurredDate(LocalDate.of(2017, 2, 13)).reason(reason).build();
    StockCardLineItemDto lineItemDto = StockCardLineItemDto.builder().lineItem(lineItem).source(FacilityDto.builder().name("HF1").build()).build();
    return StockCardDto.builder().stockOnHand(1).facility(FacilityDto.builder().name("HC01").build()).program(ProgramDto.builder().name("HIV").build()).orderable(OrderableDto.builder().productCode("ABC01").build()).lineItems(Arrays.asList(lineItemDto)).build();
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) StockCardLineItemDto(org.openlmis.stockmanagement.dto.StockCardLineItemDto) StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem)

Example 15 with StockCardLineItemReason

use of org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason in project openlmis-stockmanagement by OpenLMIS.

the class StockCardLineItemReasonDataBuilder method build.

/**
 * Creates new instance of {@link StockCardLineItemReason} with properties.
 * @return created stock card line item reason.
 */
public StockCardLineItemReason build() {
    StockCardLineItemReason reason = new StockCardLineItemReason(name, description, reasonType, reasonCategory, isFreeTextAllowed);
    reason.setId(id);
    return reason;
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)

Aggregations

StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)28 Test (org.junit.Test)18 StockCardLineItemReasonDataBuilder (org.openlmis.stockmanagement.testutils.StockCardLineItemReasonDataBuilder)8 ResultActions (org.springframework.test.web.servlet.ResultActions)6 UUID (java.util.UUID)4 ValidReasonAssignment (org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment)4 UUID.randomUUID (java.util.UUID.randomUUID)3 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)3 BaseIntegrationTest (org.openlmis.stockmanagement.BaseIntegrationTest)2 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)2 StockCardLineItem (org.openlmis.stockmanagement.domain.card.StockCardLineItem)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)2 ValidReasonAssignmentDto (org.openlmis.stockmanagement.dto.ValidReasonAssignmentDto)2 Profiler (org.slf4j.profiler.Profiler)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 List (java.util.List)1 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)1 PhysicalInventoryLineItemAdjustment (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItemAdjustment)1 ValidDestinationAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment)1