<?php
session_start();
require('../fpdf/fpdf.php');
require('plantilla_general.php');
require '../../../conexion.php';
require_once('../../../include/sesiones.php');

$cliente=$_GET['cliente'];
$hasta1=$_GET['hasta'];
$sucursal_form=$_GET['sucursal_form'];

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->AliasNbPages();
$pdf->Cell(0,10,'VENTAS CXC HASTA '.$hasta1,0,1);
$pdf->SetFont('Arial','',7);

if($cliente==0)
{
    $query=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta1' and sucursal='$sucursal_form' and empresa='$empresa' and ven_estado='sin pagar' group by cliente_clie_id ") or die(mysqli_error($con));
    $TOTAL_TODO=0;
    while ($roww=mysqli_fetch_array($query))    
    {
        $cliente_clie_id=$roww['Cliente_clie_id'];
        $pdf->SetFillColor(215,235,2415);
        $pdf->SetFont('Arial','B',8);
        $pdf->Cell(159,4,utf8_decode($roww['ven_nombre']),1,0,'C',1);
        $pdf->Cell(30,4,'TEL: ' .utf8_decode($roww['ven_telefono']),1,0,'C',1);
        $pdf->Ln();

        $pdf->SetFillColor(235,225,215);
        $pdf->SetFont('Arial','B',8);
        $pdf->Cell(23,4,utf8_decode('FACTURA N°'),1,0,'C',1);
        $pdf->Cell(23,4,'FECHA',1,0,'C',1);
        $pdf->Cell(35,4,'FECHA VENCIMIENTO',1,0,'C',1);
        $pdf->Cell(23,4,'DIAS',1,0,'C',1);
        $pdf->Cell(22,4,'TOTAL',1,0,'C',1);
        $pdf->Cell(21,4,'ABONO',1,0,'C',1);
        $pdf->Cell(21,4,utf8_decode('DEVOLUCIÓN'),1,0,'C',1);
        $pdf->Cell(21,4,'SALDO',1,0,'C',1);
        $pdf->Ln();    

        $total_cliente=0;
        $total_abonos=0;
        $total_saldo=0;
        $total_devoluciones=0;
        $sumador_saldo=0;   
        $saldo_vencido=0;
        $registross=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta1' and sucursal='$sucursal_form' and empresa='$empresa' and cliente_clie_id='$cliente_clie_id' and ven_estado='sin pagar' ") or die(mysqli_error($con));
        while ($reg=mysqli_fetch_array($registross))
        {             
            $ven_id=$reg['ven_id'];
            $ven_fecha=$reg['ven_fecha'];
            $ven_dias=$reg['ven_dias'];

            $vence = strtotime($ven_fecha."+ ".$ven_dias." days");
            $vence_fecha=date("Y-m-d",$vence) . "\n";
            $fecha=date('Y-m-d ');

            $hasta=date("Y-m-d",$vence) . "\n";
            $hoy=$fecha;
            $dias = (strtotime($hasta)-strtotime($hoy))/86400;
            $dias = abs($dias);

            if ($hasta >= $hoy){ $dias = '-' .floor($dias);}
            else{ $dias = '+' .floor($dias); }

            $sumador_combo=0;
            $sumador_total=0;
            $neto=0;
            $total_descuento=0;
            $total_iva=0;
            $sumador_articulos=0;

            $vencido=0;
            $total_retencion=0;
            $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_venta where empresa='$empresa' and ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
            $rets=mysqli_fetch_array($ret);
            $total_retencion=$rets['ret_importe'];

            include('../../../include/total_venta.php');

            $sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;
            $total_cliente+=$sumador_total;
           
            //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=$sumador_total-$total_abono-$devo_total;
            if ($saldo_t<=0) 
            {
                $saldo='0';
            }
            else
            {
                $saldo=$saldo_t;
            }

            $ven_fecha=$reg['ven_fecha'];
            $ven_dias=$reg['ven_dias'];

            $vence = strtotime($ven_fecha."+ ".$ven_dias." days");
            date("Y-m-d",$vence) . "\n";
            $fecha=date('Y-m-d ');

            $hasta=date("Y-m-d",$vence) . "\n";
            $hoy=$fecha;
            $dias = (strtotime($hasta)-strtotime($hoy))/86400;
            $dias = abs($dias);

            if($hasta >= $hoy)
            {
                $dias = '-' .floor($dias);
            }
            else
            {
                $dias = '+' .floor($dias);
                $vencido+=$saldo;
            }



            $saldo=($sumador_total-$total_abono)-$devo_total;
            $total_abonos+=$total_abono;
            $total_saldo+=$saldo;

            $pdf->SetWidths(array(23,23,35,23,22,21,21,21));
            srand(time()*1000000);
            $pdf->Row(array($reg['ven_n_venta'],$reg['ven_fecha'],$vence_fecha,$dias,number_format($sumador_total,0),number_format($total_abono,0),number_format($devo_total,0), number_format($saldo,0)));

            $sumador_saldo+=$saldo;
            $saldo_vencido+= $vencido;
        }

        $pdf->SetFillColor(255,128,0);
        $pdf->SetFont('Arial','B',8);
        $pdf->Cell(104,4,'TOTALES',1,0,'C',1);
        $pdf->Cell(22,4,number_format($total_cliente,0),1,0,'C',1);
        $pdf->Cell(21,4,number_format($total_abonos,0),1,0,'C',1);
        $pdf->Cell(21,4,number_format($total_devoluciones,0),1,0,'C',1);
        $pdf->Cell(21,4,number_format($total_saldo,0),1,0,'C',1);
        $pdf->Ln();

        if ($saldo_vencido>0)
        {
            $pdf->SetFillColor(255,0,0);
            $pdf->SetFont('Arial','B',8);
            $pdf->Cell(168,4,'CARTERA VENCIDA',1,0,'R',1);
            $pdf->Cell(21,4,number_format($saldo_vencido,0),1,0,'C',1);
            $pdf->Ln();
            $pdf->SetFillColor(255,255,255);

            $pdf->SetFillColor(255,255,255);
            $pdf->SetFont('Arial','B',8);
            $pdf->Cell(189,4,'',1,0,'C',1);
            $pdf->Ln(); 
          
        }
          $TOTAL_TODO+=$total_saldo;
        
    }


    $pdf->Ln();
    $pdf->SetFillColor(255,255,0);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(168,4,'TOTAL CARTERA',1,0,'C',1);
    $pdf->Cell(21,4,number_format($TOTAL_TODO,0),1,0,'C',1);
    $pdf->Output();
}




