@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void duplicateEnMasse(QueryDefinitionDTO saleList)
      throws IabakoUniqueConstraintException, IabakoActionForbiddenException,
          IabakoPackageForbiddenException, IabakoStockException {
    saleList.setPageSize(-1);
    saleList.excludeAllColumns();
    queryDefinitionDAO.executeQueryDefinition(saleList);
    Map<Long, QueryResultDTO> sales = saleList.getResults();

    for (Long idSale : sales.keySet()) {
      Sale sale = saleDAO.findById(idSale);
      duplicate(sale);
    }
  }
  public Map<ColumnDefinition, Double> getTotals(QueryDefinitionDTO queryDefinitionDTO) {

    queryDefinitionDTO.setCustomQuery(true);

    queryDefinitionDTO.setSearchMotorQuery(SearchMotorQueryEnum.SalePayedTotal);
    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.payedTotal);
    queryDefinitionDAO.executeQueryDefinition(queryDefinitionDTO);
    Double payedTotal =
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.payedTotal)
                .getValue();

    queryDefinitionDTO.setSearchMotorQuery(SearchMotorQueryEnum.SaleReceivableTotal);
    queryDefinitionDTO.getCustomColumns().clear();
    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.receivableTotal);
    queryDefinitionDAO.executeQueryDefinition(queryDefinitionDTO);
    Double receivableTotal =
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.receivableTotal)
                .getValue();

    queryDefinitionDTO.setSearchMotorQuery(SearchMotorQueryEnum.SaleTotals);
    queryDefinitionDTO.getCustomColumns().clear();

    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.priceTotal);
    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.totalBeforeTax);
    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.totalAfterTax);
    queryDefinitionDTO.addCustomColumn(SaleColumnEnum.totalNoTax);

    queryDefinitionDAO.executeQueryDefinition(queryDefinitionDTO);

    Map<ColumnDefinition, Double> results = new HashMap<ColumnDefinition, Double>();

    results.put(SaleColumnEnum.payed, payedTotal);
    results.put(SaleColumnEnum.receivable, receivableTotal);
    results.put(
        SaleColumnEnum.price,
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.priceTotal)
                .getValue());
    results.put(
        SaleColumnEnum.priceBeforeTax,
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.totalBeforeTax)
                .getValue());
    results.put(
        SaleColumnEnum.priceAfterTax,
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.totalAfterTax)
                .getValue());
    results.put(
        SaleColumnEnum.priceNoTax,
        (Double)
            new ArrayList<QueryResultDTO>(queryDefinitionDTO.getResults().values())
                .get(0)
                .getValues()
                .get(SaleColumnEnum.totalNoTax)
                .getValue());

    return results;
  }
  public void addProductsServicesToSale(
      boolean isDraft, Sale saleFromDB, List<SaleProductService> productsServices)
      throws IabakoStockException {

    Enterprise enterprise = getEnterpriseFromSessionUser();
    String productsServicesAsString = "";

    Map<Product, Double> stockReductionMap = new HashMap<Product, Double>();

    // 1. Create productService as String (for table results)
    saleFromDB.setProductsServicesAsString(null);

    saleDAO.cleanProductService(saleFromDB);

    for (SaleProductService productService : productsServices) {

      String quantityString =
          productService.getQuantity() == 0d
                  || productService.getQuantity()
                          / new Double(productService.getQuantity()).intValue()
                      == 1
              ? new Double(productService.getQuantity()).intValue() + ""
              : ServerTools.formatAmount(
                  saleFromDB.getEnterprise().getLanguage(), productService.getQuantity());

      if (productService.getProduct() != null) {

        if (!productService.getProduct().isStockDisabled()) {
          Double quantity = stockReductionMap.get(productService.getProduct());
          stockReductionMap.put(
              productService.getProduct(),
              quantity == null
                  ? productService.getQuantity()
                  : quantity + productService.getQuantity());
        }

        quantityString +=
            productService.getProduct().getPriceUnit() == PriceUnit.unit
                ? ""
                : messages.getLabel(productService.getProduct().getPriceUnit().getLabelKey());

        productsServicesAsString +=
            productService.getProduct().getName() + " (" + quantityString + ")\n";
      } else {
        productsServicesAsString +=
            productService.getService().getName() + " (" + quantityString + ")\n";
      }
    }
    if (!GenericTools.isEmpty(productsServicesAsString)) {
      productsServicesAsString =
          productsServicesAsString.substring(0, productsServicesAsString.lastIndexOf("\n"));
      productsServicesAsString =
          productsServicesAsString.length() > 1000
              ? productsServicesAsString.substring(0, 1000)
              : productsServicesAsString;
      saleFromDB.setProductsServicesAsString(productsServicesAsString);
      saleFromDB.setProductsServices(productsServices);
    }

    // 2. Reset Product/Service Enterprise (and do not change Enterprise!)
    boolean enterpriseHasChildren = enterprise.getAllRelatedEnterprises().size() > 1;
    Product prod;
    Service service;
    for (SaleProductService saleProductService : saleFromDB.getProductsServices()) {
      if ((prod = saleProductService.getProduct()) != null
          && saleProductService.getProduct().getEnterprise() == null) {
        saleProductService
            .getProduct()
            .setEnterprise(
                enterpriseHasChildren
                    ? productDAO.findById(prod.getId()).getEnterprise()
                    : enterprise);

      } else if ((service = saleProductService.getService()) != null
          && saleProductService.getService().getEnterprise() == null) {
        saleProductService
            .getService()
            .setEnterprise(
                enterpriseHasChildren
                    ? serviceDAO.findById(service.getId()).getEnterprise()
                    : enterprise);
      }
    }

    // 3. Stock reduction
    if (isDraft) {
      return;
    }

    if (stockReductionMap.keySet().isEmpty()) {
      return;
    }

    ProductStockModification productStockModification =
        productDAO.persistProductStockModification(false);

    String errorMessage = "";
    for (Product product : stockReductionMap.keySet()) {
      if (product.getQuantity() < stockReductionMap.get(product)) {
        String unit = messages.getLabel(product.getPriceUnit().getLabelKey());
        unit = GenericTools.isEmpty(unit) ? " " + messages.getLabel("stock_unit") : unit;

        String stringQuantity =
            product.getQuantity() == 0d
                    || product.getQuantity() / new Double(product.getQuantity()).intValue() == 1
                ? new Double(product.getQuantity()).intValue() + ""
                : ServerTools.formatAmount(
                    saleFromDB.getEnterprise().getLanguage(), product.getQuantity());

        errorMessage +=
            "\n"
                + messages.getLabel(
                    "stock_sale_not_enough_text", product.getName(), stringQuantity, unit);
      }

      product.setQuantity(
          ServerTools.round(product.getQuantity() - stockReductionMap.get(product)));
      productDAO.merge(product);

      productDAO.trackStockModification(
          product,
          product.getQuantity(),
          stockReductionMap.get(product) * -1,
          productStockModification);
    }
    if (!GenericTools.isEmpty(errorMessage)) {
      errorMessage += messages.getLabel("stock_sale_not_enough_resolve_text");
      throw new IabakoStockException("stock_sale_not_enough_title", errorMessage, true);
    }

    // 4. Tag management
    for (SaleProductService saleProductService : saleFromDB.getProductsServices()) {
      if ((prod = saleProductService.getProduct()) != null
          && saleProductService.getProduct().getBusinessTag() != null) {
        BusinessTagSale businessTagSale = new BusinessTagSale();
        businessTagSale.setBusinessTag(prod.getBusinessTag());
        businessTagSale.setProduct(prod);
        businessTagSale.setSale(saleFromDB);
        saleFromDB.getBusinessTagList().add(businessTagSale);

      } else if ((service = saleProductService.getService()) != null
          && saleProductService.getService().getBusinessTag() != null) {
        BusinessTagSale businessTagSale = new BusinessTagSale();
        businessTagSale.setBusinessTag(service.getBusinessTag());
        businessTagSale.setService(service);
        businessTagSale.setSale(saleFromDB);
        saleFromDB.getBusinessTagList().add(businessTagSale);
      }
    }
  }