/*
 * 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.StatsPaymentRenovationView;
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.StatsPaymentRenovationViewCfg;
import java.math.BigDecimal;
import java.util.Calendar;

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

    public List<StatsPaymentRenovationView> fillPaymentDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillPaymentDataTable");
        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<StatsPaymentRenovationView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsPaymentRenovationViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRenovationView.class, StatsPaymentRenovationViewCfg.QUERY_FIND_ALL_PAYMENT_RENOVATION_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRenovationView.class, StatsPaymentRenovationViewCfg.QUERY_FIND_ALL_PAYMENT_RENOVATION_BETWEEN_DATES, parameters);
        }

        return dataComplete;
    }

    public BigDecimal fillTotalPayment(Date startDate, Date endDate) {
        logger.debug("fillTotalPayment");
        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<StatsPaymentRenovationView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRenovationView.class, StatsPaymentRenovationViewCfg.QUERY_FIND_ALL_PAYMENT_RENOVATION_BETWEEN_DATES, parameters);
        BigDecimal totalPayment = new BigDecimal("0");
        for (StatsPaymentRenovationView payment : dataComplete) {
            totalPayment = totalPayment.add(payment.getTotalPayment());
        }
        return totalPayment;
    }

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

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