public List<CustomField> getCustomFields(Enterprise enterprise) {
    String query =
        "SELECT field FROM CustomField field WHERE field.enterprise = :enterprise order by field.position asc";

    enterprise =
        enterprise.getParentEnterprise() != null ? enterprise.getParentEnterprise() : enterprise;

    return (List<CustomField>)
        entityManager.createQuery(query).setParameter("enterprise", enterprise).getResultList();
  }
  /**
   * This method is used by SaleModificationForm
   *
   * @param sale
   * @return
   * @throws IabakoActionForbiddenException
   */
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Sale save(Sale sale)
      throws IabakoActionForbiddenException, IabakoUniqueConstraintException {

    Enterprise enterprise = getEnterpriseFromSessionUser();
    if (sale.getId() != null && enterprise.getAllRelatedEnterprises().size() > 1) {
      // Do not change Enterprise!
      sale.setEnterprise(saleDAO.findById(sale.getId()).getEnterprise());
    } else {
      sale.setEnterprise(enterprise);
    }

    if (enterprise != null && enterprise.isDemo()) {
      throw new IabakoActionForbiddenException(
          "action_forbidden_for_demo_title", "action_forbidden_for_demo_text", true);
    }

    if (!getDAO().isUniqueNumber(sale)) {
      throw new IabakoUniqueConstraintException(
          messages.getLabel("validation_sale_unique_constraint_error_title"),
          messages.getLabel("validation_sale_unique_constraint_error_text", sale.getNumber()),
          true,
          true);
    }

    Sale saleFromDB = saleDAO.findById(sale.getId());
    setAttributesFromDB(sale, saleFromDB);

    if (!enterprise.getPaymentRequestConfig().isEnabled()
        || (sale.getClient() != null && sale.getClient().isPaymentRequestDisabled())) {
      // If paymentRequest disable, then do not change initial value (it doesn't come from client
      // world)
      sale.setPaymentRequestDisabled(saleFromDB.isPaymentRequestDisabled());
    }

    sale.setDate(GenericTools.getDateFromString(sale.getDateString()));

    trackingService.addTrackingToUserSession(TrackingType.saleModify, sale);

    return rawSave(sale);
  }
  public List<CustomField> getCustomCriteriaSearchFields(int limit) {

    User user = getSessionUser();

    String query =
        "SELECT field FROM CustomField field "
            + "WHERE field.criteriaSearch = true "
            + "AND field.enterprise = :enterprise "
            + "AND field.position < :limit order by field.position asc";

    Enterprise enterprise = user.getEnterprise();
    enterprise =
        enterprise.getParentEnterprise() != null ? enterprise.getParentEnterprise() : enterprise;

    return (List<CustomField>)
        entityManager
            .createQuery(query)
            .setParameter("enterprise", enterprise)
            .setParameter("limit", limit)
            .getResultList();
  }
  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);
      }
    }
  }
  /**
   * Method to ALWAYS USED IN SALE MAKE (PERSIST AND DRAFT MODE) We can't pass by cascading persist
   * of SaleProductServices, Payments and Installments because GWT does not support (serialize)
   * PersistentBag and Proxy/Lazy hibernate objects
   *
   * @param sale Sale object from GWT client (navigator/JS world)
   * @param payments List of Payment from GWT client (navigator/JS world)
   * @param installments List of Installments from GWT client (navigator/JS world)
   */
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public synchronized Sale save(
      boolean isDraft,
      Sale sale,
      List<SaleProductService> productsServices,
      List<Expense> expenses,
      List<Payment> payments,
      List<Installment> installments)
      throws IabakoActionForbiddenException, IabakoPackageForbiddenException,
          IabakoUniqueConstraintException, IabakoStockException {

    Enterprise enterprise = getEnterpriseFromSessionUser();

    if (enterprise != null && enterprise.isDemo()) {
      throw new IabakoActionForbiddenException(
          "action_forbidden_for_demo_title", "action_forbidden_for_demo_text", true);
    }

    if (!getDAO().isUniqueNumber(sale)) {
      throw new IabakoUniqueConstraintException(
          messages.getLabel("validation_sale_unique_constraint_error_title"),
          messages.getLabel("validation_sale_unique_constraint_error_text", sale.getNumber()),
          true,
          true);
    }

    if (sale.getEnterprise() == null) {
      sale.setEnterprise(enterprise);
    }

    if (sale.getClient() != null && sale.getClient().getEnterprise() == null) {
      sale.getClient().setEnterprise(enterprise);
    }

    sale.setDate(GenericTools.getDateFromString(sale.getDateString()));

    addProductsServicesToSale(isDraft, sale, productsServices);
    addCustomInstallmentsDefinition(sale, installments);

    sale.setExpenses(expenses);
    // This is because expenses is @GWTTransient and It should not be used too much (so it helps to
    // avoid unnecessary server calls).
    sale.setSaleWithExpenses(!expenses.isEmpty());

    if (!isDraft) {
      addClientTag(sale);
      addPaymentsInstallments(sale, payments, installments);
      sale.setRecalculatedTotal(
          ServerTools.round(sale.calculateAmountFromPaymentsAndInstalments()));
      sale.setTaxPercent(sale.getPonderedTaxPercent());
    } else {
      sale.setTaxPercent(sale.getPonderedTaxPercent());
      sale.setRecalculatedTotal(sale.calculateAmountFromProductsServices());
    }
    sale.setTotalBeforeTax(
        sale.getTaxPercent() > 0
            ? ServerTools.round(sale.getRecalculatedTotal() / (1 + sale.getTaxPercent() / 100))
            : null);
    sale.setTotalAfterTax(sale.getTaxPercent() > 0 ? sale.getRecalculatedTotal() : null);
    sale.setTotalNoTax(sale.getTaxPercent() > 0 ? null : sale.getRecalculatedTotal());

    boolean isCreation = sale.getId() == null;
    sale = calculateStatusAndSave(sale, isDraft);
    setBusinessInfo(sale);

    for (Expense e : expenses) {
      // Sale.expenses is a detached relationship. It must be merge manually (not by cascade
      // hibernate function)
      e.setSale(sale);
      e.setPaymentDate(GenericTools.getDateFromString(e.getPaymentDateString()));
      e.setEnterprise(enterprise);
      expenseService.save(e);
    }

    Sale quote = checkIfQuote(sale);

    if (isCreation && quote != null) {
      trackingService.addTrackingToUserSession(TrackingType.quoteTransformed, quote);

    } else if (isDraft) {
      trackingService.addTrackingToUserSession(TrackingType.saleDraft, sale);

    } else {
      trackingService.addTrackingToUserSession(TrackingType.saleNew, sale);
      userService.addInvoiceReceiptToEnterpriseBalance(sale, null);
    }

    return sale;
  }