<?php
include("../../db.php");
require('fpdf.php');

//estraggo impostazioni etichette
$result = mysql_query("SELECT *, DATE_FORMAT(dataora,'%d/%m/%Y') AS data FROM orders LEFT JOIN ordersitems ON orders.id_order= ordersitems.id_order WHERE orders.id_order = ".$_REQUEST['id']);

if(mysql_num_rows($result) > 0){
	
	//destinatario
	$idordine = mysql_result($result,0,'id_order');
	$codordine = str_pad($idordine,6,'0',STR_PAD_LEFT);
	$data = mysql_result($result,0,'data');
	$type = strtoupper(mysql_result($result,0,'type'));
	mysql_result($result,0,'priority') == 1 ? $sped = 'con assicurazione' : $sped = 'senza assicurazione';
	
	$buyer = 'Spett.le'.chr(10).chr(10);
	$buyer .= mysql_result($result,0,'surname').' ';
	$buyer .= mysql_result($result,0,'name').chr(10);
	$buyer .= mysql_result($result,0,'address').chr(10);
	$buyer .= mysql_result($result,0,'cap').' - ';
	$buyer .= mysql_result($result,0,'city');
	$buyer .= ' ('.mysql_result($result,0,'prov').')'.chr(10);
	$buyer .= mysql_result($result,0,'stato').'     Tel: '.mysql_result($result,0,'tel');
	$totprz = mysql_result($result,0,'prz');
	
	mysql_data_seek($result,0);

	//articoli
	while($row = mysql_fetch_array($result)){
		$arrItems[$row['item_name']]['qta'] = $row['item_qta'];
		$arrItems[$row['item_name']]['prz'] = $row['item_prz'];
	}
}


$pdf=new FPDF();
$pdf->SetAutoPageBreak(true,15);
$pdf->SetMargins(15,15,15);
$pdf->SetFillColor(240);
$pdf->AddPage();
$pdf->AliasNbPages();

//intestazione
$pdf->Image('../images/logofatt.gif', 15, 15, 23, 7, 'GIF');
$pdf->SetFont('arial','B',20);
$pdf->SetXY(45,15);
$pdf->SetFont('arial','B',20);
$pdf->Cell(180,10,'Store - Fondazione Pistoletto','',1);
$pdf->setFontSize(8);
$pdf->SetX(45);
$pdf->MultiCell(180,4,'via Serralunga, 27'.chr(10).'13900 - Biella - Italy'.chr(10).'P.IVA IT 01945080024','',1);
$pdf->Ln(10);

//destinatario
$pdf->SetFont('arial','',8);
$pdf->SetDrawColor(220);
$pdf->MultiCell(90,5,$buyer,1,'',true);

//info generali fattura
$pdf->SetXY(105,47);
$pdf->Cell(90,10,'Codice ordine:   '.$codordine,1,1,'',true);
$pdf->SetX(105);
$pdf->Cell(90,10,'Data ordine:   '.$data,1,1,'',true);
$pdf->SetX(105);
$pdf->Cell(90,10,'Pagamento:   '.$type.' ('.$sped.')',1,1,'',true);

$pdf->Ln(15);

//intestazione elenco items
$pdf->SetFont('arial','B',8);
$pdf->Cell(105,10,'Prodotto','B',0,'',true);
$pdf->Cell(25,10,'Prezzo unitario','B',0,'R',true);
$pdf->Cell(25,10,'Quantità','B',0,'R',true);
$pdf->Cell(25,10,'Totale','B',1,'R',true);

//ciclo elenco prodotti
$pdf->SetFont('arial','',8);
foreach($arrItems as $name => $dati){
	$pdf->Cell(105,8,$name,'B',0);
	$pdf->Cell(25,8,$dati['prz'].' €','B',0,'R');
	$pdf->Cell(25,8,$dati['qta'],'B',0,'R');
	$pdf->Cell(25,8,number_format(($dati['prz'] * $dati['qta']),2,',','').' €','B',1,'R');
	
	$prz = $prz + ($dati['prz'] * $dati['qta']);
}

$pdf->SetY(-35);

/*
//totale parziale
$pdf->SetFont('arial','B',10);
$pdf->Cell(155,7,'Subtotale','B',0,'',true);
$pdf->SetFont('arial','',10);
$pdf->Cell(25,7,number_format($prz,2,',','').' €','B',1,'R',true);
*/

//spedizione
$pdf->SetFont('arial','B',10);
$pdf->Cell(155,7,'Costo spedizione','',0,'',true);
$pdf->SetFont('arial','',10);
$pdf->Cell(25,7,number_format(($totprz-$prz),2,',','').' €','',1,'R',true);

$pdf->SetFillColor(100);
$pdf->SetTextColor(255);

//totale
$pdf->SetFont('arial','B',10);
$pdf->Cell(155,7,'Prezzo totale (iva compresa)','B',0,'',true);
$pdf->Cell(25,7,number_format($totprz,2,',','').' €','B',1,'R',true);
$pdf->Ln(5);

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