<?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 PRODUCCIÓN SEMANAL DE RBL C.A.'),'',1,'C');
       $pdf->SetFont('Arial','B',10);
    $pdf->Ln(2);
    $pdf->Cell(0,7,'SEMENA DEL: '.$inicial.' AL: '.$final.'','',1,'C');
    $pdf->Ln(1);
    $pdf->SetFont('Arial','B',8.4);


    $pdf->Cell(43,7,utf8_decode('FECHA DE PRODUCCIÓN'),'LTRB',0,'C',true);
    $pdf->Cell(83,7,utf8_decode('RUBRO/PRODUCTO PRODUCIDO'),'LTRB',0,'C',true);
    $pdf->Cell(31.5,7,utf8_decode('PROD. AL DIA'),'LTRB',0,'C',true);

    $pdf->Ln(8);




if ($products!=0) {

$result2=mysqli_query($connect, "SELECT a.product_id, sum(a.q) as suma, a.created_at, b.name FROM `operation` a, product b WHERE `product_id` = '$products' AND `operation_type_id` = 1 AND a.`created_at` BETWEEN '$inicial' AND '$final' 
    and b.id=a.product_id GROUP by a.product_id, a.created_at, b.name");

$result3=mysqli_query($connect, "SELECT a.product_id, sum(a.q) as suma, a.created_at, b.name FROM `operation` a, product b WHERE `product_id` = '$products' AND `operation_type_id` = 2 AND a.`created_at` BETWEEN '$inicial' AND '$final' and  a.sell_id NOT IN (SELECT id  FROM `sell` WHERE `person_id` = 45 and operation_type_id=2 )
    and b.id=a.product_id GROUP by a.product_id, a.created_at, b.name");


$result4=mysqli_query($connect, "SELECT a.product_id, sum(a.q) as suma, a.created_at, b.name FROM `operation` a, product b WHERE `product_id` = '$products' AND `operation_type_id` = 2 AND a.`created_at` BETWEEN '$inicial' AND '$final'  and  a.sell_id  IN (SELECT id  FROM `sell` WHERE `person_id` = 45 and operation_type_id=2 )
    and b.id=a.product_id GROUP by a.product_id, a.created_at, b.name");


}


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


 $name = $row2['name'];
 $suma = $row2['suma'];
 $forma_pago = $row2['forma_pago'];

    $pdf->Cell(43,7,$fecha,'LTRB',0,'C');
    $pdf->Cell(83,7,$name,'LTRB',0,'C');
    $pdf->Cell(31.5,7,$suma,'LTRB',1,'C');

    $total_total+=$suma;
}

 $pdf->Cell(157.5,7,'Total: '.$total_total,'LTRB',0,'R',true);
    













     $pdf->Ln(18);

    $pdf->Cell(43,7,utf8_decode('FECHA DE PRODUCCIÓN'),'LTRB',0,'C',true);
    $pdf->Cell(83,7,utf8_decode('RUBRO/PRODUCTO PRODUCIDO'),'LTRB',0,'C',true);
    $pdf->Cell(31.5,7,utf8_decode('VENTA AL DIA'),'LTRB',1,'C',true);
$total_total2='';
 while($row3 = mysqli_fetch_array($result3))
{  

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

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


 $name = $row3['name'];
 $suma = $row3['suma'];
 $forma_pago = $row3['forma_pago'];

    $pdf->Cell(43,7,$fecha,'LTRB',0,'C');
    $pdf->Cell(83,7,$name,'LTRB',0,'C');
    $pdf->Cell(31.5,7,$suma,'LTRB',1,'C');

     $total_total2+=$suma;
}

 $pdf->Cell(157.5,7,'Total: '.$total_total2,'LTRB',0,'R',true);













 $pdf->Ln(18);
 $pdf->Cell(157.5,7,utf8_decode('USO BLOQUERA'),'LTRB',1,'C',true);
    $pdf->Cell(43,7,utf8_decode('FECHA DE PRODUCCIÓN'),'LTRB',0,'C',true);
    $pdf->Cell(83,7,utf8_decode('RUBRO/PRODUCTO USADO'),'LTRB',0,'C',true);
    $pdf->Cell(31.5,7,utf8_decode('CANTIDAD AL DIA'),'LTRB',1,'C',true);
$total_total3='';
 while($row3 = mysqli_fetch_array($result4))
{  

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

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


 $name = $row3['name'];
 $suma = $row3['suma'];
 $forma_pago = $row3['forma_pago'];

    $pdf->Cell(43,7,$fecha,'LTRB',0,'C');
    $pdf->Cell(83,7,$name,'LTRB',0,'C');
    $pdf->Cell(31.5,7,$suma,'LTRB',1,'C');

     $total_total3+=$suma;
}

 $pdf->Cell(157.5,7,'Total: '.$total_total3,'LTRB',1,'R',true);
 $pdf->Ln(4);
 $monoto=$total_total+$total_total2+$total_total3;
$pdf->Cell(157.5,7,'TOTAL GENERAL: '.$monoto,'LTRB',0,'R',true);









mysqli_close($connect);
   

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