/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2016 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.cash.management.service;

import java.math.BigDecimal;
import java.util.List;

import org.joda.time.LocalDate;

import com.axelor.apps.base.db.BankDetails;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.cash.management.db.Forecast;
import com.axelor.apps.cash.management.db.ForecastGenerator;
import com.axelor.apps.cash.management.db.ForecastReason;
import com.axelor.apps.cash.management.db.repo.ForecastRepository;
import com.google.inject.Inject;

public class ForecastService {

	@Inject
	protected GeneralService generalService;

	public void generate(ForecastGenerator forecastGenerator) {
		LocalDate fromDate = forecastGenerator.getFromDate();
		LocalDate toDate = forecastGenerator.getToDate();
		LocalDate itDate = new LocalDate(fromDate);
		LocalDate todayDate = generalService.getTodayDate();

		if (forecastGenerator.getForecastList() != null && !forecastGenerator.getForecastList().isEmpty()) {
			List<Forecast> forecastList = forecastGenerator.getForecastList();
			for (Forecast forecast : forecastList) {
				if (forecast.getRealizedSelect() == ForecastRepository.REALISED_SELECT_NO) {
					forecastList.remove(forecast);
				} else if (forecast.getRealizedSelect() == ForecastRepository.REALISED_SELECT_AUTO
						&& forecast.getEstimatedDate().isAfter(todayDate)) {
					forecastList.remove(forecast);
				}
			}
		}

		while (!itDate.isAfter(toDate)) {
			Forecast forecast = this.createForecast(forecastGenerator.getCompany(), forecastGenerator.getBankDetails(),
					forecastGenerator.getTypeSelect(), forecastGenerator.getAmount(), itDate,
					forecastGenerator.getForecastReason(), forecastGenerator.getComments(),
					forecastGenerator.getRealizedSelect());
			forecastGenerator.addForecastListItem(forecast);
			itDate.plusMonths(forecastGenerator.getPeriodicitySelect());
		}
	}

	public Forecast createForecast(Company company, BankDetails bankDetails, int typeSelect, BigDecimal amount,
			LocalDate estimatedDate, ForecastReason reason, String comments, int realizedSelect) {

		Forecast forecast = new Forecast();
		forecast.setCompany(company);
		forecast.setBankDetails(bankDetails);
		forecast.setTypeSelect(typeSelect);
		forecast.setAmount(amount);
		forecast.setEstimatedDate(estimatedDate);
		forecast.setForecastReason(reason);
		forecast.setComments(comments);
		forecast.setRealizedSelect(realizedSelect);

		return forecast;
	}

}

@@ -772,16 +772,43 @@
 ecimal;%0A
+import java.util.Iterator;%0A
 import j
@@ -1814,21 +1814,25 @@
 %0A%09%09%09
-for (
+Iterator%3C
 Forecast
  for
@@ -1819,32 +1819,38 @@
 terator%3CForecast
+%3E it =
  forecast : fore
@@ -1846,26 +1846,85 @@
 cast
- : forecastList) %7B
+List.iterator();%0A%09%09%09while (it.hasNext()) %7B%0A%09%09%09%09Forecast forecast = it.next();
 %0A%09%09%09
@@ -2002,35 +2002,25 @@
 _NO) %7B%0A%09%09%09%09%09
-forecastLis
+i
 t.remove(for
@@ -2012,32 +2012,24 @@
 %09%09it.remove(
-forecast
 );%0A%09%09%09%09%7D els
@@ -2170,27 +2170,17 @@
  %7B%0A%09%09%09%09%09
-forecastLis
+i
 t.remove
@@ -2180,24 +2180,16 @@
 .remove(
-forecast
 );%0A%09%09%09%09%7D

Use an iterator loop instead of a foreach loop in order to be able to safely remove elements from the collection during the iteration.