use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.
the class QuantityValidator method validateEventItems.
private void validateEventItems(StockEventDto event, List<StockEventLineItemDto> items) {
StockCard foundCard = tryFindCard(event, items.get(0));
if (event.isPhysicalInventory()) {
validateQuantities(items);
}
// create line item from event line item and add it to stock card for recalculation
calculateStockOnHand(event, items, foundCard);
}
use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.
the class StockCardRepositoryIntegrationTest method generateInstance.
private StockCard generateInstance(UUID facility, UUID program, UUID product, UUID lot) {
StockEvent event = new StockEventDataBuilder().withoutId().withFacility(facility).withProgram(program).build();
event = stockEventsRepository.save(event);
StockCardLineItem lineItem = new StockCardLineItemDataBuilder().withoutId().withOriginEvent(event).build();
StockCard stockCard = new StockCardDataBuilder(event).withoutId().withOrderable(product).withLot(lot).withLineItem(lineItem).build();
lineItem.setStockCard(stockCard);
return stockCard;
}
use of org.openlmis.stockmanagement.domain.card.StockCard 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"));
}
use of org.openlmis.stockmanagement.domain.card.StockCard 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);
}
use of org.openlmis.stockmanagement.domain.card.StockCard in project openlmis-stockmanagement by OpenLMIS.
the class StockCardService method findOrCreateCard.
private StockCard findOrCreateCard(StockEventDto eventDto, StockEventLineItemDto eventLineItem, UUID savedEventId, List<StockCard> cardsToUpdate) {
OrderableLotIdentity identity = identityOf(eventLineItem);
StockCard card = eventDto.getContext().findCard(identity);
if (null == card) {
card = cardsToUpdate.stream().filter(elem -> identityOf(elem).equals(identity)).findFirst().orElse(null);
}
if (null == card) {
card = createStockCardFrom(eventDto, eventLineItem, savedEventId);
}
if (cardsToUpdate.stream().noneMatch(elem -> identityOf(elem).equals(identity))) {
cardsToUpdate.add(card);
}
return card;
}
Aggregations