<?php

/*****************************************************************************************************************************************
 * DEV: CONTRALORIA DE ESTADOS
 * PROYECTO: SISTEMA INTEGRAL ADMINISTRATIVO DE CONTRALORIAS DE ESTADOS
 * MODULO: index
 * PROCESO: index del sistema
 * PROGRAMADORES:
 * | # |          NOMBRES Y APELLIDOS              |               CORREO               |          TELEFONO              |
 * | 1 |          Daniel Muñoz                     |d.munoz@contraloriamonagas.gob.ve   |         0412-8358676           |
 * |   |                                           |                                    |                                |
 * |___|___________________________________________|____________________________________|________________________________|
 *
 * VERSION
 *
 * |          PROGRAMADOR                  |          FECHA          |       VERSION      |
 * |               #1                      |        27-08-2015       |         1.0        |
 * |                                       |                         |                    |
 * |_______________________________________|_________________________|____________________|
 *
 *****************************************************************************************************************************************/
require_once RUTA_Modulo . DS . 'modNM/controladores/FuncionesNomina/funcionesNominaControlador.php';

class nominaConsolidadaControlador extends Controlador
{
    use funcionesNomina {
        funcionesNomina::__construct as private __fnConstruct;
    }

    private $atFPDF;
    private $atEjecucionProcesos;
    private $atConsultasComunes;
    private $atTipoNomina;

    public function __construct()
    {
        parent::__construct();
        $this->__fnConstruct();
        $this->atEjecucionProcesos = $this->metCargarModelo('ejecucionProcesos', 'procesos');
        $this->atConsultasComunes = $this->metCargarModelo('consultasComunes', 'reportes');
        $this->atTipoNomina = $this->metCargarModelo('tipoNomina', 'maestros');
        $this->metObtenerLibreria('headerNominaConsolidada', 'modNM');
        $this->atFPDF = new pdfNominaConsolidada('L', 'mm', array(216, 595));//595
    }

    public function metIndex()
    {
        $complementosJs = array(
            'bootstrap-datepicker/bootstrap-datepicker',
        );
        $complementoCss = array(
            'bootstrap-datepicker/datepicker'
        );
        $js[] = 'Aplicacion/appFunciones';

        $this->atVista->metCargarCssComplemento($complementoCss);
        $this->atVista->metCargarJsComplemento($complementosJs);
        $this->atVista->metCargarJs($js);

        $yearActual=date('Y');
        $this->atVista->assign('yearActual', $yearActual);
        $this->atVista->assign('tipoNomina', $this->atTipoNomina->metListarTipoNomina(1));
        
        $this->atVista->metRenderizar('index');
    }


