/*
 * 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.LoanEmployee;
import com.arrebol.apc.model.admin.LoanEmployeeDetails;
import com.arrebol.apc.model.admin.constance.ExpenseCompanyCfg;
import com.arrebol.apc.model.admin.constance.GoalCfg;
import com.arrebol.apc.model.views.LoanEmployeeView;
import com.arrebol.apc.model.core.HumanResource;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.views.LoanEmployeeAllDataView;
import com.arrebol.apc.model.views.LoanEmployeeDetailAllDataView;
import com.arrebol.apc.model.views.StatsEmployeeSavingView;
import com.arrebol.apc.model.views.constance.LoanEmployeeDetailAllDataViewCfg;
import com.arrebol.apc.model.views.constance.LoanEmployeeViewCfg;
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.LoanEmployeeAllDataCfg;
import com.arrebol.apc.model.views.constance.StatsEmployeeSavingViewCfg;
import java.math.BigDecimal;

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

    public List<LoanEmployeeView> fillLoanEmployeeDataTable(Date startDate, Date endDate) {
        logger.debug("fillLoanEmployeeDataTable");
        List<ModelParameter> parameters = new ArrayList<>();

//        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
//        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<LoanEmployeeView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(LoanEmployeeView.class, LoanEmployeeViewCfg.QUERY_FIND_ALL_LOAN_EMPLOYEE_HIST, parameters);
        return dataComplete;
    }

    public BigDecimal fillTotalAmountLoan(Date startDate, Date endDate) {
        logger.debug("fillTotalAmountLoan");
        List<ModelParameter> parameters = new ArrayList<>();

//        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
//        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<LoanEmployeeView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(LoanEmployeeView.class, LoanEmployeeViewCfg.QUERY_FIND_ALL_LOAN_EMPLOYEE_HIST, parameters);
        BigDecimal totalAmountLoan = BigDecimal.ZERO;
        for (LoanEmployeeView employee : dataComplete) {
            totalAmountLoan = totalAmountLoan.add(employee.getAmountLoan());
        }
        return totalAmountLoan;
    }

    public List<LoanEmployeeDetailAllDataView> getLoanEmployeeDetailByIdLoan(String idLoanEmployee) {

        logger.debug("getLoanEmployeeDetailByIdLoan");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(LoanEmployeeDetailAllDataViewCfg.FIELD_ID_LOAN, idLoanEmployee));
        List<LoanEmployeeDetailAllDataView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(LoanEmployeeDetailAllDataView.class,
                LoanEmployeeDetailAllDataViewCfg.QUERY_FIND_ALL_LOAN_EMPLOYEE_DETAIL_BY_ID_LOAN, parameters);

        return dataComplete;
    }

    public LoanEmployee getLoanEmployeeById(String idLoanEmployee) {

        logger.debug("getLoanEmployeeById");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(LoanEmployeeAllDataCfg.FIELD_ID_LOAN, idLoanEmployee));
        List<LoanEmployee> dataComplete = genericEntityRepository.xmlQueryAPCEntities(LoanEmployee.class,
                LoanEmployeeAllDataCfg.QUERY_FIND_ALL_LOAN_EMPLOYEE_BY_ID, parameters);

        return dataComplete.get(0);
    }

    public boolean saveLoanEmployee(LoanEmployee loan, HumanResource hr) {
        logger.debug("saveLoanEmployee");
        boolean success = genericEntityRepository.insertAPCEntity(loan);

        if (hr != null) {
            genericEntityRepository.updateAPCEntity(hr);
        }
        return success;
    }

    public boolean saveLoanEmployeeDetail(LoanEmployeeDetails loanDetail, HumanResource hr, LoanEmployee loanEmployee) {
        logger.debug("saveLoanEmployeeDetail");
        boolean success = genericEntityRepository.insertAPCEntity(loanDetail);

        if (hr != null) {
            genericEntityRepository.updateAPCEntity(hr);
        }

        if (loanEmployee != null) {
            genericEntityRepository.updateAPCEntity(loanEmployee);
        }

        return success;
    }
    
    /**
     *
     * @param status
     * @param expenseCompanyIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateLoanEmployeeByStatus(ActiveStatus status, String expenseCompanyIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateLoanEmployeeByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(LoanEmployeeAllDataCfg.FIELD_ACTIVE_STATUS, status));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(LoanEmployeeAllDataCfg.FIELD_ID, expenseCompanyIdToUpdate));

        
        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(LoanEmployeeAllDataCfg.QUERY_UPDATE_LOAN_EMPLOYEE_BY_STATUS, parameters);
    }
    
        /**
     *
     * @param status
     * @param expenseCompanyIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateLoanEmployeeDetailByStatus(String idUser, Date createdOn) {
        logger.debug("updateLoanEmployeeDetailByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

       
        parameters.add(new ModelParameter(LoanEmployeeDetailAllDataViewCfg.FIELD_ID_USER, idUser ));
        parameters.add(new ModelParameter(LoanEmployeeDetailAllDataViewCfg.FIELD_CREATED_ON, createdOn));

        
        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(LoanEmployeeDetailAllDataViewCfg.QUERY_UPDATE_LOAN_EMPLOYEE_DETAIL_BY_STATUS, parameters);
    }
    
    
    public List<LoanEmployeeDetails> findLoanDetailToUpdate(String idUser, Date createdOn) {
        logger.debug("updateLoanEmployeeDetailByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

       
        parameters.add(new ModelParameter(LoanEmployeeDetailAllDataViewCfg.FIELD_ID_USER, idUser ));
        parameters.add(new ModelParameter(LoanEmployeeDetailAllDataViewCfg.FIELD_CREATED_ON, createdOn));

        List<LoanEmployeeDetails> dataComplete = genericEntityRepository.xmlQueryAPCEntities(LoanEmployeeDetails.class, LoanEmployeeDetailAllDataViewCfg.QUERY_FIND_LOAN_DETAL_TO_UPDATE, parameters);
        return dataComplete;
    }

    
 public boolean updateLoanEmployee(LoanEmployee loanEmployee) {
        logger.debug("savePayroll");
        boolean success = genericEntityRepository.updateAPCEntity(loanEmployee);

        return success;
    }
    
    final Logger logger = LogManager.getLogger(LoanEmployeeController.class);
    private final GenericEntityRepository genericEntityRepository;

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