<?php 

session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include('DF80000.php');

date_default_timezone_set('America/Mexico_City');
       

        require_once 'excel/PHPExcel.php';

        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()
        ->setCreator("Raul")
        ->setTitle("Movimientos_entrada")
        ->setDescription("Movimientos_entrada");

        $arrayMeses = array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio','Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
 
        $arrayDias = array( 'Domingo', 'Lunes', 'Martes','Miercoles', 'Jueves', 'Viernes', 'Sabado');
        $objPHPExcel->setActiveSheetIndex(0);//pestaña activa
        
        $objPHPExcel->getActiveSheet()->setTitle('movimientos');//titulo a la hoja
        $styleArray = array('borders' => array('allborders' =>array('style' => PHPExcel_Style_Border::BORDER_THIN)));
        
        $folio=$_POST['folio'];
        $datos = mysqli_query($con ,"SELECT farticulos.nparte1, farticulos.np_desc1 FROM dgrequid
                                            INNER JOIN farticulos on dgrequid.nparte1=farticulos.nparte1
                                            WHERE dgrequid.fol_ven1 = '$folio'");
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $fecha= $arrayMeses[date('m')-1]."  ".date('Y');
        $fecha_completa= $arrayDias[date('w')].", ".date('d')." de ".$arrayMeses[date('m')-1]." de ".date('Y');
        $objPHPExcel->getActiveSheet()->setCellValue('A2', 'Fecha:  '.$fecha_completa);

        while(@$nom = mysqli_fetch_array ($datos)){
        $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Nombre del producto: '.$nom['np_desc1']);
        $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Clave: '.$nom['nparte1']);
        }

        $objPHPExcel->getActiveSheet()->setCellValue('c5', 'MOVIMIENTO ');
        $objPHPExcel->getActiveSheet()->setCellValue('D5', '# SALIDA.');
        $objPHPExcel->getActiveSheet()->setCellValue('E5', 'REQUISICION.');
        $objPHPExcel->getActiveSheet()->setCellValue('F5', 'FECHA ');

                $result = mysqli_query($con ,"SELECT * FROM dgrequid WHERE fol_ven1 = '$folio'");
        while(@$dato = mysqli_fetch_array ($result)){
                @$articulos = mysqli_query($con ,"
                                        SELECT dgreqdev.fol_ven, dgreqdev.folio_control , dgreqdev.fec_ven ,'ENTRADAS'  as mov FROM dgreqded INNER JOIN dgreqdev on dgreqded.cons_ven=dgreqdev.fol_ven
                                                                                where dgreqded.fec_ven> '$dato[1]'
                                                                                and dgreqded.nparte1='$dato[3]'
                        order by fec_ven desc");
                                        
                
                $numero=6;
                $numerossal=mysqli_num_rows($articulos);
                $objPHPExcel->getActiveSheet()->setCellValue('A5', 'total:  '.$numerossal);
                 while(@$movi = mysqli_fetch_array ($articulos)){
                        $letra='C';
                        $objPHPExcel->getActiveSheet()->setCellValue($letra.$numero,$movi['mov'] );
                        $letra++;
                        $objPHPExcel->getActiveSheet()->setCellValue($letra.$numero,$movi['fol_ven'] );
                        $letra++;
                        $objPHPExcel->getActiveSheet()->setCellValue($letra.$numero,$movi['folio_control'] );
                        $letra++;
                        $fec1 = $movi['fec_ven'];
                        include("DF20000.php");
                        $objPHPExcel->getActiveSheet()->setCellValue($letra.$numero, $f_f  );
                        
                        
                        $numero++;
                 }

        }

        
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header('Content-Disposition: attachment;filename="REPORTE de entradas posteriores y/o salidas '.$fecha.'.xlsx"');
        header('Cache-Control: max-age=0');

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

 ?>