    public function metPdfNominaConsolidada($idPeriodoProceso)
    {
        $nomina=$this->atEjecucionProcesos->metObtenerProceso($idPeriodoProceso);
        if($nomina['cod_proceso']=='PS' || $nomina['cod_proceso']=='PSFM' || $nomina['cod_proceso']=='APPEN' || $nomina['cod_proceso']=='APJUB' || $nomina['cod_proceso']=='PSAA'  )
        {
            $empleado = $this->atConsultasComunes->metEmpleadosNominaSobreviviente($idPeriodoProceso);
        }else {
            $empleado = $this->atConsultasComunes->metEmpleadosNomina($idPeriodoProceso);
        }
        for ($i = 0; $i < count($empleado); $i++) {
            $empleados[$i] = array(
                'idEmpleado' => $empleado[$i]['pk_num_empleado'],
                'ci' => $empleado[$i]['ind_cedula_documento'],
                'nombre' => $empleado[$i]['ind_nombre1'] . ' ' . $empleado[$i]['ind_nombre2'] . ' ' . $empleado[$i]['ind_apellido1'] . ' ' . $empleado[$i]['ind_apellido2'],
            );
            $empleados[$i]['asignaciones'] = $this->atConsultasComunes->metEmpleadosAsignaciones($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codAsignaciones = $empleados[$i]['asignaciones'];
            $totalAsignacionesEmpleado = 0;
            foreach ($codAsignaciones AS $codAsignacion) {
                $empleados[$i]['codAsignaciones'][] = $codAsignacion['cod_concepto'];
                $totalAsignacionesEmpleado = $totalAsignacionesEmpleado + $codAsignacion['num_monto'];
            }
            $empleados[$i]['totalAsignacionesEmpleado'] = $totalAsignacionesEmpleado;

            $empleados[$i]['deducciones'] = $this->atConsultasComunes->metEmpleadosDeducciones($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codDeducciones = $empleados[$i]['deducciones'];
            $totalDeduccionesEmpleado = 0;
            foreach ($codDeducciones AS $codDeduccion) {
                $empleados[$i]['codDeducciones'][] = $codDeduccion['cod_concepto'];
                $totalDeduccionesEmpleado = $totalDeduccionesEmpleado + $codDeduccion['num_monto'];
            }
            $empleados[$i]['totalDeduccionesEmpleado'] = $totalDeduccionesEmpleado;

            $empleados[$i]['aportes'] = $this->atConsultasComunes->metEmpleadosAportes($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codAportes = $empleados[$i]['aportes'];
            $totalAportesEmpleado = 0;
            foreach ($codAportes AS $codAporte) {
                $empleados[$i]['codAporte'][] = $codAporte['cod_concepto'];
                $totalAportesEmpleado = $totalAportesEmpleado + $codAporte['num_monto'];
            }
            $empleados[$i]['totalAportesEmpleado'] = $totalAportesEmpleado;
        }
        $tituloAsignaciones = $this->atConsultasComunes->metTituloAsignaciones($idPeriodoProceso);
        $tituloDeducciones = $this->atConsultasComunes->metTituloDeducciones($idPeriodoProceso);
        $tituloAportes = $this->atConsultasComunes->metTituloAportes($idPeriodoProceso);

        #### PDF ####
        $this->atFPDF->SetTipoNom(utf8_decode($nomina['ind_titulo_boleta']));
        $this->atFPDF->SetProceso(utf8_decode($nomina['ind_nombre_proceso']));

        $this->atFPDF->setDesde(date('d-m-Y', strtotime($nomina['fec_desde'])));
        $this->atFPDF->setHasta(date('d-m-Y', strtotime($nomina['fec_hasta'])));
        $this->atFPDF->setTipoHeader('nomina');

        $this->atFPDF->AliasNbPages();
        $this->atFPDF->SetMargins(10, 5, 5);
        $this->atFPDF->SetAutoPageBreak(10);
        $this->atFPDF->AddPage();
        $this->atFPDF->SetDrawColor(255, 255, 255);
        $this->atFPDF->SetFillColor(255, 255, 255);
        $this->atFPDF->SetTextColor(0, 0, 0);

####### ASIGNACIONES ########
        if ($tituloAsignaciones) {
            $this->atFPDF->SetFont('Arial', 'B', 8);
            $this->atFPDF->Cell(190, 5, ("ASIGNACIONES"), 0, 1, 'C');
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 6);
            $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
            foreach ($tituloAsignaciones AS $titulos) {
                $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
            }
            $this->atFPDF->Cell(21, 6, ('T.ASIG.'), 1, 1, 'R', 1);
            $ln = 0;
            $N = 1;
            foreach ($empleados AS $empleado) {
                if ($empleado['asignaciones']) {
                    $ln++;
                    $y = $this->atFPDF->GetY();
                    if ($y > 170) {
                        $this->atFPDF->AddPage();
                        $this->atFPDF->SetFont('Arial', 'B', 8);
                        $this->atFPDF->Cell(190, 5, ("ASIGNACIONES"), 0, 1, 'C');
                        $this->atFPDF->SetFillColor(200, 200, 200);
                        $this->atFPDF->SetFont('Arial', 'B', 6);
                        $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
                        $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
                        $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
                        foreach ($tituloAsignaciones AS $titulos) {
                            $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
                        }
                        $this->atFPDF->Cell(21, 6, ('T.ASIG.'), 1, 1, 'R', 1);
                    } else {
                        $this->atFPDF->SetY($y);
                    }
                    if ($ln % 2 == 0) {
                        $this->atFPDF->SetFillColor(240, 240, 240);
                    } else {
                        $this->atFPDF->SetFillColor(255, 255, 255);
                    }
                    $this->atFPDF->SetFont('Arial', '', 7);
                    $this->atFPDF->Cell(7, 6, $N++, 1, 0, 'C', 1);
                    $this->atFPDF->Cell(15, 6, number_format($empleado['ci'], 0, '', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Cell(60, 6, utf8_decode($empleado['nombre']), 1, 0, 'L', 1);
                    $asignaciones = $empleado['asignaciones'];
                    $codAsignaciones = $empleado['codAsignaciones'];
                    $total = 0;

                    foreach ($tituloAsignaciones AS $titulos) {
                        if (in_array($titulos['cod_concepto'], $codAsignaciones)) {
                            foreach ($asignaciones AS $asignacion) {
                                if ($titulos['cod_concepto'] == $asignacion['cod_concepto']) {
                                    $total = $total + $asignacion['num_monto'];
                                    $this->atFPDF->Cell(21, 6, number_format($asignacion['num_monto'], 2, ',', '.'), 1, 0, 'R', 1);

                                }

                            }

                        } else {
                            $this->atFPDF->Cell(21, 6, number_format(0, 2, ',', '.'), 1, 0, 'R', 1);
                        }
                    }


                    $this->atFPDF->SetFont('Arial', 'B', 7);
                    $this->atFPDF->Cell(21, 6, number_format($total, 2, ',', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Ln();

                }


            }
            ### Totales ###
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->Cell(7, 6, '', 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ' ', 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, 'Totales: ', 1, 0, 'R', 1);
            $this->atFPDF->SetFont('Arial', 'B', 7);

            $totalAsignaciones = 0;
            foreach ($tituloAsignaciones AS $titulos) {
                $totalAsignaciones = $totalAsignaciones + $titulos['TOTAL'];
                $this->atFPDF->Cell(21, 6, number_format($titulos['TOTAL'], 2, ',', '.'), 1, 0, 'R', 1);
            }

            $this->atFPDF->Cell(21, 6, number_format($totalAsignaciones, 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Ln(6);
            ### Leyenda ###
            $this->atFPDF->SetDrawColor(255, 255, 255);
            $this->atFPDF->SetTextColor(0, 0, 0);
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 4);
            $this->atFPDF->Cell(30, 5, 'LEYENDA', 1, 0, 'C', 1);
            $this->atFPDF->Cell(50, 5, 'DESCRIPCION', 1, 1, 'C', 1);

            foreach ($tituloAsignaciones AS $titulos) {
                $this->atFPDF->SetFillColor(200, 200, 200);
                $this->atFPDF->SetFont('Arial', 'B', 4);
                $this->atFPDF->Cell(30, 5, utf8_decode($titulos['ind_reporte']), 1, 0, 'L', 1);
                $this->atFPDF->SetFillColor(255, 255, 255);
                $this->atFPDF->SetFont('Arial', '', 4);
                $this->atFPDF->Cell(50, 5, utf8_decode($titulos['ind_impresion']), 1, 1, 'L', 1);
            }

            $this->atFPDF->AddPage();
        }

####### FIN ########
####### DEDUCCIONES ########
        if ($tituloDeducciones) {
            $this->atFPDF->SetFont('Arial', 'B', 8);
            $this->atFPDF->Cell(190, 5, ("DEDUCCIONES"), 0, 1, 'C');
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 6);
            $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
            foreach ($tituloDeducciones AS $titulos) {
                $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
            }
            $this->atFPDF->Cell(21, 6, ('T.DEDUC.'), 1, 1, 'R', 1);
            $ln = 0;
            $N = 1;
            foreach ($empleados AS $empleado) {
                if ($empleado['deducciones']) {
                    $ln++;
                    $y = $this->atFPDF->GetY();
                    if ($y > 170) {
                        $this->atFPDF->AddPage();
                        $this->atFPDF->SetFont('Arial', 'B', 8);
                        $this->atFPDF->Cell(190, 5, ("DEDUCCIONES"), 0, 1, 'C');
                        $this->atFPDF->SetFillColor(200, 200, 200);
                        $this->atFPDF->SetFont('Arial', 'B', 6);
                        $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
                        $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
                        $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
                        foreach ($tituloDeducciones AS $titulos) {
                            $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
                        }
                        $this->atFPDF->Cell(21, 6, ('T.DEDUC.'), 1, 1, 'R', 1);
                    } else {
                        $this->atFPDF->SetY($y);
                    }
                    if ($ln % 2 == 0) {
                        $this->atFPDF->SetFillColor(240, 240, 240);
                    } else {
                        $this->atFPDF->SetFillColor(255, 255, 255);
                    }
                    $this->atFPDF->SetFont('Arial', '', 7);
                    $this->atFPDF->Cell(7, 6, $N++, 1, 0, 'C', 1);
                    $this->atFPDF->Cell(15, 6, number_format($empleado['ci'], 0, '', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Cell(60, 6, utf8_decode($empleado['nombre']), 1, 0, 'L', 1);
                    $deducciones = $empleado['deducciones'];
                    $codDeducciones = $empleado['codDeducciones'];
                    $total = 0;
                    foreach ($tituloDeducciones AS $titulos) {
                        if (in_array($titulos['cod_concepto'], $codDeducciones)) {
                            foreach ($deducciones AS $deduccion) {
                                if ($titulos['cod_concepto'] == $deduccion['cod_concepto']) {
                                    $total = $total + $deduccion['num_monto'];
                                    $this->atFPDF->Cell(21, 6, number_format($deduccion['num_monto'], 2, ',', '.'), 1, 0, 'R', 1);
                                }
                            }
                        } else {
                            $this->atFPDF->Cell(21, 6, number_format(0, 2, ',', '.'), 1, 0, 'R', 1);
                        }
                    }

                    $this->atFPDF->SetFont('Arial', 'B', 7);
                    $this->atFPDF->Cell(21, 6, number_format($total, 2, ',', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Ln();
                }
            }
            ### Totales ###
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->Cell(7, 6, '', 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ' ', 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, 'Totales: ', 1, 0, 'R', 1);
            $this->atFPDF->SetFont('Arial', 'B', 7);
            $totalDeducciones = 0;
            foreach ($tituloDeducciones AS $titulos) {
                $totalDeducciones = $totalDeducciones + $titulos['TOTAL'];
                $this->atFPDF->Cell(21, 6, number_format($titulos['TOTAL'],2,',','.'), 1, 0, 'R', 1);
            }
            $this->atFPDF->Cell(21, 6, number_format($totalDeducciones, 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Ln(6);
            ### Leyenda ###
            $this->atFPDF->SetDrawColor(255, 255, 255);
            $this->atFPDF->SetTextColor(0, 0, 0);
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 4);
            $this->atFPDF->Cell(30, 5, 'LEYENDA', 1, 0, 'C', 1);
            $this->atFPDF->Cell(50, 5, 'DESCRIPCION', 1, 1, 'C', 1);
            foreach ($tituloDeducciones AS $titulos) {
                $this->atFPDF->SetFillColor(200, 200, 200);
                $this->atFPDF->SetFont('Arial', 'B', 4);
                $this->atFPDF->Cell(30, 5, utf8_decode($titulos['ind_reporte']), 1, 0, 'L', 1);
                $this->atFPDF->SetFillColor(255, 255, 255);
                $this->atFPDF->SetFont('Arial', '', 4);
                $this->atFPDF->Cell(50, 5, utf8_decode($titulos['ind_impresion']), 1, 1, 'L', 1);
            }
            $this->atFPDF->AddPage();
        }
####### FIN ########
####### APORTES ########
        if ($tituloAportes) {
            $this->atFPDF->SetFont('Arial', 'B', 8);
            $this->atFPDF->Cell(190, 5, ("APORTES"), 0, 1, 'C');
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 6);
            $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
            foreach ($tituloAportes AS $titulos) {
                $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
            }
            $this->atFPDF->Cell(21, 6, ('T.APORTES.'), 1, 1, 'R', 1);
            $ln = 0;
            $N = 1;
            foreach ($empleados AS $empleado) {
                if ($empleado['aportes']) {
                    $ln++;
                    $y = $this->atFPDF->GetY();
                    if ($y > 170) {
                        $this->atFPDF->AddPage();
                        $this->atFPDF->SetFont('Arial', 'B', 8);
                        $this->atFPDF->Cell(190, 5, ("APORTES"), 0, 1, 'C');
                        $this->atFPDF->SetFillColor(200, 200, 200);
                        $this->atFPDF->SetFont('Arial', 'B', 6);
                        $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
                        $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
                        $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
                        foreach ($tituloAportes AS $titulos) {
                            $this->atFPDF->Cell(21, 6, utf8_decode($titulos['ind_reporte']), 1, 0, 'R', 1);
                        }
                        $this->atFPDF->Cell(21, 6, ('T.APORTES.'), 1, 1, 'R', 1);
                    } else {
                        $this->atFPDF->SetY($y);
                    }
                    if ($ln % 2 == 0) {
                        $this->atFPDF->SetFillColor(240, 240, 240);
                    } else {
                        $this->atFPDF->SetFillColor(255, 255, 255);
                    }
                    $this->atFPDF->SetFont('Arial', '', 7);
                    $this->atFPDF->Cell(7, 6, $N++, 1, 0, 'C', 1);
                    $this->atFPDF->Cell(15, 6, number_format($empleado['ci'], 0, '', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Cell(60, 6, utf8_decode($empleado['nombre']), 1, 0, 'L', 1);
                    $aportes = $empleado['aportes'];
                    $codAporte = $empleado['codAporte'];
                    $total = 0;
                    foreach ($tituloAportes AS $titulos) {
                        if (in_array($titulos['cod_concepto'], $codAporte)) {
                            foreach ($aportes AS $aporte) {
                                if ($titulos['cod_concepto'] == $aporte['cod_concepto']) {
                                    $total = $total + $aporte['num_monto'];
                                    $this->atFPDF->Cell(21, 6, number_format($aporte['num_monto'], 2, ',', '.'), 1, 0, 'R', 1);
                                }
                            }
                        } else {
                            $this->atFPDF->Cell(21, 6, number_format(0, 2, ',', '.'), 1, 0, 'R', 1);
                        }
                    }

                    $this->atFPDF->SetFont('Arial', 'B', 7);
                    $this->atFPDF->Cell(21, 6, number_format($total, 2, ',', '.'), 1, 0, 'R', 1);
                    $this->atFPDF->Ln();
                }
            }
            ### Totales ###
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->Cell(7, 6, '', 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, ' ', 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, 'Totales: ', 1, 0, 'R', 1);
            $this->atFPDF->SetFont('Arial', 'B', 7);
            $totalAportes = 0;
            foreach ($tituloAportes AS $titulos) {
                $totalAportes = $totalAportes + $titulos['TOTAL'];
                $this->atFPDF->Cell(21, 6, number_format($titulos['TOTAL'],2,',','.'), 1, 0, 'R', 1);
            }
            $this->atFPDF->Cell(21, 6, number_format($totalAportes, 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Ln(6);
            ### Leyenda ###
            $this->atFPDF->SetDrawColor(255, 255, 255);
            $this->atFPDF->SetTextColor(0, 0, 0);
            $this->atFPDF->SetFillColor(200, 200, 200);
            $this->atFPDF->SetFont('Arial', 'B', 4);
            $this->atFPDF->Cell(30, 5, 'LEYENDA', 1, 0, 'C', 1);
            $this->atFPDF->Cell(50, 5, 'DESCRIPCION', 1, 1, 'C', 1);
            foreach ($tituloAportes AS $titulos) {
                $this->atFPDF->SetFillColor(200, 200, 200);
                $this->atFPDF->SetFont('Arial', 'B', 4);
                $this->atFPDF->Cell(30, 5, utf8_decode($titulos['ind_reporte']), 1, 0, 'L', 1);
                $this->atFPDF->SetFillColor(255, 255, 255);
                $this->atFPDF->SetFont('Arial', '', 4);
                $this->atFPDF->Cell(50, 5, utf8_decode($titulos['ind_impresion']), 1, 1, 'L', 1);
            }
            $this->atFPDF->AddPage();
        }
####### FIN ########
####### REMUNERACION ########
        $this->atFPDF->SetFont('Arial', 'B', 8);
        $this->atFPDF->Cell(190, 5, ("TOTAL REMUNERACION"), 0, 1, 'C');
        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->SetFont('Arial', 'B', 6);
        $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
        $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
        $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
        $this->atFPDF->Cell(21, 6, 'T.ASIG', 1, 0, 'R', 1);
        $this->atFPDF->Cell(21, 6, 'T.DEDUC.', 1, 0, 'R', 1);
        $this->atFPDF->Cell(21, 6, 'T.PAGAR.', 1, 0, 'R', 1);
        $this->atFPDF->Cell(30, 6, ('FIRMA'), 1, 1, 'R', 1);
        $ln = 0;
        $N = 1;

        $asignacionEmpleados = 0;
        $deduccionEmpleados = 0;

        foreach ($empleados AS $empleado) {
            $asignacionEmpleados = $asignacionEmpleados + $empleado['totalAsignacionesEmpleado'];
            $deduccionEmpleados = $deduccionEmpleados + $empleado['totalDeduccionesEmpleado'];
            $ln++;
            $y = $this->atFPDF->GetY();
            if ($y > 170) {
                $this->atFPDF->AddPage();
                $this->atFPDF->SetFont('Arial', 'B', 8);
                $this->atFPDF->Cell(190, 5, ("TOTAL REMUNERACION"), 0, 1, 'C');
                $this->atFPDF->SetFillColor(200, 200, 200);
                $this->atFPDF->SetFont('Arial', 'B', 6);
                $this->atFPDF->Cell(7, 6, utf8_decode('Nº'), 1, 0, 'C', 1);
                $this->atFPDF->Cell(15, 6, ('CEDULA'), 1, 0, 'R', 1);
                $this->atFPDF->Cell(60, 6, ('NOMBRES Y APELLIDOS'), 1, 0, 'L', 1);
                $this->atFPDF->Cell(21, 6, 'T.ASIG', 1, 0, 'R', 1);
                $this->atFPDF->Cell(21, 6, 'T.DEDUC.', 1, 0, 'R', 1);
                $this->atFPDF->Cell(21, 6, 'T.PAGAR.', 1, 0, 'R', 1);
                $this->atFPDF->Cell(30, 6, ('FIRMA'), 1, 1, 'R', 1);
            } else {
                $this->atFPDF->SetY($y);
            }
            if ($ln % 2 == 0) {
                $this->atFPDF->SetFillColor(240, 240, 240);
            } else {
                $this->atFPDF->SetFillColor(255, 255, 255);
            }
            $this->atFPDF->SetFont('Arial', '', 7);
            $this->atFPDF->Cell(7, 6, $N++, 1, 0, 'C', 1);
            $this->atFPDF->Cell(15, 6, number_format($empleado['ci'], 0, '', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(60, 6, utf8_decode($empleado['nombre']), 1, 0, 'L', 1);
            $this->atFPDF->Cell(21, 6, number_format($empleado['totalAsignacionesEmpleado'], 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(21, 6, number_format($empleado['totalDeduccionesEmpleado'], 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(21, 6, number_format(($empleado['totalAsignacionesEmpleado'] - $empleado['totalDeduccionesEmpleado']), 2, ',', '.'), 1, 0, 'R', 1);
            $this->atFPDF->Cell(30, 6, '', 1, 0, 'R', 1);
            $this->atFPDF->Ln();
        }
        $preparadoPor = $this->atConsultasComunes->metMostrarEmpleado($nomina['fk_rhb001_num_empleado_procesa']);
        $revisadoPor = $this->atConsultasComunes->metMostrarEmpleado($nomina['fk_rhb001_num_empleado_aprueba']);
        $conformadoPor = $this->atConsultasComunes->metBuscarCargo('DIRECTOR (A) DE RECURSOS HUMANOS ');
        $aprobadoPor = $this->atConsultasComunes->metBuscarCargo('DIRECTOR (A) GENERAL');


        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->SetFont('Arial', 'B', 7);
        $this->atFPDF->Cell(7, 6, '', 1, 0, 'C', 1);
        $this->atFPDF->Cell(15, 6, ' ', 1, 0, 'R', 1);
        $this->atFPDF->Cell(60, 6, 'Totales: ', 1, 0, 'R', 1);
        $this->atFPDF->Cell(21, 6, number_format($totalAsignaciones, 2, ',', '.'), 1, 0, 'R', 1);
        $this->atFPDF->Cell(21, 6, number_format($deduccionEmpleados, 2, ',', '.'), 1, 0, 'R', 1);
        $this->atFPDF->Cell(21, 6, number_format($totalAsignaciones - $deduccionEmpleados, 2, ',', '.'), 1, 0, 'R', 1);
        $this->atFPDF->SetFont('Arial', 'B', 8);
        $this->atFPDF->Ln(6);
        $this->atFPDF->SetDrawColor(255, 255, 255);
        $this->atFPDF->SetTextColor(0, 0, 0);
        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->SetFont('Arial', 'B', 5);
        $this->atFPDF->Cell(30, 5, 'LEYENDA', 1, 0, 'C', 1);
        $this->atFPDF->Cell(50, 5, 'DESCRIPCION', 1, 1, 'C', 1);
        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->SetFont('Arial', 'B', 5);
        $this->atFPDF->Cell(30, 5, utf8_decode('T.ASIG'), 1, 0, 'L', 1);
        $this->atFPDF->SetFillColor(255, 255, 255);
        $this->atFPDF->SetFont('Arial', '', 5);
        $this->atFPDF->Cell(150, 5, utf8_decode('TOTAL ASIGNACIÓN'), 1, 1, 'L', 1);
        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->Cell(30, 5, utf8_decode('T.DEDUC'), 1, 0, 'L', 1);
        $this->atFPDF->SetFillColor(255, 255, 255);
        $this->atFPDF->SetFont('Arial', '', 5);
        $this->atFPDF->Cell(150, 5, utf8_decode('TOTAL DEDUCCIÓN'), 1, 1, 'L', 1);
        $this->atFPDF->SetFillColor(200, 200, 200);
        $this->atFPDF->Cell(30, 5, utf8_decode('T.PAGAR'), 1, 0, 'L', 1);
        $this->atFPDF->SetFillColor(255, 255, 255);
        $this->atFPDF->SetFont('Arial', '', 5);
        $this->atFPDF->Cell(150, 5, utf8_decode('TOTAL PAGAR'), 1, 1, 'L', 1);
        $this->atFPDF->Ln();

        $yf = $this->atFPDF->GetY();
        if($yf>160) $yf = 160;
        $this->atFPDF->Rect(10, $y + 6, 70, 0.1, "DF");

        $this->atFPDF->SetXY(200, $yf + 10);
        $this->atFPDF->SetTextColor(0, 0, 0);
        $this->atFPDF->SetFont('Arial', '', 6);
        $this->atFPDF->Cell(200, 3, '______________________________________________                                    ___________________________________________', 0, 1, 'L');

        $this->atFPDF->SetFont('Arial', '', 6);
        $this->atFPDF->SetXY(200, $yf + 15);
        $this->atFPDF->Cell(100, 3, ('Elaborado Por:                                                                                                         Conformado Por:'), 0, 0, 'L');
        $this->atFPDF->Cell(100, 3, (''), 0, 1, 'L');
        $this->atFPDF->SetXY(200, $yf + 18);
        $this->atFPDF->MultiCell(60, 5, utf8_decode($preparadoPor['nombre']), 0, 'L');//nombre de quien elabora
        $this->atFPDF->SetXY(276, $yf + 18);
        $this->atFPDF->Cell(100, 5, utf8_decode($revisadoPor['nombre']), 0, 1, 'L');//nombre de quien revisa
        $this->atFPDF->SetXY(200, $yf + 20);
        $this->atFPDF->MultiCell(60, 5, utf8_decode($preparadoPor['ind_descripcion_cargo']), 0, 'L');//cargo de quien elabora
        $this->atFPDF->SetXY(276, $yf + 20);
        $this->atFPDF->Cell(100, 5, utf8_decode($revisadoPor['ind_descripcion_cargo']), 0, 1, 'L');//Cargo de quien revisa

        /*$this->atFPDF->Rect(10, $y + 20, 70, 0.1, "DF");
        $this->atFPDF->SetXY(200, $yf + 35);
        $this->atFPDF->Cell(100, 3, ' ____________________________________________', 0, 1, 'L');
        $this->atFPDF->SetFont('Arial', '', 6);
        $this->atFPDF->SetXY(200, $yf + 40);
        $this->atFPDF->Cell(100, 3, ('Aprobado Por:'), 0, 0, 'L');
        $this->atFPDF->Cell(100, 3, (''), 0, 1, 'L');
        $this->atFPDF->SetXY(200, $yf + 43);
        $this->atFPDF->Cell(100, 3, utf8_decode($aprobadoPor['nombre']), 0, 0, 'L');//nombre de quien conforma
        $this->atFPDF->SetXY(200, $yf + 45);
        $this->atFPDF->Cell(100, 3, utf8_decode($aprobadoPor['ind_descripcion_cargo']), 0, 0, 'L');//cargo de quien conforma*/

        $this->atFPDF->Output();
    }

    public function metCalcNominaConsolidada($idPeriodoProceso)
    {

        $nomina = $this->atEjecucionProcesos->metObtenerProceso($idPeriodoProceso);
        $empleado = $this->atConsultasComunes->metEmpleadosNomina($idPeriodoProceso);
        for ($i = 0; $i < count($empleado); $i++) {
            $empleados[$i] = array(
                'idEmpleado' => $empleado[$i]['pk_num_empleado'],
                'ci' => $empleado[$i]['ind_cedula_documento'],
                'nombre' => $empleado[$i]['ind_nombre1'] . ' ' . $empleado[$i]['ind_nombre2'] . ' ' . $empleado[$i]['ind_apellido1'] . ' ' . $empleado[$i]['ind_apellido2'],
            );
            $empleados[$i]['asignaciones'] = $this->atConsultasComunes->metEmpleadosAsignaciones($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codAsignaciones = $empleados[$i]['asignaciones'];
            $totalAsignacionesEmpleado = 0;
            foreach ($codAsignaciones AS $codAsignacion) {
                $empleados[$i]['codAsignaciones'][] = $codAsignacion['cod_concepto'];
                $totalAsignacionesEmpleado = $totalAsignacionesEmpleado + $codAsignacion['num_monto'];
            }
            $empleados[$i]['totalAsignacionesEmpleado'] = $totalAsignacionesEmpleado;

            $empleados[$i]['deducciones'] = $this->atConsultasComunes->metEmpleadosDeducciones($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codDeducciones = $empleados[$i]['deducciones'];
            $totalDeduccionesEmpleado = 0;
            foreach ($codDeducciones AS $codDeduccion) {
                $empleados[$i]['codDeducciones'][] = $codDeduccion['cod_concepto'];
                $totalDeduccionesEmpleado = $totalDeduccionesEmpleado + $codDeduccion['num_monto'];
            }
            $empleados[$i]['totalDeduccionesEmpleado'] = $totalDeduccionesEmpleado;

            $empleados[$i]['aportes'] = $this->atConsultasComunes->metEmpleadosAportes($idPeriodoProceso, $empleado[$i]['pk_num_empleado']);
            $codAportes = $empleados[$i]['aportes'];
            $totalAportesEmpleado = 0;
            foreach ($codAportes AS $codAporte) {
                $empleados[$i]['codAporte'][] = $codAporte['cod_concepto'];
                $totalAportesEmpleado = $totalAportesEmpleado + $codAporte['num_monto'];
            }
            $empleados[$i]['totalAportesEmpleado'] = $totalAportesEmpleado;
        }
        $tituloAsignaciones = $this->atConsultasComunes->metTituloAsignaciones($idPeriodoProceso);
        $tituloDeducciones = $this->atConsultasComunes->metTituloDeducciones($idPeriodoProceso);
        $tituloAportes = $this->atConsultasComunes->metTituloAportes($idPeriodoProceso);

        $this->metObtenerLibreria('PHPExcel', 'PHPExcel/Classes');
        $objPHPExcel = new PHPExcel();

        // Propiedades del documento
        $objPHPExcel->getProperties()->setCreator("Contraloria del Estado Sucre")
            ->setLastModifiedBy("Contraloria del Estado Sucre")
            ->setTitle("Nomina Consolidada")
            ->setSubject("Nomina Consolidada")
            ->setDescription("Nomina Consolidada de la Contraloria del Estado Sucre")
            ->setKeywords("Nomina Consolidada")
            ->setCategory("Archivo de Nomina Consolidada");


        $objPHPExcel->setActiveSheetIndex(0)->mergeCells('B1:E1');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B1', 'REPÚBLICA BOLIVARIANA DE VENEZUELA');
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells('B2:D2');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B2', 'CONTRALORÍA DEL ESTADO SUCRE');
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells('B3:D3');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B3', 'DIRECCIÓN DE RECURSOS HUMANOS');


// Se agregan los titulos del reporte
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B5', 'ASIGNACIONES NÓMINA: ' . utf8_decode($nomina['ind_titulo_boleta']));        // Titulo del reporte

//Ancho de las columnas
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(3);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('W')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('X')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AC')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AD')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AE')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AF')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AG')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AH')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AI')->setWidth(15);
        $row = 7;

        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, 'Nº');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, 'CEDULA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $row, 'NOMBRES Y APELLIDOS');

        $col = 3;


        $styleArray = array(
            'font' => array(
                'bold' => true,
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
                'top' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                ),
            ),
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation' => 90,
                'startcolor' => array(
                    'argb' => 'FFA0A0A0',
                ),
                'endcolor' => array(
                    'argb' => 'FFFFFFFF',
                ),
            ),
        );
        $objPHPExcel->getActiveSheet()->getStyle('A7:CC7')->applyFromArray($styleArray);
        $objPHPExcel->getActiveSheet()->getStyle('B1:B5')->applyFromArray($styleArray);


        foreach ($tituloAsignaciones AS $titulos) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $titulos['ind_reporte']);
            $col++;
        }
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, 'TOTAL ASIG.');


        $N = 1;
        $fil = 8;
        foreach ($empleados AS $empleado) {
            $col = 0;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, $N);
            $col++;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, number_format($empleado['ci'], 0, '', '.'));
            $col++;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, $empleado['nombre']);
            $col++;
            $asignaciones = $empleado['asignaciones'];
            $codAsignaciones = $empleado['codAsignaciones'];
            $total = 0;

            foreach ($tituloAsignaciones AS $titulos) {
                if (in_array($titulos['cod_concepto'], $codAsignaciones)) {
                    foreach ($asignaciones AS $asignacion) {
                        if ($titulos['cod_concepto'] == $asignacion['cod_concepto']) {
                            $total = $total + $asignacion['num_monto'];
                            $sb=$asignacion['num_monto'];
                            if ($asignacion['num_monto'] == "" || $asignacion['num_monto'] == NULL) {
                                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, 'hola');

                            } else {
                                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil,number_format($asignacion['num_monto'], 2, ',', '.'));
                            }
                        } else {
                            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil,number_format(0, 2, ',', '.'));

                        }

                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil,number_format($sb, 2, ',', '.'));


                    }
                    $col++;
                } else {
                    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil,number_format(0, 2, ',', '.'));
                    $col++;
                }

                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil,number_format($total, 2, ',', '.'));
            }
            $fil++;
            $N++;
        }

        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $fil, 'TOTALES');


        $col=3;
        $totalAsignaciones = 0;

        foreach ($tituloAsignaciones AS $titulos) {


            $totalAsignaciones = $totalAsignaciones + $titulos['TOTAL'];
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, number_format($titulos['TOTAL'], 2, ',', '.'));
            $col++;
        }
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $fil, number_format($totalAsignaciones, 2, ',', '.'));


        /* $e = "E6";

        $styleArray = array('font' => array('name' => 'Arial', 'size' => 10),
            'borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array('argb' => 'FFF')))
        );
        $rango = "A4:$e";*/
        //$objPHPExcel->getActiveSheet()->getStyle($rango)->applyFromArray($styleArray);
        // Cambiar el nombre de hoja de cálculo
        $objPHPExcel->getActiveSheet()->setTitle('consolidada ' . utf8_decode($nomina['ind_titulo_boleta']));


        // Establecer índice de hoja activa a la primera hoja , por lo que Excel abre esto como la primera hoja
        $objPHPExcel->setActiveSheetIndex(0);


        /*-----------------------------------------------------------------------------------------------------
        |--------------------------          GENERA LA SEGUNDA PAGINA -----------------------------------------
        -------------------------------------------------------------------------------------------------------*/
        $nueva_hoja1 = $objPHPExcel->createSheet();

        $nueva_hoja1->setTitle('DEDUCCIONES'); // definimos el titulo

        $nueva_hoja1->getColumnDimension('A')->setWidth(3);
        $nueva_hoja1->getColumnDimension('B')->setWidth(12);
        $nueva_hoja1->getColumnDimension('C')->setWidth(50);
        $nueva_hoja1->setCellValue('B1', 'REPÚBLICA BOLIVARIANA DE VENEZUELA');
        $nueva_hoja1->setCellValue('B2', 'CONTRALORÍA DEL ESTADO SUCRE');
        $nueva_hoja1->setCellValue('B3', 'DIRECCIÓN DE RECURSOS HUMANOS');


// Se agregan los titulos del reporte

        $nueva_hoja1->setCellValue('B5', 'DEDUCCIONES : ' . utf8_decode($nomina['ind_titulo_boleta']));        // Titulo del reporte


//Ancho de las columnas
        $nueva_hoja1->getColumnDimension('A')->setWidth(3);
        $nueva_hoja1->getColumnDimension('B')->setWidth(12);
        $nueva_hoja1->getColumnDimension('C')->setWidth(50);
        $nueva_hoja1->getColumnDimension('D')->setWidth(15);
        $nueva_hoja1->getColumnDimension('F')->setWidth(15);
        $nueva_hoja1->getColumnDimension('G')->setWidth(15);
        $nueva_hoja1->getColumnDimension('H')->setWidth(15);
        $nueva_hoja1->getColumnDimension('E')->setWidth(15);
        $nueva_hoja1->getColumnDimension('J')->setWidth(15);
        $nueva_hoja1->getColumnDimension('I')->setWidth(15);
        $nueva_hoja1->getColumnDimension('H')->setWidth(15);
        $nueva_hoja1->getColumnDimension('K')->setWidth(15);
        $nueva_hoja1->getColumnDimension('L')->setWidth(15);
        $nueva_hoja1->getColumnDimension('M')->setWidth(15);
        $nueva_hoja1->getColumnDimension('N')->setWidth(15);
        $nueva_hoja1->getColumnDimension('O')->setWidth(15);
        $nueva_hoja1->getColumnDimension('P')->setWidth(15);
        $nueva_hoja1->getColumnDimension('Q')->setWidth(15);
        $nueva_hoja1->getColumnDimension('R')->setWidth(15);
        $nueva_hoja1->getColumnDimension('S')->setWidth(15);
        $nueva_hoja1->getColumnDimension('T')->setWidth(15);
        $nueva_hoja1->getColumnDimension('U')->setWidth(15);
        $nueva_hoja1->getColumnDimension('V')->setWidth(15);
        $nueva_hoja1->getColumnDimension('W')->setWidth(15);
        $nueva_hoja1->getColumnDimension('X')->setWidth(15);
        $nueva_hoja1->getColumnDimension('Y')->setWidth(15);
        $nueva_hoja1->getColumnDimension('Z')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AA')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AB')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AC')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AD')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AE')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AH')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AF')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AG')->setWidth(15);
        $nueva_hoja1->getColumnDimension('AI')->setWidth(15);

        $row = 7;

        $styleArray = array(
            'font' => array(
                'bold' => true,
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
                'top' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                ),
            ),
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation' => 90,
                'startcolor' => array(
                    'argb' => 'FFA0A0A0',
                ),
                'endcolor' => array(
                    'argb' => 'FFFFFFFF',
                ),
            ),
        );
        $nueva_hoja1->getStyle('A7:CC7')->applyFromArray($styleArray);
        $nueva_hoja1->getStyle('B1:B5')->applyFromArray($styleArray);


        $nueva_hoja1->setCellValueByColumnAndRow(0, $row, 'Nº');
        $nueva_hoja1->setCellValueByColumnAndRow(1, $row, 'CEDULA');
        $nueva_hoja1->setCellValueByColumnAndRow(2, $row, 'NOMBRES Y APELLIDOS');

        $col = 3;

        foreach ($tituloDeducciones AS $titulosD) {
            $nueva_hoja1->setCellValueByColumnAndRow($col, $row, $titulosD['ind_reporte']);
            $col++;
        }
        $nueva_hoja1->setCellValueByColumnAndRow($col, $row, 'TOTAL ASIG.');


        $N = 1;
        $fil = 8;
        foreach ($empleados AS $empleado) {
            $col = 0;
            $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, $N);
            $col++;
            $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($empleado['ci'], 0, '', '.'));
            $col++;
            $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, $empleado['nombre']);
            $col++;
            $deducciones = $empleado['deducciones'];
            if (isset($empleado['codDeducciones'])){
                $codDeducciones = $empleado['codDeducciones'];
            }

            $total = 0;

            foreach ($tituloDeducciones AS $titulos) {
                if (in_array($titulos['cod_concepto'], $codDeducciones)) {
                    foreach ($deducciones AS $deduccion) {
                        if ($titulos['cod_concepto'] == $deduccion['cod_concepto']) {
                            $total = $total + $deduccion['num_monto'];
                            $saldo=$deduccion['num_monto'];
                            if ($deduccion['num_monto'] == "" || $deduccion['num_monto'] == NULL) {
                                $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, 'hola');

                            } else {
                                $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($deduccion['num_monto'], 2, ',', '.'));
                            }
                        } else {
                            $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format(0, 2, ',', '.'));

                        }
                        $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($saldo, 2, ',', '.'));
                       // $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, $titulos['cod_concepto'] .' - '. $deduccion['cod_concepto']);

                    }
                    $col++;
                } else {
                    $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format(0, 2, ',', '.'));
                    $col++;
                }

                $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($total, 2, ',', '.'));
            }
            $fil++;
            $N++;
        }

        $nueva_hoja1->setCellValueByColumnAndRow(2, $fil, 'TOTALES');


        $col=3;
        $totalDeduccionesEmpleado = 0;
        foreach ($tituloDeducciones AS $titulos) {

            $totalDeduccionesEmpleado = $totalDeduccionesEmpleado + $titulos['TOTAL'];
            $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($titulos['TOTAL'], 2, ',', '.'));
            $col++;

        }
        $nueva_hoja1->setCellValueByColumnAndRow($col, $fil, number_format($totalDeduccionesEmpleado, 2, ',', '.'));

        /*------------------------------------------------------------------------------------------------------
         |--------------------------          GENERA LA TERCERA PAGINA -----------------------------------------
         -------------------------------------------------------------------------------------------------------*/


        $nueva_hoja2 = $objPHPExcel->createSheet();

        $nueva_hoja2->setTitle('APORTES'); // definimos el titulo

        $nueva_hoja2->getColumnDimension('A')->setWidth(3);
        $nueva_hoja2->getColumnDimension('B')->setWidth(12);
        $nueva_hoja2->getColumnDimension('C')->setWidth(50);
        $nueva_hoja2->getColumnDimension('D')->setWidth(15);
        $nueva_hoja2->getColumnDimension('F')->setWidth(15);
        $nueva_hoja2->getColumnDimension('E')->setWidth(15);
        $nueva_hoja2->getColumnDimension('G')->setWidth(15);
        $nueva_hoja2->getColumnDimension('H')->setWidth(15);
        $nueva_hoja2->getColumnDimension('I')->setWidth(15);
        $nueva_hoja2->getColumnDimension('H')->setWidth(15);
        $nueva_hoja2->getColumnDimension('J')->setWidth(15);
        $nueva_hoja2->getColumnDimension('K')->setWidth(15);
        $nueva_hoja2->getColumnDimension('L')->setWidth(15);
        $nueva_hoja2->getColumnDimension('M')->setWidth(15);
        $nueva_hoja2->getColumnDimension('N')->setWidth(15);
        $nueva_hoja2->getColumnDimension('O')->setWidth(15);
        $nueva_hoja2->getColumnDimension('P')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Q')->setWidth(15);
        $nueva_hoja2->getColumnDimension('R')->setWidth(15);
        $nueva_hoja2->getColumnDimension('S')->setWidth(15);
        $nueva_hoja2->getColumnDimension('T')->setWidth(15);
        $nueva_hoja2->getColumnDimension('U')->setWidth(15);
        $nueva_hoja2->getColumnDimension('V')->setWidth(15);
        $nueva_hoja2->getColumnDimension('W')->setWidth(15);
        $nueva_hoja2->getColumnDimension('X')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Y')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Z')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AA')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AB')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AC')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AD')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AE')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AF')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AG')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AH')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AI')->setWidth(15);


        $styleArray = array(
            'font' => array(
                'bold' => true,
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
                'top' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                ),
            ),
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation' => 90,
                'startcolor' => array(
                    'argb' => 'FFA0A0A0',
                ),
                'endcolor' => array(
                    'argb' => 'FFFFFFFF',
                ),
            ),
        );
        $nueva_hoja2->getStyle('A7:CC7')->applyFromArray($styleArray);
        $nueva_hoja2->getStyle('B1:B5')->applyFromArray($styleArray);


        $nueva_hoja2->setCellValue('B1', 'REPÚBLICA BOLIVARIANA DE VENEZUELA');
        $nueva_hoja2->setCellValue('B2', 'CONTRALORÍA DEL ESTADO SUCRE');
        $nueva_hoja2->setCellValue('B3', 'DIRECCIÓN DE RECURSOS HUMANOS');


