/*
 * 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.OtherExpense;
import com.arrebol.apc.model.admin.constance.OtherExpenseCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.core.User;
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 OtherExpenseController implements Serializable {

    /**
     *
     * Searching all other expenses by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @param idUser
     * @return
     */
    public List<OtherExpense> fillOtherExpenseDatatable(String officeId, Date startDate, Date endDate, String idUser) {
        logger.debug("fillOtherExpenseDatatable");
        List<OtherExpense> 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(OtherExpenseCfg.FIELD_OFFICE, new Office(officeId)));
        parameters.add(new ModelParameter(OtherExpenseCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(OtherExpenseCfg.PARAM_END_DATE, endDate));
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(OtherExpenseCfg.FIELD_USER, new User(idUser)));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(OtherExpense.class, OtherExpenseCfg.QUERY_FIND_ALL_OTHER_EXPENSE_BY_OFFICE_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(OtherExpense.class, OtherExpenseCfg.QUERY_FIND_ALL_OTHER_EXPENSE_BY_OFFICE_BETWEEN_DATES, parameters);
        }

        return dataComplete;
    }

    /**
     *
     * @param otherExpense
     * @return boolean
     */
    public boolean saveOtherExpense(OtherExpense otherExpense) {
        logger.debug("saveOtherExpense");
        boolean success = genericEntityRepository.insertAPCEntity(otherExpense);

        return success;
    }

    /**
     *
     * @param otherExpense
     * @return boolean
     */
    public boolean deleteOtherExpense(OtherExpense otherExpense) {
        logger.debug("deleteOtherExpense");
        boolean success = genericEntityRepository.deleteAPCEntityById(otherExpense);

        return success;
    }

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

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

}
