<?php 
    require_once('conecta.php');
    /*$desde_fecha=$_POST['desde_fecha'];
    $hasta_fecha=$_POST['hasta_fecha'];*/

    $desde_fecha="2026-03-01";
    $hasta_fecha="2026-03-31";

    // Usamos comillas dobles para envolver la consulta
    $sql="SELECT v.idventa,
                v.cliente,
                c.denominacion,
                SUM(d.costo) AS costo,
                SUM(d.precio) AS precio,
                SUM(d.importe) AS importe,
                SUM(d.subtotal) AS subtotal, 
                SUM(d.descuento) AS descuento,
                SUM(d.ganancia) AS ganancia,
                v.total_venta
                FROM venta v INNER JOIN det_venta d ON v.idventa = d.idventa
                             inner join cliente c on v.cliente = c.idcliente 
                WHERE v.anulada = 'NO' AND v.fecha BETWEEN '$desde_fecha' AND '$hasta_fecha'
                GROUP BY v.cliente";
    $result = mysqli_query($conecta,$sql) or die("No fue posible conectar a la BD!.$sql");
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>INFORME VENTAS</title>
</head>
<body>
    <table width="100%" align="center">
    <tr>
        <td>
            <img src="images/logo_lodeantes.png" width="60" height="60" alt="Logo">
        </td>
        <td>
            <h1>Informe de Ventas Resumido</h1>
        </td>
    </tr>
    </table>
    
    <table width="50%">
    <!-- <tr>
        <td>Desde Fecha:<?php echo date("d/m/Y",strtotime($desde_fecha)); ?></td>
        <td>Hasta Fecha:<?php echo date("d/m/Y",strtotime($hasta_fecha)); ?></td>        
    </tr>
    </table>-->
    <hr>
	<table width='100%'>
		<thead>
			<tr>
				<th>Cliente</th>
				<th>Costo</th>
				<th>Precio</th>
				<th>Importe</th>
                <th>Sub Total</th>
                <th>Descuento</th>
                <th>Ganancia</th>
			</tr>
		</thead>
        <?php 
            $total_costo=0; 
            $total_precio=0; 
            $total_importe=0; 
            $total_subtotal=0; 
            $total_descuento=0; 
            $total_ganancia=0; 
            while($linea = mysqli_fetch_assoc($result)) { ?>
			<tr>
				<td><?php echo utf8_encode($linea['denominacion']); ?></td>
				<td><?php echo number_format($linea['costo'], 0, ',', '.'); ?></td>
				<td><?php echo number_format($linea['precio'], 0, ',', '.'); ?></td>
				<td><?php echo number_format($linea['importe'], 0, ',', '.'); ?></td>
				<td><?php echo number_format($linea['subtotal'], 0, ',', '.'); ?></td>
                <td><?php echo number_format($linea['descuento'], 0, ',', '.'); ?></td>
                <td><?php echo number_format($linea['ganancia'], 0, ',', '.'); ?></td>
			</tr>
        <?php 
            $total_costo = $total_costo + $linea['costo'];
            $total_precio = $total_precio + $linea['precio'];
            $total_importe = $total_importe + $linea['importe'];
            $total_subtotal = $total_subtotal + $linea['subtotal'];
            $total_descuento = $total_descuento + $linea['descuento'];
            $total_ganancia = $total_ganancia + $linea['ganancia'];
            }
        ?>
        <tr>
        <td>Totales</td>
        <td><?php echo $total_costo;?></td>
        <td><?php echo $total_precio;?></td>
        <td><?php echo $total_importe;?></td>
        <td><?php echo $total_subtotal;?></td>
        <td><?php echo $total_descuento;?></td>
        <td><?php echo $total_ganancia;?></td>
        </tr>	
	</table>
</body>
</html>