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

$sql0=$mysqli->query("SELECT * FROM `z_pedidos` WHERE `estado` != 4 AND `estado` != 5 AND `estado` != 0 AND `estado` != 1 AND `fecha` BETWEEN '2022-06-01 00:00:00.000000' AND '2022-09-30 23:59:59.999999 '");
while($row0=$sql0->fetch_assoc()){
    $codigo = $row0['codigopedido'];
    $code = substr("000$codigo", -6, 6);
    echo $code.'-'.moneda($row0['total']).' - '.moneda($row0['adicional']).' - '.sclien($row0['idcliente'])['nombre'].' - '.sinfo('z_epedidos',$row0['estado'])['estado'].'<br/>';
    
        $suma1 = 0;
    $sql1=$mysqli->query("SELECT * FROM `z_abonos` WHERE (`codigopedido`='$codigo' OR `codigopedido`='$code')");
    $num1 = $sql1->num_rows;
    echo 'ABONOS<br/>';
    if($num1 > 0){
        while($row1=$sql1->fetch_assoc()){
            echo $row1['abono'].' - ';
            $suma1 = $suma1 + $row1['abono'];
        }
        echo '<span style="color:red;">'.moneda($suma1).'</span>';
        $sumat = $sumat +$suma1;
    }
    $suma2 = 0;
    $sql2=$mysqli->query("SELECT * FROM `z_pagos` WHERE (`idpedido`='$codigo' OR `idpedido`='$code')");
    $num2 = $sql2->num_rows;
    echo '<br/>PAGOS<br/>';
    if($num2 > 0){
        while($row2=$sql2->fetch_assoc()){
            echo $row2['monto'].' - ';
            $suma2 = $suma2 +  $row2['monto'];
        }
        echo moneda($suma2); 
        $sumat = $sumat +               $suma2;
    }
    
    $subtotal = $suma1+$suma2;
    if(($row0['total']+$row0['adicional'])!=$subtotal){
        $sumae = $row0['total']+$row0['adicional'];
        $sumaet = $sumaet + $sumae;
        echo '<BR/>ERROR '.$sumae;
    }
    echo '<hr/>';
}
echo $sumaet.'<br/>';
echo $sumat;
?>