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

$result=mysqli_query($connect, "select * from financiero where forma_pago <> 6 and mov_contable=2 and fecha_movimiento between '$inicial' and '$final' order by fecha_movimiento, forma_pago asc ");

$result2=mysqli_query($connect, "select * from financiero where mov_contable=1 and fecha_movimiento between '$inicial' and '$final' order by fecha_movimiento,  forma_pago asc ");

$result3=mysqli_query($connect, "SELECT mov_contable, forma_pago, SUM(monto) as monto1 FROM financiero 
  WHERE mov_contable=1 and fecha_movimiento between '$inicial' and '$final' GROUP by forma_pago, mov_contable order by mov_contable");

$se="SELECT mov_contable, forma_pago, SUM(monto) as monto1 FROM financiero 
  WHERE mov_contable=2 and fecha_movimiento between '$inicial' and '$final' GROUP by forma_pago, mov_contable order by mov_contable";
//echo json_encode($se);


    $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',9);
    $pdf->SetXY(10, 45);
 
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell(0,7,'CONTROL DE GANANCIAS 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(9);
   $pdf->Cell(0,7,'PRECIO DE ENTRADA','',1,'C');
  $pdf->Cell(19,7,utf8_decode('Cod. Prod.'),'LTRB',0,'C',true);
    $pdf->Cell(100,7,utf8_decode('Denominacion'),'LTRB',0,'C',true);
    $pdf->Cell(33,7,utf8_decode('Cantidad. Sal.'),'LTRB',0,'C',true);
    $pdf->Cell(33,7,utf8_decode('Total.'),'LTRB',1,'C',true);


$result=mysqli_query($connect, "SELECT a.product_id, b.price_out, b.price_in, b.name, SUM(a.`q`) FROM `operation` a, product b WHERE a.operation_type_id=2 and a.created_at between '$inicial' and '$final'and b.id=a.product_id group by a.product_id");

    
$total_total11='';
 while($row2 = mysqli_fetch_array($result))
{  

 $product_id = $row2['product_id'];
 $name = $row2['name'];
 $product_id = $row2['product_id'];
 $suma = $row2['SUM(a.`q`)'];
 $price_out = $row2['price_out'];
 $price_in = $row2['price_in'];
 $total_total11+=$suma*$price_in;

 $pdf->Cell(19,7,$product_id,'LTRB',0,'C');
 $pdf->Cell(100,7,$name,'LTRB',0,'C');
 $pdf->Cell(33,7,$suma,'LTRB',0,'C');
 $pdf->Cell(33,7,number_format($suma*$price_in,2, ',', '.'),'LTRB',1,'C');
}

  $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total11,2, ',', '.'),'LTRB',1,'C',true);









    $pdf->Ln(9);
   $pdf->Cell(0,7,'PRECIO DE SALIDA','',1,'C');
    $pdf->Cell(19,7,utf8_decode('Cod. Prod.'),'LTRB',0,'C',true);
    $pdf->Cell(100,7,utf8_decode('Denominacion'),'LTRB',0,'C',true);
    $pdf->Cell(33,7,utf8_decode('Cantidad. Sal.'),'LTRB',0,'C',true);
    $pdf->Cell(33,7,utf8_decode('Total.'),'LTRB',1,'C',true);


$result=mysqli_query($connect, "SELECT a.product_id, b.price_out, b.price_in, b.name, SUM(a.`q`) FROM `operation` a, product b WHERE a.operation_type_id=2 and a.created_at between '$inicial' and '$final'and b.id=a.product_id group by a.product_id");

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

 $product_id = $row2['product_id'];
 $name = $row2['name'];
 $product_id = $row2['product_id'];
 $suma = $row2['SUM(a.`q`)'];
 $price_out = $row2['price_out'];
 $price_in = $row2['price_in'];
 $total_total+=$suma*$price_out;

 $pdf->Cell(19,7,$product_id,'LTRB',0,'C');
 $pdf->Cell(100,7,$name,'LTRB',0,'C');
 $pdf->Cell(33,7,$suma,'LTRB',0,'C');
 $pdf->Cell(33,7,number_format($suma*$price_out,2, ',', '.'),'LTRB',1,'C');
}

    $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total,2, ',', '.'),'LTRB',1,'C',true);















$pdf->Ln(9);
   $pdf->Cell(0,7,'GASTOS','',1,'C');
    $pdf->Cell(65,7,utf8_decode('Denominacion'),'LTRB',0,'C',true);
    $pdf->Cell(87,7,utf8_decode('Observaciones'),'LTRB',0,'C',true);
    $pdf->Cell(33,7,utf8_decode('Total.'),'LTRB',1,'C',true);


$result=mysqli_query($connect, "SELECT * FROM `financiero`  
  WHERE fecha_movimiento between '$inicial' and '$final'
    and denominacion not like '%INVENCEM%' 
    and denominacion not like '%COMPRESOR%'
    and denominacion not like '%RAFAEL BERMUDEZ%'
    and denominacion not like '%JUAN CARLOS CONTRERAS%'
    and denominacion not like '%CARLOS CONTRERAS%' and mov_contable=2 and forma_pago not in(6) 
    and observaciones not like '%INVECEM%'
        and observaciones not like '%INVENCEM%'
    and denominacion not like '%INVECEM%'
    and observaciones not like '%DIVISA%' and observaciones not like '%BOLSAS%' ");

    
$total_total33='';
 while($row2 = mysqli_fetch_array($result))
{  

 $product_id = $row2['product_id'];
 $denominacion = $row2['denominacion'];
 $observaciones = $row2['observaciones'];
 $suma = $row2['SUM(a.`q`)'];
 $monto = $row2['monto'];
 $price_in = $row2['price_in'];
$total_total33+=$monto;


$pdf->SetFont('Arial','B',8);
 $pdf->Cell(65,7,$denominacion,'LTRB',0,'C');
 $pdf->Cell(87,7,$observaciones,'LTRB',0,'C');
 $pdf->SetFont('Arial','B',9);
 $pdf->Cell(33,7,number_format($monto,2, ',', '.'),'LTRB',1,'C');
}

  $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total33,2, ',', '.'),'LTRB',1,'C',true);

$pdf->Ln(9);

  $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES INGRESOS P. ENTRADA:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total,2, ',', '.'),'LTRB',1,'C',true);

 $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES INGRESOS P. SALIDA:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total11,2, ',', '.'),'LTRB',1,'C',true);

 $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTALES GASTOS:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total33,2, ',', '.'),'LTRB',1,'C',true);

 $pdf->Cell(19,7,'','',0,'C',true);
    $pdf->Cell(100,7,'','',0,'R',true);
    $pdf->Cell(33,7,'TOTAL GANANCIAS:','',0,'C',true);
    $pdf->Cell(33,7,number_format($total_total-$total_total11-$total_total33,2, ',', '.'),'LTRB',1,'C',true);




mysqli_close($connect);
   

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