use of com.qcadoo.model.api.search.SearchQueryBuilder in project mes by qcadoo.
the class OrderStatesListenerServicePFTD method getProductQuantityTakenForOrder.
private BigDecimal getProductQuantityTakenForOrder(final Long productId, final Long orderId) {
DataDefinition issueDD = dataDefinitionService.get(ProductFlowThruDivisionConstants.PLUGIN_IDENTIFIER, ProductFlowThruDivisionConstants.MODEL_ISSUE);
SearchQueryBuilder searchQueryBuilder = issueDD.find("SELECT SUM(issue.issueQuantity) AS totalQuantity " + "FROM #productFlowThruDivision_issue issue JOIN issue.warehouseIssue AS warehouseIssue " + "WHERE warehouseIssue.order = :order_id AND issue.product = :product_id AND issue.issued = :issued " + "GROUP BY warehouseIssue.order, issue.product, issue.issued");
searchQueryBuilder.setLong("order_id", orderId);
searchQueryBuilder.setLong("product_id", productId);
searchQueryBuilder.setBoolean("issued", true);
Entity result = searchQueryBuilder.setMaxResults(1).uniqueResult();
return result != null ? result.getDecimalField("totalQuantity") : BigDecimal.ZERO;
}
use of com.qcadoo.model.api.search.SearchQueryBuilder in project mes by qcadoo.
the class ProductionTrackingListenerServicePFTD method updateCostsForOrder.
public void updateCostsForOrder(final Entity order) {
DataDefinition positionDD = getPositionDD();
SearchQueryBuilder searchQueryBuilder = positionDD.find("SELECT pr.id AS product, SUM(p.quantity) AS quantity, SUM(p.quantity * p.price) AS price " + "FROM #materialFlowResources_position p JOIN p.document AS d join p.product AS pr " + "WHERE d.order = :order_id AND d.type = :type " + "GROUP BY d.order, d.type, pr.id");
searchQueryBuilder.setLong("order_id", order.getId());
searchQueryBuilder.setString("type", DocumentType.INTERNAL_OUTBOUND.getStringValue());
SearchResult result = searchQueryBuilder.list();
List<ProductWithQuantityAndCost> productsWithQuantitiesAndCosts = Lists.newArrayList();
for (Entity costsForProduct : result.getEntities()) {
Long product = (Long) costsForProduct.getField(PositionFields.PRODUCT);
BigDecimal quantity = costsForProduct.getDecimalField(PositionFields.QUANTITY);
BigDecimal cost = costsForProduct.getDecimalField(PositionFields.PRICE);
productsWithQuantitiesAndCosts.add(new ProductWithQuantityAndCost(product, quantity, cost));
}
List<Entity> updatedCosts = costNormsForMaterialsService.updateCostsForProductInOrder(order, productsWithQuantitiesAndCosts);
order.setField(OrderFieldsCNFM.TECHNOLOGY_INST_OPER_PRODUCT_IN_COMPS, updatedCosts);
}
use of com.qcadoo.model.api.search.SearchQueryBuilder in project mes by qcadoo.
the class ProductionTrackingDocumentsHelper method getQuantitiesForProductsAndLocation.
public Map<Long, Map<String, BigDecimal>> getQuantitiesForProductsAndLocation(final List<Entity> trackingOperationProductInComponents, final Entity location) {
Map<Long, Map<String, BigDecimal>> quantities = Maps.newHashMap();
if (trackingOperationProductInComponents.size() > 0) {
StringBuilder hql = new StringBuilder();
hql.append("SELECT p.id AS productId, SUM(r.quantity) AS quantity, b.number AS batchNumber ");
hql.append("FROM #materialFlowResources_resource AS r ");
hql.append("JOIN r.product AS p ");
hql.append("JOIN r.location AS l ");
hql.append("LEFT JOIN r.batch AS b ");
hql.append("GROUP BY p.id, l.id, b.number ");
hql.append("HAVING p.id IN (:productIds) ");
hql.append("AND l.id = :locationId ");
SearchQueryBuilder sqb = getResourceDD().find(hql.toString());
sqb.setParameter("locationId", location.getId());
sqb.setParameterList("productIds", trackingOperationProductInComponents.stream().map(trackingOperationProductInComponent -> trackingOperationProductInComponent.getBelongsToField(TrackingOperationProductInComponentFields.PRODUCT).getId()).collect(Collectors.toList()));
List<Entity> productsAndQuantities = sqb.list().getEntities();
productsAndQuantities.forEach(productAndQuantity -> {
Long productId = (Long) productAndQuantity.getField("productId");
String batchNumber = productAndQuantity.getStringField("batchNumber");
if (Objects.isNull(batchNumber)) {
batchNumber = "";
}
Map<String, BigDecimal> batchQuantities = quantities.get(productId);
if (Objects.isNull(batchQuantities)) {
batchQuantities = Maps.newHashMap();
}
batchQuantities.put(batchNumber, productAndQuantity.getDecimalField("quantity"));
quantities.put(productId, batchQuantities);
});
}
return quantities;
}
use of com.qcadoo.model.api.search.SearchQueryBuilder in project mes by qcadoo.
the class PPSHelperTest method shouldGetPpsForOrderReturnExistingPpsId.
@Test
public final void shouldGetPpsForOrderReturnExistingPpsId() {
// given
Long givenOrderId = 1L;
Long expectedPpsId = 50L;
given(dataDefinitionService.get(ProductionPerShiftConstants.PLUGIN_IDENTIFIER, ProductionPerShiftConstants.MODEL_PRODUCTION_PER_SHIFT)).willReturn(dataDefinition);
SearchQueryBuilder searchCriteriaBuilder = mock(SearchQueryBuilder.class);
given(dataDefinition.find("select id as ppsId from #productionPerShift_productionPerShift where order.id = :orderId")).willReturn(searchCriteriaBuilder);
given(searchCriteriaBuilder.setMaxResults(Mockito.anyInt())).willReturn(searchCriteriaBuilder);
given(searchCriteriaBuilder.setLong(Mockito.anyString(), Mockito.eq(givenOrderId))).willReturn(searchCriteriaBuilder);
given(searchCriteriaBuilder.uniqueResult()).willReturn(entity);
given(entity.getField("ppsId")).willReturn(expectedPpsId);
// when
final Long resultPpsId = ppsHelper.getPpsIdForOrder(givenOrderId);
// then
Assert.assertEquals(expectedPpsId, resultPpsId);
}
use of com.qcadoo.model.api.search.SearchQueryBuilder in project mes by qcadoo.
the class PlannedProgressDataProvider method findProjections.
private List<Entity> findProjections(final Interval searchInterval) {
Preconditions.checkArgument(searchInterval != null, "Search interval must be not null");
SearchQueryBuilder sqb = getDataDefinition().find(HQL_QUERY);
sqb.setDate("DATE_FROM", searchInterval.getStart().toDate());
sqb.setDate("DATE_TO", searchInterval.getEnd().toDate());
return sqb.list().getEntities();
}
Aggregations