/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2017 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.hr.service.lunch.voucher;

import java.math.RoundingMode;
import java.util.List;

import javax.inject.Inject;

import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.LunchVoucherAdvance;
import com.axelor.apps.hr.db.LunchVoucherMgt;
import com.axelor.apps.hr.db.LunchVoucherMgtLine;
import com.axelor.apps.hr.db.repo.LunchVoucherAdvanceRepository;
import com.axelor.apps.hr.service.employee.EmployeeService;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;

public class LunchVoucherMgtLineServiceImpl implements LunchVoucherMgtLineService {
	
	@Inject
	protected EmployeeService employeeService;
	
	@Override
	public LunchVoucherMgtLine create(Employee employee, LunchVoucherMgt lunchVoucherMgt) throws AxelorException {
		LunchVoucherMgtLine lunchVoucherMgtLine = new LunchVoucherMgtLine();
		
		lunchVoucherMgtLine.setEmployee(employee);
		lunchVoucherMgtLine.setInAdvanceNbr(computeEmployeeLunchVoucherAdvance(employee));
		lunchVoucherMgtLine.setDaysWorkedNbr(employeeService.getDaysWorkedInPeriod(employee, lunchVoucherMgt.getLeavePeriod().getFromDate(), lunchVoucherMgt.getLeavePeriod().getToDate()).setScale(0, RoundingMode.HALF_UP).intValue());
		lunchVoucherMgtLine.setLunchVoucherNumber(lunchVoucherMgtLine.getDaysWorkedNbr() - lunchVoucherMgtLine.getInAdvanceNbr());
		
		return lunchVoucherMgtLine;
	}

	private Integer computeEmployeeLunchVoucherAdvance(Employee employee) {
		int number = 0;
		List<LunchVoucherAdvance> list = Beans.get(LunchVoucherAdvanceRepository.class).all().filter("self.employee.id = ?1 AND self.nbrLunchVouchersUsed < self.nbrLunchVouchers", employee.getId()).fetch();
		
		for (LunchVoucherAdvance item : list) {
			number += item.getNbrLunchVouchers() - item.getNbrLunchVouchersUsed();
		}
		
		return number;
	}

	@Override
	public void compute(LunchVoucherMgtLine lunchVoucherMgtLine) throws AxelorException {
		lunchVoucherMgtLine.setLunchVoucherNumber(lunchVoucherMgtLine.getDaysWorkedNbr() - 
				(lunchVoucherMgtLine.getCanteenEntries() + lunchVoucherMgtLine.getDaysOverseas() + lunchVoucherMgtLine.getInAdvanceNbr() + lunchVoucherMgtLine.getInvitation()));
	}

}

@@ -1918,91 +1918,19 @@
 ;%0A%09%09
-lunchVoucherMgtLine.setLunchVoucherNumber(lunchVoucherMgtLine.getDaysWorkedNbr() - 
+%0A%09%09compute(
 lunc
@@ -1944,34 +1944,16 @@
 rMgtLine
-.getInAdvanceNbr()
 );%0A%09%09%0A%09%09

Use compute method.