/**
   * Methode permettant de récupérer la liste des échéances à prélever en fonction de la société et
   * de la date de prélèvement
   *
   * @param company Une société
   * @param debitDate Une date de prélèvement
   * @return
   */
  public List<PaymentScheduleLine> getPaymentScheduleLineToDebit(AccountingBatch accountingBatch) {

    Company company = accountingBatch.getCompany();
    LocalDate debitDate = accountingBatch.getDebitDate();
    Currency currency = accountingBatch.getCurrency();

    PaymentMode paymentMode = company.getAccountConfig().getDirectDebitPaymentMode();

    List<PaymentScheduleLine> paymentScheduleLineList =
        paymentScheduleLineRepo
            .all()
            .filter(
                "self.statusSelect = ?1 AND self.paymentSchedule.statusSelect = ?2 AND self.paymentSchedule.company = ?3 "
                    + "AND self.scheduleDate <= ?4 "
                    + "AND self.debitBlockingOk IN ('false',null) "
                    + "AND self.paymentSchedule.currency = ?5 "
                    + "AND self.paymentSchedule.paymentMode = ?6 ORDER BY self.scheduleDate",
                PaymentScheduleLineRepository.STATUS_IN_PROGRESS,
                PaymentScheduleRepository.STATUS_CONFIRMED,
                company,
                debitDate,
                currency,
                paymentMode)
            .fetch();

    if (paymentScheduleLineList.size() < 50) {
      log.debug(
          "\n Liste des échéances retenues : {} \n",
          this.toStringPaymentScheduleLineList(paymentScheduleLineList));
    } else {
      log.debug("\n Nombres échéances retenues : {} \n", paymentScheduleLineList.size());
    }

    return paymentScheduleLineList;
  }
 /**
  * Y a t'il d'autres échéance a exporter pour le même payeur ?
  *
  * @param pslList : une liste d'échéance
  * @param psl
  * @return
  */
 public boolean hasOtherPaymentScheduleLine(
     List<PaymentScheduleLine> pslList, PaymentScheduleLine psl) {
   int i = 0;
   for (PaymentScheduleLine paymentScheduleLine : pslList) {
     paymentScheduleLine = paymentScheduleLineRepo.find(paymentScheduleLine.getId());
     if (psl.getPaymentSchedule().equals(paymentScheduleLine.getPaymentSchedule())) {
       i++;
     }
   }
   return i > 1;
 }
 /**
  * Procédure permettant de récupérer l'objet de gestion déjà créé lors du prélèvement d'une autre
  * échéance
  *
  * @param pslList La liste d'échéance à prélever
  * @param psl L'échéance à prélever
  * @return L'objet de gestion trouvé
  */
 public DirectDebitManagement getDirectDebitManagement(
     List<PaymentScheduleLine> pslList, PaymentScheduleLine psl) {
   for (PaymentScheduleLine paymentScheduleLine : pslList) {
     paymentScheduleLine = paymentScheduleLineRepo.find(paymentScheduleLine.getId());
     if (psl.getPaymentSchedule().equals(paymentScheduleLine.getPaymentSchedule())) {
       if (paymentScheduleLine.getDirectDebitManagement() != null) {
         return paymentScheduleLine.getDirectDebitManagement();
       }
     }
   }
   return null;
 }
  /**
   * Méthode permettant de retrouver l'échéance rejetée qui à impliquer la création de la nouvelle
   * échéance
   *
   * @param paymentScheduleLine La nouvelle échéance
   */
  public PaymentScheduleLine getPaymentScheduleLineRejectOrigin(
      PaymentScheduleLine paymentScheduleLine) {

    return paymentScheduleLineRepo
        .all()
        .filter(
            "self.paymentSchedule = ?1 AND self.scheduleLineSeq = ?2 AND self.statusSelect = ?3 ORDER BY self.rejectDate DESC",
            paymentScheduleLine.getPaymentSchedule(),
            paymentScheduleLine.getScheduleLineSeq(),
            PaymentScheduleLineRepository.STATUS_CLOSED)
        .fetchOne();
  }
  @Transactional(rollbackOn = {AxelorException.class, Exception.class})
  public PaymentScheduleLine generateExportMensu(
      PaymentScheduleLine paymentScheduleLine,
      List<PaymentScheduleLine> paymentScheduleLineList,
      Company company)
      throws AxelorException {

    PaymentSchedule paymentSchedule = paymentScheduleLine.getPaymentSchedule();

    this.testBankDetails(paymentSchedule);

    AccountConfig accountConfig = company.getAccountConfig();

    Account account = accountConfig.getCustomerAccount();
    PaymentMode paymentMode = accountConfig.getDirectDebitPaymentMode();

    BigDecimal amount = paymentScheduleLine.getInTaxAmount();
    Partner partner = paymentSchedule.getPartner();

    Move move =
        moveService
            .getMoveCreateService()
            .createMove(
                paymentModeService.getPaymentModeJournal(paymentMode, company),
                company,
                null,
                partner,
                paymentMode);

    this.setDebitNumber(paymentScheduleLineList, paymentScheduleLine, company);

    MoveLine moveLine =
        moveLineRepo.save(
            moveLineServices.createMoveLine(
                move, partner, account, amount, false, today, 1, paymentScheduleLine.getName()));

    move.addMoveLineListItem(moveLine);

    if (paymentScheduleLine.getFromReject()) {
      // lettrage avec le rejet
      PaymentScheduleLine rejectedPaymentScheduleLine =
          this.getPaymentScheduleLineRejectOrigin(paymentScheduleLine);
      if (rejectedPaymentScheduleLine.getRejectMoveLine() != null
          && rejectedPaymentScheduleLine
                  .getRejectMoveLine()
                  .getAmountRemaining()
                  .compareTo(BigDecimal.ZERO)
              == 1) {
        reconcileService.reconcile(rejectedPaymentScheduleLine.getRejectMoveLine(), moveLine);
      }
    } else {
      // Lettrage du paiement avec les factures d'échéances
      this.reconcileDirectDebit(moveLine, paymentSchedule);
    }

    move.addMoveLineListItem(
        moveLineServices.createMoveLine(
            move,
            partner,
            paymentModeService.getCompanyAccount(paymentMode, company),
            amount,
            true,
            today,
            2,
            null));

    this.validateMove(move);

    paymentScheduleLine.setDirectDebitAmount(amount);
    paymentScheduleLine.setInTaxAmountPaid(amount);
    paymentScheduleLine.setAdvanceOrPaymentMove(moveRepo.find(move.getId()));
    paymentScheduleLine.setAdvanceMoveLine(moveLine);
    paymentScheduleLine.setStatusSelect(PaymentScheduleLineRepository.STATUS_VALIDATED);
    return paymentScheduleLineRepo.save(paymentScheduleLine);
  }