/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2018 Axelor (<http://axelor.com>).
 *
 * 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.
 *
 * 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.
 *
 * 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.base.service;

import com.axelor.apps.base.db.Blocking;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Partner;
import com.axelor.apps.base.service.app.AppBaseService;
import com.axelor.inject.Beans;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class BlockingService {

    protected LocalDate today;

    public BlockingService() {
        this.today = Beans.get(AppBaseService.class).getTodayDate();
    }

    /**
     * Checks if {@code partner} is blocked for the {@code blockingType}
     *
     * @param partner      Partner to check blocking
     * @param company      Company associated with the blocking
     * @param blockingType Type of blocking
     * @return blocking if partner is blocked for provided company and blocking type, null otherwise
     */
    public Blocking getBlocking(Partner partner, Company company, int blockingType) {
        List<Blocking> blockings = partner.getBlockingList();

        for (Blocking blocking : blockings) {
            if (blocking.getCompanySet().contains(company) && blocking.getBlockingSelect().equals(blockingType) && blocking.getBlockingToDate().compareTo(today) >= 0) {
                return blocking;
            }
        }

        return null;
    }

    /**
     * @param company
     * @param blockingType
     * @return the query to get blocked partners ids for the given company and blocking type
     */
    public String listOfBlockedPartner(Company company, int blockingType) {
        return "SELECT DISTINCT partner.id " +
				"FROM Partner partner " +
				"LEFT JOIN partner.blockingList blocking " +
				"LEFT JOIN blocking.companySet company " +
				"WHERE blocking.blockingSelect = " + blockingType +
                " AND blocking.blockingToDate >= '" +
                Beans.get(AppBaseService.class).getTodayDate().format(DateTimeFormatter.ofPattern("YYYY-MM-dd")) +
                "' AND company.id = " + company.getId();
    }
}

@@ -2264,16 +2264,30 @@
  return 
+String.format(
 %22SELECT 
@@ -2306,25 +2306,16 @@
 tner.id 
-%22 +%0A%09%09%09%09%22
 FROM Par
@@ -2327,28 +2327,48 @@
 partner %22 +%0A
-%09%09%09%09
+                        
 %22LEFT JOIN p
@@ -2400,20 +2400,40 @@
 ing %22 +%0A
-%09%09%09%09
+                        
 %22LEFT JO
@@ -2471,12 +2471,32 @@
 %22 +%0A
-%09%09%09%09
+                        
 %22WHE
@@ -2528,27 +2528,23 @@
 t = 
-%22 + blockingType +%0A
+%25d %22 +%0A        
     
@@ -2556,17 +2556,16 @@
        %22
- 
 AND bloc
@@ -2588,16 +2588,20 @@
 ate %3E= '
+%25s' 
 %22 +%0A    
@@ -2611,16 +2611,77 @@
         
+         %22AND company.id = %25d%22,%0A                blockingType,
  Beans.g
@@ -2773,50 +2773,9 @@
 d%22))
- +%0A                %22' AND company.id = %22 +
+,
  com
@@ -2786,16 +2786,17 @@
 .getId()
+)
 ;%0A    %7D%0A

use format instead of concatenation str