@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 QueryDefinitionDTO getSearchQueryDefinitionDTO(QueryDefinitionDTO qd) {
    qd.setSearchMotorQuery(SearchMotorQueryEnum.SaleSearch);
    qd.addCriteria(SaleColumnEnum.saleQuote, QueryOperatorEnum.EQUALS, false);
    qd.addCriteria(
        SaleColumnEnum.saleEnterprise,
        QueryOperatorEnum.IN,
        (ArrayList) getSessionUser().getEnterprise().getVisibleEnterprisesFinancialData());
    queryDefinitionDAO.executeQueryDefinition(qd);

    for (QueryResultDTO result : qd.getResults().values()) {
      if (result.getValues().get(SaleColumnEnum.status).getValue()
          == FinancialStatusSale._0_draft) {
        result.setIdPrefix("_draft_");
      }
      String prodSerColumn =
          (String) result.getValues().get(SaleColumnEnum.productsServices).getValue();
      if (prodSerColumn != null && prodSerColumn.contains("\n")) {
        result.getValues().get(SaleColumnEnum.productsServices).setValue("\n" + prodSerColumn);
      }
    }

    return qd;
  }
  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;
  }