// Se agregan los titulos del reporte

        $nueva_hoja2->setCellValue('B5', 'APORTES : ' . utf8_decode($nomina['ind_titulo_boleta']));        // Titulo del reporte


//Ancho de las columnas
        $nueva_hoja2->getColumnDimension('A')->setWidth(3);
        $nueva_hoja2->getColumnDimension('B')->setWidth(12);
        $nueva_hoja2->getColumnDimension('C')->setWidth(50);


        $row = 7;

        $nueva_hoja2->setCellValueByColumnAndRow(0, $row, 'Nº');
        $nueva_hoja2->setCellValueByColumnAndRow(1, $row, 'CEDULA');
        $nueva_hoja2->setCellValueByColumnAndRow(2, $row, 'NOMBRES Y APELLIDOS');

        $col = 3;

        foreach ($tituloAportes AS $titulosA) {
            $nueva_hoja2->setCellValueByColumnAndRow($col, $row, $titulosA['ind_reporte']);
            $col++;
        }
        $nueva_hoja2->setCellValueByColumnAndRow($col, $row, 'TOTAL ASIG.');


        $N = 1;
        $fil = 8;
        foreach ($empleados AS $empleado) {
            $col = 0;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, $N);
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($empleado['ci'], 0, '', '.'));
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, $empleado['nombre']);
            $col++;
            $aportes = $empleado['aportes'];
            
            if (isset($empleado['codAporte'])){
                $codAporte = $empleado['codAporte'];
            }
            $total = 0;

            foreach ($tituloAportes AS $titulos) {
                if (in_array($titulos['cod_concepto'], $codAporte)) {
                    foreach ($aportes AS $aporte) {
                        if ($titulos['cod_concepto'] == $aporte['cod_concepto']) {
                            $total = $total + $aporte['num_monto'];
                            $saldo=$aporte['num_monto'];

                            if ($aporte['num_monto'] == "" || $aporte['num_monto'] == NULL) {
                                $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, 'hola');

                            } else {
                                $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($aporte['num_monto'], 2, ',', '.'));
                            }
                        } else {
                            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format(0, 2, ',', '.'));

                        }
                        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($saldo, 2, ',', '.'));


                    }
                    $col++;
                } else {
                    $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format(0, 2, ',', '.'));
                    $col++;
                }

                $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($total, 2, ',', '.'));
            }
            $fil++;
            $N++;
        }

        $nueva_hoja2->setCellValueByColumnAndRow(2, $fil, 'TOTALES');


        $col=3;
        $totalAportesEmpleado = 0;
        foreach ($tituloAportes AS $titulos) {

            $totalAportesEmpleado = $totalAportesEmpleado + $titulos['TOTAL'];
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($titulos['TOTAL'], 2, ',', '.'));
            $col++;

        }
        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($totalAportesEmpleado, 2, ',', '.'));


        /*------------------------------------------------------------------------------------------------------
        |--------------------------          GENERA LA ULTIMA PAGINA -------------------------------------------
        -------------------------------------------------------------------------------------------------------*/


        $nueva_hoja2 = $objPHPExcel->createSheet();

        $nueva_hoja2->setTitle('TOTALES'); // definimos el titulo

        $nueva_hoja2->getColumnDimension('A')->setWidth(3);
        $nueva_hoja2->getColumnDimension('B')->setWidth(12);
        $nueva_hoja2->getColumnDimension('C')->setWidth(50);
        $nueva_hoja2->getColumnDimension('D')->setWidth(15);
        $nueva_hoja2->getColumnDimension('F')->setWidth(15);
        $nueva_hoja2->getColumnDimension('E')->setWidth(15);
        $nueva_hoja2->getColumnDimension('G')->setWidth(15);
        $nueva_hoja2->getColumnDimension('H')->setWidth(15);
        $nueva_hoja2->getColumnDimension('I')->setWidth(15);
        $nueva_hoja2->getColumnDimension('H')->setWidth(15);
        $nueva_hoja2->getColumnDimension('J')->setWidth(15);
        $nueva_hoja2->getColumnDimension('K')->setWidth(15);
        $nueva_hoja2->getColumnDimension('L')->setWidth(15);
        $nueva_hoja2->getColumnDimension('M')->setWidth(15);
        $nueva_hoja2->getColumnDimension('N')->setWidth(15);
        $nueva_hoja2->getColumnDimension('O')->setWidth(15);
        $nueva_hoja2->getColumnDimension('P')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Q')->setWidth(15);
        $nueva_hoja2->getColumnDimension('R')->setWidth(15);
        $nueva_hoja2->getColumnDimension('S')->setWidth(15);
        $nueva_hoja2->getColumnDimension('T')->setWidth(15);
        $nueva_hoja2->getColumnDimension('U')->setWidth(15);
        $nueva_hoja2->getColumnDimension('V')->setWidth(15);
        $nueva_hoja2->getColumnDimension('W')->setWidth(15);
        $nueva_hoja2->getColumnDimension('X')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Y')->setWidth(15);
        $nueva_hoja2->getColumnDimension('Z')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AA')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AB')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AC')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AD')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AE')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AF')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AG')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AH')->setWidth(15);
        $nueva_hoja2->getColumnDimension('AI')->setWidth(15);


        $styleArray = array(
            'font' => array(
                'bold' => true,
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
                'top' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                ),
            ),
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation' => 90,
                'startcolor' => array(
                    'argb' => 'FFA0A0A0',
                ),
                'endcolor' => array(
                    'argb' => 'FFFFFFFF',
                ),
            ),
        );
        $nueva_hoja2->getStyle('A7:CC7')->applyFromArray($styleArray);
        $nueva_hoja2->getStyle('B1:B5')->applyFromArray($styleArray);

        $nueva_hoja2->setCellValue('B1', 'REPÚBLICA BOLIVARIANA DE VENEZUELA');
        $nueva_hoja2->setCellValue('B2', 'CONTRALORÍA DEL ESTADO SUCRE');
        $nueva_hoja2->setCellValue('B3', 'DIRECCIÓN DE RECURSOS HUMANOS');


