Example #1
0
  /**
   * Creates position with given field values (with the same base and given unit)
   *
   * @param product
   * @param quantity
   * @param price
   * @param batch
   * @param expirationDate
   * @param productionDate
   * @param resource
   * @return Created position entity
   */
  public Entity createPosition(
      final Entity product,
      final BigDecimal quantity,
      final BigDecimal price,
      final String batch,
      final Date productionDate,
      final Date expirationDate,
      final Entity resource) {
    DataDefinition positionDD =
        dataDefinitionService.get(
            MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
            MaterialFlowResourcesConstants.MODEL_POSITION);
    Entity position = positionDD.create();

    position.setField(PositionFields.PRODUCT, product);
    position.setField(PositionFields.QUANTITY, quantity);
    position.setField(PositionFields.GIVEN_QUANTITY, quantity);
    position.setField(PositionFields.GIVEN_UNIT, product.getStringField(ProductFields.UNIT));
    position.setField(PositionFields.PRICE, price);
    position.setField(PositionFields.BATCH, batch);
    position.setField(PositionFields.PRODUCTION_DATE, productionDate);
    position.setField(PositionFields.EXPIRATION_DATE, expirationDate);
    position.setField(PositionFields.RESOURCE, resource);
    return position;
  }
  public Entity createResource(
      final Entity position,
      final Entity warehouse,
      final Entity resource,
      final BigDecimal quantity,
      Object date) {
    DataDefinition resourceDD =
        dataDefinitionService.get(
            MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
            MaterialFlowResourcesConstants.MODEL_RESOURCE);

    Entity newResource = resourceDD.create();

    if (position != null) {
      Entity document = position.getBelongsToField(PositionFields.DOCUMENT);

      if (document != null) {
        Entity user = document.getBelongsToField(DocumentFields.USER);

        newResource.setField(
            ResourceFields.USER_NAME,
            user.getStringField(_FIRST_NAME) + " " + user.getStringField(L_LAST_NAME));
      }
    }

    newResource.setField(ResourceFields.TIME, date);
    newResource.setField(ResourceFields.LOCATION, warehouse);
    newResource.setField(
        ResourceFields.PRODUCT, resource.getBelongsToField(PositionFields.PRODUCT));
    newResource.setField(ResourceFields.QUANTITY, quantity);
    newResource.setField(ResourceFields.AVAILABLE_QUANTITY, quantity);
    newResource.setField(ResourceFields.RESERVED_QUANTITY, BigDecimal.ZERO);
    newResource.setField(ResourceFields.PRICE, resource.getField(PositionFields.PRICE));
    newResource.setField(ResourceFields.BATCH, resource.getField(PositionFields.BATCH));
    newResource.setField(
        ResourceFields.EXPIRATION_DATE, resource.getField(PositionFields.EXPIRATION_DATE));
    newResource.setField(
        ResourceFields.PRODUCTION_DATE, resource.getField(PositionFields.PRODUCTION_DATE));
    newResource.setField(
        ResourceFields.STORAGE_LOCATION, resource.getField(ResourceFields.STORAGE_LOCATION));
    newResource.setField(
        ResourceFields.ADDITIONAL_CODE, resource.getField(ResourceFields.ADDITIONAL_CODE));
    newResource.setField(ResourceFields.CONVERSION, resource.getField(ResourceFields.CONVERSION));
    newResource.setField(
        ResourceFields.PALLET_NUMBER, resource.getField(ResourceFields.PALLET_NUMBER));
    newResource.setField(
        ResourceFields.TYPE_OF_PALLET, resource.getField(ResourceFields.TYPE_OF_PALLET));
    newResource.setField(ResourceFields.GIVEN_UNIT, resource.getField(ResourceFields.GIVEN_UNIT));

    BigDecimal quantityInAdditionalUnit =
        numberService.setScale(
            quantity.multiply(resource.getDecimalField(ResourceFields.CONVERSION)));

    newResource.setField(ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT, quantityInAdditionalUnit);

    setResourceAttributesFromResource(newResource, resource);

    resourceStockService.addResourceStock(newResource);
    return resourceDD.save(newResource);
  }
  public Entity createResource(
      final Entity document, final Entity warehouse, final Entity position, final Object date) {
    DataDefinition resourceDD =
        dataDefinitionService.get(
            MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
            MaterialFlowResourcesConstants.MODEL_RESOURCE);

    Entity product = position.getBelongsToField(PositionFields.PRODUCT);
    Entity resource = resourceDD.create();
    Entity user = document.getBelongsToField(DocumentFields.USER);

    resource.setField(
        ResourceFields.USER_NAME,
        user.getStringField(_FIRST_NAME) + " " + user.getStringField(L_LAST_NAME));
    resource.setField(ResourceFields.TIME, date);
    resource.setField(ResourceFields.LOCATION, warehouse);
    resource.setField(ResourceFields.PRODUCT, position.getBelongsToField(PositionFields.PRODUCT));
    resource.setField(ResourceFields.QUANTITY, position.getField(PositionFields.QUANTITY));
    resource.setField(ResourceFields.RESERVED_QUANTITY, BigDecimal.ZERO);
    resource.setField(
        ResourceFields.AVAILABLE_QUANTITY, position.getDecimalField(PositionFields.QUANTITY));
    resource.setField(ResourceFields.PRICE, position.getField(PositionFields.PRICE));
    resource.setField(ResourceFields.BATCH, position.getField(PositionFields.BATCH));
    resource.setField(
        ResourceFields.EXPIRATION_DATE, position.getField(PositionFields.EXPIRATION_DATE));
    resource.setField(
        ResourceFields.PRODUCTION_DATE, position.getField(PositionFields.PRODUCTION_DATE));
    resource.setField(
        ResourceFields.STORAGE_LOCATION, position.getField(PositionFields.STORAGE_LOCATION));
    resource.setField(
        ResourceFields.ADDITIONAL_CODE, position.getField(PositionFields.ADDITIONAL_CODE));
    resource.setField(
        ResourceFields.PALLET_NUMBER, position.getField(PositionFields.PALLET_NUMBER));
    resource.setField(
        ResourceFields.TYPE_OF_PALLET, position.getField(PositionFields.TYPE_OF_PALLET));
    resource.setField(ResourceFields.WASTE, position.getField(PositionFields.WASTE));

    if (StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
      resource.setField(ResourceFields.GIVEN_UNIT, product.getField(ProductFields.UNIT));
      resource.setField(
          ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT, position.getField(PositionFields.QUANTITY));
      resource.setField(ResourceFields.CONVERSION, BigDecimal.ONE);
    } else {
      resource.setField(ResourceFields.GIVEN_UNIT, position.getField(PositionFields.GIVEN_UNIT));
      resource.setField(
          ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT,
          position.getField(PositionFields.GIVEN_QUANTITY));
      resource.setField(ResourceFields.CONVERSION, position.getField(PositionFields.CONVERSION));
    }

    setResourceAttributesFromPosition(resource, position);

    resourceStockService.addResourceStock(resource);
    return resourceDD.save(resource);
  }
