/**
   * Solder le trop-perçu si il respect les règles de seuil
   *
   * @param creditMoveLine
   * @param company
   * @throws AxelorException
   */
  public void balanceCredit(MoveLine creditMoveLine, Company company, boolean updateCustomerAccount)
      throws AxelorException {
    if (creditMoveLine != null) {
      BigDecimal creditAmountRemaining = creditMoveLine.getAmountRemaining();
      LOG.debug("Montant à payer / à lettrer au crédit : {}", creditAmountRemaining);

      if (creditAmountRemaining.compareTo(BigDecimal.ZERO) > 0) {
        if (creditAmountRemaining.plus().compareTo(company.getThresholdDistanceFromRegulation())
            < 0) {

          LOG.debug("Seuil respecté");

          Partner partner = creditMoveLine.getPartner();
          Account account = creditMoveLine.getAccount();

          if (company.getMiscOperationJournal() == null) {
            throw new AxelorException(
                String.format(
                    "%s :\n Veuillez configurer un journal des O.D. pour la société %s",
                    GeneralService.getExceptionAccountingMsg(), company.getName()),
                IException.CONFIGURATION_ERROR);
          }

          Move newMove =
              ms.createMove(company.getMiscOperationJournal(), company, null, partner, null, false);

          // Création de la ligne au crédit
          MoveLine newCreditMoveLine =
              mls.createMoveLine(
                  newMove,
                  partner,
                  company.getCashPositionVariationAccount(),
                  creditAmountRemaining,
                  false,
                  false,
                  today,
                  2,
                  false,
                  false,
                  false,
                  null);

          // Création de la ligne au débit
          MoveLine newDebitMoveLine =
              mls.createMoveLine(
                  newMove,
                  partner,
                  account,
                  creditAmountRemaining,
                  true,
                  false,
                  today,
                  1,
                  false,
                  false,
                  false,
                  null);

          newMove.getMoveLineList().add(newCreditMoveLine);
          newMove.getMoveLineList().add(newDebitMoveLine);
          ms.validateMove(newMove, updateCustomerAccount);
          newMove.save();

          // Création de la réconciliation
          Reconcile newReconcile =
              this.createReconcile(newDebitMoveLine, creditMoveLine, creditAmountRemaining);
          this.confirmReconcile(newReconcile, updateCustomerAccount);
          newReconcile.save();
        }
      }
    }
  }
  /**
   * Procédure permettant de gérer les écarts de règlement, check sur la case à cocher 'Peut être
   * soldé' Alors nous utilisons la règle de gestion consitant à imputer l'écart sur un compte
   * transitoire si le seuil est respecté
   *
   * @param reconcile Une reconciliation
   * @throws AxelorException
   */
  @Transactional(rollbackOn = {AxelorException.class, Exception.class})
  public void canBeZeroBalance(Reconcile reconcile) throws AxelorException {

    MoveLine debitMoveLine = reconcile.getLineDebit();

    BigDecimal debitAmountRemaining = debitMoveLine.getAmountRemaining();
    LOG.debug("Montant à payer / à lettrer au débit : {}", debitAmountRemaining);
    if (debitAmountRemaining.compareTo(BigDecimal.ZERO) > 0) {
      Company company = reconcile.getLineDebit().getMove().getCompany();
      if (debitAmountRemaining.plus().compareTo(company.getThresholdDistanceFromRegulation()) < 0
          || reconcile.getMustBeZeroBalanceOk()) {

        LOG.debug("Seuil respecté");

        Partner partner = debitMoveLine.getPartner();
        Account account = debitMoveLine.getAccount();

        if (company.getMiscOperationJournal() == null) {
          throw new AxelorException(
              String.format(
                  "%s :\n Veuillez configurer un journal des O.D. pour la société %s",
                  GeneralService.getExceptionAccountingMsg(), company.getName()),
              IException.CONFIGURATION_ERROR);
        }

        Move newMove =
            ms.createMove(company.getMiscOperationJournal(), company, null, partner, null, false);

        // Création de la ligne au crédit
        MoveLine newCreditMoveLine =
            mls.createMoveLine(
                newMove,
                partner,
                account,
                debitAmountRemaining,
                false,
                false,
                today,
                1,
                false,
                false,
                false,
                null);

        // Création de la ligne au debit
        MoveLine newDebitMoveLine =
            mls.createMoveLine(
                newMove,
                partner,
                company.getCashPositionVariationAccount(),
                debitAmountRemaining,
                true,
                false,
                today,
                2,
                false,
                false,
                false,
                null);

        newMove.getMoveLineList().add(newCreditMoveLine);
        newMove.getMoveLineList().add(newDebitMoveLine);
        ms.validateMove(newMove);
        newMove.save();

        // Création de la réconciliation
        Reconcile newReconcile =
            this.createReconcile(debitMoveLine, newCreditMoveLine, debitAmountRemaining);
        this.confirmReconcile(newReconcile);
        newReconcile.save();
      }
    }

    reconcile.setCanBeZeroBalanceOk(false);
    LOG.debug("Fin de la gestion des écarts de règlement");
  }