Esempio n. 1
0
    public void autompleteSearch(final String[] args) {
      if ((belongsToFieldDefinition == null || belongsToEntityId != null)) {
        SearchCriteriaBuilder searchCriteriaBuilder = getDataDefinition().find();

        if (StringUtils.hasText(currentCode)) {
          searchCriteriaBuilder.add(
              SearchRestrictions.ilike(fieldCode, currentCode, SearchMatchMode.ANYWHERE));
        }

        if (belongsToFieldDefinition != null
            && belongsToEntityId != null
            && belongsToFieldDefinition.getType() instanceof BelongsToType) {
          BelongsToType type = (BelongsToType) belongsToFieldDefinition.getType();
          searchCriteriaBuilder.add(
              SearchRestrictions.belongsTo(
                  belongsToFieldDefinition.getName(),
                  type.getDataDefinition().get(belongsToEntityId)));
        }

        if (getDataDefinition().isActivable()) {
          if (oldSelectedEntityId == null) {
            searchCriteriaBuilder.add(SearchRestrictions.eq("active", true));
          } else {
            searchCriteriaBuilder.add(
                SearchRestrictions.or(
                    SearchRestrictions.eq("active", true),
                    SearchRestrictions.idEq(oldSelectedEntityId)));
          }
        }

        searchCriteriaBuilder.addOrder(SearchOrders.asc(fieldCode));

        if (criteriaModifier != null) {
          criteriaModifier.modifyCriteria(searchCriteriaBuilder, criteriaModifierParameter);
        }

        SearchResult results = searchCriteriaBuilder.list();

        autocompleteEntitiesNumber = results.getTotalNumberOfEntities();

        if (results.getTotalNumberOfEntities() > 25) {
          autocompleteMatches = new LinkedList<Entity>();
        } else {
          autocompleteMatches = results.getEntities();
        }
      } else {
        autocompleteMatches = new LinkedList<Entity>();
      }

      autocompleteCode = currentCode;
      requestRender();
    }
  private List<Entity> getResourcesForLocationAndProductFEFO(
      final Entity warehouse,
      final Entity product,
      final Entity additionalCode,
      final Entity position) {
    List<Entity> resources = Lists.newArrayList();

    if (additionalCode != null) {
      SearchCriteriaBuilder scb =
          getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources =
          scb.add(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode))
              .addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE))
              .list()
              .getEntities();

      scb = getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources.addAll(
          scb.add(
                  SearchRestrictions.or(
                      SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE),
                      SearchRestrictions.ne("additionalCode.id", additionalCode.getId())))
              .addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE))
              .list()
              .getEntities());
    }

    if (resources.isEmpty()) {
      SearchCriteriaBuilder scb =
          getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources =
          scb.addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE)).list().getEntities();
    }

    return resources;
  }
  public Multimap<Long, BigDecimal> getQuantitiesInWarehouse(
      final Entity warehouse, Multimap<Entity, Entity> productsAndPositions) {
    Multimap<Long, BigDecimal> result = ArrayListMultimap.create();

    String algorithm = warehouse.getStringField(LocationFieldsMFR.ALGORITHM);

    for (Map.Entry<Entity, Entity> productAndPosition : productsAndPositions.entries()) {
      Entity resource = productAndPosition.getValue().getBelongsToField(PositionFields.RESOURCE);

      if (algorithm.equalsIgnoreCase(WarehouseAlgorithm.MANUAL.getStringValue())
          && resource != null) {
        result.put(
            productAndPosition.getKey().getId(),
            resource.getDecimalField(ResourceFields.AVAILABLE_QUANTITY));
      } else {
        Entity additionalCode =
            productAndPosition.getValue().getBelongsToField(PositionFields.ADDITIONAL_CODE);
        BigDecimal conversion =
            productAndPosition.getValue().getDecimalField(PositionFields.CONVERSION);
        Entity reservation =
            reservationsService.getReservationForPosition(productAndPosition.getValue());
        List<Entity> resources = Lists.newArrayList();

        if (additionalCode != null) {
          SearchCriteriaBuilder scb =
              getSearchCriteriaForResourceForProductAndWarehouse(
                  productAndPosition.getKey(), warehouse);

          if (!StringUtils.isEmpty(
              productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
          } else {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
          }

          resources =
              scb.add(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode))
                  .list()
                  .getEntities();

          scb =
              getSearchCriteriaForResourceForProductAndWarehouse(
                  productAndPosition.getKey(), warehouse);

          if (!StringUtils.isEmpty(
              productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
          } else {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
          }

          resources.addAll(
              scb.add(
                      SearchRestrictions.or(
                          SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE),
                          SearchRestrictions.ne("additionalCode.id", additionalCode.getId())))
                  .list()
                  .getEntities());
        }

        if (resources.isEmpty()) {
          SearchCriteriaBuilder scb =
              getSearchCriteriaForResourceForProductAndWarehouse(
                  productAndPosition.getKey(), warehouse);

          if (!StringUtils.isEmpty(
              productAndPosition.getKey().getStringField(ProductFields.ADDITIONAL_UNIT))) {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, conversion));
          } else {
            scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
          }

          resources = scb.list().getEntities();
        }

        BigDecimal reservedQuantity = BigDecimal.ZERO;
        if (reservation != null) {
          reservedQuantity = reservation.getDecimalField(ReservationFields.QUANTITY);
        }
        if (result.containsKey(productAndPosition.getKey().getId())) {
          BigDecimal currentQuantity =
              result
                  .get(productAndPosition.getKey().getId())
                  .stream()
                  .reduce(reservedQuantity, BigDecimal::add);

          result.put(
              productAndPosition.getKey().getId(),
              (resources
                      .stream()
                      .map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY))
                      .reduce(BigDecimal.ZERO, BigDecimal::add))
                  .add(currentQuantity));
        } else {
          result.put(
              productAndPosition.getKey().getId(),
              resources
                  .stream()
                  .map(res -> res.getDecimalField(ResourceFields.AVAILABLE_QUANTITY))
                  .reduce(reservedQuantity, BigDecimal::add));
        }
      }
    }

    return result;
  }