use of org.openlmis.stockmanagement.domain.event.StockEvent 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.event.StockEvent 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.event.StockEvent 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;
}
use of org.openlmis.stockmanagement.domain.event.StockEvent in project openlmis-stockmanagement by OpenLMIS.
the class StockCard method createStockCardFrom.
/**
* Create stock card from stock event dto and its line item.
*
* @param stockEventDto the origin event dto.
* @param eventLineItem event line item.
* @param savedEventId the saved event id.
* @return Created stock card.
*/
public static StockCard createStockCardFrom(StockEventDto stockEventDto, StockEventLineItemDto eventLineItem, UUID savedEventId) {
StockCardBuilder builder = StockCard.builder();
if (null != savedEventId) {
StockEvent event = new StockEvent();
event.setId(savedEventId);
builder = builder.originEvent(event);
}
return builder.programId(stockEventDto.getProgramId()).facilityId(stockEventDto.getFacilityId()).orderableId(eventLineItem.getOrderableId()).lotId(eventLineItem.getLotId()).lineItems(new ArrayList<>()).stockOnHand(0).build();
}
use of org.openlmis.stockmanagement.domain.event.StockEvent 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;
}
Aggregations