<?php
	include('clases/clase.conexion.php'); 
	include('funciones/variables.php');
	include_once "Spreadsheet/Writer.php";
	
	$VarCon = VarServer("MYSQLI");
	$obj = new clsConexion($VarCon[0],$VarCon[1],$VarCon[2],$VarCon[3],$VarCon[4]);


	$varAnio = $_GET["anio"];
	$varMes  = $_GET["mes"];
	$varPro  = $_GET["pro"];
	
	$xls = new Spreadsheet_Excel_Writer();
	$xls->send($_GET["page"].".xls");
	
	$negrita =& $xls->addFormat();
	$negrita->setBold();
	$format_decimal =& $xls->addFormat();
	$format_decimal->setNumFormat("#,##0.00;[Red](#,##0.00)");
	$format_entero =& $xls->addFormat();
	$format_entero->setNumFormat("#,##0;[Red]#,##0");
	$format_porcen =& $xls->addFormat();
	$format_porcen->setNumFormat("0.00%");
	
	$sqlcab = "call spReporteVenta_DetalleFamilia_Cab('$varAnio','$varMes',$varPro);";
	$obj->AbrirConexion();
	$rscab = $obj->Query($sqlcab);
	$obj->CerrarConexion();
	
	while ($rowcab = $obj->FetchArray($rscab)) {
		$varNom = $rowcab["SUBFAMILIA"];
		$sheet =& $xls->addWorksheet($rowcab["SUBFAMILIA"]);
		$varSub = $rowcab["CSUB"];
		$sql = "call spReporteVenta_DetalleFamilia_Det('$varAnio', '$varMes', $varPro, '$varSub'); ";
		$obj->AbrirConexion();
		$rs = $obj->Query($sql);
		$obj->CerrarConexion();
		$col = $obj->NumField($rs);
		// Escribimos Nombres de Columnas
		for ($j=0;$j<$col;$j++) {
			$sheet->write(0,$j+1, $obj->FieldName($rs,$j),$negrita);
			}
		$varFila = 0;
		
		$varPos = 0;
		$varSum1 = 0;
		$varSum2 = 0;
		$varVen_Act = "";
		$varVen_Ant = "";
		while($fila = $obj->FetchArray($rs)) {
			$varVen_Act = $fila["VENDEDOR"];//.$fila["PRODUCTO"];
			$varFila=$varFila+1;
			$varPos++;
			
			if($varVen_Act<>$varVen_Ant && $varVen_Ant <> "") {
				$varVen_Ant = $varVen_Act;
				$varPos = 1;
				$sheet->write($varFila,8, $varSum1,$negrita);
				$sheet->write($varFila,10, $varSum2,$negrita);
				$varSum1 = 0;
				$varSum2 = 0;
				$varFila=$varFila+2;
				}
			$varSum1 = $varSum1 + $fila["CANTIDAD"];
			$varSum2 = $varSum2 + $fila["MONTO"];
			$sheet->write($varFila,0, $varPos);
			$varVen_Ant = $varVen_Act;
			for ($j=0;$j<$col;$j++) {
				$sheet->write($varFila,$j+1, $fila[$j]);
				}
			}
		}
	$xls->close();
?>
