<head>
<script src="./pace/pace.min.js"></script>
<link href="./pace/themes/black/pace-theme-corner-indicator.css" rel="stylesheet" />
</head>
<?php
date_default_timezone_set('America/Lima');
include'conexion.php';//CONEXION A LA BD
include_once 'Classes/PHPExcel.php';
$ano = $_COOKIE['variable1'];
$mes = $_COOKIE['variable2'];
$anomes=$ano.$mes;
$mesactual= date('Ym');
if($mes==12){
$ano2=$ano+1;
$mes_siguiente=$ano2.'01';
}
else{
$mes_siguiente=$anomes+1;
}
$tabla2="ventad$mes_siguiente";
$tabla= "ventad$anomes";
$objXLS = new PHPExcel();
$cod=1;
$hojas=0;
//armamos la consulta
$sqlprod="select V.cdarticulo, A.dsarticulo from $tabla V inner join articulo A on V.cdarticulo=A.cdarticulo group by V.cdarticulo,A.dsarticulo order by V.cdarticulo";
//ejecutamos la consulta
$prod=odbc_exec($conexion,$sqlprod);
//obtenemos la cantidad de filas de la consulta
$numprod= odbc_num_rows($prod);
$i = 0;
while(odbc_fetch_row($prod)) {
$i++;
${"cdprod" . $i} = trim(odbc_result($prod,'cdarticulo'));
${"dsprod" . $i} = trim(odbc_result($prod,'dsarticulo'));
}
while($cod<=$numprod)
{
$objXLS->createSheet();
$objSheet = $objXLS->setActiveSheetIndex($hojas);
if(isset($_POST['generar_reporte']))
{
// ENCABEZADOS
$objSheet->setCellValue('C3', 'DOCUMENTO');
$objSheet->setCellValue('D3', 'CANTIDAD');
$objSheet->setCellValue('E3', 'SUBTOTAL');
$objSheet->setCellValue('F3', 'FECHA');
// QUERY PARA CREAR EL REPORTE
$numero=3;
if ($cod==1){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod1
order by fecproceso,nrodocumento";
$titulo=$dsprod1;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod1
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod1
order by fecproceso,nrodocumento";
$titulo=$dsprod1;
}
}elseif($cod==2){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod2
order by fecproceso,nrodocumento";
$titulo=$dsprod2;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod2
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod2
order by fecproceso,nrodocumento";
$titulo=$dsprod2;
}
}elseif($cod==3){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod3
order by fecproceso,nrodocumento";
$titulo=$dsprod3;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod3
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod3
order by fecproceso,nrodocumento";
$titulo=$dsprod3;
}
}elseif($cod==4){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod4
order by fecproceso,nrodocumento";
$titulo=$dsprod4;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod4
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod4
order by fecproceso,nrodocumento";
$titulo=$dsprod4;
}
}elseif($cod==5){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod5
order by fecproceso,nrodocumento";
$titulo=$dsprod5;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod5
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod5
order by fecproceso,nrodocumento";
$titulo=$dsprod5;
}
}elseif($cod==6){
if ($anomes==$mesactual){
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod6
order by fecproceso,nrodocumento";
$titulo=$dsprod6;
}else{
$sqlventa="SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod6
union SELECT fecproceso,nrodocumento,cantidad,mtosubtotal FROM $tabla2
where month(fecproceso)=$mes and cdtipodoc=12 and cdarticulo=$cdprod6
order by fecproceso,nrodocumento";
$titulo=$dsprod6;
}
}
$can=odbc_exec($conexion,$sqlventa);
while(odbc_fetch_row($can)){
$numero++;
$objSheet->setCellValue('C'.$numero, odbc_result($can, 'nrodocumento'));
$objSheet->setCellValue('D'.$numero, odbc_result($can, 'cantidad'));
$objSheet->setCellValue('E'.$numero, odbc_result($can, 'mtosubtotal'));
$objSheet->setCellValue('F'.$numero, date('d/m/Y',strtotime(odbc_result($can, 'fecproceso'))));
}
$objXLS->getActiveSheet($hojas)->getColumnDimension('C')->setAutoSize(true);
$objXLS->getActiveSheet($hojas)->getColumnDimension('D')->setAutoSize(true);
$objXLS->getActiveSheet($hojas)->getColumnDimension('E')->setAutoSize(true);
$objXLS->getActiveSheet($hojas)->getColumnDimension('F')->setAutoSize(true);
$objXLS->getActiveSheet($hojas)->setTitle($titulo);
$objXLS->setActiveSheetIndex($hojas);
}
$hojas++;
$cod++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename=Kardex$ano-$mes.xlsx");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
?>