/*
 * 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.ExpenseCompany;
import com.arrebol.apc.model.admin.constance.ExpenseCompanyCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.ExpenseCompanyType;
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 ExpenseCompanyController implements Serializable {

    /**
     *
     * Searching all expense company in by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<ExpenseCompany> fillxpenseCompanyInDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillxpenseCompanyDatatable");
        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(ExpenseCompanyCfg.FIELD_OFFICE, new Office(officeId)));
        //parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_EXPENSE_COMPANY_TYPE, ExpenseCompanyType.PAYMENT_IN));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(ExpenseCompany.class, ExpenseCompanyCfg.QUERY_FIND_ALL_EXPENSE_COMPANY_BY_OFFICE_BETWEEN_DATES, parameters);
    }

    /**
     *
     * Searching all expense company out by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<ExpenseCompany> fillxpenseCompanyOutDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillxpenseCompanyOutDatatable");
        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(ExpenseCompanyCfg.FIELD_OFFICE, new Office(officeId)));
        //parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.FIELD_EXPENSE_COMPANY_TYPE, ExpenseCompanyType.PAYMENT_OUT));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(ExpenseCompanyCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(ExpenseCompany.class, ExpenseCompanyCfg.QUERY_FIND_ALL_EXPENSE_COMPANY_BY_OFFICE_BETWEEN_DATES, parameters);
    }

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

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

        parameters.add(new ModelParameter(ExpenseCompanyCfg.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(ExpenseCompanyCfg.FIELD_ID, expenseCompanyIdToUpdate));

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(ExpenseCompanyCfg.QUERY_UPDATE_EXPENSE_COMPANY_BY_STATUS, parameters);
    }

    /**
     *
     * @param expenseCompany
     * @return boolean
     */
    public boolean saveExpenseCompany(ExpenseCompany expenseCompany) {
        logger.debug("saveExpenseCompany");
        boolean success = genericEntityRepository.insertAPCEntity(expenseCompany);

        return success;
    }

    /**
     *
     * Searching expense by id.
     *
     * @param expenseId
     * @return
     */
    public ExpenseCompany getExpenseById(String expenseId) {
        logger.debug("getExpenseById");

        return (ExpenseCompany) genericEntityRepository.selectAPCEntityById(ExpenseCompany.class, expenseId);
    }

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

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

}
