/*
 * 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.account.web;

import com.axelor.apps.account.db.PaymentSchedule;
import com.axelor.apps.account.db.repo.PaymentScheduleRepository;
import com.axelor.apps.account.exception.IExceptionMessage;
import com.axelor.apps.account.service.IrrecoverableService;
import com.axelor.apps.account.service.PaymentScheduleService;
import com.axelor.apps.base.db.IAdministration;
import com.axelor.apps.base.service.administration.SequenceService;
import com.axelor.exception.service.TraceBackService;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.google.inject.Inject;

public class PaymentScheduleController {
	
	@Inject
	PaymentScheduleService paymentScheduleService;
	
	@Inject
	PaymentScheduleRepository paymentScheduleRepo;
	
	// Validation button
	public void validate(ActionRequest request, ActionResponse response) {

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);
		paymentSchedule = paymentScheduleRepo.find(paymentSchedule.getId());
		
		try {
			paymentScheduleService.validatePaymentSchedule(paymentSchedule);
			response.setReload(true);
		}
		catch(Exception e)  { TraceBackService.trace(response, e); }
	}
	
	// Cancel button
	public void cancel(ActionRequest request, ActionResponse response) {

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);
		paymentSchedule = paymentScheduleRepo.find(paymentSchedule.getId());
		
		try {
			paymentScheduleService.toCancelPaymentSchedule(paymentSchedule);
			response.setReload(true);
		}
		catch(Exception e)  { TraceBackService.trace(response, e); }
	}
	
	//Called on onSave event
	public void paymentScheduleScheduleId(ActionRequest request, ActionResponse response){

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);
		
		if (paymentSchedule.getPaymentScheduleSeq() == null) {
		
			String num = Beans.get(SequenceService.class).getSequenceNumber(IAdministration.PAYMENT_SCHEDULE, paymentSchedule.getCompany());
		
			if(num == null || num.isEmpty()) {
				
				response.setError(String.format(I18n.get(IExceptionMessage.PAYMENT_SCHEDULE_5), paymentSchedule.getCompany().getName())); 
			}
			else {
				response.setValue("paymentScheduleSeq", num);			
			}
		}
	}
	
	// Creating payment schedule lines button
	public void createPaymentScheduleLines(ActionRequest request, ActionResponse response) {

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);
		paymentSchedule = paymentScheduleRepo.find(paymentSchedule.getId());
		
		paymentScheduleService.createPaymentScheduleLines(paymentSchedule);
		response.setReload(true);
	}
	
	public void passInIrrecoverable(ActionRequest request, ActionResponse response)  {

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);		
		paymentSchedule = paymentScheduleRepo.find(paymentSchedule.getId());
		
		try  {
			Beans.get(IrrecoverableService.class).passInIrrecoverable(paymentSchedule);
			response.setReload(true);
		}
		catch(Exception e)  { TraceBackService.trace(response, e); }		
	}
	
	public void notPassInIrrecoverable(ActionRequest request, ActionResponse response)  {

		PaymentSchedule paymentSchedule = request.getContext().asType(PaymentSchedule.class);		
		paymentSchedule = paymentScheduleRepo.find(paymentSchedule.getId());
		
		try  {
			Beans.get(IrrecoverableService.class).notPassInIrrecoverable(paymentSchedule);
			response.setReload(true);
		}
		catch(Exception e)  { TraceBackService.trace(response, e); }
	}
}

@@ -1337,16 +1337,55 @@
 sponse;%0A
+import com.google.common.base.Strings;%0A
 import c
@@ -2433,19 +2433,23 @@
  %7D%0A%09%7D%0A%09%0A
-%09
+    
 //
+ 
 Called o
@@ -2463,17 +2463,20 @@
 e event%0A
-%09
+    
 public v
@@ -2552,21 +2552,45 @@
 esponse)
+ 
 %7B%0A
-%0A%09%09
+        try %7B%0A            
 PaymentS
@@ -2671,16 +2671,46 @@
 s);%0A
-%09%09%0A%09%09if 
+%0A            if (Strings.isNullOrEmpty
 (pay
@@ -2749,26 +2749,30 @@
 eq()
- == null) %7B%0A%09%09%0A%09%09%09
+)) %7B%0A%0A                
 Stri
@@ -2864,16 +2864,40 @@
 CHEDULE,
+%0A                       
  payment
@@ -2924,58 +2924,79 @@
 ));%0A
-%09%09%0A%09%09%09if(num == null %7C%7C num.is
+%0A                if (Strings.isNullOr
 Empty(
+num
 )) %7B%0A
-%09%09%09%09%0A%09%09%09%09
+                    
 resp
@@ -3070,16 +3070,44 @@
 DULE_5),
+%0A                           
  payment
@@ -3144,91 +3144,229 @@
 )));
- %0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09response.setValue(%22paymentScheduleSeq%22, num);%09%09%09%0A%09%09%09%7D%0A%09%09%7D%0A%09
+%0A                %7D else %7B%0A                    response.setValue(%22paymentScheduleSeq%22, num);%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A            TraceBackService.trace(response, e);%0A        %7D%0A    
 %7D%0A
-%09
 %0A%09//

improve payment schedule sequence generation.