<commit_before>/**
 * Axelor Business Solutions
 * <p>
 * Copyright (C) 2017 Axelor (<http://axelor.com>).
 * <p>
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.account.service;

import com.axelor.apps.account.db.AccountManagement;
import com.axelor.apps.account.db.AccountingSituation;
import com.axelor.apps.account.db.PaymentMode;
import com.axelor.apps.account.db.repo.PaymentModeRepository;
import com.axelor.apps.account.service.payment.PaymentModeService;
import com.axelor.apps.base.db.BankDetails;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Partner;
import com.axelor.apps.base.service.BankDetailsServiceImpl;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.tool.StringTool;
import com.axelor.inject.Beans;

import java.util.ArrayList;
import java.util.List;

public class BankDetailsServiceAccountImpl extends BankDetailsServiceImpl {

    /**
     * In this implementation, we use the O2M in payment mode.
     * @param company
     * @param paymentMode
     * @return
     */
    @Override
    public String createCompanyBankDetailsDomain(Company company,
                                                 PaymentMode paymentMode) {

        if (!Beans.get(GeneralService.class).getGeneral().getManageMultiBanks()) {
            return super.createCompanyBankDetailsDomain(company, paymentMode);
        } else {
            if (paymentMode == null) {
                return "self.id IN (0)";
            }
            List<AccountManagement> accountManagementList = paymentMode.getAccountManagementList();

            List<BankDetails> authorizedBankDetails = new ArrayList<>();

            for (AccountManagement accountManagement : accountManagementList) {
                if (accountManagement.getCompany() != null
                        && accountManagement.getCompany().equals(company)) {
                    authorizedBankDetails.add(accountManagement.getBankDetails());
                }
            }
            if (authorizedBankDetails.isEmpty()) {
                return "self.id IN (0)";
            } else {
                return "self.id IN ("
                        + StringTool.getIdFromCollection(authorizedBankDetails)
                        + ") AND self.active = true";
            }
        }
    }

	/**
	 * Find a default bank details.
	 * @param company
	 * @param paymentMode
	 * @param partner
	 * @return  the default bank details in accounting situation if it is active
	 *          and allowed by the payment mode, or an authorized bank details if
     *          he is the only one authorized.
	 */
    @Override
    public BankDetails getDefaultCompanyBankDetails(Company company,
                                                    PaymentMode paymentMode,
                                                    Partner partner) {

        if (paymentMode == null) {
            return null;
        }

        BankDetails candidateBankDetails = getDefaultCompanyBankDetailsFromPartner(company, paymentMode, partner);

        List<BankDetails>authorizedBankDetails = Beans.get(PaymentModeService.class).
                getCompatibleBankDetailsList(paymentMode, company);
        if (candidateBankDetails != null
                && authorizedBankDetails.contains(candidateBankDetails)
                && candidateBankDetails.getActive()) {
            return candidateBankDetails;
        }
        //we did not find a bank details in accounting situation
        else {
            if (authorizedBankDetails.size() == 1) {
                return authorizedBankDetails.get(0);
            } else {
                return null;
            }<commit_msg>RM8283: Fix default bankdetails with no multibanks<commit_after>/**
 * Axelor Business Solutions
 * <p>
 * Copyright (C) 2017 Axelor (<http://axelor.com>).
 * <p>
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.account.service;

import com.axelor.apps.account.db.AccountManagement;
import com.axelor.apps.account.db.AccountingSituation;
import com.axelor.apps.account.db.PaymentMode;
import com.axelor.apps.account.db.repo.PaymentModeRepository;
import com.axelor.apps.account.service.payment.PaymentModeService;
import com.axelor.apps.base.db.BankDetails;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Partner;
import com.axelor.apps.base.service.BankDetailsServiceImpl;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.tool.StringTool;
import com.axelor.inject.Beans;

import java.util.ArrayList;
import java.util.List;

public class BankDetailsServiceAccountImpl extends BankDetailsServiceImpl {

    /**
     * In this implementation, we use the O2M in payment mode.
     * @param company
     * @param paymentMode
     * @return
     */
    @Override
    public String createCompanyBankDetailsDomain(Company company,
                                                 PaymentMode paymentMode) {

        if (!Beans.get(GeneralService.class).getGeneral().getManageMultiBanks()) {
            return super.createCompanyBankDetailsDomain(company, paymentMode);
        } else {
            if (paymentMode == null) {
                return "self.id IN (0)";
            }
            List<AccountManagement> accountManagementList = paymentMode.getAccountManagementList();

            List<BankDetails> authorizedBankDetails = new ArrayList<>();

            for (AccountManagement accountManagement : accountManagementList) {
                if (accountManagement.getCompany() != null
                        && accountManagement.getCompany().equals(company)) {
                    authorizedBankDetails.add(accountManagement.getBankDetails());
                }
            }
            if (authorizedBankDetails.isEmpty()) {
                return "self.id IN (0)";
            } else {
                return "self.id IN ("
                        + StringTool.getIdFromCollection(authorizedBankDetails)
                        + ") AND self.active = true";
            }
        }
    }

	/**
	 * Find a default bank details.
	 * @param company
	 * @param paymentMode
	 * @param partner
	 * @return  the default bank details in accounting situation if it is active
	 *          and allowed by the payment mode, or an authorized bank details if
     *          he is the only one authorized.
	 */
    @Override
    public BankDetails getDefaultCompanyBankDetails(Company company,
                                                    PaymentMode paymentMode,
                                                    Partner partner) {

        if (!Beans.get(GeneralService.class).getGeneral().getManageMultiBanks()) {
            return super.getDefaultCompanyBankDetails(company, paymentMode, partner);
        } else {
            if (paymentMode == null) {
                return null;
            }

            BankDetails candidateBankDetails = getDefaultCompanyBankDetailsFromPartner(company, paymentMode, partner);

            List<BankDetails> authorizedBankDetails = Beans.get(PaymentModeService.class).
                    getCompatibleBankDetailsList(paymentMode, company);
            if (candidateBankDetails != null
                    && authorizedBankDetails.contains(candidateBankDetails)
                    && candidateBankDetails.getActive()) {
                return candidateBankDetails;
            }
            //we did not find a bank details in accounting situation
            else {
                if (authorizedBankDetails.size() == 1) {
                    return authorizedBankDetails.get(0);
                } else {
                    return null;
                }
            }
{'fraction_non_alphanumeric': 0.08438914027149322, 'fraction_numerical': 0.0027149321266968325, 'mean_word_length': 2.2817371937639197, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410127', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2098, 'n_words': 602}