/*
 * 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.Advance;
import com.arrebol.apc.model.admin.constance.AdvanceCfg;
import com.arrebol.apc.model.admin.constance.BonusCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
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;

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

    /**
     *
     * Searching all advances by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Advance> fillAdvanceDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillAdvanceDatatable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(AdvanceCfg.FIELD_OFFICE, new Office(officeId)));
        //parameters.add(new ModelParameter(AdvanceCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(BonusCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(BonusCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(Advance.class, AdvanceCfg.QUERY_FIND_ALL_ADVANCE_BY_OFFICE_BETWEEN_DATES, parameters);
    }

    /**
     *
     * @param advance
     * @return boolean
     */
    public boolean saveAdvance(Advance advance) {
        logger.debug("saveAdvance");
        boolean success = genericEntityRepository.insertAPCEntity(advance);

        return success;

    }

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

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

        parameters.add(new ModelParameter(AdvanceCfg.FIELD_ACTIVE_STATUS, status));
        parameters.add(new ModelParameter(AdvanceCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(AdvanceCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(AdvanceCfg.FIELD_ID, advanceIdToUpdate));

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(AdvanceCfg.QUERY_UPDATE_ADVANCE_BY_STATUS, parameters);
    }

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

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

}
