<?php

header('Content-Type: text/html; charset=utf-8');

require("fpdf182/fpdf.php");
require_once("phpqrcode/qrlib.php");



include('db.php');

$cmd=$_GET['cmd'];

$ver=$db->prepare('SELECT tbl_invoice.*, serveur.nom FROM tbl_invoice, serveur WHERE serveur.id=tbl_invoice.serveur AND tbl_invoice.num_cmd=?');
$ver->execute(array($cmd));
while ($vers=$ver->fetch()) {
    $num_cmd=$vers['num_cmd'];
    $nom_serveur=$vers['nom'];
    $tables=$vers['tables'];
    $total=$vers['total'];
    $remise=$vers['remise'];
    $nap=$vers['nap'];
    $order_date=$vers['order_date'];
    $order_time=$vers['order_time'];
}


$sel=$db->prepare('SELECT * FROM param');
$sel->execute(array());
while ($sels=$sel->fetch()) {
  $_SESSION['raison_sociale']=$sels['raison_sociale'];
  $_SESSION['adresse']=$sels['adresse'];
  $_SESSION['tel']=$sels['tel'];
}

$col=$db->prepare('SELECT * FROM facturation WHERE num_cmd=?');
    $col->execute(array($cmd));
    while ($cols=$col->fetch()) {
        $emcf_qr_code=$cols['emcf_qr_code'];
        $emcf_date=$cols['emcf_date'];
        $emcf_code_me_ce_fdgi=$cols['emcf_code_me_ce_fdgi'];
        $emcf_counters=$cols['emcf_counters'];
        $emcf_nim=$cols['emcf_nim'];
        if (strstr($emcf_code_me_ce_fdgi, "TEST")) {
            $emcf_code_me_ce_fdgi=strtr($emcf_code_me_ce_fdgi, "TEST-", " ");
        }
    }
    
    

$pdf = new FPDF ('P','mm',array(80,150));

$pdf->AddPage();


$pdf->SetFont('Arial','B',13);

$pdf->Cell(60,8,$_SESSION['raison_sociale'],1,1,'C');

$pdf->SetFont('Arial','B',7);

//$pdf->Cell(60,5,'Block 5 Saadi Town, Karachi',0,1,'C');
$pdf->Cell(60,5,'Contact : '.$_SESSION['tel'].'',0,1,'C');
$pdf->Ln(2);
$pdf->Cell(60,5,'Adresse : '.$_SESSION['adresse'].'',0,1,'C');


//Line(x1,y1,x2,y2);
//$pdf->Line(7,38,72,38);




//Line break
$pdf->Ln(10);

$pdf->SetFont('Arial','B',7);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(10,4,'Table: ',0,0,'');
//$pdf->SetFont('Arial','BI',8);
$pdf->Cell(35,4,utf8_decode($tables),0,0,'');


//$pdf->SetFont('Arial','BI',8);
$pdf->Cell(12,4,'Serveur: ',0,0,'');

$pdf->SetFont('Arial','B',7);
$pdf->Cell(40,4,utf8_decode($nom_serveur),0,1,'');
$pdf->Ln(2);


$pdf->SetFont('Arial','B',7);
$pdf->Cell(20,4,'Ticket no: ',0,0,'');

$pdf->SetFont('Arial','B',7);
$pdf->Cell(10,4,$num_cmd,0,1,'');
$pdf->Ln(2);

//data to be stored in qr
$item =$emcf_qr_code;

//file path
$file = "images/qr1.png";

//other parameters
$ecc = 'H';
$pixel_size = 20;
$frame_size = 5;

// Generates QR Code and Save as PNG
QRcode::png($item, $file, $ecc, $pixel_size, $frame_size);

// Displaying the stored QR code if you want
//echo "<div><img src='".$file."' width='150'></div>";
$pdf->Image('images/qr1.png', 55, 20, 20, 20, 'png','images/qr1.png');

//Line break
$pdf->Ln(1);



$pdf->SetFont('Arial','B',7);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(8,4,'Date : ',0,0,'');
//$pdf->SetFont('Arial','BI',8);
$pdf->Cell(40,4,$order_date,0,0,'');


//$pdf->SetFont('Arial','BI',8);
$pdf->Cell(10,4,'Heure : ',0,0,'');

$pdf->SetFont('Arial','B',7);
$pdf->Cell(40,4,$order_time,0,1,'');
$pdf->Ln(2);

/////////////////////////



$pdf->SetX(7);
$pdf->SetFont('Courier','B',7);
$pdf->SetFillColor(208,208,208);
$pdf->Cell(38,5,'DESIGNATION',1,0,'C'); //100
$pdf->Cell(11,5,'QTE',1,0,'C');
$pdf->Cell(8,5,'PRIX',1,0,'C');
$pdf->Cell(12,5,'TOTAL',1,1,'C');


