<?php
session_start();
require('../fpdf/fpdf.php');
require('plantilla_general.php');
require '../../../conexion.php';
require_once('../../../include/sesiones.php');
$cliente=$_GET['cliente'];
$hasta=$_GET['hasta'];
$sucursal_form=$_GET['sucursal_form'];

$saldo_global=0;
$query_str="SELECT * FROM ventas WHERE sucursal='$sucursal_form' AND empresa='$empresa' AND ven_estado<>'anulada' AND ven_estado='sin pagar' AND cliente_clie_id='$cliente' ORDER BY ven_id DESC";

$query=mysqli_query($con, $query_str);

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->AliasNbPages();
$pdf->Cell(0,10,'VENTAS CXC DETALLADAS HASTA '.$hasta,0,1 );
$pdf->SetFont('Arial','',7);
    
while ($roww=mysqli_fetch_array($query))    
{
    $ven_id=$roww['ven_id'];
    $total=0;

    if ($precio_iva=='no') {
        include('.detalles.php');
     } else {
        include('.detalles_iva.php');
     }

    //abono
    $sql=mysqli_query($con, "SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id' ");
    $sql=mysqli_fetch_array($sql);
    $total_abono=$sql['total'];
    //devolucion
    $devo_total=0;
    $sql1=mysqli_query($con, "SELECT * from devolucion where empresa='$empresa' and ventas_ven_id='$ven_id'");
    while ($row=mysqli_fetch_array($sql1))
    {
        $dev_precio=$row['dev_precio'];
        $devo_total+=$dev_precio; 
    }

    $saldo_t=$total-$total_abono-$devo_total;
    if ($saldo_t<=0) 
    {
        $saldo='0';
    }
    else
    {
        $saldo=$saldo_t;
    }

    $pdf->Ln();
    $pdf->SetFillColor(255,255,255);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(165,4,'ABONO',1,0,'R',1);
    $pdf->Cell(25,4,number_format($total_abono,0),1,0,'R',1);
    $pdf->Ln();
    $pdf->SetFillColor(255,255,255);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(165,4,'DEVOLUCION',1,0,'R',1);
    $pdf->Cell(25,4,number_format($devo_total,0),1,0,'R',1);
    $pdf->Ln();
    $pdf->SetFillColor(255,0,0);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(165,4,'SALDO',1,0,'R',1);
    $pdf->Cell(25,4,number_format($saldo_t,0),1,0,'R',1);
    $pdf->Ln();
    $pdf->Ln();
    $saldo_global+=$saldo_t;
}

$pdf->SetFillColor(239,215,025);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(165,4,'TOTAL CARTERA',1,0,'C',1);
$pdf->Cell(25,4,number_format($saldo_global,0),1,0,'R',1);
$pdf->Ln();          
$pdf->Output();
?>