Search in sources :

Example 1 with SearchQueryBuilder

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) DataDefinition(com.qcadoo.model.api.DataDefinition)

Example 2 with SearchQueryBuilder

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) ProductWithQuantityAndCost(com.qcadoo.mes.costNormsForMaterials.orderRawMaterialCosts.domain.ProductWithQuantityAndCost)

Example 3 with SearchQueryBuilder

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 4 with SearchQueryBuilder

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);
}
Also used : SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder) Test(org.junit.Test)

Example 5 with SearchQueryBuilder

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();
}
Also used : SearchQueryBuilder(com.qcadoo.model.api.search.SearchQueryBuilder)

Aggregations

SearchQueryBuilder (com.qcadoo.model.api.search.SearchQueryBuilder)26 Entity (com.qcadoo.model.api.Entity)18 BigDecimal (java.math.BigDecimal)7 DataDefinition (com.qcadoo.model.api.DataDefinition)6 List (java.util.List)4 Map (java.util.Map)4 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)3 SearchRestrictions (com.qcadoo.model.api.search.SearchRestrictions)3 Date (java.util.Date)3 Collectors (java.util.stream.Collectors)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Service (org.springframework.stereotype.Service)3 Maps (com.google.common.collect.Maps)2 BasicConstants (com.qcadoo.mes.basic.constants.BasicConstants)2 MaterialFlowResourcesConstants (com.qcadoo.mes.materialFlowResources.constants.MaterialFlowResourcesConstants)2 NumberService (com.qcadoo.model.api.NumberService)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 Objects (java.util.Objects)2 DateTime (org.joda.time.DateTime)2 Function (com.google.common.base.Function)1