$select=$db->prepare('SELECT * FROM table_invoice_details WHERE num_cmd=? AND valid=1');
$select->execute(array($num_cmd));
//$item=$select->fetch(PDO::FETCH_OBJ)
    
    
    while($item=$select->fetch(PDO::FETCH_OBJ)){
        
     $pdf->SetX(7);   
        $pdf->SetFont('Helvetica','B',7);
$pdf->Cell(38,5,utf8_decode($item->designation),1,0,'L'); //100
$pdf->Cell(11,5,$item->qte,1,0,'C');
$pdf->Cell(8,5,$item->price,1,0,'C');
$pdf->Cell(12,5,$item->total,1,1,'C');
        
    }










$pdf->Ln(2);
//product table code

$pdf->SetX(7);
$pdf->SetFont('courier','B',7);
$pdf->Cell(20,5,'',0,0,'L'); //100
//$pdf->Cell(20,5,'',0,0,'C');
$pdf->Cell(29,5,'SOUS-TOTAL',1,0,'C');
$pdf->Cell(20,5,$total,1,1,'C');
$pdf->Ln(2);


$pdf->SetX(7);
$pdf->SetFont('courier','B',7);
$pdf->Cell(20,5,'',0,0,'L'); //100
//$pdf->Cell(20,5,'',0,0,'C');
$pdf->Cell(29,5,'REMISE',1,0,'C');
$pdf->Cell(20,5,$remise,1,1,'C');
$pdf->Ln(2);






$pdf->SetX(7);
$pdf->SetFont('courier','B',7);
$pdf->Cell(20,5,'',0,0,'L'); //100
//$pdf->Cell(20,5,'',0,0,'C');
$pdf->Cell(29,5,'NET A PAYER',1,0,'C');
$pdf->Cell(20,5,$nap,1,1,'C');
$pdf->Ln(2);


//$pdf->SetX(7);
//$pdf->SetFont('courier','B',8);
//$pdf->Cell(20,5,'',0,0,'L'); //100
////$pdf->Cell(20,5,'',0,0,'C');
//$pdf->Cell(25,5,'PAID',1,0,'C');
//$pdf->Cell(20,5,$row->paid,1,1,'C');



//$pdf->SetX(7);
//$pdf->SetFont('courier','B',8);
//$pdf->Cell(20,5,'',0,0,'L'); //100
////$pdf->Cell(20,5,'',0,0,'C');
//$pdf->Cell(25,5,'DUE',1,0,'C');
//$pdf->Cell(20,5,$row->due,1,1,'C');


//$pdf->SetX(7);
//$pdf->SetFont('courier','B',8);
//$pdf->Cell(20,5,'',0,0,'L'); //100
//$pdf->Cell(20,5,'',0,0,'C');
//$pdf->Cell(25,5,'MODE PAIEMENT',1,0,'C');
//$pdf->Cell(20,5,$row->payment_type,1,1,'C');


//$pdf->Cell(60,5,'Invoice',0,1,'C');

//$pdf->SetFont('Arial','',8);
//$pdf->Cell(85,5,'Invoice :' .$row->invoice_id,0,1,'R');
//
//$pdf->SetFont('Arial','',8);
//$pdf->Cell(87,5,'Date : '.$row->order_date,0,1,'R');



$pdf->SetX(7);
$pdf->Cell(20,7,'-----------Infos Mecef--------------',0,1,'');


$pdf->SetFont('Arial','BI',8);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(15,4,'Heure : ',0,0,'');
$pdf->SetFont('Arial','B',7);
$pdf->Cell(20,4,utf8_decode($emcf_date),0,1,'');
$pdf->Ln(1);

$pdf->SetFont('Arial','BI',8);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(15,4,'Code DGI : ',0,0,'');
$pdf->SetFont('Arial','B',7);
$pdf->Cell(20,4,utf8_decode($emcf_code_me_ce_fdgi),0,1,'');
$pdf->Ln(1);

$pdf->SetFont('Arial','BI',8);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(17,4,'Compteurs : ',0,0,'');
$pdf->SetFont('Arial','B',7);
$pdf->Cell(20,4,utf8_decode($emcf_counters),0,1,'');
$pdf->Ln(1);

$pdf->SetFont('Arial','BI',8);
//$pdf->Cell(20,4,'Date : ',0,0,'');
$pdf->Cell(10,4,'NIM : ',0,0,'');
$pdf->SetFont('Arial','B',7);
$pdf->Cell(20,4,utf8_decode($emcf_nim),0,1,'');
$pdf->Ln(2);


$pdf->SetX(7);
$pdf->SetFont('Courier','BI',8);
$pdf->Cell(75,3,'MERCI',0,1,'');
$pdf->Ln(2);

//$pdf->SetX(7);
//$pdf->SetFont('Courier','BI',8);
//$pdf->Cell(75,3,'Contact  : +229 96127228',0,1,'');




$pdf->Output();
?>