<?php session_start();

if (isset($_SESSION['tipousuario']))
		{if ($_SESSION['tipousuario']=='1')
		header ('location:menu_personal.php');
		}

if  (trim($_SESSION['nombre_validar'])=='') header ('location:index.php');
include ("fphp.php");
$enlace=cargar_conexion();
require_once 'PHPExcel/PHPExcel.php';

if (isset($_GET['id_sync'])) {
  $id_sincronizacion = $_GET['id_sync'];

  $query= "SELECT log_errores, desperiodo FROM sincronizacion_dynamics WHERE id_sincronizacion = $id_sincronizacion";
  $consulta=mysqli_query($enlace,$query) or die ("Error in query: $query. ".mysqli_error($enlace));
  $row=mysqli_fetch_array($consulta);
  $array_errores= unserialize($row['log_errores']);
  $desperiodo = $row['desperiodo'];

  if (count($array_errores)>0){
    $total_contador_celdas_excel = 6;
    $objPHPExcel = new PHPExcel();
    // Asignar hoja de calculo activa
    $objPHPExcel->setActiveSheetIndex(0);

    //Ancho
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('A')->setWidth(15);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('B')->setWidth(30);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('E')->setWidth(40);
    $objPHPExcel->getActiveSheet(0)->getColumnDimension('F')->setWidth(200);
    //Negrita
    $objPHPExcel->getActiveSheet(0)->getStyle("A1")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("A3")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("A5")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("B5")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("C5")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("D5")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("E5")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet(0)->getStyle("F5")->getFont()->setBold(true);
    //Tipo de celda
    $objPHPExcel->getActiveSheet(0)->getStyle('F')->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );

    // Definir titulos de excel
    $objPHPExcel->getActiveSheet(0)
                ->setCellValue('A1', 'LOG DE ERRORES')
                ->setCellValue('A3', "Periodo")
                ->setCellValue('B3', $desperiodo)
                ->setCellValue('A5', "Identificación")
                ->setCellValue('B5', "Descripción")
                ->setCellValue('C5', "Cuenta")
                ->setCellValue('D5', "Monto")
                ->setCellValue('E5', "Codigo de Error")
                ->setCellValue('F5', "Detalle de Error");

    //Imprime errores
    foreach($array_errores as $log) {
      $objPHPExcel->getActiveSheet(0)
                  ->setCellValue('A'.$total_contador_celdas_excel, $log['identificacion'])
                  ->setCellValue('B'.$total_contador_celdas_excel, $log['desconcepto'])
                  ->setCellValue('C'.$total_contador_celdas_excel, $log['cuenta'])
                  ->setCellValue('D'.$total_contador_celdas_excel, $log['monto'])
                  ->setCellValue('E'.$total_contador_celdas_excel, $log['codigo_error'])
                  ->setCellValue('F'.$total_contador_celdas_excel, $log['mensaje_error']);
      $total_contador_celdas_excel++;
    }
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="Log_sincronizacion_dynamics.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
  }
}


?>
