/**
   * Permet de déréconcilier
   *
   * @param reconcile Une reconciliation
   * @return L'etat de la réconciliation
   * @throws AxelorException
   */
  @Transactional(rollbackOn = {AxelorException.class, Exception.class})
  public String unreconcile(Reconcile reconcile) throws AxelorException {

    LOG.debug("In unreconcile ....");
    LOG.debug("Credit .... {} " + reconcile.getLineCredit().getAmountPaid());
    LOG.debug("Debit .... {}" + reconcile.getLineDebit().getAmountPaid());

    if (reconcile.getState().equals("2")) {
      if (reconcile.getLineCredit() != null && reconcile.getLineDebit() != null) {
        // Change the state
        reconcile.setState("3");
        // Add the reconciled amount to the reconciled amount in the move line
        reconcile
            .getLineCredit()
            .setAmountPaid(
                reconcile.getLineCredit().getAmountPaid().subtract(reconcile.getAmount()));
        reconcile
            .getLineDebit()
            .setAmountPaid(
                reconcile.getLineDebit().getAmountPaid().subtract(reconcile.getAmount()));

        // Update amount remaining on invoice or refund
        if (reconcile.getLineDebit().getMove().getInvoice() != null) {
          reconcile
              .getLineDebit()
              .getMove()
              .getInvoice()
              .setInTaxTotalRemaining(
                  ms.getInTaxTotalRemaining(
                      reconcile.getLineDebit().getMove().getInvoice(),
                      reconcile.getLineDebit().getMove().getInvoice().getPartnerAccount()));
        }
        if (reconcile.getLineCredit().getMove().getInvoice() != null) {
          reconcile
              .getLineCredit()
              .getMove()
              .getInvoice()
              .setInTaxTotalRemaining(
                  ms.getInTaxTotalRemaining(
                      reconcile.getLineCredit().getMove().getInvoice(),
                      reconcile.getLineCredit().getMove().getInvoice().getPartnerAccount()));
        }

        reconcile.save();
        LOG.debug("End Unreconcile.");
        return reconcile.getState();
      } else {
        throw new AxelorException(
            String.format(
                "%s :\nReconciliation %s: Merci de renseigner les lignes d'écritures concernées.",
                GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
            IException.CONFIGURATION_ERROR);
      }
    } else {
      throw new AxelorException(
          String.format(
              "%s :\nReconciliation %s: Vous ne pouvez pas délétrer en dehors de l'état 'Confirmée'.",
              GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
          IException.CONFIGURATION_ERROR);
    }
  }
  /**
   * Permet de confirmer une réconciliation On ne peut réconcilier que des moveLine ayant le même
   * compte
   *
   * @param reconcile Une reconciliation
   * @return L'etat de la reconciliation
   * @throws AxelorException
   */
  @Transactional(rollbackOn = {AxelorException.class, Exception.class})
  public String confirmReconcile(Reconcile reconcile, boolean updateCustomerAccount)
      throws AxelorException {

    LOG.debug("In confirmReconcile ....");
    if (reconcile != null) {
      if (reconcile.getLineCredit() != null && reconcile.getLineDebit() != null) {
        // Check if move lines accounts are the same (debit and credit)
        LOG.debug(
            "Compte ligne de credit : {} , Compte ligne de debit : {}",
            reconcile.getLineCredit().getAccount(),
            reconcile.getLineDebit().getAccount());
        if (!reconcile.getLineCredit().getAccount().equals(reconcile.getLineDebit().getAccount())) {
          throw new AxelorException(
              String.format(
                  "%s :\nReconciliation %s: Les lignes d'écritures sélectionnées doivent concerner le même compte comptable.",
                  GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
              IException.CONFIGURATION_ERROR);
        }
        // Check if the amount to reconcile is != zero
        else if (reconcile.getAmount() == null
            || reconcile.getAmount().compareTo(BigDecimal.ZERO) == 0) {
          throw new AxelorException(
              String.format(
                  "%s :\nReconciliation %s: Le montant réconcilié doit être différent de zéro.",
                  GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
              IException.INCONSISTENCY);

        } else {
          // Check if the amount to reconcile is less than the 2 moves credit and debit
          LOG.debug("AMOUNT  : : : {}", reconcile.getAmount());
          LOG.debug(
              "credit - paid  : : : {}",
              reconcile
                  .getLineCredit()
                  .getCredit()
                  .subtract(reconcile.getLineCredit().getAmountPaid()));
          LOG.debug(
              "debit - paid  : : : {}",
              reconcile
                  .getLineDebit()
                  .getDebit()
                  .subtract(reconcile.getLineDebit().getAmountPaid()));

          if ((reconcile
                      .getAmount()
                      .compareTo(
                          reconcile
                              .getLineCredit()
                              .getCredit()
                              .subtract(reconcile.getLineCredit().getAmountPaid()))
                  > 0
              || (reconcile
                      .getAmount()
                      .compareTo(
                          reconcile
                              .getLineDebit()
                              .getDebit()
                              .subtract(reconcile.getLineDebit().getAmountPaid()))
                  > 0))) {
            throw new AxelorException(
                String.format(
                    "%s :\nReconciliation %s: Le montant réconcilié doit être inférieur ou égale au montant restant à réconcilier des lignes d'écritures.",
                    GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
                IException.INCONSISTENCY);

          } else {

            // Add the reconciled amount to the reconciled amount in the move line
            reconcile
                .getLineCredit()
                .setAmountPaid(
                    reconcile.getLineCredit().getAmountPaid().add(reconcile.getAmount()));
            reconcile
                .getLineDebit()
                .setAmountPaid(reconcile.getLineDebit().getAmountPaid().add(reconcile.getAmount()));

            // Update amount remaining on invoice or refund
            if (reconcile.getLineDebit().getMove().getInvoice() != null) {
              reconcile
                  .getLineDebit()
                  .getMove()
                  .getInvoice()
                  .setInTaxTotalRemaining(
                      ms.getInTaxTotalRemaining(
                          reconcile.getLineDebit().getMove().getInvoice(),
                          reconcile.getLineDebit().getMove().getInvoice().getPartnerAccount()));
            }
            if (reconcile.getLineCredit().getMove().getInvoice() != null) {
              reconcile
                  .getLineCredit()
                  .getMove()
                  .getInvoice()
                  .setInTaxTotalRemaining(
                      ms.getInTaxTotalRemaining(
                          reconcile.getLineCredit().getMove().getInvoice(),
                          reconcile.getLineCredit().getMove().getInvoice().getPartnerAccount()));
            }

            this.updatePartnerAccountingSituation(reconcile, updateCustomerAccount);

            // Change the state
            reconcile.setState("2");

            if (reconcile.getCanBeZeroBalanceOk()) {
              // Alors nous utilisons la règle de gestion consitant à imputer l'écart sur un compte
              // transitoire si le seuil est respecté
              canBeZeroBalance(reconcile);
            }

            reconcile.save();

            LOG.debug("End confirmReconcile.");
            return reconcile.getState();
          }
        }
      } else {
        throw new AxelorException(
            String.format(
                "%s :\nReconciliation %s: Merci de renseigner les lignes d'écritures concernées.",
                GeneralService.getExceptionAccountingMsg(), reconcile.getFullName()),
            IException.CONFIGURATION_ERROR);
      }
    } else {
      LOG.debug("***************** ****** *** NO ID *** ****** **********************");
      return null;
    }
  }