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

import com.arrebol.apc.controller.drive.DriverController;
import com.arrebol.apc.model.views.CobranzaLastWeekByUserView;
import com.arrebol.apc.model.views.ColocationLastWeekByUserView;
import com.arrebol.apc.model.views.InformationLoanLastWeekView;
import com.arrebol.apc.model.views.ResumeNewCustomerLastWeekView;
import com.arrebol.apc.model.views.ResumenInOutLastWeekByUserView;
import com.arrebol.apc.model.views.ResumenTotalLastWeekView;
import com.arrebol.apc.model.views.SubtotalLastWeekByUserView;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named("driverLastManager")
@ViewScoped
public class DriverLastBean extends GenericBean implements Serializable, Datatable{

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public DriverController getDriverCtrl() {
        return driverCtrl;
    }

    public void setDriverCtrl(DriverController driverCtrl) {
        this.driverCtrl = driverCtrl;
    }

    public List<InformationLoanLastWeekView> getData() {
        return data;
    }

    public void setData(List<InformationLoanLastWeekView> data) {
        this.data = data;
    }

    public InformationLoanLastWeekView getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(InformationLoanLastWeekView selectedData) {
        this.selectedData = selectedData;
    }

    public List<ColocationLastWeekByUserView> getDataColocation() {
        return dataColocation;
    }

    public void setDataColocation(List<ColocationLastWeekByUserView> dataColocation) {
        this.dataColocation = dataColocation;
    }

    public ColocationLastWeekByUserView getSelectedColocation() {
        return selectedColocation;
    }

    public void setSelectedColocation(ColocationLastWeekByUserView selectedColocation) {
        this.selectedColocation = selectedColocation;
    }

    public List<SubtotalLastWeekByUserView> getDataSubtotal() {
        return dataSubtotal;
    }

    public void setDataSubtotal(List<SubtotalLastWeekByUserView> dataSubtotal) {
        this.dataSubtotal = dataSubtotal;
    }

    public SubtotalLastWeekByUserView getSelectedSubtotal() {
        return selectedSubtotal;
    }

    public void setSelectedSubtotal(SubtotalLastWeekByUserView selectedSubtotal) {
        this.selectedSubtotal = selectedSubtotal;
    }

    public List<ResumenInOutLastWeekByUserView> getDataResumen() {
        return dataResumen;
    }

    public void setDataResumen(List<ResumenInOutLastWeekByUserView> dataResumen) {
        this.dataResumen = dataResumen;
    }

    public ResumenInOutLastWeekByUserView getSelectedResumen() {
        return selectedResumen;
    }

    public void setSelectedResumen(ResumenInOutLastWeekByUserView selectedResumen) {
        this.selectedResumen = selectedResumen;
    }

    public List<CobranzaLastWeekByUserView> getDataCobranza() {
        return dataCobranza;
    }

    public void setDataCobranza(List<CobranzaLastWeekByUserView> dataCobranza) {
        this.dataCobranza = dataCobranza;
    }

    public CobranzaLastWeekByUserView getSelectedCobranza() {
        return selectedCobranza;
    }

    public void setSelectedCobranza(CobranzaLastWeekByUserView selectedCobranza) {
        this.selectedCobranza = selectedCobranza;
    }

    public List<ResumenTotalLastWeekView> getDataTotal() {
        return dataTotal;
    }

    public void setDataTotal(List<ResumenTotalLastWeekView> dataTotal) {
        this.dataTotal = dataTotal;
    }

    public ResumenTotalLastWeekView getSelectedTotal() {
        return selectedTotal;
    }

    public void setSelectedTotal(ResumenTotalLastWeekView selectedTotal) {
        this.selectedTotal = selectedTotal;
    }
    
    public List<ResumeNewCustomerLastWeekView> getDataCtesNvos() {
        return dataCtesNvos;
    }

    public void setDataCtesNvos(List<ResumeNewCustomerLastWeekView> dataCtesNvos) {
        this.dataCtesNvos = dataCtesNvos;
    }

    public ResumeNewCustomerLastWeekView getSelectedCtesNvos() {
        return selectedCtesNvos;
    }

