<?php
session_start();





require_once('fpdf.php');
class PDF extends FPDF{
	function Header()
{ 

}
function Footer()
{
    $this->SetY(15);
    $this->SetFont('Arial','I',12);
}
}


      $db_host = 'localhost';
$db_name = 'rblcomve_inventiolite';
$db_user = 'rblcomve_root';
$db_pass = '$0WBF2agJ.G_';
$connect = new mysqli ($db_host, $db_user, $db_pass, $db_name);

$inicial=$_POST['inicial'];
$final=$_POST['final'];
$products=$_POST['products'];




    $pdf = new PDF('P','mm','A4');
    $pdf->SetMargins(12,12,12);
    $pdf->SetAutoPageBreak(true,29);
    $pdf->AddPage();

     
 $pdf->Image('logo.png',70,15, 65, 40);


    $pdf->SetFillColor(102,178,255);
    $pdf->SetFont('Arial','B',8.4);
    $pdf->SetXY(10, 45);
 
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell(0,7,utf8_decode('CONTROL DE VENTAS MAYORES DE RBL C.A.'),'',1,'C');
       $pdf->SetFont('Arial','B',10);
    $pdf->Ln(2);
    $result2=mysqli_query($connect, "SELECT name from product where id=$products");
     while($row2 = mysqli_fetch_array($result2))
{  

 $name = $row2['name'];
}

    $pdf->Cell(0,7,'PRODUCTO: '.$name.'','',1,'C');
    $pdf->Ln(1);
    $pdf->SetFont('Arial','B',8.4);

    $pdf->Cell(13,7,utf8_decode(''),'',0,'C');
    $pdf->Cell(43,7,utf8_decode('Cod. Cliente'),'LTRB',0,'C',true);
    $pdf->Cell(83,7,utf8_decode('Nombres y Apellidos'),'LTRB',0,'C',true);
    $pdf->Cell(31.5,7,utf8_decode('Nro. Ventas'),'LTRB',0,'C',true);

    $pdf->Ln(8);




if ($products!=0) {

$result2=mysqli_query($connect, "SELECT e.`person_id`, sum(p.q) as suma, w.name, w.lastname, w.rif_ci 
FROM `sell` e, operation p, person w
WHERE e.`id` in (SELECT sell_id from operation where q>50)
and p.sell_id=e.id and p.product_id=$products and p.`operation_type_id`=2 and p.q>50
and w.id=e.person_id
group by e.`person_id` order by suma desc");


}


$total_total='';
 while($row2 = mysqli_fetch_array($result2))
{  

 $product_id = $row2['product_id'];
 $created_at = $row2['created_at'];

$dias = array('Domingo:','Lunes:','Martes:','Miercoles:','Jueves:','Viernes:');
$fecha = $dias[date('N', strtotime($created_at))].' '. $created_at;


 $person_id = $row2['person_id'];
 $suma = $row2['suma'];
 $name = $row2['name'];
 $lastname = $row2['lastname'];
$pdf->Cell(13,7,utf8_decode(''),'',0,'C');
    $pdf->Cell(43,7,$person_id,'LTRB',0,'C');
    $pdf->Cell(83,7,$name.' '.$lastname,'LTRB',0,'C');
    $pdf->Cell(31.5,7,$suma,'LTRB',1,'C');

    $total_total+=$suma;
}
$pdf->Cell(13,7,utf8_decode(''),'',0,'C');
 $pdf->Cell(157.5,7,'Total: '.$total_total,'LTRB',0,'R',true);
    





mysqli_close($connect);
   

$pdf->Output('mov_ventas.pdf','D');
?>