// Se agregan los titulos del reporte

        $nueva_hoja2->setCellValue('B5', 'TOTALES: ' . utf8_decode($nomina['ind_titulo_boleta']));        // Titulo del reporte


//Ancho de las columnas
        $nueva_hoja2->getColumnDimension('A')->setWidth(3);
        $nueva_hoja2->getColumnDimension('B')->setWidth(12);
        $nueva_hoja2->getColumnDimension('C')->setWidth(50);


        $row = 7;

        $nueva_hoja2->setCellValueByColumnAndRow(0, $row, 'Nº');
        $nueva_hoja2->setCellValueByColumnAndRow(1, $row, 'CEDULA');
        $nueva_hoja2->setCellValueByColumnAndRow(2, $row, 'NOMBRES Y APELLIDOS');
        $nueva_hoja2->setCellValueByColumnAndRow(3, $row, 'T.ASIG.');
        $nueva_hoja2->setCellValueByColumnAndRow(4, $row, 'T.DEDUC.');
        $nueva_hoja2->setCellValueByColumnAndRow(5, $row, 'T.PAGAR.');


        $N = 1;
        $fil = 8;
        foreach ($empleados AS $empleado) {
            $col = 0;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, $N);
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($empleado['ci'], 0, '', '.'));
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, $empleado['nombre']);
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($empleado['totalAsignacionesEmpleado'], 2, ',', '.'));
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format($empleado['totalDeduccionesEmpleado'], 2, ',', '.'));
            $col++;
            $nueva_hoja2->setCellValueByColumnAndRow($col, $fil, number_format(($empleado['totalAsignacionesEmpleado']- $empleado['totalDeduccionesEmpleado']), 2, ',', '.'));

            $fil++;
            $N++;
        }
        $col=2;
        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil,'TOTALES');
        $col++;
        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil,number_format($totalAsignaciones, 2, ',', '.'));
        $col++;
        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil,number_format($totalDeduccionesEmpleado, 2, ',', '.'));
        $col++;
        $nueva_hoja2->setCellValueByColumnAndRow($col, $fil,number_format(($totalAsignaciones-$totalDeduccionesEmpleado), 2, ',', '.'));










        // Redirigir la salida al navegador web de un cliente ( Excel5 )
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="Nomina_consolidada: '.utf8_decode($nomina['ind_titulo_boleta']).'.xls"');
        header('Cache-Control: max-age=0');
        // Si usted está sirviendo a IE 9 , a continuación, puede ser necesaria la siguiente
        header('Cache-Control: max-age=1');

        // Si usted está sirviendo a IE a través de SSL , a continuación, puede ser necesaria la siguiente
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

        $respuesta = array('estatus' => 1);
        echo json_encode($respuesta);
    }
}