Example #4
0
 public Entity createDocument(
     UserService userService, NumberGeneratorService numberGeneratorService) {
   DataDefinition documentDD =
       dataDefinitionService.get(
           MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
           MaterialFlowResourcesConstants.MODEL_DOCUMENT);
   Entity document = documentDD.create();
   document.setField(DocumentFields.TIME, new Date());
   document.setField(DocumentFields.USER, userService.getCurrentUserEntity().getId());
   document.setField(DocumentFields.STATE, DocumentState.DRAFT.getStringValue());
   document.setField(
       DocumentFields.NUMBER,
       numberGeneratorService.generateNumber(
           MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
           MaterialFlowResourcesConstants.MODEL_DOCUMENT));
   document.setField(DocumentFields.POSITIONS, Lists.newArrayList());
   return document;
 }
  @Test
  public void shouldCreatetTechnologyInstOperProductInComp() throws Exception {
    // given
    Long orderId = 1L;
    Long technologyId = 2L;
    BigDecimal nominalCost1 = BigDecimal.TEN;
    BigDecimal nominalCost2 = BigDecimal.TEN;

    when(productQuantities.entrySet()).thenReturn(entrySet);
    when(entrySet.iterator()).thenReturn(iterator);
    when(iterator.hasNext()).thenReturn(true, true, false);
    when(iterator.next()).thenReturn(entry1, entry2);

    when(entry1.getKey()).thenReturn(prod1);
    when(entry2.getKey()).thenReturn(prod2);

    when(prod1.getDecimalField("nominalCost")).thenReturn(nominalCost1);
    when(prod2.getDecimalField("nominalCost")).thenReturn(nominalCost2);

    when(order.getBelongsToField("technology")).thenReturn(technology);
    when(order.getId()).thenReturn(orderId);
    when(orderDD.get(orderId)).thenReturn(orderFromDB);
    when(technology.getId()).thenReturn(technologyId);

    when(costNormsForMaterialsService.getProductQuantitiesFromTechnology(technologyId))
        .thenReturn(productQuantities);
    when(techInsOperCompProdInDD.create())
        .thenReturn(techInsOperCompProdIn1, techInsOperCompProdIn2);
    when(techInsOperCompProdIn1.getDataDefinition()).thenReturn(techInsOperCompProdInDD);
    when(techInsOperCompProdIn2.getDataDefinition()).thenReturn(techInsOperCompProdInDD);
    when(techInsOperCompProdInDD.save(techInsOperCompProdIn1))
        .thenReturn(techInsOperCompProdIn1, techInsOperCompProdIn2);
    // when
    orderHooksCNFM.fillOrderOperationProductsInComponents(orderDD, order);

    // then
    Mockito.verify(techInsOperCompProdIn1).setField("order", order);
    Mockito.verify(techInsOperCompProdIn1).setField("product", prod1);
    Mockito.verify(techInsOperCompProdIn1).setField("nominalCost", nominalCost1);
    Mockito.verify(techInsOperCompProdIn2).setField("order", order);
    Mockito.verify(techInsOperCompProdIn2).setField("product", prod2);
    Mockito.verify(techInsOperCompProdIn2).setField("nominalCost", nominalCost2);
  }
  @Before
  public void init() {
    MockitoAnnotations.initMocks(this);

    DataDefinition materialCostsComponentDD = mock(DataDefinition.class);
    given(materialCostsComponentDD.create()).willReturn(createdEntity);

    DataDefinitionService dataDefinitionService = mock(DataDefinitionService.class);
    given(
            dataDefinitionService.get(
                CostNormsForMaterialsConstants.PLUGIN_IDENTIFIER,
                CostNormsForMaterialsConstants.MODEL_TECHNOLOGY_INST_OPER_PRODUCT_IN_COMP))
        .willReturn(materialCostsComponentDD);

    orderMaterialCostsEntityBuilder = new OrderMaterialCostsEntityBuilderImpl();

    ReflectionTestUtils.setField(
        orderMaterialCostsEntityBuilder, "dataDefinitionService", dataDefinitionService);
  }
  @Test
  public final void shouldGenerateWorkPlanEntity() throws Exception {
    // given
    Entity emptyWorkPlan = mock(Entity.class);
    when(workPlanDD.create()).thenReturn(emptyWorkPlan);
    when(workPlanDD.save(emptyWorkPlan)).thenReturn(emptyWorkPlan);
    when(emptyWorkPlan.getDataDefinition()).thenReturn(workPlanDD);

    Entity order = mock(Entity.class);

    @SuppressWarnings("unchecked")
    Iterator<Entity> iterator = mock(Iterator.class);
    when(iterator.hasNext()).thenReturn(true, true, true, false);
    when(iterator.next()).thenReturn(order);

    @SuppressWarnings("unchecked")
    List<Entity> orders = mock(List.class);
    when(orders.iterator()).thenReturn(iterator);
    when(orders.size()).thenReturn(3);
    when(orders.get(Mockito.anyInt())).thenReturn(order);

    @SuppressWarnings("rawtypes")
    ArgumentCaptor<List> listArgCaptor = ArgumentCaptor.forClass(List.class);
    ArgumentCaptor<String> stringArgCaptor = ArgumentCaptor.forClass(String.class);

    // when
    workPlanService.generateWorkPlanEntity(orders);

    // then
    verify(emptyWorkPlan, times(1)).setField(Mockito.eq("orders"), listArgCaptor.capture());
    @SuppressWarnings("unchecked")
    List<Entity> resultOrders = listArgCaptor.getValue();
    Assert.assertEquals(orders.size(), resultOrders.size());
    Assert.assertSame(order, resultOrders.get(0));
    Assert.assertSame(order, resultOrders.get(1));
    Assert.assertSame(order, resultOrders.get(2));

    verify(emptyWorkPlan, times(1)).setField(Mockito.eq("name"), stringArgCaptor.capture());
    Assert.assertEquals(TRANSLATED_STRING, stringArgCaptor.getValue());

    verify(emptyWorkPlan, times(1)).setField(Mockito.eq("type"), stringArgCaptor.capture());
    Assert.assertEquals(WorkPlanType.NO_DISTINCTION.getStringValue(), stringArgCaptor.getValue());
  }
  private List<Entity> updateResources(
      Entity warehouse, Entity position, WarehouseAlgorithm warehouseAlgorithm) {
    DataDefinition positionDD =
        dataDefinitionService.get(
            MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER,
            MaterialFlowResourcesConstants.MODEL_POSITION);

    List<Entity> newPositions = Lists.newArrayList();

    Entity product = position.getBelongsToField(PositionFields.PRODUCT);

    List<Entity> resources =
        getResourcesForWarehouseProductAndAlgorithm(
            warehouse, product, position, warehouseAlgorithm);

    BigDecimal quantity = position.getDecimalField(PositionFields.QUANTITY);

    resourceStockService.removeResourceStock(product, warehouse, quantity);
    for (Entity resource : resources) {
      BigDecimal resourceQuantity = resource.getDecimalField(ResourceFields.QUANTITY);
      BigDecimal resourceAvailableQuantity =
          resource.getDecimalField(ResourceFields.AVAILABLE_QUANTITY);

      Entity newPosition = positionDD.create();

      newPosition.setField(
          PositionFields.PRODUCT, position.getBelongsToField(PositionFields.PRODUCT));
      newPosition.setField(
          PositionFields.GIVEN_QUANTITY, position.getDecimalField(PositionFields.GIVEN_QUANTITY));
      newPosition.setField(
          PositionFields.GIVEN_UNIT, position.getStringField(PositionFields.GIVEN_UNIT));
      newPosition.setField(PositionFields.PRICE, resource.getField(ResourceFields.PRICE));
      newPosition.setField(PositionFields.BATCH, resource.getField(ResourceFields.BATCH));
      newPosition.setField(
          PositionFields.PRODUCTION_DATE, resource.getField(ResourceFields.PRODUCTION_DATE));
      newPosition.setField(
          PositionFields.EXPIRATION_DATE, resource.getField(ResourceFields.EXPIRATION_DATE));
      newPosition.setField(PositionFields.RESOURCE, resource);
      newPosition.setField(
          PositionFields.STORAGE_LOCATION, resource.getField(ResourceFields.STORAGE_LOCATION));
      newPosition.setField(
          PositionFields.ADDITIONAL_CODE, resource.getField(ResourceFields.ADDITIONAL_CODE));
      newPosition.setField(PositionFields.CONVERSION, position.getField(PositionFields.CONVERSION));
      newPosition.setField(
          PositionFields.PALLET_NUMBER, resource.getField(ResourceFields.PALLET_NUMBER));
      newPosition.setField(
          PositionFields.TYPE_OF_PALLET, resource.getField(ResourceFields.TYPE_OF_PALLET));
      // newPosition.setField(PositionFields.GIVEN_UNIT,
      // resource.getField(ResourceFields.GIVEN_UNIT));

      setPositionAttributesFromResource(newPosition, resource);

      if (quantity.compareTo(resourceAvailableQuantity) >= 0) {
        quantity = quantity.subtract(resourceAvailableQuantity, numberService.getMathContext());
        if (resourceQuantity.compareTo(resourceAvailableQuantity) <= 0) {
          resource.getDataDefinition().delete(resource.getId());
        } else {
          BigDecimal newResourceQuantity = resourceQuantity.subtract(resourceAvailableQuantity);
          BigDecimal resourceConversion = resource.getDecimalField(ResourceFields.CONVERSION);
          BigDecimal quantityInAdditionalUnit = newResourceQuantity.multiply(resourceConversion);
          resource.setField(ResourceFields.AVAILABLE_QUANTITY, BigDecimal.ZERO);
          resource.setField(ResourceFields.QUANTITY, newResourceQuantity);
          resource.setField(
              ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT,
              numberService.setScale(quantityInAdditionalUnit));
          resource.getDataDefinition().save(resource);
        }

        newPosition.setField(
            PositionFields.QUANTITY, numberService.setScale(resourceAvailableQuantity));

        BigDecimal givenResourceQuantity = convertToGivenUnit(resourceAvailableQuantity, position);

        newPosition.setField(
            PositionFields.GIVEN_QUANTITY, numberService.setScale(givenResourceQuantity));

        newPositions.add(newPosition);

        if (BigDecimal.ZERO.compareTo(quantity) == 0) {
          return newPositions;
        }
      } else {
        resourceQuantity = resourceQuantity.subtract(quantity, numberService.getMathContext());
        resourceAvailableQuantity =
            resourceAvailableQuantity.subtract(quantity, numberService.getMathContext());
        if (position.getBelongsToField(PositionFields.RESOURCE) != null
            && reservationsService.reservationsEnabledForDocumentPositions()) {
          BigDecimal reservedQuantity =
              resource
                  .getDecimalField(ResourceFields.RESERVED_QUANTITY)
                  .subtract(quantity, numberService.getMathContext());
          resource.setField(ResourceFields.RESERVED_QUANTITY, reservedQuantity);
        }
        BigDecimal resourceConversion = resource.getDecimalField(ResourceFields.CONVERSION);
        BigDecimal quantityInAdditionalUnit = resourceQuantity.multiply(resourceConversion);

        resource.setField(
            ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT,
            numberService.setScale(quantityInAdditionalUnit));
        resource.setField(ResourceFields.QUANTITY, numberService.setScale(resourceQuantity));
        resource.setField(ResourceFields.AVAILABLE_QUANTITY, resourceAvailableQuantity);

        resource.getDataDefinition().save(resource);
        newPosition.setField(PositionFields.QUANTITY, numberService.setScale(quantity));

        BigDecimal givenQuantity = convertToGivenUnit(quantity, position);

        newPosition.setField(PositionFields.GIVEN_QUANTITY, numberService.setScale(givenQuantity));
        newPositions.add(newPosition);

        return newPositions;
      }
    }

    position.addError(
        position.getDataDefinition().getField(PositionFields.QUANTITY),
        "materialFlow.error.position.quantity.notEnough");

    return Lists.newArrayList(position);
  }
 public OperationProductComponentImpl(
     final OperationCompType operationType, final DataDefinition opcDataDef) {
   this.entity = opcDataDef.create();
   this.operationType = operationType;
 }