Search in sources :

Example 6 with PhysicalInventory

use of org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryDtoTest method shouldConvertIntoInventoryJpaModelForDraft.

@Test
public void shouldConvertIntoInventoryJpaModelForDraft() throws Exception {
    // given
    PhysicalInventoryDto piDto = createInventoryDto();
    // when
    PhysicalInventory inventory = piDto.toPhysicalInventoryForDraft();
    // then
    fieldsEqual(piDto, inventory);
    assertThat(inventory.getIsDraft(), is(true));
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) Test(org.junit.Test)

Example 7 with PhysicalInventory

use of org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryDtoTest method shouldCreateDtoFromJpaModel.

@Test
public void shouldCreateDtoFromJpaModel() throws Exception {
    // given
    PhysicalInventory inventory = createInventoryDto().toPhysicalInventoryForDraft();
    // when
    PhysicalInventoryDto dto = PhysicalInventoryDto.from(inventory);
    // then
    assertThat(dto.getProgramId(), is(inventory.getProgramId()));
    assertThat(dto.getFacilityId(), is(inventory.getFacilityId()));
    assertThat(dto.getOccurredDate(), is(inventory.getOccurredDate()));
    assertThat(dto.getDocumentNumber(), is(inventory.getDocumentNumber()));
    assertThat(dto.getSignature(), is(inventory.getSignature()));
    assertThat(dto.getIsStarter(), is(false));
    assertThat(dto.getLineItems().size(), is(1));
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) Test(org.junit.Test)

Example 8 with PhysicalInventory

use of org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory 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 9 with PhysicalInventory

use of org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryService method createNewDraft.

/**
 * Create new draft.
 *
 * @param dto physical inventory dto.
 * @return the saved inventory.
 */
public PhysicalInventoryDto createNewDraft(PhysicalInventoryDto dto) {
    LOGGER.info("create physical inventory draft");
    physicalInventoryValidator.validateEmptyDraft(dto);
    checkPermission(dto.getProgramId(), dto.getFacilityId());
    checkIfDraftExists(dto);
    dto.setId(null);
    PhysicalInventory save = physicalInventoriesRepository.save(dto.toEmptyPhysicalInventory());
    dto.setId(save.getId());
    return dto;
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory)

Example 10 with PhysicalInventory

use of org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryService method deletePhysicalInventory.

/**
 * Delete draft.
 *
 * @param id physical inventory id.
 */
public void deletePhysicalInventory(UUID id) {
    PhysicalInventory foundInventory = physicalInventoriesRepository.findOne(id);
    if (foundInventory != null) {
        checkPermission(foundInventory.getProgramId(), foundInventory.getFacilityId());
        if (!foundInventory.getIsDraft()) {
            throw new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED);
        }
        physicalInventoriesRepository.delete(foundInventory);
    } else {
        throw new ResourceNotFoundException(new Message(ERROR_PHYSICAL_INVENTORY_NOT_FOUND, id));
    }
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) ResourceNotFoundException(org.openlmis.stockmanagement.exception.ResourceNotFoundException)

Aggregations

PhysicalInventory (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory)23 Test (org.junit.Test)12 UUID (java.util.UUID)7 PhysicalInventoryDto (org.openlmis.stockmanagement.dto.PhysicalInventoryDto)7 UUID.randomUUID (java.util.UUID.randomUUID)6 ResourceNotFoundException (org.openlmis.stockmanagement.exception.ResourceNotFoundException)3 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)3 Message (org.openlmis.stockmanagement.util.Message)3 PhysicalInventoryLineItemDto (org.openlmis.stockmanagement.dto.PhysicalInventoryLineItemDto)2 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)1 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)1 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)1 PhysicalInventoryLineItem (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem)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 ERROR_PHYSICAL_INVENTORY_NOT_FOUND (org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PHYSICAL_INVENTORY_NOT_FOUND)1