/**
 * 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.bankpayment.service.batch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.joda.time.LocalDate;

import com.axelor.apps.bankpayment.db.BankPaymentBatch;
import com.axelor.apps.bankpayment.db.EbicsCertificate;
import com.axelor.apps.bankpayment.db.EbicsUser;
import com.axelor.apps.bankpayment.db.repo.BankPaymentBatchRepository;
import com.axelor.apps.bankpayment.db.repo.EbicsCertificateRepository;
import com.axelor.apps.bankpayment.db.repo.EbicsUserRepository;
import com.axelor.apps.base.db.Batch;
import com.axelor.apps.base.db.repo.BatchRepository;
import com.axelor.apps.base.service.administration.AbstractBatch;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.message.db.Template;
import com.axelor.apps.message.db.repo.TemplateRepository;
import com.axelor.apps.message.service.TemplateMessageService;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.google.inject.Inject;

public class BatchBankPayment extends AbstractBatch {
	
	protected BankPaymentBatch bankPaymentBatch;
	
	@Inject
	private TemplateRepository templateRepo;
	
	@Inject
	private TemplateMessageService templateMessageService;
	
	@Inject
	private BatchRepository batchRepo;
	
	@Override
	protected void start() throws IllegalArgumentException, IllegalAccessException, AxelorException {
		
		super.start();
		
		bankPaymentBatch = Beans.get(BankPaymentBatchRepository.class).find(batch.getBankPaymentBatch().getId());

	}
		
	@Override
	protected void process() {
		
		Template template = templateRepo.find(bankPaymentBatch.getTemplate().getId());
				
		List<EbicsUser> users = Beans.get(EbicsUserRepository.class).all().filter("self.a005Certificate != null OR self.e002Certificate != null OR self.x002Certificate != null").fetch();
		
		Set<EbicsCertificate> certificatesSet = new HashSet<>();
		
		LocalDate today = Beans.get(GeneralService.class).getTodayDate();
		LocalDate commingDay = today.plusDays(bankPaymentBatch.getDaysNbr());
		
		for (EbicsUser user : users) {
			if (user.getA005Certificate() != null && user.getA005Certificate().getValidTo().isBefore(commingDay)) {
				certificatesSet.add(user.getA005Certificate());
			}

			if (user.getE002Certificate() != null && user.getE002Certificate().getValidTo().isBefore(commingDay)) {
				certificatesSet.add(user.getE002Certificate());
			}

			if (user.getX002Certificate() != null && user.getX002Certificate().getValidTo().isBefore(commingDay)) {
				certificatesSet.add(user.getX002Certificate());
			}
		}
				
		certificatesSet.addAll(Beans.get(EbicsCertificateRepository.class).all().
				filter("self.ebicsBank != null AND self.validTo <= ?1", commingDay).fetch());
		
		for(EbicsCertificate certificate : certificatesSet) {
				
			certificate.addBatchSetItem(batchRepo.find(batch.getId()));
				
			try {
				templateMessageService.generateMessage(certificate, template);
			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | AxelorException
					| IOException e) {
					e.printStackTrace();
			}
		}
	}
	
	public Batch ebicsCertificate(BankPaymentBatch bankPaymentBatch) {
		
		return Beans.get(BatchBankPayment.class).run(bankPaymentBatch);
	}
	
}

@@ -769,24 +769,52 @@
 OException;%0A
+import java.time.LocalDate;%0A
 import java.
@@ -905,40 +905,8 @@
 t;%0A%0A
-import org.joda.time.LocalDate;%0A
 %0Aimp
@@ -1467,29 +1467,18 @@
 ce.a
-dministration.General
+pp.AppBase
 Serv
@@ -2682,23 +2682,23 @@
 ans.get(
-General
+AppBase
 Service.

Fix after cherry-pick