else
{
    $query=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta1' and empresa='$empresa' and ven_estado='sin pagar' AND cliente_clie_id='$cliente'") or die(mysqli_error($con));
    $roww=mysqli_fetch_array($query);

    $pdf->SetFillColor(215,235,2415);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(159,4,utf8_decode($roww['ven_nombre']),1,0,'C',1);
    $pdf->Cell(30,4,'TEL: ' .utf8_decode($roww['ven_telefono']),1,0,'C',1);
    $pdf->Ln();

    $pdf->SetFillColor(235,225,215);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(23,4,utf8_decode('FACTURA N°'),1,0,'C',1);
    $pdf->Cell(23,4,'FECHA',1,0,'C',1);
    $pdf->Cell(35,4,'FECHA VENCIMIENTO',1,0,'C',1);
    $pdf->Cell(23,4,'DIAS',1,0,'C',1);
    $pdf->Cell(22,4,'TOTAL',1,0,'C',1);
    $pdf->Cell(21,4,'ABONO',1,0,'C',1);
    $pdf->Cell(21,4,utf8_decode('DEVOLUCIÓN'),1,0,'C',1);
    $pdf->Cell(21,4,'SALDO',1,0,'C',1);
    $pdf->Ln();    

    $cliente_clie_id=$roww['Cliente_clie_id'];
    $sumador_saldo=0;   
    $saldo_vencido=0;

    $registross=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta1' and empresa='$empresa' and cliente_clie_id='$cliente_clie_id' and ven_estado='sin pagar' ") or die(mysqli_error($con));
    while ($reg=mysqli_fetch_array($registross))
    { 
        $ven_id=$reg['ven_id'];            
        
        $sumador_combo=0;
        $sumador_total=0;
        $neto=0;
        $total_descuento=0;
        $total_iva=0;
        $sumador_articulos=0;

        $vencido=0;

        $total_retencion=0;
        $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_venta where empresa='$empresa' and ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
        $rets=mysqli_fetch_array($ret);
        $total_retencion=$rets['ret_importe'];

        include('../../../include/total_venta.php');

        $sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;

        $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'];

        $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=$sumador_total-$total_abono-$devo_total;
        if ($saldo_t<=0) 
        {
            $saldo='0';
        }
        else
        {
            $saldo=$saldo_t;
        }   

        $ven_fecha=$reg['ven_fecha'];
        $ven_dias=$reg['ven_dias'];

        $vence = strtotime($ven_fecha."+ ".$ven_dias." days");
        $vence_fecha=date("Y-m-d",$vence) . "\n";
        date("Y-m-d",$vence) . "\n";
        $fecha=date('Y-m-d ');

        $hasta=date("Y-m-d",$vence) . "\n";
        $hoy=$fecha;
        $dias = (strtotime($hasta)-strtotime($hoy))/86400;
        $dias = abs($dias);

        if($hasta >= $hoy)
        {
            $dias = '-' .floor($dias);
        }
        else
        {
            $dias = '+' .floor($dias);
            $vencido+=$saldo;
        }

        $pdf->SetWidths(array(23,23,35,23,22,21,21,21));
        srand(time()*1000000);
        $pdf->Row(array($reg['ven_n_venta'],$reg['ven_fecha'],$vence_fecha,$dias,number_format($sumador_total,0),number_format($total_abono,0),number_format($devo_total,0), number_format($saldo,0)));

        $sumador_saldo+=$saldo;
        $saldo_vencido+= $vencido;
    }
    
    $pdf->SetFillColor(255,128,0);
    $pdf->Cell(168,4,'TOTAL',1,0,'R',1);
    $pdf->Cell(21,4,number_format($sumador_saldo,0),1,0,'C',1);
    $pdf->Ln();

    $pdf->SetFillColor(255,0,0);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(168,4,'CARTERA VENCIDA',1,0,'R',1);
    $pdf->Cell(21,4,number_format($saldo_vencido,0),1,0,'C',1);
    $pdf->Ln();
    $pdf->SetFillColor(255,255,255);

  

   

    $pdf->Output();
}
?>