/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller.admin;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.MoneyDaily;
import com.arrebol.apc.model.admin.constance.MoneyDailyCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.views.MoneyDailyByUserCertifier;
import com.arrebol.apc.model.views.constance.MoneyDailyByUserCertifierViewCfg;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class MoneyDailyController implements Serializable {

    /**
     *
     * Searching all money daily by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @param idUser
     * @return
     */
    public List<MoneyDaily> fillMoneyDailyDatatable(String officeId, Date startDate, Date endDate, String idUser) {
        logger.debug("fillMoneyDailyDatatable");
        List<MoneyDaily> dataComplete = new ArrayList<>();
        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(MoneyDailyCfg.FIELD_OFFICE, new Office(officeId)));
        parameters.add(new ModelParameter(MoneyDailyCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(MoneyDailyCfg.PARAM_END_DATE, endDate));
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(MoneyDailyCfg.FIELD_USER, new User(idUser)));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(MoneyDaily.class, MoneyDailyCfg.QUERY_FIND_ALL_MONEY_DAILY_BY_OFFICE_BETWEEN_DATES_USER, parameters);
        }else{
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(MoneyDaily.class, MoneyDailyCfg.QUERY_FIND_ALL_MONEY_DAILY_BY_OFFICE_BETWEEN_DATES, parameters);
        }

        return dataComplete;
    }

    /**
     *
     * @param money
     * @return boolean
     */
    public boolean saveMoneyDaily(MoneyDaily money) {
        logger.debug("saveMoneyDaily");
        boolean success = genericEntityRepository.insertAPCEntity(money);

        return success;
    }

    /**
     *
     * @param money
     * @return boolean
     */
    public boolean deleteMoneyDaily(MoneyDaily money) {
        logger.debug("deleteMoneyDaily");
        boolean success = genericEntityRepository.deleteAPCEntityById(money);

        return success;
    }

    public List<MoneyDailyByUserCertifier> findAllAmountByUserCertifierView(String officeId) {
        logger.debug("findAllAmountByUserCertifierView");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(MoneyDailyByUserCertifierViewCfg.FIELD_VIEW_OFFICE, officeId));

        return genericEntityRepository.xmlQueryAPCEntities(MoneyDailyByUserCertifier.class, MoneyDailyByUserCertifierViewCfg.QUERY_FIND_ALL_MONEY_DAILY_BY_CERTIFIER_BY_OFFICE, parameters);
    }

    /**
     *
     * Searching money daily by id.
     *
     * @param moneyDailyId
     * @return
     */
    public MoneyDaily getMoneyDailyById(String moneyDailyId) {
        logger.debug("getMoneyDailyById");

        return (MoneyDaily) genericEntityRepository.selectAPCEntityById(MoneyDaily.class, moneyDailyId);
    }

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

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

}
