<?
$r = "../";
require_once($r."inc/funciones.php");
$mysqli = connectDB($r);
$con = $_POST['con'];
$de = $_POST['de'];
$a = $_POST['a'];

if($con=='consu'){
    $sql=$mysqli->query("SELECT `id`,`medio` FROM `z_pagos_medios`");
    $table = '<table class="table">';
    while($row=$sql->fetch_assoc()){
        $idmedio = $row['id'];
        $sql1=$mysqli->query("SELECT SUM(z_pagos.`monto`) AS 'total' FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.`fecha` BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'  AND z_pagos.idmedio=$idmedio");
        $row1=$sql1->fetch_assoc();
        $sql2=$mysqli->query("SELECT SUM(z_abonos.`abono`) AS 'total' FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.`fecha` BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'  AND z_abonos.idmedio=$idmedio");
        $row2=$sql2->fetch_assoc();
        $monto = $row1['total']+$row2['total'];
        if($monto>0){$table .= "<tr><td>".$row['medio']."</td><td>".moneda($monto)."</td></tr>";}
        
    }
    $table .= '</table>';
    echo $table;
    exit();
}
if($con=='hoy'){
    $sql=$mysqli->query("SELECT `id`,`medio` FROM `z_pagos_medios`");
    $table = '<table class="table">';
    while($row=$sql->fetch_assoc()){
        $idmedio = $row['id'];
        $sql1=$mysqli->query("SELECT SUM(z_pagos.`monto`) AS 'total' FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.`fecha` LIKE '%$a%' AND z_pagos.idmedio=$idmedio");
        $row1=$sql1->fetch_assoc();
        $sql2=$mysqli->query("SELECT SUM(z_abonos.`abono`) AS 'total' FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.`fecha` LIKE '%$a%' AND z_abonos.idmedio=$idmedio");
        $row2=$sql2->fetch_assoc();
        $monto = $row1['total']+$row2['total'];
        if($monto>0){$table .= "<tr><td>".$row['medio']."</td><td>".moneda($monto)."</td></tr>";}
        
    }
    $table .= '</table>';
    echo $table;
    exit();
}
if($con=='mes'){
    $sql=$mysqli->query("SELECT `id`,`medio` FROM `z_pagos_medios`");
    $table = '<table class="table">';
    while($row=$sql->fetch_assoc()){
        $idmedio = $row['id'];
        $sql1=$mysqli->query("SELECT SUM(z_pagos.`monto`) AS 'total' FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.`fecha` LIKE '%$a%' AND z_pagos.idmedio=$idmedio");
        $row1=$sql1->fetch_assoc();
        $sql2=$mysqli->query("SELECT SUM(z_abonos.`abono`) AS 'total' FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.`fecha` LIKE '%$a%' AND z_abonos.idmedio=$idmedio");
        $row2=$sql2->fetch_assoc();
        $monto = $row1['total']+$row2['total'];
        if($monto>0){$table .= "<tr><td>".$row['medio']."</td><td>".moneda($monto)."</td></tr>";}
        
    }
    $table .= '</table>';
    echo $table;
    exit();
}
if($con=='umes'){
    $sql=$mysqli->query("SELECT `id`,`medio` FROM `z_pagos_medios`");
    $table = '<table class="table">';
    while($row=$sql->fetch_assoc()){
        $idmedio = $row['id'];
        $sql1=$mysqli->query("SELECT SUM(z_pagos.`monto`) AS 'total' FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.`fecha`  BETWEEN '$de' AND '$a' AND z_pagos.idmedio=$idmedio");
        $row1=$sql1->fetch_assoc();
        $sql2=$mysqli->query("SELECT SUM(z_abonos.`abono`) AS 'total' FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.`fecha`  BETWEEN '$de' AND '$a' AND z_abonos.idmedio=$idmedio");
        $row2=$sql2->fetch_assoc();
        $monto = $row1['total']+$row2['total'];
        if($monto>0){$table .= "<tr><td>".$row['medio']."</td><td>".moneda($monto)."</td></tr>";}
        
    }
    $table .= '</table>';
    echo $table;
    exit();
}

?>