use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project mes by qcadoo.
the class ResourceManagementServiceImpl method getQuantitiesInWarehouse.
public Multimap<Long, BigDecimal> getQuantitiesInWarehouse(final Entity warehouse, final Multimap<Entity, Entity> productsAndPositions) {
Multimap<Long, BigDecimal> result = ArrayListMultimap.create();
for (Map.Entry<Entity, Entity> productAndPosition : productsAndPositions.entries()) {
Entity additionalCode = productAndPosition.getValue().getBelongsToField(PositionFields.ADDITIONAL_CODE);
BigDecimal conversion = productAndPosition.getValue().getDecimalField(PositionFields.CONVERSION);
Entity batch = productAndPosition.getValue().getBelongsToField(PositionFields.BATCH);
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));
}
if (Objects.nonNull(batch)) {
scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
}
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));
}
if (Objects.nonNull(batch)) {
scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
}
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 (Objects.nonNull(batch)) {
scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, batch));
}
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();
}
if (result.containsKey(productAndPosition.getKey().getId())) {
BigDecimal currentQuantity = result.get(productAndPosition.getKey().getId()).stream().reduce(BigDecimal.ZERO, 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(BigDecimal.ZERO, BigDecimal::add));
}
}
return result;
}
use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project mes by qcadoo.
the class LocationValidators method noReservationExists.
public boolean noReservationExists(Entity entity) {
if (entity.getId() == null) {
return true;
}
SearchCriteriaBuilder scb = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_RESERVATION).find();
scb.add(SearchRestrictions.isNotNull(ReservationFields.POSITION));
scb.add(SearchRestrictions.belongsTo(ReservationFields.LOCATION, entity));
scb.setProjection(alias(rowCount(), "cnt"));
scb.addOrder(asc("cnt"));
Entity countProjection = scb.setMaxResults(1).uniqueResult();
return ((Long) countProjection.getField("cnt")).compareTo(0L) == 0;
}
use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project mes by qcadoo.
the class LocationValidators method noDraftDocumentExists.
public boolean noDraftDocumentExists(Entity entity) {
if (entity.getId() == null) {
return true;
}
SearchCriteriaBuilder scb = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_DOCUMENT).find();
scb.add(SearchRestrictions.eq(DocumentFields.STATE, DocumentState.DRAFT.getStringValue()));
scb.add(SearchRestrictions.in(DocumentFields.TYPE, DOCUMENT_TYPES));
scb.add(SearchRestrictions.belongsTo(DocumentFields.LOCATION_FROM, entity));
scb.setProjection(alias(rowCount(), "cnt"));
scb.addOrder(asc("cnt"));
Entity countProjection = scb.setMaxResults(1).uniqueResult();
return ((Long) countProjection.getField("cnt")).compareTo(0L) == 0;
}
use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project mes by qcadoo.
the class MaterialRequirementCoverageForOrderServiceImpl method getUsedQuantityForOtherFromProductionTrackings.
private BigDecimal getUsedQuantityForOtherFromProductionTrackings(final Entity order, final Entity technologyOperationComponent, final Entity product, final String typeOfProductionRecording) {
BigDecimal usedQuantity = BigDecimal.ZERO;
SearchCriteriaBuilder searchCriteriaBuilder = dataDefinitionService.get(ProductionCountingConstants.PLUGIN_IDENTIFIER, ProductionCountingConstants.MODEL_PRODUCTION_TRACKING).find().add(SearchRestrictions.belongsTo(ProductionTrackingFields.ORDER, order));
if (TypeOfProductionRecording.FOR_EACH.getStringValue().equals(typeOfProductionRecording)) {
searchCriteriaBuilder.add(SearchRestrictions.belongsTo(ProductionTrackingFields.TECHNOLOGY_OPERATION_COMPONENT, technologyOperationComponent));
}
List<Entity> productionTrackings = searchCriteriaBuilder.list().getEntities();
for (Entity productionTracking : productionTrackings) {
Entity trackingOperationProductInComponent = productionTracking.getHasManyField(ProductionTrackingFields.TRACKING_OPERATION_PRODUCT_IN_COMPONENTS).find().add(SearchRestrictions.belongsTo(TrackingOperationProductInComponentFields.PRODUCT, product)).setMaxResults(1).uniqueResult();
if (trackingOperationProductInComponent != null) {
BigDecimal quantity = trackingOperationProductInComponent.getDecimalField(TrackingOperationProductInComponentFields.USED_QUANTITY);
if (quantity != null) {
usedQuantity = usedQuantity.add(quantity, numberService.getMathContext());
}
}
}
return usedQuantity;
}
use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project mes by qcadoo.
the class MaterialRequirementCoverageForOrderServiceImpl method getCoverageProductsForOrder.
private List<Entity> getCoverageProductsForOrder(final Entity order) {
SearchCriteriaBuilder scb = dataDefinitionService.get(OrderSuppliesConstants.PLUGIN_IDENTIFIER, OrderSuppliesConstants.MODEL_COVERAGE_PRODUCT).find();
scb.add(SearchRestrictions.belongsTo(CoverageProductFields.ORDER, OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER, order.getId()));
return scb.list().getEntities();
}
Aggregations