<?php
error_reporting(0);
ini_set('display_errors', 0);
ob_start(); 
include ("jpgraph/src/jpgraph.php");
include ("jpgraph/src/jpgraph_pie.php");
include ("jpgraph/src/jpgraph_pie3d.php");
require_once (dirname(__FILE__) . "/config/db.php");
require_once (dirname(__FILE__) . "/config/conexion.php");

if (!$con) {
    die("Error de conexión: " . mysqli_connect_error());
}

$desde = $_GET['desde'];
$hasta = $_GET['hasta'];

$sTable = "compras";
$sWhere = " WHERE 1=1";

if (!empty($desde) && !empty($hasta)) {
    $sWhere .= " AND fecha BETWEEN '$desde' AND '$hasta'";
} elseif (!empty($desde)) {
    $sWhere .= " AND fecha = '$desde'";
}

$deuda = 0;
$pagos = 0;

$sql = "SELECT por_pagar, abono FROM $sTable $sWhere";
$query = mysqli_query($con, $sql);

while ($d = mysqli_fetch_array($query)) {
    $deuda += $d['por_pagar'];  
    $pagos += $d['abono'];      
}

if ($deuda == 0 && $pagos == 0) { $data = array(0.1, 0.1); } 
else { $data = array($pagos, $deuda); }

$graph = new PieGraph(400, 400); 
$graph->img->SetAntiAliasing();
$graph->title->Set("RESUMEN DE COMPRAS ($)");
$graph->title->SetFont(FF_FONT1, FS_BOLD);

$p1 = new PiePlot3D($data);
$p1->SetSliceColors(array("#3498db", "#e74c3c")); 
$p1->SetSize(0.5);
$p1->SetCenter(0.5);

$p1->value->SetFont(FF_FONT1, FS_BOLD);
$p1->value->SetColor("black");
$p1->SetLabelPos(0.5);

$nombres = array("Abonado", "Pendiente");
$p1->SetLegends($nombres);
$p1->ExplodeAll();

$graph->Add($p1);
ob_end_clean(); 
$graph->Stroke();