<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="ro.emid.common.db.framework.ReportStatement" %>
<%@ page import="ro.emid.common.db.IncidentAccessDB" %>
<%@ page import="ro.emid.incidente.obj.RC" %>
<%@ page import="java.io.File" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.incidente.db.RCAccessDB" %>
<%@ page import="ro.emid.common.utils.*" %>
<%@ page import="ro.emid.common.constants.*" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ include file="../incidente/declaratii.jsp"%>

<%! final Logger LOG = Logger.getLogger("raportMonitorizareIntreruperi_jsp"); %>

<%! boolean showTestValues = false; %>

<% boolean editMode = request.getAttribute("pw.edt.rc") != null; %>


<%
    LOG.debug("test loging report statements");

    long dataStart = System.currentTimeMillis();
    boolean useDiferentaMinuteFunction = true;

    /**
     numarul de consumatori afectati reprezinta numarul de intreruperi
     */

    long tStart = System.currentTimeMillis();

    int anRaport = ObjectUtils.getIntValue( request.getParameter( "an"), -1);

    if( anRaport == -1){
        anRaport = ro.emid.common.utils.Util.getAn(new Date());
    }

    RC rc = new RC();

    rc = RCAccessDB.getEntry(anRaport, RC.Tip.RAPORT_MONITORIZARE_INTR);

    if( rc == null){
        rc = new RC();
        rc.setAn( anRaport);
        rc.setTip( RC.Tip.RAPORT_MONITORIZARE_INTR);
    }

    FSC fscRaport = FSCUtils.getFsc(request);

    Category centruExploatareFsc = CategoryAccessDB.getById(fscRaport.getCentruId());
    if (centruExploatareFsc == null) {
        centruExploatareFsc = new Category();
    }

    Category sucursalaFsc = CategoryAccessDB.getById(fscRaport.getSucursalaId());
    if (sucursalaFsc == null) {
        sucursalaFsc = new Category();
    }
    Category filialaFsc = CategoryAccessDB.getById(fscRaport.getFilialaId());
    if (filialaFsc == null) {
        filialaFsc = new Category();
    }



    String titluRaport = "";

    if ( centruExploatareFsc.getId() > 0){
        titluRaport = filialaFsc.getName() + " - " + sucursalaFsc.getName() + " - " + centruExploatareFsc.getName();
    } else if( sucursalaFsc.getId() > 0){
        titluRaport = filialaFsc.getName() + " - " + sucursalaFsc.getName();
    } else if( filialaFsc.getId() > 0){
        titluRaport = filialaFsc.getName();
    } else {
        //Electrica SA
        titluRaport = "Electrica SA";
    }

    /**
     * INTRERUPERI PLANIFICATE : Lucrari Programate
     */

    final StringBuffer buffer = new StringBuffer();
    buffer.append(" SELECT \n").append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START_IC, 'MM') LUNA,\n" ).append(
            "    SUM( lp.UTILIZATORI_JT ) NR_CONS_JT,\n" ).append(
            "    SUM( lp.UTILIZATORI_MT ) NR_CONS_MT,\n" ).append(
            "    SUM( lp.UTILIZATORI_IT ) NR_CONS_IT \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp\n");

    ReportStatement stNrIntreruperiLpDinFise = new ReportStatement(
        buffer.toString()
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START_IC, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START_IC, 'MM') LUNA, \n" ).append(
            "    SUM( lfInfo.NUMAR_CONSUMATORI_JT), SUM( lfInfo.NUMAR_CONSUMATORI_MT), SUM( lfInfo.NUMAR_CONSUMATORI_IT) \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp \n" ).append(
            "    INNER JOIN LPD_INSTALATIE_AFECTATA leg ON ( lp.ID = leg.LUCRARE_ID) \n" ).append(
            "    INNER JOIN lf_info lfInfo ON ( leg.COD_LF = lfInfo.COD_SAP_LF AND TO_CHAR( lp.DATA_START_IC, 'YYYY')=lfInfo.AN ) ");

    ReportStatement stNrIntreruperiLpDinNom = new ReportStatement(
        buffer.toString()
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START_IC, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n" ).append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START_IC, 'MM') LUNA,\n" ).append(
            "    SUM( lp.UTILIZATORI_JT ) NR_CONS_JT,\n" ).append(
            "    SUM( lp.UTILIZATORI_MT ) NR_CONS_MT,\n" ).append(
            "    SUM( lp.UTILIZATORI_IT ) NR_CONS_IT \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp \n");

    ReportStatement stNrIntrPlanifRealimLaTimpDinFise = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND DURATA_INTR <= DURATA_PROGR "
            //" AND DATA_PROGRAMARE_START < DATA_INTR_STOP AND DATA_INTR_STOP <= DATA_PROGRAMARE_STOP "
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START_IC, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n" ).append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START, 'MM') LUNA, \n" ).append(
            "    SUM( lfInfo.NUMAR_CONSUMATORI_JT), \n" ).append(
            "    SUM( lfInfo.NUMAR_CONSUMATORI_MT), \n" ).append(
            "    SUM( lfInfo.NUMAR_CONSUMATORI_IT) \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp \n" ).append(
            "    INNER JOIN LPD_INSTALATIE_AFECTATA leg ON ( lp.ID = leg.LUCRARE_ID) \n" ).append(
            "    INNER JOIN lf_info lfInfo ON ( leg.COD_LF = lfInfo.COD_SAP_LF AND TO_CHAR( lp.DATA_START_IC, 'YYYY')=lfInfo.AN ) ");

    ReportStatement stNrLucrariRealimentateLaTimpDinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND DATA_PROGRAMARE_START < DATA_INTR_STOP AND DATA_INTR_STOP <= DATA_PROGRAMARE_STOP "
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START, 'MM') "
    );

    /**
     *  Pentru calcul durata medie intrerupere
     */
    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START_IC, 'MM') LUNA,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_JT > 0 THEN GETDIFERENTAMINUTE( lp.DATA_STOP_IC, lp.DATA_START_IC) ELSE 0 END) DURATA_LP_JT,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_MT > 0 THEN GETDIFERENTAMINUTE( lp.DATA_STOP_IC, lp.DATA_START_IC) ELSE 0 END) DURATA_LP_MT,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_IT > 0 THEN GETDIFERENTAMINUTE( lp.DATA_STOP_IC, lp.DATA_START_IC) ELSE 0 END) DURATA_LP_IT \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp\n");
    ReportStatement stDurataIntrerupereLpDinFise = new ReportStatement(
        buffer.toString()
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START_IC, 'MM') "
    );

    /**
     * numar intreruperi ca fiind numar de lucrari ***
     */
    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "    lp.mediu,\n" ).append(
            "    TO_CHAR( lp.DATA_START_IC, 'MM') LUNA,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_JT > 0 THEN 1 ELSE 0 END ) NR_INTR_JT,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_MT > 0 THEN 1 ELSE 0 END ) NR_INTR_MT,\n" ).append(
            "    SUM( CASE WHEN lp.UTILIZATORI_IT > 0 THEN 1 ELSE 0 END ) NR_INTR_IT \n" ).append(
            " FROM VW_INTR_PLANIFICATE lp\n");

    ReportStatement stConsumatoriAfectatiLP = new ReportStatement(
        buffer.toString()
    ).setGroupStatement(
            " GROUP BY lp.mediu, TO_CHAR( lp.DATA_START_IC, 'MM') "
    );


    /**
     * INTRERUPERI NEPLANIFICATE : Incidente si Deranjamente
     */
    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN i.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN i.MEDIU = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA,\n" ).append(
            "    SUM( i.UTILIZATORI_JT ) NR_CONS_JT,\n" ).append(
            "    SUM( i.UTILIZATORI_MT ) NR_CONS_MT,\n" ).append(
            "    SUM( i.UTILIZATORI_IT ) NR_CONS_IT\n" ).append(
            " FROM incidente i \n");

    ReportStatement stNrIntrFiDinFise = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < i.DURATA_INCIDENT AND i.MEDIU IN (1, 2) AND i.MOD_COMPL_CONS = " + ro.emid.common.constants.IncidentConstants.ConsumatoriCompletati.MANUAL
    ).setGroupStatement(
            " GROUP BY i.MEDIU, TO_CHAR( i.DATA, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN lfInfo.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN lfinfo.mediu = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "TO_CHAR( i.DATA, 'MM') LUNA, \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_JT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_MT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_IT) \n" ).append(
            " FROM incidente i \n" ).append(
            " INNER JOIN incidente_lf leg ON ( i.ID=leg.INCIDENT_ID ) \n" ).append(
            " INNER JOIN lf_info lfInfo ON ( leg.COD_LF=lfInfo.COD_SAP_LF ) \n");

    ReportStatement stNrIntrFiDinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < i.DURATA_INCIDENT AND lfinfo.mediu IN (1, 2) AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.AUTOMAT
    ).setGroupStatement(
            " GROUP BY lfInfo.mediu, TO_CHAR( i.DATA, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN d.MEDIUL = 1 THEN 'U' \n" ).append(
            " WHEN d.MEDIUL = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( d.DATA_ANUNTARE, 'MM'), \n" ).append(
            "    SUM( d.nr_case + d.nr_ap + d.nr_mici + d.nr_mari), \n").append(
            "    SUM( 0), \n").append(
            "    SUM( 0) \n").append(
            " FROM deranjamente d \n");

    ReportStatement stNrIntrFD = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            useDiferentaMinuteFunction ?
                    " AND 3 < getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE) AND d.MEDIUL IN (1, 2) " :
                    " AND 3 < d.DURATA_REZOLVARE AND d.MEDIUL IN (1, 2) "
    ).setGroupStatement(
            " GROUP BY d.mediul, TO_CHAR( d.DATA_ANUNTARE, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN i.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN i.MEDIU = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA,\n" ).append(
            "    SUM( i.UTILIZATORI_JT ) NR_CONS_JT,\n" ).append(
            "    SUM( i.UTILIZATORI_MT ) NR_CONS_MT,\n" ).append(
            "    SUM( i.UTILIZATORI_IT ) NR_CONS_IT\n" ).append(
            " FROM incidente i \n");

    ReportStatement stNrIntreruperi12DinFise = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < DURATA_INCIDENT AND DURATA_INCIDENT/60 < 12 AND i.MEDIU = 1 "  +  //urban
                    " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.MANUAL
    ).setGroupStatement(
            " GROUP BY i.mediu, TO_CHAR( i.DATA, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN lfInfo.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN lfinfo.mediu = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "TO_CHAR( i.DATA, 'MM') LUNA, \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_JT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_MT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_IT) \n" ).append(
            " FROM incidente i \n" ).append(
            " INNER JOIN incidente_lf leg ON ( i.ID=leg.INCIDENT_ID ) \n" ).append(
            " INNER JOIN lf_info lfInfo ON ( leg.COD_LF=lfInfo.COD_SAP_LF) \n");

    ReportStatement stNrIntreruperi12DinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < DURATA_INCIDENT AND DURATA_INCIDENT/60 < 12 AND lfInfo.MEDIU = 1 " +  //urban
                    " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.AUTOMAT
    ).setGroupStatement(
            " GROUP BY lfInfo.mediu, TO_CHAR( i.DATA, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN d.MEDIUL = 1 THEN 'U' \n" ).append(
            " WHEN d.MEDIUL = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( d.DATA_ANUNTARE, 'MM'), \n" ).append(
            "    SUM( d.nr_case + d.nr_ap + d.nr_mici + d.nr_mari), \n").append(
            "    SUM( 0), \n").append(
            "    SUM( 0) \n").append(
            " FROM deranjamente d \n");

    ReportStatement stNrIntrFD12 = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            useDiferentaMinuteFunction ?
                    " AND 3 < getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE) AND getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE)/60 < 12  AND d.MEDIUL = 1 " : //urban
                    " AND 3 < d.DURATA_REZOLVARE AND d.DURATA_REZOLVARE/60 < 12  AND d.MEDIUL = 1 " //urban
    ).setGroupStatement(
            " GROUP BY d.mediul, TO_CHAR( d.DATA_ANUNTARE, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN i.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN i.MEDIU = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA,\n" ).append(
            "    SUM( i.UTILIZATORI_JT ) NR_CONS_JT,\n" ).append(
            "    SUM( i.UTILIZATORI_MT ) NR_CONS_MT,\n" ).append(
            "    SUM( i.UTILIZATORI_IT ) NR_CONS_IT\n" ).append(
            " FROM incidente i \n");

    ReportStatement stNrIntreruperi24DinFise = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < DURATA_INCIDENT AND DURATA_INCIDENT/60 < 24 AND i.MEDIU = 2 " + //mediu rural
                    " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.MANUAL
    ).setGroupStatement(
            " GROUP BY i.mediu, TO_CHAR( i.DATA, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN lfInfo.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN lfinfo.mediu = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "TO_CHAR( i.DATA, 'MM') LUNA, \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_JT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_MT), \n" ).append(
            "SUM( lfInfo.NUMAR_CONSUMATORI_IT) \n" ).append(
            " FROM incidente i \n" ).append(
            " INNER JOIN incidente_lf leg ON ( i.ID=leg.INCIDENT_ID ) \n" ).append(
            " INNER JOIN lf_info lfInfo ON ( leg.COD_LF=lfInfo.COD_SAP_LF ) \n");

    ReportStatement stNrIntreruperi24DinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND 3 < DURATA_INCIDENT AND DURATA_INCIDENT/60 < 24 AND lfInfo.MEDIU = 2 " + //mediu rural
                    " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.AUTOMAT
    ).setGroupStatement(
            " GROUP BY lfInfo.mediu, TO_CHAR( i.DATA, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN d.MEDIUL = 1 THEN 'U' \n" ).append(
            " WHEN d.MEDIUL = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( d.DATA_ANUNTARE, 'MM'), \n" ).append(
            "    SUM( d.nr_case + d.nr_ap + d.nr_mici + d.nr_mari), \n").append(
            "    SUM( 0), \n").append(
            "    SUM( 0) \n").append(
            " FROM deranjamente d \n");

    ReportStatement stNrIntrFD24 = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            useDiferentaMinuteFunction ?
                    " AND 3 < getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE) AND getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE)/60 < 24  AND d.MEDIUL = 2 " : //mediu lrural
                    " AND 3 < d.DURATA_REZOLVARE AND DURATA_REZOLVARE/60 < 24  AND d.MEDIUL = 2 " //mediu lrural
    ).setGroupStatement(
            " GROUP BY d.mediul, TO_CHAR( d.DATA_ANUNTARE, 'MM')"
    );

    /**
     *  Pentru calcul durata medie intrerupere
     */
    //IE - Durata incidente, completati manual
    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN i.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN i.MEDIU = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_JT > 0 THEN i.DURATA_INCIDENT ELSE 0 END) DURATA_INCIDENTE_JT,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_MT > 0 THEN i.DURATA_INCIDENT ELSE 0 END) DURATA_INCIDENTE_MT,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_IT > 0 THEN i.DURATA_INCIDENT ELSE 0 END) DURATA_INCIDENTE_IT\n" ).append(
            " FROM incidente i \n");

    ReportStatement stDurataIncidenteDinFi = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.MANUAL
    ).setGroupStatement(
            " GROUP BY i.mediu, TO_CHAR( i.DATA, 'MM')"
    );

    //FI - durata incidentelor compl automat
    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "    si.MEDIU, \n").append(
            "    TO_CHAR( i.DATA, 'MM') LUNA, \n" ).append(
            "    SUM( CASE WHEN si.PE_JT > 0 THEN i.DURATA_INCIDENT ELSE 0 END ) AS DURATA_INCIDENTE_JT, \n" ).append(
            "    SUM( CASE WHEN si.PE_MT > 0 THEN i.DURATA_INCIDENT ELSE 0 END ) AS DURATA_INCIDENTE_MT, \n" ).append(
            "    SUM( CASE WHEN si.PE_IT > 0 THEN i.DURATA_INCIDENT ELSE 0 END ) AS DURATA_INCIDENTE_IT  \n" ).append(
            " FROM incidente i INNER JOIN (\n" ).append(
            "    SELECT\n" ).append(
            "        i.ID,\n" ).append(
            "        CASE WHEN lf_info.MEDIU = 1 THEN 'U' \n" ).append(
            "             WHEN lf_info.mediu = 2 THEN 'R' \n" ).append(
            "        END MEDIU,\n" ).append(
            "        count(*) AS NR_REAL_INST,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_JT > 0 THEN 1 ELSE 0 END) AS PE_JT,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_MT > 0 THEN 1 ELSE 0 END) AS PE_MT,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_IT > 0 THEN 1 ELSE 0 END) AS PE_IT\n" ).append(
            "    FROM incidente i \n" ).append(
            "        INNER JOIN incidente_lf leg ON ( i.ID=leg.INCIDENT_ID )\n" ).append(
            "        INNER JOIN lf_info ON ( leg.COD_LF=lf_info.COD_SAP_LF)\n" ).append(
            "    WHERE lf_info.AN = " ).append( anRaport ).append(
            "    GROUP BY i.ID, lf_info.MEDIU \n" ).append(
            " ) si ON (i.ID = si.ID)");

    ReportStatement stDurataIncidenteDinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.AUTOMAT +
                    " AND TO_CHAR( i.DATA, 'YYYY') = " + anRaport +
                    " AND si.NR_REAL_INST > 0 "
    ).setGroupStatement(
            " GROUP BY si.mediu, TO_CHAR( i.DATA, 'MM') "
    );

    //FD - durata deranjamente
    ReportStatement stDurataDer = new ReportStatement(
            " SELECT \n" +
                    "CASE WHEN d.MEDIUL = 1 THEN 'U' \n" +
                    " WHEN d.MEDIUL = 2 THEN 'R' " +
                    "END MEDIU, \n" +
                    "    TO_CHAR( d.DATA_ANUNTARE, 'MM'), \n" +
                    (useDiferentaMinuteFunction ?
                            "    SUM( getDiferentaMinute(d.DATA_REZOLVARE, d.DATA_ANUNTARE)), \n" :
                            "    SUM( d.DURATA_REZOLVARE ), \n") +
                    "    SUM( 0), \n"+
                    "    SUM( 0) \n"+
                    " FROM deranjamente d \n"
    ).setGroupStatement(
            " GROUP BY d.mediul, TO_CHAR( d.DATA_ANUNTARE, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN i.MEDIU = 1 THEN 'U' \n" ).append(
            " WHEN i.MEDIU = 2 THEN 'R' \n" ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_JT > 0 THEN 1 ELSE 0 END ) NR_INTR_JT,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_MT > 0 THEN 1 ELSE 0 END ) NR_INTR_MT,\n" ).append(
            "    SUM( CASE WHEN i.UTILIZATORI_IT > 0 THEN 1 ELSE 0 END ) NR_INTR_IT \n" ).append(
            " FROM incidente i \n");

    ReportStatement stIntriIncidenteDinFi = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.MANUAL
    ).setGroupStatement(
            " GROUP BY i.mediu, TO_CHAR( i.DATA, 'MM')"
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "    si.MEDIU, \n" ).append(
            "    TO_CHAR( i.DATA, 'MM') LUNA, \n" ).append(
            "    SUM( CASE WHEN si.PE_JT > 0 THEN 1 ELSE 0 END ) AS NR_INTR_JT, \n" ).append(
            "    SUM( CASE WHEN si.PE_MT > 0 THEN 1 ELSE 0 END ) AS NR_INTR_MT, \n" ).append(
            "    SUM( CASE WHEN si.PE_IT > 0 THEN 1 ELSE 0 END ) AS NR_INTR_IT  \n" ).append(
            " FROM incidente i INNER JOIN (\n" ).append(
            "    SELECT\n" ).append(
            "        i.ID,\n" ).append(
            "        CASE WHEN lf_info.MEDIU = 1 THEN 'U' \n" ).append(
            "             WHEN lf_info.mediu = 2 THEN 'R' \n" ).append(
            "        END MEDIU,\n" ).append(
            "        count(*) AS NR_REAL_INST,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_JT > 0 THEN 1 ELSE 0 END) AS PE_JT,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_MT > 0 THEN 1 ELSE 0 END) AS PE_MT,\n" ).append(
            "        SUM( CASE WHEN lf_info.NUMAR_CONSUMATORI_IT > 0 THEN 1 ELSE 0 END) AS PE_IT\n" ).append(
            "    FROM incidente i \n" ).append(
            "        INNER JOIN incidente_lf leg ON ( i.ID=leg.INCIDENT_ID )\n" ).append(
            "        INNER JOIN lf_info ON ( leg.COD_LF=lf_info.COD_SAP_LF)\n" ).append(
            "    WHERE lf_info.AN = " ).append( anRaport ).append(
            "    GROUP BY i.ID, lf_info.MEDIU \n" ).append(
            " ) si ON (i.ID = si.ID)");

    final ReportStatement stIntrIncidenteDinNom = new ReportStatement(
        buffer.toString()
    ).addStringCriteria(
            " AND i.MOD_COMPL_CONS = " + IncidentConstants.ConsumatoriCompletati.AUTOMAT +
                    " AND TO_CHAR( i.DATA, 'YYYY') = " + anRaport +
                    " AND si.NR_REAL_INST > 0 "
    ).setGroupStatement(
            " GROUP BY si.mediu, TO_CHAR( i.DATA, 'MM') "
    );

    buffer.delete(0, buffer.length());
    buffer.append(" SELECT \n").append(
            "CASE WHEN d.MEDIUL = 1 THEN 'U' \n" ).append(
            " WHEN d.MEDIUL = 2 THEN 'R' " ).append(
            "END MEDIU, \n" ).append(
            "    TO_CHAR( d.DATA_ANUNTARE, 'MM'), \n" ).append(
            "    SUM( 1), \n").append(
            "    SUM( 0), \n").append(
            "    SUM( 0) \n").append(
            " FROM deranjamente d \n");

    ReportStatement stConsumatoriAfectatiDer = new ReportStatement(
        buffer.toString()
    ).setGroupStatement(
            " GROUP BY d.mediul, TO_CHAR( d.DATA_ANUNTARE, 'MM')"
    );

    stNrIntreruperiLpDinFise.addStringCriteria( " AND TO_CHAR( lp.DATA_START_IC, 'YYYY') = " +  anRaport );
    stNrIntrPlanifRealimLaTimpDinFise.addStringCriteria( " AND TO_CHAR( lp.DATA_START_IC, 'YYYY') = " +  anRaport );
    stDurataIntrerupereLpDinFise.addStringCriteria( " AND TO_CHAR( lp.DATA_START_IC, 'YYYY') = " +  anRaport );
    stConsumatoriAfectatiLP.addStringCriteria( " AND TO_CHAR( lp.DATA_START_IC, 'YYYY') = " +  anRaport );

    stNrIntrFiDinFise.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stNrIntrFiDinNom.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport)
            .addStringCriteria(" AND lfInfo.AN=" + anRaport);
    stNrIntrFD.addStringCriteria( " AND TO_CHAR( d.DATA_ANUNTARE, 'YYYY') = " +  anRaport);

    stNrIntreruperi12DinFise.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stNrIntreruperi12DinNom.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport)
            .addStringCriteria(" AND lfInfo.AN=" + anRaport);
    stNrIntrFD12.addStringCriteria( " AND TO_CHAR( d.DATA_ANUNTARE, 'YYYY') = " +  anRaport);

    stNrIntreruperi24DinFise.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stNrIntreruperi24DinNom.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport)
            .addStringCriteria(" AND lfInfo.AN=" + anRaport);
    stNrIntrFD24.addStringCriteria( " AND TO_CHAR( d.DATA_ANUNTARE, 'YYYY') = " +  anRaport);


    //pt durata medie neplanificate
    stDurataIncidenteDinFi.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stDurataIncidenteDinNom.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stIntriIncidenteDinFi.addStringCriteria( " AND TO_CHAR( i.DATA, 'YYYY') = " +  anRaport);
    stDurataDer.addStringCriteria( " AND TO_CHAR( d.DATA_ANUNTARE, 'YYYY') = " +  anRaport);
    stConsumatoriAfectatiDer.addStringCriteria( " AND TO_CHAR( d.DATA_ANUNTARE, 'YYYY') = " +  anRaport);

    if( fscRaport.getFilialaId() > 0){
        stNrIntreruperiLpDinFise.addOrSetFilter("AND", "lp.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntrPlanifRealimLaTimpDinFise.addOrSetFilter("AND", "lp.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));

        stNrIntrFiDinFise.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntrFiDinNom.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntrFD.addOrSetFilter("AND", "d.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));

        stNrIntreruperi12DinFise.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntreruperi12DinNom.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntrFD12.addOrSetFilter("AND", "d.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));

        stNrIntreruperi24DinFise.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntreruperi24DinNom.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stNrIntrFD24.addOrSetFilter("AND", "d.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));

        //pt durata medie planificate
        stDurataIntrerupereLpDinFise.addOrSetFilter("AND", "lp.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stConsumatoriAfectatiLP.addOrSetFilter("AND", "lp.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));

        //pt durata medie neplanificate
        stDurataIncidenteDinFi.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stDurataIncidenteDinNom.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stIntriIncidenteDinFi.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stIntrIncidenteDinNom.addOrSetFilter("AND", "i.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stDurataDer.addOrSetFilter("AND", "d.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
        stConsumatoriAfectatiDer.addOrSetFilter("AND", "d.FILIALA_ID", "=", new Long( fscRaport.getFilialaId()));
    }

    if( fscRaport.getSucursalaId() > 0){
        stNrIntreruperiLpDinFise.addOrSetFilter("AND", "lp.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntrPlanifRealimLaTimpDinFise.addOrSetFilter("AND", "lp.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));

        stNrIntrFiDinFise.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntrFiDinNom.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntrFD.addOrSetFilter("AND", "d.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));

        stNrIntreruperi12DinFise.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntreruperi12DinNom.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntrFD12.addOrSetFilter("AND", "d.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));

        stNrIntreruperi24DinFise.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntreruperi24DinNom.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stNrIntrFD24.addOrSetFilter("AND", "d.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));

        //pt durata medie planificate
        stDurataIntrerupereLpDinFise.addOrSetFilter("AND", "lp.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stConsumatoriAfectatiLP.addOrSetFilter("AND", "lp.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));


        //pt durata medie neplanificate
        stDurataIncidenteDinFi.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stDurataIncidenteDinNom.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stIntriIncidenteDinFi.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stIntrIncidenteDinNom.addOrSetFilter("AND", "i.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stDurataDer.addOrSetFilter("AND", "d.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
        stConsumatoriAfectatiDer.addOrSetFilter("AND", "d.SUCURSALA_ID", "=", new Long( fscRaport.getSucursalaId()));
    }

    if( fscRaport.getCentruId() > 0){
        stNrIntreruperiLpDinFise.addOrSetFilter("AND", "lp.CENTRU_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntrPlanifRealimLaTimpDinFise.addOrSetFilter("AND", "lp.CENTRU_ID", "=", new Long( fscRaport.getCentruId()));

        stNrIntrFiDinFise.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntrFiDinNom.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntrFD.addOrSetFilter("AND", "d.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));

        stNrIntreruperi12DinFise.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntreruperi12DinNom.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntrFD12.addOrSetFilter("AND", "d.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));

        stNrIntreruperi24DinFise.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntreruperi24DinNom.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stNrIntrFD24.addOrSetFilter("AND", "d.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));

        //pt durata medie planificate
        stDurataIntrerupereLpDinFise.addOrSetFilter("AND", "lp.CENTRU_ID", "=", new Long( fscRaport.getCentruId()));
        stConsumatoriAfectatiLP.addOrSetFilter("AND", "lp.CENTRU_ID", "=", new Long( fscRaport.getCentruId()));

        //pt durata medie neplanificate
        stDurataIncidenteDinFi.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stDurataIncidenteDinNom.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stIntriIncidenteDinFi.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stIntrIncidenteDinNom.addOrSetFilter("AND", "i.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stDurataDer.addOrSetFilter("AND", "d.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
        stConsumatoriAfectatiDer.addOrSetFilter("AND", "d.SECTIE_ID", "=", new Long( fscRaport.getCentruId()));
    }

    List mNrIntreruperiLpDinFise = stNrIntreruperiLpDinFise.getListOfArrays("Get numar lucrari");
    List mNrIntreruperiLpRealimLaTimp = stNrIntrPlanifRealimLaTimpDinFise.getListOfArrays("Get numar lucrari realimentate la timp");

    List mNrIntrFiDinFise = stNrIntrFiDinFise.getListOfArrays("Get numar intreruperi din Fise de incident");
    List mNrIntrFiDinNom  = stNrIntrFiDinNom.getListOfArrays("Get numar intreruperi din Nomenclator");
    List mNrIntrFD  = stNrIntrFD.getListOfArrays("Get numar intreruperi din FD");

    List mNrIntrFiDinFise12 = stNrIntreruperi12DinFise.getListOfArrays("Get numar intreruperi 12 fin Fise");
    List mNrIntrFiDinNom12 = stNrIntreruperi12DinNom.getListOfArrays("Get numar intreruperi 12 din Nom");
    List mNrIntrFD12 = stNrIntrFD12.getListOfArrays("Get numar intreruperi 12 din FD");

    List mNrIntrFiDinFise24 = stNrIntreruperi24DinFise.getListOfArrays("Get numar intreruperi 24 din Fise");
    List mNrIntrFiDinNom24 = stNrIntreruperi24DinNom.getListOfArrays("Get numar incidente 24");
    List mNrItrFD24 = stNrIntrFD24.getListOfArrays("Get numar intreruperi 24 din FD");

    //pt durata medie planificate
    List m_dm_DurataLpDinFise = stDurataIntrerupereLpDinFise.getListOfArrays("Get durata intreruperi LP");
    List m_dm_ConsLpDinFise   = stConsumatoriAfectatiLP.getListOfArrays("Get cons. af. LP din fise");

    //pt durata medie neplanificate 
    List m_dm_DurataIncidenteDinFise = stDurataIncidenteDinFi.getListOfArrays("Get durata incidente din FI");
    List m_dm_DurataIncidenteDinNom = stDurataIncidenteDinNom.getListOfArrays("Get durata incidente din Nom");
    List m_dm_DurataDer = stDurataDer.getListOfArrays("Get durata FD");

    List m_dm_ConsFiDinFise = stIntriIncidenteDinFi.getListOfArrays("Nr cons af Fi din Fi");
    List m_dm_ConsFiDinNom = stIntrIncidenteDinNom.getListOfArrays("Nr Intr Fi din Nom");
    List m_dm_ConsDinFD = stConsumatoriAfectatiDer.getListOfArrays("Nr cons af. D");

    //inserting data for xls export
    List dataToExport = new LinkedList();

    LOG.info("MATRICI:");

    printMatrix( m_dm_DurataLpDinFise, "m_dm_DurataLpDinFise");

    printMatrix( m_dm_ConsLpDinFise, "m_dm_ConsLpDinFise");

    printMatrix(m_dm_DurataIncidenteDinFise, "m_dm_DurataIncidenteDinFise");

    printMatrix( m_dm_DurataIncidenteDinNom, "nm_dm_DurataIncidenteDinNom");

    printMatrix( m_dm_DurataDer, "nm_dm_DurataDer");

    printMatrix(m_dm_ConsFiDinFise, "nm_dm_ConsFiDinFise");

    printMatrix( m_dm_ConsFiDinNom, "nm_dm_ConsFiDinNom");

    printMatrix( m_dm_ConsDinFD, "nm_dm_ConsDinFD");
%>


<style type="text/css">
    .clasu{
        width:50px;
        text-align:right;
    }
</style>

<TABLE border=0 cellPading=0 cellSpacing=0 width="100%">
    <TR>
        <TD>
            <%@ include file="../header-localizare.jsp"%>
            <span class="slash">/</span> <A class="link" href="<%=AppConstants.APP_NAME%>?action=rapoarte">Rapoarte</A>
            <span class="slash">/</span> Monitorizarea întreruperilor de lungă durată
        </TD>
    </TR>
</TABLE>


<div class="spacer">&nbsp;</div>

<form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=raport-monitorizare-intreruperi" method="post">
<table class="gsaptd" width="100%">
    <tr>
        <td align="left">
            <emid:filtru-localizare/>
        </td>
        <td align="right">
            <input type="hidden" name="toExport" value="0">
            <input type="hidden" name="run_report" value="1"/>

            <% Integer[] ani = ro.emid.common.utils.Util.addCurrentYear(IncidentAccessDB.getAniProducereIncident("")); %>

            <label for="an">An:</label>

            <select id="an" name="an">
                <% for (Integer an : ani) { %>
                    <option value="<%=an%>" <%=anRaport == an ? "selected" : ""%> ><%=an%></option>
                <% } %>
            </select>

            <input type="submit" value="Rulează" /> &nbsp;
            <input type="button" value="Export" onclick="document.zaforma.toExport.value = '1'; document.zaforma.submit();"/>
        </td>
    </tr>
</table>
</form>


<div class="spacer">&nbsp;</div>



<% if( editMode){ %>
<form action="<%=AppConstants.APP_NAME%>" method="post">
<input type="hidden" name="action" value="saveRC">
<input type="hidden" name="cid" value="<%=rc.getId()%>">
<input type="hidden" name="can" value="<%=rc.getAn()%>">
<input type="hidden" name="ctip" value="<%=rc.getTip()%>">
<% } %>

<table border="1" bgcolor="white" width="100%" style="font-size:12px">
<col align="center">
<col align="left">
<col align="center">
<thead>
<tr>
    <th rowspan="2" class="gsapthead">Nr. crt.</th>
    <th rowspan="2"  colspan="2" class="gsapthead">Indicatori de performanta</th>
    <th colspan="13" class="gsapthead">Valoare realizata lunar/anual</th>
</tr>
<tr>
    <th class="gsapthead">1</th>
    <th class="gsapthead">2</th>
    <th class="gsapthead">3</th>
    <th class="gsapthead">4</th>
    <th class="gsapthead">5</th>
    <th class="gsapthead">6</th>
    <th class="gsapthead">7</th>
    <th class="gsapthead">8</th>
    <th class="gsapthead">9</th>
    <th class="gsapthead">10</th>
    <th class="gsapthead">11</th>
    <th class="gsapthead">12</th>
    <th class="gsapthead">Anual</th>
    <% if( editMode){ %>
    <th class="gsapthead">C. <input type="submit" value="S"></th>
    <% } %>
</tr>
</thead>

<%
    int i = 0, k = 0;
    double c = 0;
%>

<tbody>
<!-- mediu urban -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">Numarul de intreruperi planificate in mediul urban</td>
    <td><b>JT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "U", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<!-- mediul  rural -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">Numarul de intreruperi planificate in mediul rural</td>
    <td><b>JT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum(mNrIntreruperiLpDinFise, null, null, "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpDinFise, null, null, "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>


<!-- urban -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi planificate in mediul urban pentru care
        restabilirea alimentarii cu energie electrica s-a realizat in timpul planificat anuntat
    </td>
    <td><b>JT</b></td>
    <%=printRowSum( mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum( mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum( mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<!-- rural -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi planificate in mediul rural pentru care
        restabilirea alimentarii cu energie electrica s-a realizat in timpul planificat anuntat
    </td>
    <td><b>JT</b></td>
    <%=printRowSum(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntreruperiLpRealimLaTimp, null, null, "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<!-- urban -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Durata medie a unei intreruperi planificate in mediul urban
    </td>
    <td><b>JT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_MT, c = getConstanta(++k, rc) )%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<!-- rural -->
<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Durata medie a unei intreruperi  planificate in mediul rural
    </td>
    <td><b>JT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowDurataMedie(
            m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataLpDinFise, null, null,
            m_dm_ConsLpDinFise, null, null,
            "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>


<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi neplanificate in mediul urban
    </td>
    <td><b>JT</b></td>
    <%=printRowSum( mNrIntrFiDinFise, mNrIntrFiDinNom, mNrIntrFD, "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, mNrIntrFD, "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum( mNrIntrFiDinFise, mNrIntrFiDinNom, null, "U", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, null, "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum (mNrIntrFiDinFise, mNrIntrFiDinNom, null, "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, null, "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi neplanificate in mediul rural
    </td>
    <td><b>JT</b></td>
    <%=printRowSum (mNrIntrFiDinFise, mNrIntrFiDinNom, mNrIntrFD, "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, mNrIntrFD, "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum (mNrIntrFiDinFise, mNrIntrFiDinNom, null, "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, null, "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum (mNrIntrFiDinFise, mNrIntrFiDinNom, null, "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise, mNrIntrFiDinNom, null, "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi neplanificate cu durata mai mica de 12 ore,  pentru mediul urban
    </td>
    <td><b>JT</b></td>
    <%=printRowSum(mNrIntrFiDinFise12, mNrIntrFiDinNom12, mNrIntrFD12, "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise12, mNrIntrFiDinNom12, mNrIntrFD12, "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum(mNrIntrFiDinFise12, mNrIntrFiDinNom12, null, "U", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise12, mNrIntrFiDinNom12, null, "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum(mNrIntrFiDinFise12, mNrIntrFiDinNom12, null, "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise12, mNrIntrFiDinNom12, null, "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<tr>
    <td rowspan="3"><%=++i%></td>
    <td rowspan="3">
        Numarul de intreruperi neplanificate cu durata mai mica de 24 ore, pentru mediul rural
    </td>
    <td><b>JT</b></td>
    <%=printRowSum(mNrIntrFiDinFise24, mNrIntrFiDinNom24, mNrItrFD24, "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise24, mNrIntrFiDinNom24, mNrItrFD24, "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowSum(mNrIntrFiDinFise24, mNrIntrFiDinNom24, null, "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise24, mNrIntrFiDinNom24, null, "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowSum(mNrIntrFiDinFise24, mNrIntrFiDinNom24, null, "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printToExport(mNrIntrFiDinFise24, mNrIntrFiDinNom24, null, "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>

<tr>
    <td rowspan="3" ><%=++i%></td>
    <td rowspan="3" >
        Durata medie a unei intreruperi  neplanificate, pentru mediul urban
    </td>
    <td><b>JT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "U", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td rowspan="3" ><%=++i%></td>
    <td rowspan="3" >
        Durata medie a unei intreruperi  neplanificate, pentru mediul rural
    </td>
    <td><b>JT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_JT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_JT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>MT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_MT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_MT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
<tr>
    <td><b>IT</b></td>
    <%=printRowDurataMedie(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_IT, c = getConstanta(++k, rc))%>
    <%dataToExport.add(printRowDurataToExport(m_dm_DurataIncidenteDinFise, m_dm_DurataIncidenteDinNom, m_dm_DurataDer, m_dm_ConsFiDinFise, m_dm_ConsFiDinNom, m_dm_ConsDinFD,
            "R", CI_IT, c = getConstanta(k, rc)));%>
    <% if( editMode){%>
    <td>
        <input type="text" class="clasu" name="c<%=k%>" value="<%=c%>">
    </td>
    <% } %>
</tr>
</tbody>
</table>

<% if( editMode){ %>
</form>
<% } %>



<div style="font-size:10px">
    Nota: <br>
    <li> Pentru intreruperile planificate datele sunt luate din Fisele de Lucrari Programate. <br/>
    <li> Pentru intreruperile neplanificate datele sunt luate dupa cum urmeaza: <br/>
        &nbsp;&nbsp;&nbsp; - din Fisele de Incident daca consumatorii sunt completate manual. <br/>
        &nbsp;&nbsp;&nbsp; - din nomenclatorul Numar Consumatori daca consumatorii sunt completate automat prin selectia de instalatii afectate in fereastra Browse. <br/>
        &nbsp;&nbsp;&nbsp; - din Fisele de Deranjament. <br/>
    <li> Pentru calculul numarului de intreruperi se considera <u>o intrerupere fa fiind un consumator afectat.</u>, completat in fisa. <br/>
    <li> Pentru calculul duratei medii unei intreruperi se iau in consideratie <u>numarul de fise</u> (incident, deranjament sau lucrare programata) si <u>durata de intrerupere</u> din acestea. <br/>
</div>


<style type="text/css">
    .luna1, .luna2, .luna12{
        background-color:#eaeafc;
    }

    .luna3, .luna4, .luna5{
        background-color:#e4feca;
    }

    .luna6, .luna7, .luna8{
        background-color:#ccff99;
    }

    .luna9, .luna10, .luna11{
        background-color:#ffeddb;
    }

    .anual{

    }
</style>


<%
    long tStop = System.currentTimeMillis();
%>

<%
    String publicPath = null;
    if( ObjectUtils.getIntValue(request.getParameter("toExport"), 0) == 1){
        String root = AppConstants.PUBLIC_RESOURCE_PATH + File.separator + "raport";
        SimpleDateFormat _df = new SimpleDateFormat("dd.MM.yyyy");
        String folder = _df.format( new java.util.Date());
        //cleanup...
        File[]folders = new File( root).listFiles();
        if( folders != null) for (int z = 0; z < folders.length; z++) {
            File file = folders[z];
            if( !file.getName().equals( folder)){
                File[]d=file.listFiles();
                if( d!=null) for (int j = 0; j < d.length; j++) {
                    File file1 = d[j];
                    file1.delete();
                }
                file.delete( );  //cleanup
            }
        }

        File f = new File( root, folder);
        if (!f.exists()) f.mkdir();

        String fileName = "Raport monitorizare intreruperi - " + System.currentTimeMillis() + ".xls";
        String privatePath = root + File.separator + folder + File.separator + fileName;
        String templatePath = GlobalAppConstants.PRIVATE_PATH + File.separator + "template" + File.separator + "Raport Intreruperi Lunga Durata.xls";

        publicPath = AppConstants.WEB_RESOURCE_PATH + "/raport/" + folder + "/" + fileName;

        f = new File(privatePath);
        if (f.exists()) {
            f.delete();
        }

        RaportMonitorizareItreruperiExcelExporter exporter = new RaportMonitorizareItreruperiExcelExporter();
        exporter.begin(templatePath, privatePath);

        try{
            exporter.export(dataToExport);
        } catch(Throwable th){
            LOG.debug("de aici sare - " + th);
        }

        exporter.end();
    }
%>

<%! private final int CI_JT = 2;%>
<%! private final int CI_MT = 3;%>
<%! private final int CI_IT = 4;%>

<%!
    /**
     Afisarea unei linii. Se face suma din fiecare matrice. Daca matricea este null, se ignora.
     */
    private String printRowSum( List mIntreruperiDinFise, List mIntreruperiDinNom, List mIntreruperiDinFD, String mediu, int ciNivelTensiune, double c){

        StringBuffer out = new StringBuffer();

        int anual = 0;

        for( int luna = 1; luna <= 12; luna++){

            //lunar
            int lunar =
                    getIntValue( mIntreruperiDinFise, mediu, luna, ciNivelTensiune) +
                            (mIntreruperiDinNom != null ? getIntValue( mIntreruperiDinNom, mediu, luna, ciNivelTensiune) : 0) +
                            (mIntreruperiDinFD  != null ? getIntValue( mIntreruperiDinFD, mediu, luna, ciNivelTensiune) : 0) ;

            anual += lunar;

            out.append("<td align=\"right\" width=\"40\" class=\"luna" + luna + "\">");

            out.append( (int) applyC( lunar, c));

            out.append( "</td>");
        }

        //anual
        out.append( "<td align=\"right\" class=\"anual\">");

        out.append( (int) applyC( anual, c));

        out.append( "</td>");


        return out.toString();

    }%>

<%!
    private List printToExport(List mIntreruperiDinFise, List mIntreruperiDinNom, List mIntreruperiDinFD, String mediu, int ciNivelTensiune, double c) {

        List out = new LinkedList();

        int anual = 0;

        for (int luna = 1; luna <= 12; luna++) {

            //lunar
            int lunar =
                    getIntValue(mIntreruperiDinFise, mediu, luna, ciNivelTensiune) +
                            (mIntreruperiDinNom != null ? getIntValue(mIntreruperiDinNom, mediu, luna, ciNivelTensiune) : 0) +
                            (mIntreruperiDinFD != null ? getIntValue(mIntreruperiDinFD, mediu, luna, ciNivelTensiune) : 0);

            anual += lunar;

            out.add(AppConstants.format3(applyC(lunar, c)));
        }

        //anual
        out.add(AppConstants.format3(applyC(anual, c)));
        return out;
    }
%>


<%! private String printRowDurataMedie(
        List m_dm_DurataIntrerupereDinFise,
        List m_dm_DurataIntrerupereDinNom,
        List m_dm_DurataIntrerupereDer,
        List m_dm_ConsAfectatiDinFise,
        List m_dm_ConsAfectatiDinNom,
        List m_dm_ConsAfectatiDinFD,
        String mediu,
        int ciNivelTensiune, double c){


    StringBuffer out = new StringBuffer();

    try{


        long durataIntrerupereAnual  = 0;
        long anual = 0;

        for( int luna = 1; luna <= 12; luna++){

            //lunar
            long durataIntrerupere =
                    getIntValue( m_dm_DurataIntrerupereDinFise, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_DurataIntrerupereDinNom, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_DurataIntrerupereDer, mediu, luna, ciNivelTensiune);

            long numarConsumatori =
                    getIntValue( m_dm_ConsAfectatiDinFise, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_ConsAfectatiDinNom, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_ConsAfectatiDinFD, mediu, luna, ciNivelTensiune);


            long lunar = numarConsumatori != 0 ? durataIntrerupere/numarConsumatori : 0;

            lunar = (int)applyC(lunar, c);

            durataIntrerupereAnual += lunar;

            out.append("<td align=\"right\" width=\"40\" class=\"luna" + luna + "\">");


            if( lunar == 0){
                out.append(0);
            }else{
                out.append( lunar/60 ).append( "h ").append( lunar%60).append("'");
            }

            out.append( "</td>");
        }

        //anual
        anual = durataIntrerupereAnual / 12;

        out.append( "<td align=\"right\" class=\"anual\">");

        if( anual == 0){
            out.append(0);
        }else{
            out.append( anual/60 ).append( "h ").append( anual%60).append("'");
        }

        out.append( "</td>");

    }catch ( Throwable th){
        LOG.error("eroare la calcul", th);
    }
    return out.toString();

}%>

<%! private List printRowDurataToExport(
        List m_dm_DurataIntrerupereDinFise,
        List m_dm_DurataIntrerupereDinNom,
        List m_dm_DurataIntrerupereDer,
        List m_dm_ConsAfectatiDinFise,
        List m_dm_ConsAfectatiDinNom,
        List m_dm_ConsAfectatiDinFD,
        String mediu,
        int ciNivelTensiune, double c){


    List out = new LinkedList();

    try{


        long durataIntrerupereAnual  = 0;
        long anual = 0;

        for( int luna = 1; luna <= 12; luna++){

            //lunar
            long durataIntrerupere =
                    getIntValue( m_dm_DurataIntrerupereDinFise, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_DurataIntrerupereDinNom, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_DurataIntrerupereDer, mediu, luna, ciNivelTensiune);

            long numarConsumatori =
                    getIntValue( m_dm_ConsAfectatiDinFise, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_ConsAfectatiDinNom, mediu, luna, ciNivelTensiune) +
                            getIntValue( m_dm_ConsAfectatiDinFD, mediu, luna, ciNivelTensiune);


            long lunar = numarConsumatori != 0 ? durataIntrerupere/numarConsumatori : 0;

            lunar = (int)applyC(lunar, c);

            durataIntrerupereAnual += lunar;


            if( lunar == 0){
                out.add("0");
            }else{
                out.add(lunar / 60 + "h " + lunar % 60 + "'");
            }
        }

        //anual
        anual = durataIntrerupereAnual / 12;

        if( anual == 0){
            out.add("0");
        }else{
            out.add(anual / 60 + "h " + anual % 60 + "'");
        }

    }catch ( Throwable th){
        LOG.error("eroare la calcul", th);
    }
    return out;
}%>


<%! private double getDoubleValue( List matrix, String mediu, int luna, int nivelTensiune){
    return ro.emid.common.utils.Util.getIntValue(getValue(matrix, mediu, luna, nivelTensiune), 0);
}%>

<%! private int getIntValue( List matrix, String mediu, int luna, int nivelTensiune){
    return ro.emid.common.utils.Util.getIntValue(getValue(matrix, mediu, luna, nivelTensiune), 0);
}%>

<%!
    /**
     Matrix de forma: mediu, luna, JT, MT, IT
     */
    private Object getValue( List matrix, String mediu, int luna, int ciNivelTensiune ){
        Object result = null;
        if( matrix == null ) return null;
        try {
            for (int i = 0; i < matrix.size(); i++) {
                Object arr[] = (Object[]) matrix.get(i);

                String _mediu = ro.emid.common.utils.Util.getNullSafe((String) (arr[0]));
                int _luna = ro.emid.common.utils.Util.getIntValue(arr[1], -1);

                Object v = arr[ ciNivelTensiune];

                if ( mediu.equals( _mediu) && luna == _luna){
                    return v;
                }
            }
        } catch (Exception e) {
            LOG.error(e);
        }
        return result;
    }%>

<%! private void printMatrix(List matrix, String matrixName){
    StringBuffer buff = new StringBuffer();
    buff.append("\n\nMatrice: ").append( matrixName).append("\n");

    for (int i = 0; i < matrix.size(); i++) {
        Object arr[] = (Object[]) matrix.get(i);
        for (int j = 0; j < arr.length; j++) {
            Object o = arr[j];
            buff.append( o).append(", ");
        }
        buff.append("\n");
    }

    LOG.info( buff);
}%>

<%
    long dataStop = System.currentTimeMillis();
    LOG.debug("Durata generare raport: " + (dataStop - dataStart));
%>

<%!
    private double applyC( double indicator, double c){
        if( showTestValues) {
            indicator = 1;
        }

        indicator *= c;
        return indicator;
    }

    public double getConstanta( int index, RC rc){
        switch ( index){
            case 1: return rc.getC1();
            case 2: return rc.getC2();
            case 3: return rc.getC3();
            case 4: return rc.getC4();
            case 5: return rc.getC5();
            case 6: return rc.getC6();
            case 7: return rc.getC7();
            case 8: return rc.getC8();
            case 9: return rc.getC9();
            case 10: return rc.getC10();
            case 11: return rc.getC11();
            case 12: return rc.getC12();
            case 13: return rc.getC13();
            case 14: return rc.getC14();
            case 15: return rc.getC15();
            case 16: return rc.getC16();
            case 17: return rc.getC17();
            case 18: return rc.getC18();
            case 19: return rc.getC19();
            case 20: return rc.getC20();
            case 21: return rc.getC21();
            case 22: return rc.getC22();
            case 23: return rc.getC23();
            case 24: return rc.getC24();
            case 25: return rc.getC25();
            case 26: return rc.getC26();
            case 27: return rc.getC27();
            case 28: return rc.getC28();
            case 29: return rc.getC29();
            case 30: return rc.getC30();
            case 31: return rc.getC31();
            case 32: return rc.getC32();
            case 33: return rc.getC33();
            case 34: return rc.getC34();
            case 35: return rc.getC35();
            case 36: return rc.getC36();
            case 37: return rc.getC37();
            case 38: return rc.getC38();
            case 39: return rc.getC39();
            case 40: return rc.getC40();
            case 41: return rc.getC41();
            case 42: return rc.getC42();
            case 43: return rc.getC43();
            case 44: return rc.getC44();
            case 45: return rc.getC45();
            case 46: return rc.getC46();
            case 47: return rc.getC47();
            case 48: return rc.getC48();
            case 49: return rc.getC49();
            case 50: return rc.getC50();
        }

        return 1;
        //todo
//        throw new RuntimeException("Index invalid");
    }
%>

<span style="float:right; font-size:10px"><%=(dataStop - dataStart)%></span>


<script type="text/javascript">
    <% if ( publicPath != null){ %>

    setTimeout(function(){
        window.location = '<%=publicPath%>';
    }, 500);
    <% } %>
</script>