/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.apc.controller.admin;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.constance.GoalCfg;
import com.arrebol.apc.model.views.StatsPayrollView;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.arrebol.apc.model.views.constance.StatsPayrollViewCfg;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author David Rodriguez
 */
public class StatsPayrollController implements Serializable {

    public List<StatsPayrollView> fillPayrollDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillPayrollDataTable");
        List<ModelParameter> parameters = new ArrayList<>();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        startDate = calendar.getTime();

        Calendar calendarF = Calendar.getInstance();
        calendarF.setTime(endDate);
        calendarF.add(Calendar.DAY_OF_YEAR, 1);
        endDate = calendarF.getTime();
        
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<StatsPayrollView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsPayrollViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPayrollView.class, StatsPayrollViewCfg.QUERY_FIND_ALL_PAYROLL_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPayrollView.class, StatsPayrollViewCfg.QUERY_FIND_ALL_PAYROLL_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsPayrollView> mapDataPayroll = new HashMap<>();
        for (StatsPayrollView payroll : dataComplete) {
            if (mapDataPayroll.get(payroll.getIdUser()) != null) {
                mapDataPayroll.get(payroll.getIdUser()).setTotalPayroll(
                        mapDataPayroll.get(payroll.getIdUser()).getTotalPayroll().add(payroll.getTotalPayroll()));
            } else {
                mapDataPayroll.put(payroll.getIdUser(), payroll);
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataPayroll.values());
        return dataComplete;
    }

    public BigDecimal fillTotalPayroll(Date startDate, Date endDate) {
        logger.debug("fillTotalPayroll");
        List<ModelParameter> parameters = new ArrayList<>();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        startDate = calendar.getTime();

        Calendar calendarF = Calendar.getInstance();
        calendarF.setTime(endDate);
        calendarF.add(Calendar.DAY_OF_YEAR, 1);
        endDate = calendarF.getTime();
        
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<StatsPayrollView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPayrollView.class, StatsPayrollViewCfg.QUERY_FIND_ALL_PAYROLL_BETWEEN_DATES, parameters);
        BigDecimal totalPayroll = new BigDecimal("0");
        for (StatsPayrollView payroll : dataComplete) {
            totalPayroll = totalPayroll.add(payroll.getTotalPayroll());
        }
        return totalPayroll;
    }

    final Logger logger = LogManager.getLogger(StatsPayrollController.class);
    private final GenericEntityRepository genericEntityRepository;

    public StatsPayrollController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }
}
