/**
   * Y a t'il d'autres facture a exporter pour le même payeur ?
   *
   * @param moveLineList : une liste de ligne d'écriture de facture
   * @param psl
   * @return
   */
  public boolean hasOtherInvoice(List<MoveLine> moveLineList, MoveLine moveLine) {

    Partner partner = moveLine.getPartner();

    Query q =
        JPA.em()
            .createQuery(
                "select count(*) FROM MoveLine as self WHERE self IN ?1 AND self.partner = ?2 ");
    q.setParameter(1, moveLineList);
    q.setParameter(2, partner);

    if ((long) q.getSingleResult() > 1) {
      log.debug(
          "Recherche d'une autre facture à prélever (autre que l'écriture {}) pour le tiers {} : OUI",
          moveLine.getName(),
          partner.getFullName());
      return true;
    }

    log.debug(
        "Recherche d'une autre facture à prélever (autre que l'écriture {}) pour le tiers {} : NON",
        moveLine.getName(),
        partner.getFullName());

    return false;
  }
  @Override
  protected void process() {

    for (Invoice invoice : invoices(batch.getInvoiceBatch(), true)) {

      try {

        invoiceService.validate(invoiceRepo.find(invoice.getId()));
        updateInvoice(invoiceRepo.find(invoice.getId()));

      } catch (AxelorException e) {

        TraceBackService.trace(
            new AxelorException(
                String.format(I18n.get("Facture") + " %s", invoice.getInvoiceId()),
                e,
                e.getcategory()),
            IException.INVOICE_ORIGIN,
            batch.getId());
        incrementAnomaly();

      } catch (Exception e) {

        TraceBackService.trace(
            new Exception(String.format(I18n.get("Facture") + " %s", invoice.getInvoiceId()), e),
            IException.INVOICE_ORIGIN,
            batch.getId());
        incrementAnomaly();

      } finally {

        JPA.clear();
      }
    }
  }
  /**
   * Méthode permettant de récupérer le dernier 'id' utilisé pour l'objet de gestion des
   * prélèvements afin de pouvoir exclure les numéros de prélèvements consolidés, rejetés, lors du
   * prochain prélèvement.
   *
   * @return
   */
  public long getDirectDevitManagementMaxId() {

    Query q = JPA.em().createQuery("select MAX(id) FROM DirectDebitManagement");

    Object result = q.getSingleResult();

    if (result != null) {
      return (long) result;
    }
    return 0;
  }
  public void reminderPartner() {

    int i = 0;
    List<Partner> partnerList =
        Partner.all()
            .filter(
                "self.reminderClosedOk = false AND ?1 IN self.companySet",
                batch.getAccountingBatch().getCompany())
            .fetch();

    for (Partner partner : partnerList) {

      try {

        boolean remindedOk =
            reminderService.reminderGenerate(
                Partner.find(partner.getId()), batch.getAccountingBatch().getCompany());

        if (remindedOk == true) {
          updatePartner(partner);
          i++;
        }

        LOG.debug("Tiers traité : {}", partner.getName());

      } catch (AxelorException e) {

        TraceBackService.trace(
            new AxelorException(String.format("Tiers %s", partner.getName()), e, e.getcategory()),
            IException.REMINDER,
            batch.getId());
        incrementAnomaly();

      } catch (Exception e) {

        TraceBackService.trace(
            new Exception(String.format("Tiers %s", partner.getName()), e),
            IException.REMINDER,
            batch.getId());

        incrementAnomaly();

        LOG.error("Bug(Anomalie) généré(e) pour le tiers {}", partner.getName());

      } finally {

        if (i % 10 == 0) {
          JPA.clear();
        }
      }
    }
  }
  @GET
  @Path("models")
  @SuppressWarnings("all")
  public Response models() {

    final Response response = new Response();
    final List<String> all = Lists.newArrayList();

    for (Class<?> cls : JPA.models()) {
      if (security.isPermitted(AccessType.READ, (Class) cls)) {
        all.add(cls.getName());
      }
    }

    Collections.sort(all);

    response.setData(all);
    response.setTotal(all.size());
    response.setStatus(Response.STATUS_SUCCESS);
    return response;
  }
  @Override
  public Object evaluate(ActionHandler handler) {
    Map<String, Object> result = Maps.newHashMap();
    Map<String, Object> context = Maps.newHashMap();
    Map<String, Object> viewParams = Maps.newHashMap();
    List<Object> items = Lists.newArrayList();

    String viewType = null;

    for (View elem : views) {

      if (!elem.test(handler)) {
        continue;
      }

      Map<String, Object> map = Maps.newHashMap();
      map.put("name", handler.evaluate(elem.getName()));
      map.put("type", elem.getType());

      if (viewType == null) {
        viewType = elem.getType();
      }

      items.add(map);
    }

    if (contexts != null) {
      for (Context ctx : contexts) {
        if (ctx.test(handler)) {
          Object value = handler.evaluate(ctx.getExpression());
          if (ctx.getCanCopy() == Boolean.TRUE && value instanceof Model) {
            value = JPA.copy((Model) value, true);
          }
          context.put(ctx.getName(), value);

          // make it available to the evaluation context
          if (ctx.getName().startsWith("_")) {
            handler.getContext().put(ctx.getName(), value);
          }
        }
      }
    }

    if (!context.containsKey("_id") && handler.getContext().containsKey("id")) {
      context.put("_id", handler.evaluate("eval: id"));
    }

    if (params != null) {
      for (Param param : params) {
        Object value = param.value;
        if ("false".equals(value)) value = false;
        if ("true".equals(value)) value = true;
        viewParams.put(param.name, value);
      }
    }

    String domain = this.getDomain();
    if (domain != null && domain.contains("$")) {
      domain = handler.evaluate("eval: \"" + domain + "\"").toString();
    }

    String title = this.getLocalizedTitle();
    if (title != null && title.contains("$")) {
      title = handler.evaluate("eval: \"" + title + "\"").toString();
    }

    result.put("title", title);
    result.put("icon", getIcon());
    result.put("model", getModel());
    result.put("viewType", viewType);
    result.put("views", items);
    result.put("domain", domain);
    result.put("context", context);
    result.put("params", viewParams);

    return result;
  }
 public String getTitle() {
   return JPA.translate(title, title, model, "filter");
 }