Search in sources :

Example 11 with PhysicalInventoryDto

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

the class PhysicalInventoryControllerIntegrationTest method shouldReturnBadRequestOnSaveDraftWhenEntityInvalid.

@Test
public void shouldReturnBadRequestOnSaveDraftWhenEntityInvalid() {
    // given
    UUID physicalInventoryId = UUID.randomUUID();
    PhysicalInventoryDto expectedDraft = generatePhysicalInventory();
    when(physicalInventoryService.saveDraft(expectedDraft, physicalInventoryId)).thenThrow(new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_LINE_ITEMS_MISSING));
    // when
    restAssured.given().header(HttpHeaders.AUTHORIZATION, getTokenHeader()).contentType(APPLICATION_JSON).pathParam("id", physicalInventoryId).body(expectedDraft).when().put(ID_URL).then().statusCode(400);
    // then
    assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
Also used : PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with PhysicalInventoryDto

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

the class PhysicalInventoryControllerIntegrationTest method generatePhysicalInventory.

private PhysicalInventoryDto generatePhysicalInventory() {
    PhysicalInventoryDto inventory = new PhysicalInventoryDto();
    inventory.setId(UUID.randomUUID());
    inventory.setFacilityId(UUID.randomUUID());
    inventory.setProgramId(UUID.randomUUID());
    inventory.setOccurredDate(LocalDate.now());
    inventory.setDocumentNumber("number");
    inventory.setIsStarter(false);
    inventory.setSignature("signature");
    inventory.setLineItems(Collections.singletonList(generatePhysicalInventoryLineItem()));
    return inventory;
}
Also used : PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto)

Example 13 with PhysicalInventoryDto

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

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

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

the class PhysicalInventoryValidatorTest method testValidateBasedOnIfExistingIsDraft.

private void testValidateBasedOnIfExistingIsDraft(boolean isDraft) {
    PhysicalInventoryDto inventory = newInventory();
    PhysicalInventory existingInventory = mock(PhysicalInventory.class);
    when(existingInventory.getIsDraft()).thenReturn(isDraft);
    when(repository.findOne(inventory.getId())).thenReturn(existingInventory);
    validator.validateDraft(inventory, inventory.getId());
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto)

Aggregations

PhysicalInventoryDto (org.openlmis.stockmanagement.dto.PhysicalInventoryDto)28 Test (org.junit.Test)20 UUID (java.util.UUID)11 PhysicalInventory (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory)7 UUID.randomUUID (java.util.UUID.randomUUID)5 PhysicalInventoryLineItemDto (org.openlmis.stockmanagement.dto.PhysicalInventoryLineItemDto)3 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)3 List (java.util.List)2 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)2 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Random (java.util.Random)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)1 PhysicalInventoryLineItem (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem)1 ResourceNotFoundException (org.openlmis.stockmanagement.exception.ResourceNotFoundException)1 ERROR_PHYSICAL_INVENTORY_DRAFT_EXISTS (org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_DRAFT_EXISTS)1 ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED (org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED)1