<commit_before>/**
 * 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.hr.web.expense;

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

import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axelor.apps.ReportFactory;
import com.axelor.apps.account.db.Move;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Product;
import com.axelor.apps.base.db.Wizard;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.base.service.message.MessageServiceBaseImpl;
import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.Expense;
import com.axelor.apps.hr.db.ExpenseLine;
import com.axelor.apps.hr.db.ExtraHours;
import com.axelor.apps.hr.db.HRConfig;
import com.axelor.apps.hr.db.repo.EmployeeRepository;
import com.axelor.apps.hr.db.repo.ExpenseRepository;
import com.axelor.apps.hr.exception.IExceptionMessage;
import com.axelor.apps.hr.report.IReport;
import com.axelor.apps.hr.service.HRMenuTagService;
import com.axelor.apps.hr.service.KilometricService;
import com.axelor.apps.hr.service.config.HRConfigService;
import com.axelor.apps.hr.service.expense.ExpenseService;
import com.axelor.apps.message.db.Message;
import com.axelor.apps.message.db.repo.MessageRepository;
import com.axelor.auth.AuthUtils;
import com.axelor.auth.db.User;
import com.axelor.db.JPA;
import com.axelor.db.Query;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.IException;
import com.axelor.exception.service.TraceBackService;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.meta.schema.actions.ActionView;
import com.axelor.meta.schema.actions.ActionView.ActionViewBuilder;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.google.common.base.Joiner;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.persist.Transactional;

public class ExpenseController {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Inject
	private Provider<HRMenuTagService> hrMenuTagServiceProvider;
	@Inject
	private Provider<HRConfigService> hrConfigServiceProvider;
	@Inject
	private Provider<ExpenseService> expenseServiceProvider;
	@Inject
	private Provider<GeneralService> generalServiceProvider;
	@Inject
	private Provider<ExpenseRepository> expenseRepositoryProvider;
	
	public void createAnalyticDistributionWithTemplate(ActionRequest request, ActionResponse response) throws AxelorException{
		ExpenseLine expenseLine = request.getContext().asType(ExpenseLine.class);
		Expense expense = expenseLine.getExpense();
		if(expense == null){
			expense = request.getContext().getParentContext().asType(Expense.class);
			expenseLine.setExpense(expense);
		}
		if(expenseLine.getAnalyticDistributionTemplate() != null){
			expenseLine = expenseServiceProvider.get().createAnalyticDistributionWithTemplate(expenseLine);
			response.setValue("analyticDistributionLineList", expenseLine.getAnalyticDistributionLineList());
		}
		else{
			throw new AxelorException(I18n.get("No template selected"), IException.CONFIGURATION_ERROR);
		}
	}
	
	public void computeAnalyticDistribution(ActionRequest request, ActionResponse response) throws AxelorException{
		ExpenseLine expenseLine = request.getContext().asType(ExpenseLine.class);
		Expense expense = expenseLine.getExpense();
		if(expense == null){
			expense = request.getContext().getParentContext().asType(Expense.class);
			expenseLine.setExpense(expense);
		}
		if(Beans.get(GeneralService.class).getGeneral().getManageAnalyticAccounting()){
			expenseLine = expenseServiceProvider.get().computeAnalyticDistribution(expenseLine);
			response.setValue("analyticDistributionLineList", expenseLine.getAnalyticDistributionLineList());
		}
	}
	
	public void editExpense(ActionRequest request, ActionResponse response)  {
		
		User user = AuthUtils.getUser();
		Company activeCompany = user.getActiveCompany();
		
		List<Expense> expenseList = Beans.get(ExpenseRepository.class).all().filter("self.user = ?1 AND self.company = ?2 AND self.statusSelect = 1 AND self.multipleUsers is false", user, activeCompany).fetch();
		if(expenseList.isEmpty()){
			response.setView(ActionView
									.define(I18n.get("Expense"))
									.model(Expense.class.getName())
									.add("form", "expense-form")
									.map());
		}
		else if(expenseList.size() == 1){
			response.setView(ActionView
					.define(I18n.get("Expense"))
					.model(Expense.class.getName())
					.add("form", "expense-form")
					.param("forceEdit", "true")
					.context("_showRecord", String.valueOf(expenseList.get(0).getId())).map());
		}
		else{
			response.setView(ActionView
					.define(I18n.get("Expense"))
					.model(Wizard.class.getName())
					.add("form", "popup-expense-form")
					.param("forceEdit", "true")
					.param("popup", "true")<commit_msg>#6482 : fix Complete my expense menu<commit_after>/**
 * 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.hr.web.expense;

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

import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axelor.apps.ReportFactory;
import com.axelor.apps.account.db.Move;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Product;
import com.axelor.apps.base.db.Wizard;
import com.axelor.apps.base.service.administration.GeneralService;
import com.axelor.apps.base.service.message.MessageServiceBaseImpl;
import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.Expense;
import com.axelor.apps.hr.db.ExpenseLine;
import com.axelor.apps.hr.db.ExtraHours;
import com.axelor.apps.hr.db.HRConfig;
import com.axelor.apps.hr.db.repo.EmployeeRepository;
import com.axelor.apps.hr.db.repo.ExpenseRepository;
import com.axelor.apps.hr.exception.IExceptionMessage;
import com.axelor.apps.hr.report.IReport;
import com.axelor.apps.hr.service.HRMenuTagService;
import com.axelor.apps.hr.service.KilometricService;
import com.axelor.apps.hr.service.config.HRConfigService;
import com.axelor.apps.hr.service.expense.ExpenseService;
import com.axelor.apps.message.db.Message;
import com.axelor.apps.message.db.repo.MessageRepository;
import com.axelor.auth.AuthUtils;
import com.axelor.auth.db.User;
import com.axelor.db.JPA;
import com.axelor.db.Query;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.IException;
import com.axelor.exception.service.TraceBackService;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.meta.schema.actions.ActionView;
import com.axelor.meta.schema.actions.ActionView.ActionViewBuilder;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.google.common.base.Joiner;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.persist.Transactional;

public class ExpenseController {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Inject
	private Provider<HRMenuTagService> hrMenuTagServiceProvider;
	@Inject
	private Provider<HRConfigService> hrConfigServiceProvider;
	@Inject
	private Provider<ExpenseService> expenseServiceProvider;
	@Inject
	private Provider<GeneralService> generalServiceProvider;
	@Inject
	private Provider<ExpenseRepository> expenseRepositoryProvider;
	
	public void createAnalyticDistributionWithTemplate(ActionRequest request, ActionResponse response) throws AxelorException{
		ExpenseLine expenseLine = request.getContext().asType(ExpenseLine.class);
		Expense expense = expenseLine.getExpense();
		if(expense == null){
			expense = request.getContext().getParentContext().asType(Expense.class);
			expenseLine.setExpense(expense);
		}
		if(expenseLine.getAnalyticDistributionTemplate() != null){
			expenseLine = expenseServiceProvider.get().createAnalyticDistributionWithTemplate(expenseLine);
			response.setValue("analyticDistributionLineList", expenseLine.getAnalyticDistributionLineList());
		}
		else{
			throw new AxelorException(I18n.get("No template selected"), IException.CONFIGURATION_ERROR);
		}
	}
	
	public void computeAnalyticDistribution(ActionRequest request, ActionResponse response) throws AxelorException{
		ExpenseLine expenseLine = request.getContext().asType(ExpenseLine.class);
		Expense expense = expenseLine.getExpense();
		if(expense == null){
			expense = request.getContext().getParentContext().asType(Expense.class);
			expenseLine.setExpense(expense);
		}
		if(Beans.get(GeneralService.class).getGeneral().getManageAnalyticAccounting()){
			expenseLine = expenseServiceProvider.get().computeAnalyticDistribution(expenseLine);
			response.setValue("analyticDistributionLineList", expenseLine.getAnalyticDistributionLineList());
		}
	}
	
	public void editExpense(ActionRequest request, ActionResponse response)  {
		
		User user = AuthUtils.getUser();
		Company activeCompany = user.getActiveCompany();
		
		List<Expense> expenseList = Beans.get(ExpenseRepository.class).all().filter("self.user = ?1 AND self.company = ?2 AND self.statusSelect = 1 AND (self.multipleUsers is false OR self.multipleUsers is null)", user, activeCompany).fetch();
		if(expenseList.isEmpty()){
			response.setView(ActionView
									.define(I18n.get("Expense"))
									.model(Expense.class.getName())
									.add("form", "expense-form")
									.map());
		}
		else if(expenseList.size() == 1){
			response.setView(ActionView
					.define(I18n.get("Expense"))
					.model(Expense.class.getName())
					.add("form", "expense-form")
					.param("forceEdit", "true")
					.context("_showRecord", String.valueOf(expenseList.get(0).getId())).map());
		}
		else{
			response.setView(ActionView
					.define(I18n.get("Expense"))
					.model(Wizard.class.getName())
					.add("form", "popup-expense-form")
					.param("forceEdit", "true")
					.param("popup", "true")
{'fraction_non_alphanumeric': 0.11847336337132255, 'fraction_numerical': 0.004594045410371941, 'mean_word_length': 5.274944567627495, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5221678', 'n_tokens_mistral': 3857, 'n_tokens_neox': 3454, 'n_words': 678}