/*
 * 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.drive;

import com.arrebol.apc.controller.util.ConnectionManager;
import com.arrebol.apc.controller.util.HibernateUtil;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.views.CobranzaLastWeekByUserView;
import com.arrebol.apc.model.views.CobranzaWeekByUserView;
import com.arrebol.apc.model.views.ColocationLastWeekByUserView;
import com.arrebol.apc.model.views.ColocationWeekByUserView;
import com.arrebol.apc.model.views.InformationLoanLastWeekView;
import com.arrebol.apc.model.views.InformationLoanWeekView;
import com.arrebol.apc.model.views.ResumeNewCustomerLastWeekView;
import com.arrebol.apc.model.views.ResumeNewCustomerWeekView;
import com.arrebol.apc.model.views.ResumenInOutLastWeekByUserView;
import com.arrebol.apc.model.views.ResumenInOutWeekByUserView;
import com.arrebol.apc.model.views.ResumenTotalLastWeekView;
import com.arrebol.apc.model.views.ResumenTotalWeekView;
import com.arrebol.apc.model.views.SubtotalLastWeekByUserView;
import com.arrebol.apc.model.views.SubtotalWeekByUserView;
import com.arrebol.apc.model.views.constance.InformationLoanWeekViewCfg;
import com.arrebol.apc.model.views.constance.ResumenNewCustomerLastWeekViewCfg;
import com.arrebol.apc.model.views.constance.ResumenNewCustomerWeekViewCfg;
import com.arrebol.apc.model.views.constance.ResumenTotalLastWeekViewCfg;
import com.arrebol.apc.model.views.constance.ResumenTotalWeekViewCfg;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class DriverController extends ConnectionManager implements Serializable{
    
    /**
     *
     * Searching all loans with activity at this week by office.
     *
     * @param officeId
     * @return
     */
    public List<InformationLoanWeekView> getAllLoanThisWeekByOffice(String officeId) {
        logger.debug("getAllLoanThisWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

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

        return genericEntityRepository.xmlQueryAPCEntities(InformationLoanWeekView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_LOANS_THIS_WEEK_BY_OFFICE, parameters);
    }
    
    public List<InformationLoanLastWeekView> getAllLoanLastWeekByOffice(String officeId) {
        logger.debug("getAllLoanLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

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

        return genericEntityRepository.xmlQueryAPCEntities(InformationLoanLastWeekView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_LOANS_LAST_WEEK_BY_OFFICE, parameters);
    }
    
    public List<ColocationWeekByUserView> getAllColocationWeekByOffice(String officeId) {
        logger.debug("getAllColocationWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(ColocationWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_COLOCATION_WEEK_BY_OFFICE, parameters);
    }
    
    public List<ColocationLastWeekByUserView> getAllColocationLastWeekByOffice(String officeId) {
        logger.debug("getAllColocationLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(ColocationLastWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_COLOCATION_LAST_WEEK_BY_OFFICE, parameters);
    }
    
    public List<SubtotalLastWeekByUserView> getAllSubtotalLastWeekByOffice(String officeId) {
        logger.debug("getAllSubtotalLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(SubtotalLastWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_SUBTOTAL_LAST_WEEK_BY_OFFICE, parameters);
    }
    
    public List<SubtotalWeekByUserView> getAllSubtotalThisWeekByOffice(String officeId) {
        logger.debug("getAllSubtotalThisWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(SubtotalWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_SUBTOTAL_WEEK_BY_OFFICE, parameters);
    }
    
    public List<ResumenInOutWeekByUserView> getAllResumenInOutThisWeekByOffice(String officeId) {
        logger.debug("getAllResumenInOutThisWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(ResumenInOutWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_RESUMEN_IN_OUT_WEEK_BY_OFFICE, parameters);
    }
    
    public List<ResumenInOutLastWeekByUserView> getAllResumenInOutLastWeekByOffice(String officeId) {
        logger.debug("getAllResumenInOutLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(ResumenInOutLastWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_RESUMEN_IN_OUT_LAST_WEEK_BY_OFFICE, parameters);
    }
    
    public List<CobranzaLastWeekByUserView> getAllCobranzaLastWeekByOffice(String officeId) {
        logger.debug("getAllCobranzaLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(CobranzaLastWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_COBRANZA_LAST_WEEK_BY_OFFICE, parameters);
    }
    
    public List<CobranzaWeekByUserView> getAllCobranzaThisWeekByOffice(String officeId) {
        logger.debug("getAllCobranzaThisWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(InformationLoanWeekViewCfg.FIELD_VIEW_OFFICE_COLOCATION, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(CobranzaWeekByUserView.class, InformationLoanWeekViewCfg.QUERY_FIND_ALL_COBRANZA_WEEK_BY_OFFICE, parameters);
    }
    
    public List<ResumenTotalLastWeekView> getAllResumenTotalLastWeekByOffice(String officeId) {
        logger.debug("getAllResumenTotalLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

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

        return genericEntityRepository.xmlQueryAPCEntities(ResumenTotalLastWeekView.class, ResumenTotalLastWeekViewCfg.QUERY_FIND_ALL_RESUMEN_TOTAL, parameters);
    }
    
    public List<ResumeNewCustomerLastWeekView> getAllResumenNewCustomerLastWeekByOffice() {
        logger.debug("getAllResumenNewCustomerLastWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        return genericEntityRepository.xmlQueryAPCEntities(ResumeNewCustomerLastWeekView.class, ResumenNewCustomerLastWeekViewCfg.QUERY_FIND_ALL_RESUMEN_NEW_CUSTOMER, parameters);
    }
    
    public List<ResumeNewCustomerWeekView> getAllResumenNewCustomerWeekByOffice() {
        logger.debug("getAllResumenNewCustomerWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        return genericEntityRepository.xmlQueryAPCEntities(ResumeNewCustomerWeekView.class, ResumenNewCustomerWeekViewCfg.QUERY_FIND_ALL_RESUMEN_NEW_CUSTOMER, parameters);
    }
    
    public List<ResumenTotalWeekView> getAllResumenTotalWeekByOffice(String officeId) {
        logger.debug("getAllResumenTotalWeekByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

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

        return genericEntityRepository.xmlQueryAPCEntities(ResumenTotalWeekView.class, ResumenTotalWeekViewCfg.QUERY_FIND_ALL_RESUMEN_TOTAL, parameters);
    }
    
         /**
     *
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<InformationLoanWeekView> getAllLoanWeekByOfficeMySQLQuery(String startDate, String idOffice) {
        logger.debug("getAllLoanWeekByOfficeMySQLQuery");
        logger.debug("Fecha inicial: " + startDate);

        List<InformationLoanWeekView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

            List<Object> rows = session.createNativeQuery(queryLoanWeek)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                InformationLoanWeekView loanWeek = new InformationLoanWeekView( );
                loanWeek.setId(results[0] == null ? "" : results[0].toString());
                loanWeek.setIdUser(results[1] == null ? "" : results[1].toString());
                loanWeek.setIdOffice(results[2] == null ? "" : results[2].toString());
                try {
                    loanWeek.setFecha(DATE_FORMAT.parse(results[3].toString()));
                } catch (ParseException ex) {
                    logger.error("driver week", ex);
                }
                loanWeek.setApoyos(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                loanWeek.setApoyosTotal(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                loanWeek.setComisionApertura(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                loanWeek.setAval(results[7] == null ? "" : results[7].toString());
                loanWeek.setCustomer(results[8] == null ? "" : results[8].toString());
                loanWeek.setDocumentoPor(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                loanWeek.setAbonoDiario(new BigDecimal(results[10] == null ? "0.0" : results[10].toString()));
                loanWeek.setAmountPaid(new BigDecimal(results[11] == null ? "0.0" : results[11].toString()));
                loanWeek.setSaldoInsoluto(new BigDecimal(results[12] == null ? "0.0" : results[12].toString()));
                loanWeek.setRouteName(results[13] == null ? "" : results[13].toString());
                loanWeek.setAsesor(results[14] == null ? "" : results[14].toString());
                loanWeek.setNumFee(Integer.parseInt(results[15] == null ? "0" : results[15].toString()));
                loanWeek.setPaymentMonday(new BigDecimal(results[16] == null ? "0.0" : results[16].toString()));
                loanWeek.setFeeMonday(new BigDecimal(results[17] == null ? "0.0" : results[17].toString()));
                loanWeek.setPaymentTuesday(new BigDecimal(results[18] == null ? "0.0" : results[18].toString()));
                loanWeek.setFeeTuesday(new BigDecimal(results[19] == null ? "0.0" : results[19].toString()));
                loanWeek.setPaymentWednesday(new BigDecimal(results[20] == null ? "0.0" : results[20].toString()));
                loanWeek.setFeeWednesday(new BigDecimal(results[21] == null ? "0.0" : results[21].toString()));
                loanWeek.setPaymentThursday(new BigDecimal(results[22] == null ? "0.0" : results[22].toString()));
                loanWeek.setFeeThursday(new BigDecimal(results[23] == null ? "0.0" : results[23].toString()));
                loanWeek.setPaymentFriday(new BigDecimal(results[24] == null ? "0.0" : results[24].toString()));
                loanWeek.setFeeFriday(new BigDecimal(results[25] == null ? "0.0" : results[25].toString()));
                loanWeek.setPaymentSaturday(new BigDecimal(results[26] == null ? "0.0" : results[26].toString()));
                loanWeek.setFeeSaturday(new BigDecimal(results[27] == null ? "0.0" : results[27].toString()));
                loanWeek.setFaltante(new BigDecimal(results[28] == null ? "0.0" : results[28].toString()));
                loanWeek.setNewCustomer(results[29] == null ? "" : results[29].toString());
                loanWeek.setRenovation(results[30] == null ? "" : results[30].toString());
                loanWeek.setEstatusPrestamo(results[31] == null ? "" : results[31].toString());
                loanWeek.setNumPagosAll(new BigDecimal(results[32] == null ? "0.0" : results[32].toString()));
                loanWeek.setNumPagosWeek(new BigDecimal(results[33] == null ? "0.0" : results[33].toString()));
                loanWeek.setFeeTodos(new BigDecimal(results[34] == null ? "0.0" : results[34].toString()));
                loanWeek.setLoanTypeName(results[35] == null ? "" : results[35].toString());
                if (results[36] == null) {
                    loanWeek.setFrozen(ActiveStatus.DISABLED);
                } else {
                    if (results[36].equals(ActiveStatus.ENEBLED.getValue())) {
                        loanWeek.setFrozen(ActiveStatus.ENEBLED);
                    } else {
                        loanWeek.setFrozen(ActiveStatus.DISABLED);
                    }
                }
                loanWeek.setTotalPaid(new BigDecimal(results[37].toString()));
                loanWeek.setNumPagosLoanType(new BigDecimal(results[38] == null ? "0.0" : results[38].toString()));
                loanWeek.setMovimientosMonday(results[39] == null ? "" : results[39].toString());
                loanWeek.setMovimientosTuesday(results[40] == null ? "" : results[40].toString());
                loanWeek.setMovimientosWednesday(results[41] == null ? "" : results[41].toString());
                loanWeek.setMovimientosThursday(results[42] == null ? "" : results[42].toString());
                loanWeek.setMovimientosFriday(results[43] == null ? "" : results[43].toString());
                loanWeek.setMovimientosSaturday(results[44] == null ? "" : results[44].toString());
                loanWeek.setRenovationMonday(results[45] == null ? "" : results[45].toString());
                loanWeek.setRenovationTuesday(results[46] == null ? "" : results[46].toString());
                loanWeek.setRenovationWednesday(results[47] == null ? "" : results[47].toString());
                loanWeek.setRenovationThursday(results[48] == null ? "" : results[48].toString());
                loanWeek.setRenovationFriday(results[49] == null ? "" : results[49].toString());
                loanWeek.setRenovationSaturday(results[50] == null ? "" : results[50].toString());
                loanWeek.setReactivation(results[51] == null ? "" : results[51].toString());
                loanWeek.setWeekOfCreation(results[52] == null ? "" : results[52].toString());
                dataList.add(loanWeek);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllLoanWeekByOfficeMySQLQuery(" + startDate + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList;
    }
    
    /**
     *
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<ColocationWeekByUserView> getAllColocationWeekByOfficeMySQLQuery(String startDate, String idOffice){
        logger.debug("getAllColocationWeekByOfficeMySQLQuery");

        List<ColocationWeekByUserView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            List<Object> rows = session.createNativeQuery(queryColocation)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                ColocationWeekByUserView colocation = new ColocationWeekByUserView( );
                colocation.setId(results[0] == null ? "" : results[0].toString());
                colocation.setUserName(results[1] == null ? "" : results[1].toString());
                colocation.setOffice(new Office(idOffice));
                colocation.setColocationMonday(new BigDecimal(results[3] == null ? "0.0" : results[3].toString()));
                colocation.setColocationTuesday(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                colocation.setColocationWednesday(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                colocation.setColocationThursday(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                colocation.setColocationFriday(new BigDecimal(results[7] == null ? "0.0" : results[7].toString()));
                colocation.setColocationSaturday(new BigDecimal(results[8] == null ? "0.0" : results[8].toString()));
                colocation.setColocationTotal(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                
                dataList.add(colocation);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllColocationWeekByOfficeMySQLQuery(" + idOffice + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList;
    }
    
     /**
     *
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<SubtotalWeekByUserView> getAllSubtotalThisWeekByOfficeMySQLQuery(String startDate, String idOffice) {
        logger.debug("getAllSubtotalThisWeekByOfficeMySQLQuery");
        
        List<SubtotalWeekByUserView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            List<Object> rows = session.createNativeQuery(querySubtotal)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                SubtotalWeekByUserView subtotal = new SubtotalWeekByUserView( );
                subtotal.setId(results[0] == null ? "" : results[0].toString());
                subtotal.setUserName(results[1] == null ? "" : results[1].toString());
                subtotal.setOffice(new Office(idOffice));
                subtotal.setSubtotalMonday(new BigDecimal(results[3] == null ? "0.0" : results[3].toString()));
                subtotal.setOpeningFeeMonday(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                subtotal.setSubtotalTuesday(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                subtotal.setOpeningFeeTuesday(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                subtotal.setSubtotalWednesday(new BigDecimal(results[7] == null ? "0.0" : results[7].toString()));
                subtotal.setOpeningFeeWednesday(new BigDecimal(results[8] == null ? "0.0" : results[8].toString()));
                subtotal.setSubtotalThursday(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                subtotal.setOpeningFeeThursday(new BigDecimal(results[10] == null ? "0.0" : results[10].toString()));
                subtotal.setSubtotalFriday(new BigDecimal(results[11] == null ? "0.0" : results[11].toString()));
                subtotal.setOpeningFeeFriday(new BigDecimal(results[12] == null ? "0.0" : results[12].toString()));
                subtotal.setSubtotalSaturday(new BigDecimal(results[13] == null ? "0.0" : results[13].toString()));
                subtotal.setOpeningFeeSaturday(new BigDecimal(results[14] == null ? "0.0" : results[14].toString()));
                subtotal.setSubtotalTotal(new BigDecimal(results[15] == null ? "0.0" : results[15].toString()));
                
                dataList.add(subtotal);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllSubtotalThisWeekByOfficeMySQLQuery(" + idOffice + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList; 
    }

     /**
     *
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<ResumenInOutWeekByUserView> getAllResumenInOutThisWeekByOfficeMySQLQuery(String startDate, String idOffice) {
        logger.debug("getAllResumenInOutThisWeekByOffice");
        
        List<ResumenInOutWeekByUserView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            List<Object> rows = session.createNativeQuery(queryResumenInOut)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                ResumenInOutWeekByUserView resumen = new ResumenInOutWeekByUserView( );
                resumen.setId(results[0] == null ? "" : results[0].toString());
                resumen.setUserName(results[1] == null ? "" : results[1].toString());
                resumen.setOffice(new Office(idOffice));
                resumen.setClosingMonday(new BigDecimal(results[3] == null ? "0.0" : results[3].toString()));
                resumen.setExpenseMonday(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                resumen.setMoneyDailyTodayMonday(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                resumen.setClosingTuesday(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                resumen.setExpenseTuesday(new BigDecimal(results[7] == null ? "0.0" : results[7].toString()));
                resumen.setMoneyDailyTodayTuesday(new BigDecimal(results[8] == null ? "0.0" : results[8].toString()));
                resumen.setClosingWednesday(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                resumen.setExpenseWednesday(new BigDecimal(results[10] == null ? "0.0" : results[10].toString()));
                resumen.setMoneyDailyTodayWednesday(new BigDecimal(results[11] == null ? "0.0" : results[11].toString()));
                resumen.setClosingThursday(new BigDecimal(results[12] == null ? "0.0" : results[12].toString()));
                resumen.setExpenseThursday(new BigDecimal(results[13] == null ? "0.0" : results[13].toString()));
                resumen.setMoneyDailyTodayThursday(new BigDecimal(results[14] == null ? "0.0" : results[14].toString()));
                resumen.setClosingFriday(new BigDecimal(results[15] == null ? "0.0" : results[15].toString()));
                resumen.setExpenseFriday(new BigDecimal(results[16] == null ? "0.0" : results[16].toString()));
                resumen.setMoneyDailyTodayFriday(new BigDecimal(results[17] == null ? "0.0" : results[17].toString()));
                resumen.setClosingSaturday(new BigDecimal(results[18] == null ? "0.0" : results[18].toString()));
                resumen.setExpenseSaturday(new BigDecimal(results[19] == null ? "0.0" : results[19].toString()));
                resumen.setMoneyDailyTodaySaturday(new BigDecimal(results[20] == null ? "0.0" : results[20].toString()));
                
                dataList.add(resumen);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllResumenInOutThisWeekByOffice(" + idOffice + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList; 
    }
    
    /**
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<CobranzaWeekByUserView> getAllCobranzaThisWeekByOfficeMySQLQuery(String startDate, String idOffice) {
        logger.debug("getAllCobranzaThisWeekByOfficeMySQLQuery");

        List<CobranzaWeekByUserView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            List<Object> rows = session.createNativeQuery(queryCobranza)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                CobranzaWeekByUserView cobranza = new CobranzaWeekByUserView( );
                cobranza.setId(results[0] == null ? "" : results[0].toString());
                cobranza.setUserName(results[1] == null ? "" : results[1].toString());
                cobranza.setOffice(new Office(idOffice));
                cobranza.setCobranzaMonday(new BigDecimal(results[3] == null ? "0.0" : results[3].toString()));
                cobranza.setCobranzaTuesday(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                cobranza.setCobranzaWednesday(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                cobranza.setCobranzaThursday(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                cobranza.setCobranzaFriday(new BigDecimal(results[7] == null ? "0.0" : results[7].toString()));
                cobranza.setCobranzaSaturday(new BigDecimal(results[8] == null ? "0.0" : results[8].toString()));
                cobranza.setCobranzaTotal(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                
                dataList.add(cobranza);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllCobranzaThisWeekByOfficeMySQLQuery(" + idOffice + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList;    
    }
     /**
     * Searching all loans with activity at this week by office.
     * @param startDate
     * @param officeId
     * @return
     */
    public List<ResumenTotalWeekView> getAllResumenTotalWeekByOfficeMySQLQuery(String startDate, String idOffice) {
        logger.debug("getAllResumenTotalWeekByOfficeMySQLQuery");

        List<ResumenTotalWeekView> dataList = new ArrayList<>();
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            List<Object> rows = session.createNativeQuery(queryResumenTotal)
                    .setParameter("startDate", startDate)
                    .setParameter("idOffice", idOffice)
                    .getResultList();
            
            transaction.commit();
            
            rows.forEach((row) -> {
                Object[] results = (Object[]) row;

                // Create Data Row
                ResumenTotalWeekView resumenTotal = new ResumenTotalWeekView( );
                resumenTotal.setId(results[0] == null ? "" : results[0].toString());
                resumenTotal.setOfficeName(results[1] == null ? "" : results[1].toString());
                resumenTotal.setClosingDayTotal(new BigDecimal(results[2] == null ? "0.0" : results[3].toString()));
                resumenTotal.setMoneyDailyTodayTotal(new BigDecimal(results[3] == null ? "0.0" : results[3].toString()));
                resumenTotal.setSubtotalTotal(new BigDecimal(results[4] == null ? "0.0" : results[4].toString()));
                resumenTotal.setOpeningFeeTotal(new BigDecimal(results[5] == null ? "0.0" : results[5].toString()));
                resumenTotal.setCobranzaToday(new BigDecimal(results[6] == null ? "0.0" : results[6].toString()));
                resumenTotal.setColocationTotal(new BigDecimal(results[7] == null ? "0.0" : results[7].toString()));
                resumenTotal.setNominaTotal(new BigDecimal(results[8] == null ? "0.0" : results[8].toString()));
                resumenTotal.setAdelantosTotal(new BigDecimal(results[9] == null ? "0.0" : results[9].toString()));
                resumenTotal.setEntradasTotal(new BigDecimal(results[10] == null ? "0.0" : results[10].toString()));
                resumenTotal.setGastosAdmonTotal(new BigDecimal(results[11] == null ? "0.0" : results[11].toString()));
                
                dataList.add(resumenTotal);
            });
            
        } catch (HibernateException e) {
            logger.error("Driver", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getAllResumenTotalWeekByOfficeMySQLQuery(" + idOffice + " " + ") ", e);
            rollback(transaction);
        }
        
        return dataList;    
    }
    
    final Logger logger = LogManager.getLogger(DriverController.class);
    private final GenericEntityRepository genericEntityRepository;

    public DriverController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }
    
    private final String queryLoanWeek =
"SELECT " +
"	l.id, " +
"	u.id as id_user, " +
"	r.id_office, " +
"	l.created_on as fecha, " +
"	lt.payment as apoyos, " +
"	lt.payment_total as apoyos_total, " +
"	lt.opening_fee as comision_apertura, " +
"	CONCAT(endor.first_name,' ', IF(ISNULL(endor.second_name) ,'', CONCAT(endor.second_name,' ')) ,endor.last_name, ' ', endor.middle_name) AS aval, " +
"	CONCAT(cus.first_name,' ', IF(ISNULL(cus.second_name) ,'', CONCAT(cus.second_name,' ')) ,cus.last_name, ' ', cus.middle_name) AS customer, " +
"	l.amount_to_pay as documento_por, " +
"	lt.payment_daily as abono_diario, " +
"	l.amount_paid, " +
"	(l.amount_to_pay - l.amount_paid) saldo_insoluto, " +
"	r.route_name, " +
"	CONCAT(hr.first_name,' ', IF(ISNULL(hr.second_name) ,'', CONCAT(hr.second_name,' ')) ,hr.last_name, ' ', hr.middle_name) AS asesor, " +
"	(SELECT COUNT(lfn.id) FROM APC_LOAN_FEE_NOTIFICATION lfn WHERE lfn.id_loan = l.id) as num_fee, " +
//"-- Lunes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  \n" +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'monday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_monday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'monday' " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_monday, " +
//"-- Martes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'tuesday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_tuesday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))  " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'tuesday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_tuesday, " +
//"-- Miercoles " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'wednesday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_wednesday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'wednesday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_wednesday, " +
//"-- Jueves " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'thursday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_thursday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'thursday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_thursday, " +
//"-- Viernes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'friday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_friday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'friday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_friday, " +
//"-- Sabado " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'saturday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_saturday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'saturday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_saturday, " +
//"-- Faltante " +
"	((lt.payment_daily * (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) > 5, 5 , COUNT(DISTINCT(DATE(ldFaltante.created_on)))) FROM APC_LOAN_DETAIL ldFaltante  " +
"	WHERE ldFaltante.id_loan = l.id AND WEEK(DATE(ldFaltante.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	YEAR(DATE(ldFaltante.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('saturday','sunday')  " +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')))  " +
"	- (SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND WEEK(DATE(ldLunes.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	YEAR(DATE(ldLunes.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER'))) as faltante, " +
//"-- new_customer " +
"	CASE WHEN l.new_customer = 'ENEBLED' AND WEEK(DATE(l.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) THEN 'Si' ELSE 'No' END as new_customer, " +
//"-- renovation " +
"	if((SELECT COUNT(id_loan_old) FROM APC_LOAN_BY_RENOVATION lbr  " +
"	INNER JOIN APC_LOAN lRenovation ON lbr.id_loan_new = lRenovation.id " +
"	WHERE id_loan_old = l.id " +
"	 AND loan_by_renovation_status = 'APPROVED' and WEEK(DATE(lRenovation.created_on),1) <=  WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	 YEAR(DATE(lRenovation.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) = 0 , 'No' , 'Si') as renovation, " +
//" -- estatus_prestamo     " +
"	l.loan_status as estatus_prestamo, " +
//"-- num_pagos_all " +
"	(SELECT COUNT(DISTINCT(DATE(ldFaltante.created_on))) FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND date(ldFaltante.created_on) > date(l.created_on) AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day))  and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')) as num_pagos_all,  " +
//"-- num_pagos_week " +
"	(SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) > 6, 6, COUNT(DISTINCT(DATE(ldFaltante.created_on)))) FROM APC_LOAN_DETAIL ldFaltante  " +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')  " +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')) as num_pagos_week, " +
//"-- fee_todos " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_todos, " +
"       lt.loan_type_name AS loan_type_name, " +
"       l.frozen AS frozen, "+

"       (SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount)) " +
"       FROM APC_LOAN_DETAIL ldLunes " +
"	WHERE ldLunes.id_loan= l.id AND ldLunes.created_on <=(DATE_ADD(DATE_ADD(:startDate, INTERVAL  -WEEKDAY(:startDate)-1 DAY), INTERVAL 7 DAY))  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','TRANSFER','RENOVATION_PAYMENT')) AS totalPaid, "    + 
"       lt.total_days AS numPagosLoanType, " +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'monday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_monday, \n" +

"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'tuesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_tuesday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'wednesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_wendsday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'thursday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_thursday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'friday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_friday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'saturday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_saturday,\n" +

"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'monday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_monday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'tuesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_tuesday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'wednesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_wendsday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'thursday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_thursday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'friday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_friday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'saturday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_saturday, "+

        "IF((SELECT count(ln.id_customer) FROM apo_pro_com_april_ten.APC_LOAN ln WHERE ln.id_customer=l.id_customer and \n" +
"	NOT (SELECT COUNT(id_loan_new) from APC_LOAN_BY_RENOVATION WHERE id_loan_new = l.id) > 0) = 0 , 'No' , 'Si') as reactivation, " +
        
            "if((Select count(lsemana.id) from APC_LOAN lsemana where lsemana.id= l.id and \n" +
"    lsemana.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) \n" +
"    and lsemana.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)))>0,'Si','No') as weekOfCreation " +
            
"FROM APC_LOAN l " +
"INNER JOIN APC_LOAN_TYPE lt ON l.id_loan_type = lt.id " +
"INNER JOIN APC_PEOPLE cus ON cus.id = l.id_customer " +
"INNER JOIN APC_PEOPLE endor ON endor.id = l.id_endorsement " +
"INNER JOIN APC_ROUTE r ON r.id = l.id_route AND r.id_office = :idOffice " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"INNER JOIN APC_USER u ON u.id = lbu.id_user " +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource " +
"WHERE " +
"l.loan_status not in ('DELETED','REJECTED') AND  " +
"((SELECT COUNT(ld.id) FROM APC_LOAN_DETAIL ld WHERE WEEK(DATE(ld.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
" YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) AND ld.id_loan = l.id) > 0 ) " +
"UNION " +
"SELECT " +
"	l.id, " +
"	u.id as id_user, " +
"	r.id_office, " +
"	l.created_on as fecha, " +
"	lt.payment as apoyos, " +
"	lt.payment_total as apoyos_total, " +
"	lt.opening_fee as comision_apertura, " +
"	CONCAT(endor.first_name,' ', IF(ISNULL(endor.second_name) ,'', CONCAT(endor.second_name,' ')) ,endor.last_name, ' ', endor.middle_name) AS aval, " +
"	CONCAT(cus.first_name,' ', IF(ISNULL(cus.second_name) ,'', CONCAT(cus.second_name,' ')) ,cus.last_name, ' ', cus.middle_name) AS customer, " +
"	l.amount_to_pay as documento_por, " +
"	lt.payment_daily as abono_diario, " +
"	l.amount_paid, " +
"	(l.amount_to_pay - l.amount_paid) saldo_insoluto, " +
"	r.route_name, " +
"	CONCAT(hr.first_name,' ', IF(ISNULL(hr.second_name) ,'', CONCAT(hr.second_name,' ')) ,hr.last_name, ' ', hr.middle_name) AS asesor, " +
"	(SELECT COUNT(lfn.id) FROM APC_LOAN_FEE_NOTIFICATION lfn WHERE lfn.id_loan = l.id) as num_fee, " +
//"-- Lunes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  \n" +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'monday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_monday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'monday' " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_monday, " +
//"-- Martes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'tuesday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_tuesday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))  " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'tuesday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_tuesday, " +
//"-- Miercoles " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'wednesday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_wednesday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'wednesday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_wednesday, " +
//"-- Jueves " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'thursday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_thursday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) " +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'thursday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_thursday, " +
//"-- Viernes " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'friday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_friday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'friday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_friday, " +
//"-- Sabado " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'saturday'  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as payment_saturday, " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldLunes.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day))" +
"	AND LOWER(DAYNAME(DATE(ldLunes.created_on))) = 'saturday'  " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_saturday, " +
//"-- Faltante " +
"	((lt.payment_daily * (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) > 5, 5 , COUNT(DISTINCT(DATE(ldFaltante.created_on)))) FROM APC_LOAN_DETAIL ldFaltante  " +
"	WHERE ldFaltante.id_loan = l.id AND WEEK(DATE(ldFaltante.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	YEAR(DATE(ldFaltante.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('saturday','sunday')  " +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')))  " +
"	- (SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND WEEK(DATE(ldLunes.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	YEAR(DATE(ldLunes.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER'))) as faltante, " +
//"-- new_customer " +
"	CASE WHEN l.new_customer = 'ENEBLED' AND WEEK(DATE(l.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) THEN 'Si' ELSE 'No' END as new_customer, " +
//"-- renovation " +
"	if((SELECT COUNT(id_loan_old) FROM APC_LOAN_BY_RENOVATION lbr  " +
"	INNER JOIN APC_LOAN lRenovation ON lbr.id_loan_new = lRenovation.id " +
"	WHERE id_loan_old = l.id " +
"	 AND loan_by_renovation_status = 'APPROVED' and WEEK(DATE(lRenovation.created_on),1) <=  WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"	 YEAR(DATE(lRenovation.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) = 0 , 'No' , 'Si') as renovation, " +
//" -- estatus_prestamo     " +
"	l.loan_status as estatus_prestamo, " +
//"-- num_pagos_all " +
"	(SELECT COUNT(DISTINCT(DATE(ldFaltante.created_on))) FROM APC_LOAN_DETAIL ldFaltante  " +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day))  and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')  " +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')) as num_pagos_all, " +
//"-- num_pagos_week " +
"	(SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) > 6, 6, COUNT(DISTINCT(DATE(ldFaltante.created_on)))) FROM APC_LOAN_DETAIL ldFaltante  " +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')  " +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER', 'FEE')) as num_pagos_week, " +
//"-- fee_todos " +
"	(SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount))  " +
"	FROM APC_LOAN_DETAIL ldLunes  " +
"	WHERE ldLunes.id_loan = l.id AND ldLunes.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) " +
"	AND ldLunes.loan_details_type IN ('FEE')) as fee_todos, " +
"       lt.loan_type_name AS loan_type_name, " +
"       l.frozen AS frozen, "+

"       (SELECT IF(ISNULL(SUM(ldLunes.payment_amount)),0,SUM(ldLunes.payment_amount)) " +
"       FROM APC_LOAN_DETAIL ldLunes " +
"	WHERE ldLunes.id_loan= l.id AND ldLunes.created_on <=(DATE_ADD(DATE_ADD(:startDate, INTERVAL  -WEEKDAY(:startDate)-1 DAY), INTERVAL 7 DAY))  " +
"	AND ldLunes.loan_details_type IN ('PAYMENT','TRANSFER','RENOVATION_PAYMENT')) AS totalPaid, "    + 
"       lt.total_days AS numPagosLoanType, " +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'monday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_monday, \n" +

"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'tuesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_tuesday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'wednesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_wendsday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'thursday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_thursday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'friday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_friday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'saturday'  \n" +
"	AND ldFaltante.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER','FEE')) as movimiento_saturday,\n" +

"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'monday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_monday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'tuesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_tuesday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'wednesday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_wendsday, \n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'thursday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_thursday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'friday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_friday,\n" +
            
"       (SELECT IF(COUNT(DISTINCT(DATE(ldFaltante.created_on))) >= 1, 'Si','No') FROM APC_LOAN_DETAIL ldFaltante  \n" +
"	WHERE ldFaltante.id_loan = l.id AND ldFaltante.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) and ldFaltante.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)) and LOWER(DAYNAME(DATE(ldFaltante.created_on))) NOT IN('sunday')\n" +
"       AND LOWER(DAYNAME(DATE(ldFaltante.created_on))) = 'saturday'  \n" +
"	AND ldFaltante.loan_details_type IN ('RENOVATION_PAYMENT')) as renovation_saturday, "+
            
        "IF((SELECT count(ln.id_customer) FROM apo_pro_com_april_ten.APC_LOAN ln WHERE ln.id_customer=l.id_customer and \n" +
"	NOT (SELECT COUNT(id_loan_new) from APC_LOAN_BY_RENOVATION WHERE id_loan_new = l.id) > 0) = 0 , 'No' , 'Si') as reactivation, " +
            "if((Select count(lsemana.id) from APC_LOAN lsemana where lsemana.id= l.id and \n" +
"    lsemana.created_on <=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 7 day)) \n" +
"    and lsemana.created_on >=(date_add(date_add(:startDate, interval  -WEEKDAY(:startDate)-1 day), interval 1 day)))>0,'Si','No') as weekOfCreation"+
"    from APC_LOAN l\n" +
"INNER JOIN APC_LOAN_TYPE lt ON l.id_loan_type = lt.id \n" +
"INNER JOIN APC_PEOPLE cus ON cus.id = l.id_customer \n" +
"INNER JOIN APC_PEOPLE endor ON endor.id = l.id_endorsement \n" +
"INNER JOIN APC_ROUTE r ON r.id = l.id_route AND r.id_office = \"caef3a64-7d1f-11ea-af3e-28f659da398e\"\n" +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id \n" +
"INNER JOIN APC_USER u ON u.id = lbu.id_user \n" +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource \n" +
"where WEEK(DATE(l.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  \n" +
" YEAR(DATE(l.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  and l.loan_status='APPROVED'\n" +
"ORDER BY fecha";
    
    private final String queryColocation = 
"SELECT " +
"u.id, " +
"CONCAT(hr.first_name, ' ' , hr.last_name) as username, " +
"ubo.id_office, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'monday')  " +
"    as colocation_monday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'tuesday')  " +
"    as colocation_tuesday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'wednesday')  " +
"    as colocation_wednesday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'thursday')  " +
"    as colocation_thursday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'friday')  " +
"    as colocation_friday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'saturday')  " +
"    as colocation_saturday, " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND al.created_by = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')))  " +
"    as colocation_total " +
"FROM APC_USER u " +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource AND hr.human_resource_status = 'ENEBLED' " +
"INNER JOIN APC_USER_BY_OFFICE ubo ON ubo.id_user = u.id AND ubo.id_office = :idOffice " +
"WHERE u.user_status = 'ENEBLED' AND  " +
"u.user_type IN ('MOBILE','BOTH');";
    
    private final String queryCobranza = 
"SELECT " +
"u.id, " +
"CONCAT(hr.first_name, ' ' , hr.last_name) as username, " +
"ubo.id_office, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' AND ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'monday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_monday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'tuesday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_tuesday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'wednesday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_wednesday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'thursday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_thursday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'friday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_friday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'saturday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_saturday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6' and ld.id_user = u.id  " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_total " +
"FROM APC_USER u " +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource AND hr.human_resource_status = 'ENEBLED' " +
"INNER JOIN APC_USER_BY_OFFICE ubo ON ubo.id_user = u.id AND ubo.id_office = :idOffice " +
"WHERE u.user_status = 'ENEBLED' AND  " +
"u.user_type IN ('MOBILE','BOTH') AND  " +
"u.certifier = 'DISABLED';";
    
    private final String querySubtotal =
"SELECT " +
"u.id, " +
"CONCAT(hr.first_name, ' ' , hr.last_name) as username, " +
"ubo.id_office, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'monday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_monday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'monday')  " +
"    as opening_fee_monday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'tuesday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_tuesday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'tuesday')  " +
"    as opening_fee_tuesday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'wednesday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_wednesday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'wednesday')  " +
"    as opening_fee_wednesday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'thursday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_thursday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'thursday')  " +
"    as opening_fee_thursday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'friday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_friday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'friday')  " +
"    as opening_fee_friday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'saturday'  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_saturday, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"    AND LOWER(DAYNAME(DATE(al.created_on))) = 'saturday')  " +
"    as opening_fee_saturday, " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_total, " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED' AND albu.id_user = u.id  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) )  " +
"    as opening_fee_total " +
"FROM APC_USER u " +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource AND hr.human_resource_status = 'ENEBLED' " +
"INNER JOIN APC_USER_BY_OFFICE ubo ON ubo.id_user = u.id AND ubo.id_office = :idOffice " +
"WHERE u.user_status = 'ENEBLED' AND  " +
"u.user_type IN ('MOBILE','BOTH') AND  " +
"u.certifier = 'DISABLED';";
    
    private final String queryResumenInOut =
"SELECT " +
"u.id, " +
"CONCAT(hr.first_name, ' ' , hr.last_name) as username, " +
"ubo.id_office, " +
//"-- Lunes " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'monday') as closing_monday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'monday') as expense_monday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'monday') as money_daily_today_monday, " +
//"-- Martes " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'tuesday') as closing_tuesday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'tuesday') as expense_tuesday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'tuesday') as money_daily_today_tuesday, " +
//"-- Miercoles " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'wednesday') as closing_wednesday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'wednesday') as expense_wednesday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'wednesday') as money_daily_today_wednesday, " +
//"-- Jueves " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'thursday') as closing_thursday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'thursday') as expense_thursday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'thursday') as money_daily_today_thursday, " +
//"-- Viernes " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'friday') as closing_friday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'friday') as expense_friday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'friday') as money_daily_today_friday, " +
//"-- Sabado " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE ld.id_user = u.id AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(ld.created_on))) = 'saturday') as closing_saturday, " +
"(SELECT IF(ISNULL(SUM(oe.expense)),0,SUM(oe.expense))  " +
"FROM APC_OTHER_EXPENSE oe " +
"WHERE oe.id_user = u.id AND WEEK(DATE(oe.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(oe.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(oe.created_on))) = 'saturday') as expense_saturday, " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE md.id_user = u.id AND WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND LOWER(DAYNAME(DATE(md.money_daily_date))) = 'saturday') as money_daily_today_saturday " +
"FROM APC_USER u " +
"INNER JOIN APC_HUMAN_RESOURCE hr ON hr.id = u.id_human_resource AND hr.human_resource_status = 'ENEBLED' " +
"INNER JOIN APC_USER_BY_OFFICE ubo ON ubo.id_user = u.id AND ubo.id_office = :idOffice " +
"WHERE u.user_status = 'ENEBLED' AND  " +
"u.user_type IN ('MOBILE','BOTH');";
    
    private final String queryResumenTotal =
"SELECT " +
"u.id, " +
"u.office_name, " +
//"-- Cortes " +
"(SELECT IF(ISNULL(SUM(ld.amount_paid)),0,SUM(ld.amount_paid))  " +
"FROM APC_CLOSING_DAY ld " +
"WHERE WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) AND ld.active_status = 'ENEBLED'  " +
"AND ld.id_office = u.id) as closing__day_total, " +
//"-- Inicios " +
"(SELECT IF(ISNULL(SUM(md.amount)),0,SUM(md.amount))  " +
"FROM APC_MONEY_DAILY md " +
"WHERE WEEK(DATE(md.money_daily_date),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(md.money_daily_date)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) AND md.id_office = u.id) as money_daily_today_total, " +
//"-- Subtotal " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as subtotal_total, " +
//"-- comision por apertura " +
"(SELECT IF(ISNULL(SUM(alt.opening_fee)),0,SUM(alt.opening_fee)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED'  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')))  " +
"    as opening_fee_total, " +
//"-- cobranza " +
"(SELECT IF(ISNULL(SUM(ld.payment_amount)),0,SUM(ld.payment_amount))  " +
"FROM APC_LOAN_DETAIL ld " +
"INNER JOIN APC_LOAN l ON l.id = ld.id_loan  " +
"INNER JOIN APC_LOAN_BY_USER lbu ON lbu.id_loan = l.id " +
"WHERE lbu.id_user = 'aad0c673-eb93-11ea-b7e1-02907d0fb4e6'   " +
"AND WEEK(DATE(ld.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"YEAR(DATE(ld.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))  " +
"AND ld.loan_details_type IN ('PAYMENT','RENOVATION_PAYMENT','TRANSFER')) as cobranza_today, " +
//"-- colocacion " +
"(SELECT IF(ISNULL(SUM(alt.payment)),0,SUM(alt.payment)) FROM APC_LOAN al  " +
"    INNER JOIN APC_LOAN_TYPE alt ON al.id_loan_type = alt.id  " +
"    INNER JOIN APC_LOAN_BY_USER albu ON albu.id_loan = al.id " +
"    WHERE loan_status = 'APPROVED'  " +
"    AND WEEK(DATE(al.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(al.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d')) )  " +
"    as colocation_total, " +
//"-- nominas " +
"(SELECT IF(ISNULL(SUM(ap.total_payment)),0,SUM(ap.total_payment)) FROM APC_PAYROLL ap  " +
"WHERE ap.id_office = u.id AND ap.active_status = 'ENEBLED'  " +
"AND WEEK(DATE(ap.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(ap.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) as nomina_total, " +
//"-- adelantos " +
"(SELECT IF(ISNULL(SUM(ap.amount)),0,SUM(ap.amount)) FROM APC_ADVANCE ap  " +
"WHERE ap.id_office = u.id AND ap.active_status = 'ENEBLED'  " +
"AND WEEK(DATE(ap.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(ap.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) as adelantos_total, " +
//"-- entradas " +
"(SELECT IF(ISNULL(SUM(ap.amount)),0,SUM(ap.amount)) FROM APC_EXPENSE_COMPANY ap  " +
"WHERE ap.id_office = u.id AND ap.active_status = 'ENEBLED' AND  " +
"ap.expense_company_type = 'PAYMENT_IN' " +
"AND WEEK(DATE(ap.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(ap.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) as entradas_total,  " +
//"-- gastos admon " +
"(SELECT IF(ISNULL(SUM(ap.amount)),0,SUM(ap.amount)) FROM APC_EXPENSE_COMPANY ap  " +
"WHERE ap.id_office = u.id AND ap.active_status = 'ENEBLED' AND  " +
"ap.expense_company_type = 'PAYMENT_OUT' " +
"AND WEEK(DATE(ap.created_on),1) = (WEEK(STR_TO_DATE(:startDate, '%Y-%m-%d'),1)) AND  " +
"    YEAR(Date(ap.created_on)) = YEAR(STR_TO_DATE(:startDate, '%Y-%m-%d'))) as gastos_admon_total " +
"FROM APC_OFFICE u " +
"WHERE u.office_status = 'ENEBLED' AND u.id = :idOffice ;";
}