    public void setSelectedCtesNvos(ResumeNewCustomerLastWeekView selectedCtesNvos) {
        this.selectedCtesNvos = selectedCtesNvos;
    }
    
    public void paintDataXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBold(true);
        cellStyle.setFont(font);
        cellStyle.setBorderTop(BorderStyle.DOUBLE);
        cellStyle.setBorderBottom(BorderStyle.DOUBLE);
        cellStyle.setBorderLeft(BorderStyle.DOUBLE);
        cellStyle.setBorderRight(BorderStyle.DOUBLE);
        cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LEMON_CHIFFON.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        short dataFormat = wb.createDataFormat().getFormat("$#,##0.00");
        
        initCellStyle(wb);
        
        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);
            cell.setCellStyle(cellStyle);
        }
                
        String aux = "";
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            HSSFRow row = sheet.getRow(i);
            InformationLoanLastWeekView loanWeekData = data.get(i-1);
            HSSFCell cell;
            String rowRenov = loanWeekData.getConditionRenovation();
            //cellStyleRow = getCellStyle(rowRenov);
            // Row General
            for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                cell = row.getCell(j);
                cell.setCellStyle(getCellStyle(rowRenov));
                
                if((j >= 7 && j <= 22) || (j == 2 || j == 3)){
                    cell.getCellStyle().setDataFormat(dataFormat);
                    aux = cell.getStringCellValue().replace("$", "");
                    aux = aux.replace(",", "");
                    cell.setCellValue(Double.parseDouble(aux));
                    cell.setCellType(CellType.NUMERIC);
                }
            }
            
            // Lunes
            cell = row.getCell(10);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionLunes()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(11);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionLunes()));
            cell.getCellStyle().setDataFormat(dataFormat);

            // Martes
            cell = row.getCell(12);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionMartes()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(13);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionMartes()));
            cell.getCellStyle().setDataFormat(dataFormat);
            
            // Miércoles
            cell = row.getCell(14);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionMiercoles()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(15);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionMiercoles()));
            cell.getCellStyle().setDataFormat(dataFormat);

            // Jueves
            cell = row.getCell(16);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionJueves()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(17);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionJueves()));
            cell.getCellStyle().setDataFormat(dataFormat);

            // Viernes
            cell = row.getCell(18);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionViernes()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(19);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionViernes()));
            cell.getCellStyle().setDataFormat(dataFormat);

            // Sábado
            cell = row.getCell(20);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionSabado()));
            cell.getCellStyle().setDataFormat(dataFormat);
            cell = row.getCell(21);
            cell.setCellStyle(getCellStyle(loanWeekData.getConditionSabado()));
            cell.getCellStyle().setDataFormat(dataFormat); 
        }
        
        String ruta = data.get(0).getRouteName();
        int regInsertados = 0;

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            HSSFRow row = sheet.getRow(i);
            InformationLoanLastWeekView loanWeekData = data.get(i-1-regInsertados);
            
            if(!ruta.equalsIgnoreCase(data.get(i-1-regInsertados).getRouteName())){
                creaRegistroTotales(ruta, sheet, dataFormat, false, i);
                ruta = data.get(i-1-regInsertados).getRouteName();
                regInsertados++;
            }
        }
        creaRegistroTotales(ruta, sheet, dataFormat, true, sheet.getPhysicalNumberOfRows()+1);
    }
    
    private void creaRegistroTotales(String ruta, HSSFSheet sheet, short dataFormat, boolean lastRow, int i){
        HSSFRow r;
        HSSFCell cellNew;
        String rutaAux = ruta;
        if (lastRow) {
            r = sheet.createRow(sheet.getPhysicalNumberOfRows() + 1);
        } else {
            sheet.shiftRows(i, sheet.getLastRowNum(), 1, true, false);
            r = sheet.createRow(i);
        }

        // Apoyos
        cellNew = r.createCell(3);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        BigDecimal suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux) && p.getWeekLoanDate() == p.getWeekCurrentDate())).map((py) -> py.getApoyos()).reduce(BigDecimal::add).isPresent()
                ? data.stream().filter((p) -> (p.getRouteName().equals(rutaAux) && p.getWeekLoanDate() == p.getWeekCurrentDate())).map((py) -> py.getApoyos()).reduce(BigDecimal::add).get()
                : BigDecimal.ZERO;
        cellNew.setCellValue(suma.doubleValue());
        // Comsion por apertura
        cellNew = r.createCell(4);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux) && p.getWeekLoanDate() == p.getWeekCurrentDate())).map((py) -> py.getComisionApertura()).reduce(BigDecimal::add).isPresent()
                ? data.stream().filter((p) -> (p.getRouteName().equals(rutaAux) && p.getWeekLoanDate() == p.getWeekCurrentDate())).map((py) -> py.getComisionApertura()).reduce(BigDecimal::add).get()
                : BigDecimal.ZERO;
        cellNew.setCellValue(suma.doubleValue());
                
        //Totales
        cellNew = r.createCell(9);
        cellNew.setCellType(CellType.STRING);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.setCellValue("TOTALES: ");
        // Lunes
        cellNew = r.createCell(10);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentMonday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(11);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeMonday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Martes
        cellNew = r.createCell(12);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentTuesday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(13);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeTuesday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Miércoles
        cellNew = r.createCell(14);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentWednesday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(15);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeWednesday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Jueves
        cellNew = r.createCell(16);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentThursday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(17);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeThursday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Viernes
        cellNew = r.createCell(18);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentFriday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(19);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeFriday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Sábado
        cellNew = r.createCell(20);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getPaymentSaturday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        cellNew = r.createCell(21);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFeeSaturday()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
        // Faltante
        cellNew = r.createCell(22);
        cellNew.setCellType(CellType.NUMERIC);
        cellNew.setCellStyle(getCellStyle("greyRow"));
        cellNew.getCellStyle().setDataFormat(dataFormat);
        suma = data.stream().filter((p) -> (p.getRouteName().equals(rutaAux))).map((py) -> py.getFaltante()).reduce(BigDecimal::add).get();
        cellNew.setCellValue(suma.doubleValue());
    }
    
    private HSSFCellStyle getCellStyle (String cellStyleColor){
        if(cellStyleColor == null){
            cellStyleColor = "White";
        }
        
        switch(cellStyleColor){
            case "redRow":
                return cellStyleRed;
            case "greenRow":
                return cellStyleGreen;
            case "yellowRow":
                return cellStyleYellow;
            case "blueRow":
                return cellStyleBlue;
            case "blueLightRow":
                return cellStyleBlueLight;
            case "greenLigthRow":
                return cellStyleGreenLight;
            case "greenStrongRow":
                return cellStyleGreenStrong;
            case "greyRow":
                return cellStyleGrey;
            case "orangeRow":
                return cellStyleOrange;
            default:
                return cellStyleWhite;
        }
    }
    
    private void initCellStyle(HSSFWorkbook wb){
        // MULTA
        fontRed = wb.createFont(); 
        cellStyleRed = wb.createCellStyle();
        fontRed.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
        //cellStyleRed.setFont(fontRed);
        
        // ABONO 0
        fontGreen = wb.createFont(); 
        cellStyleGreen = wb.createCellStyle();
        fontGreen.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleGreen.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleGreen.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
        //cellStyleGreen.setFont(fontGreen);
        
        // ABONÓ MENOS
        fontYellow = wb.createFont();
        cellStyleYellow = wb.createCellStyle();
        fontYellow.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleYellow.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleYellow.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        //cellStyleYellow.setFont(fontYellow);
        
        // ABONÓ DE MÁS
        fontBlue = wb.createFont(); 
        cellStyleBlue = wb.createCellStyle();
        fontBlue.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleBlue.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //cellStyleBlue.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
        cellStyleBlue.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());
        //cellStyleBlue.setFont(fontBlue);

        // PAGO CONGELADO
        fontBlueLight = wb.createFont();
        cellStyleBlueLight = wb.createCellStyle();
        fontBlueLight.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleBlueLight.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleBlueLight.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        
        // RENOVÓ
        fontGreenLight = wb.createFont(); 
        cellStyleGreenLight = wb.createCellStyle();
        fontGreenLight.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleGreenLight.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleGreenLight.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
        //cellStyleGreenLight.setFont(fontGreenLight);
        
        // getSaldoInsoluto == 0
        fontGreenStrong = wb.createFont(); 
        cellStyleGreenStrong = wb.createCellStyle();
        fontGreenStrong.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleGreenStrong.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleGreenStrong.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        //cellStyleGreenStrong.setFont(fontGreenStrong);
        
        // CLIENTE NUEVO
        fontOrange = wb.createFont(); 
        cellStyleOrange = wb.createCellStyle();
        fontOrange.setColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleOrange.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleOrange.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
        //cellStyleGreenLight.setFont(fontGreenLight);
        
        // Totales
        fontGrey = wb.createFont(); 
        cellStyleGrey = wb.createCellStyle();
        fontGrey.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        cellStyleGrey.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleGrey.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyleGrey.setFont(fontGrey);
        
        cellStyleWhite = wb.createCellStyle();
        cellStyleWhite.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyleWhite.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
    }
    
    private DriverController driverCtrl;
    private List<InformationLoanLastWeekView> data;
    private InformationLoanLastWeekView selectedData;
    
    private List<ColocationLastWeekByUserView> dataColocation;
    private ColocationLastWeekByUserView selectedColocation;
    
    private List<SubtotalLastWeekByUserView> dataSubtotal;
    private SubtotalLastWeekByUserView selectedSubtotal;
    
    private List<ResumenInOutLastWeekByUserView> dataResumen;
    private ResumenInOutLastWeekByUserView selectedResumen;
    
    private List<CobranzaLastWeekByUserView> dataCobranza;
    private CobranzaLastWeekByUserView selectedCobranza;
    
    private List<ResumenTotalLastWeekView> dataTotal;
    private ResumenTotalLastWeekView selectedTotal;
    
    private List<ResumeNewCustomerLastWeekView> dataCtesNvos;
    private ResumeNewCustomerLastWeekView selectedCtesNvos;
    
    // Datos para pintar las celdas
    HSSFCellStyle cellStyleRed;
    HSSFFont fontRed;
    HSSFCellStyle cellStyleGreen;
    HSSFFont fontGreen;
    HSSFCellStyle cellStyleYellow;
    HSSFFont fontYellow;
    HSSFCellStyle cellStyleBlue;
    HSSFFont fontBlue;
    HSSFCellStyle cellStyleBlueLight;
    HSSFFont fontBlueLight;
    HSSFCellStyle cellStyleGreenLight;
    HSSFFont fontGreenLight;
    HSSFCellStyle cellStyleGreenStrong;
    HSSFFont fontGreenStrong;
    HSSFCellStyle cellStyleOrange;
    HSSFFont fontOrange;
    HSSFCellStyle cellStyleWhite;
    HSSFCellStyle cellStyleGrey;
    HSSFFont fontGrey;
    
    @PostConstruct
    public void init() {
        driverCtrl = new DriverController();
        data = driverCtrl.getAllLoanLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataColocation = driverCtrl.getAllColocationLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataSubtotal = driverCtrl.getAllSubtotalLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataResumen = driverCtrl.getAllResumenInOutLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataCobranza = driverCtrl.getAllCobranzaLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataTotal = driverCtrl.getAllResumenTotalLastWeekByOffice(getLoggedUser().getOffice().getId());
        dataCtesNvos = driverCtrl.getAllResumenNewCustomerLastWeekByOffice();
        
        for(SubtotalLastWeekByUserView subtotal : dataSubtotal)
        {
            Double sumFaltante = data.stream().filter(p -> p.getIdUser().equalsIgnoreCase(subtotal.getId())).mapToDouble(h -> h.getFaltante().doubleValue()).sum();
            subtotal.setFaltante(new BigDecimal(sumFaltante));
        }
    }
    
}
