<?php
//error_reporting(E_ALL ^ E_DEPRECATED); ini_set('display_errors', 1);
include('script/variablen.php');
include('connect.php');

if ( !is_numeric($_GET['produkt']) )
{
	header("Location: http://www.fuxtools.ch/404.php");
	exit;
}

// Produkteinfos
$result_produkt = mysqli_query($dbi,"SELECT nummer, marke, produkt, hauptrubrik, text, artikelnr, daten, inkl, preis, porto, rabatt, made, black, black_rabatt, black_stk, preissperre FROM produkte WHERE nummer='".intval($_GET['produkt'])."'");
$row_produkt = $result_produkt->fetch_assoc();

// Wenn Produkt nicht vorhanden
if ( mysqli_num_rows($result_produkt) == 0 )
{
	header("Location: https://www.fuxtools.ch/404.php");
	exit;
}

$timestamp = time();
$welcher_rabatt = ''; // Normal/Sonder/Black

// Marke
$result_marke = mysqli_query($dbi,"SELECT nummer, marke, franko FROM marken WHERE nummer='".$row_produkt['marke']."' AND aktiv='1'");
$row_marke = $result_marke->fetch_assoc();

// Include the main TCPDF library (search for installation path).
require_once('script/tcpdf_6_4_4/tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// disable header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// PDF schützen
$pdf->SetProtection(array('copy','modify'), '', null, 0, null);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Fux Elektrowerkzeuge GmbH, Rümlingen');
$pdf->SetTitle(''.html_entity_decode(stripslashes($row_marke['marke'])).' '.html_entity_decode(stripslashes($row_produkt['produkt'])).'');

// add a page
$pdf->AddPage();

$pdf->Image('images/mail_header_print.png', 5, 5, 200);

$pdf->SetFont('Arial','',10);

$pdf->setListIndentWidth(6);

$pdf->setHtmlVSpace(array(
            'ul' => array(0 => array('h' => '', 'n' => 0), 1 => array('h' => '', 'n' => 0)),
            ));

$datum_pdf = date("d.m.Y", $timestamp);

$html = '
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td colspan="3" style="font-size:8px;">PDF erstellt am: '.$datum_pdf.'<br />Alle Angaben ohne Gewähr und solange Vorrat reicht!<br /></td>
	</tr>
	<tr>
		<td colspan="3" style="font-weight:bold; font-size:13px;">'.html_entity_decode(stripslashes($row_marke['marke'])).' '.html_entity_decode(stripslashes($row_produkt['produkt'])).'</td>
	</tr>';
// Artikelnummer
if ( $row_produkt['artikelnr'] != NULL )
{
$html.=	'
	<tr>
		<td colspan="3" style="font-size:8px;">Bestell- und Artikelnummer: '.html_entity_decode(stripslashes($row_produkt['artikelnr'])).'</td>
	</tr>';
}

$html .= '
	<tr>
		<td colspan="3" height="10">&nbsp;</td>
	</tr>';
	
// wenn kein Preis eingetragen
if ( $row_produkt['preis'] == '' OR $row_produkt['preis'] == '0' )
{
$html.=	'
	<tr>
		<td colspan="2">Aktueller Fuxtagespreis auf Anfrage</td>
	</tr>
	<tr>
		<td colspan="2" height="10">&nbsp;</td>
	</tr>';
}
else
{
// Start Sonderrabatt
	$datum = date("Y-m-d", $timestamp);

	// Abfrage ob SonderRabatt aktiv
	$result_rabatt = mysqli_query($dbi,"SELECT rabatt FROM rabatt WHERE marke=".$row_produkt['marke']." AND von <= '$datum' AND bis >= '$datum'");
	$row_rabatt = $result_rabatt->fetch_assoc();
	$num = mysqli_num_rows($result_rabatt);

	// Abfrage ob diese Maschine in der Rubrik SonderRabatt hat
	$result_rabatt_rubrik = mysqli_query($dbi,"SELECT nummer FROM rubrik WHERE marke=".$row_produkt['marke']." AND nummer=".$row_produkt['hauptrubrik']." AND rabatt='1'");
	$num_rabatt = mysqli_num_rows($result_rabatt_rubrik);

	// Wenn SonderRabatt aktiv
	if ( $num != '0' AND $num_rabatt != '0' )
	{
		// ohne Preissperre
		if ( $row_produkt['preissperre'] != 1 )
		{
			// wennn normaler Rabatt höher als Sonderrabatt
			if ( $row_produkt['rabatt'] >= $row_rabatt['rabatt'] )
			{
				$prozent = $row_produkt['rabatt'];
			}
			else
			{
				$prozent = $row_rabatt['rabatt'];
				$welcher_rabatt = '(Sonderrabatt '.$prozent.'%)';
			}
		}
		// mit Preissprerre
		else
		{
			$prozent = 0;
		}
	}

	// Wenn SonderRabatt nicht aktiv
	else
	{	
	$prozent = $row_produkt['rabatt'];
	}
// Ende Sonderrabatt

// START wenn Produkt Black

// Prüfen ob Black aktiv
$result_black = mysqli_query($dbi,"SELECT black_von, titel FROM black WHERE black_von <= '$datum' AND black_bis >= '$datum'");

if ( $row_produkt['black'] == 1 AND mysqli_num_rows($result_black) == 1 AND ( $row_produkt['black_stk'] != 0 OR $row_produkt['black_stk'] == '' ) )
{
	$prozent = $row_produkt['black_rabatt'];
	$black_aktiv = 1;
	$welcher_rabatt = '(Blackrabatt '.$row_produkt['black_rabatt'].'%)';
}
else
{
	$black_aktiv = 0;
}
// ENDE wenn Produkt Black

$rabatt = $row_produkt['preis'] / 100 * $prozent;
$preis = $row_produkt['preis'] - $rabatt;

// START Fixpreis Aktion
	$produkt_fix_aktion = 0;
	$result_aktion = mysqli_query($dbi,"SELECT preis FROM produkt_aktion WHERE von <= '$datum' AND bis >= '$datum' AND produkt='".$row_produkt['nummer']."'");
	$row_aktion = $result_aktion->fetch_assoc();

	if ( mysqli_num_rows($result_aktion) == 1 AND $black_aktiv != 1 AND $preis > $row_aktion['preis'] )
	{
		$preis = $row_aktion['preis'];
		$produkt_fix_aktion = 1;
	}

	if ( $produkt_fix_aktion == 1 )
	{
		$rabatt_fix_aktion = ( $row_produkt['preis'] - $preis ) * 100 / $row_produkt['preis'];
		$welcher_rabatt = '(Fixpreis Aktion '.floor($rabatt_fix_aktion).'%)';
	}
// ENDE Fixpreis Aktion

if ( $row_produkt['rabatt'] != 0 )
{
$html.=	'
	<tr>
		<td colspan="2">Listenpreis CHF '.number_format($row_produkt['preis'], 2, '.', "'").'</td>
	</tr>';
}

$html.=	'
	<tr>
		<td colspan="2"><span style="font-size:13px; font-weight:bold; color:#F92F01;">Fuxpreis CHF ';
		
// Wenn Preis über 20 x.00
if ( $preis > 20 )
{
$html .= ''.number_format(round($preis, 0), 2, '.', "'").'';
}
// Wenn Preis unter 20 x.10
else
{
$html .= ''.number_format(round($preis, 1), 2, '.', "'").'';
}

$html .= '</span><br />
	<span style="font-size:8px;">inklusive 8.1% MwSt<br />';
	
	// Porto
	// Wenn Porto für Produkt eingegeben
	if ( $row_produkt['porto'] != '' )
	{
	$html .= 'Versand CHF '.number_format($row_produkt['porto'], 2).'';
	}
	else
	{
		if ( $preis >= $row_marke['franko'] AND !is_null($row_marke['franko']) )
		{
		$html .= 'Frankolieferung';
		}
		else
		{
		$html .= 'Versand CHF 15.00';
		}
	}
	
	$html .= '</span><br /><span  style="font-size:7px;">'.$welcher_rabatt.'</span></td>
	<td align="right">';
	
if ( $row_produkt['made'] == 1 )
{	
	$html .= '<img src="images/icons/made_in_germany.jpg" width="30mm">';
}
elseif ( $row_produkt['made'] == 2 )
{
	$html .= '<img src="images/icons/made_in_switzerland.jpg" width="30mm">';
}
elseif ( $row_produkt['made'] == 3 )
{
	$html .= '<img src="images/icons/made_in_finnland.jpg" width="30mm">';
}
	
	$html .= '&nbsp;&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td colspan="3" height="10">&nbsp;</td>
	</tr>';
}

$html .= '
	<tr>
		<td width="140mm">
		<span  style="font-weight:bold; font-size:11px;">Beschreibung</span>';
		
$ul = substr($row_produkt['text'], 0, 10);

if ( $ul != '&lt;ul&gt;')
{
	$html .= '<br />';
}

$html .= '
		'.html_entity_decode(stripslashes($row_produkt['text'])).'
		<br /><br />';
		
if ( $row_produkt['daten'] != '' )
{
$html .= '
		<span  style="font-weight:bold; font-size:11px;">Technische Daten</span>';
		
$ul = substr($row_produkt['daten'], 0, 10);

if ( $ul != '&lt;ul&gt;')
{
	$html .= '<br />';
}

$html .= '
		'.html_entity_decode(stripslashes($row_produkt['daten'])).'
		<br />';
}

if ( $row_produkt['inkl'] != '' )
{
$html .= '
		<span  style="font-weight:bold; font-size:11px;">Standard-Ausrüstung</span>';
		
$ul = substr($row_produkt['inkl'], 0, 10);

if ( $ul != '&lt;ul&gt;')
{
	$html .= '<br />';
}

$html .= '
		'.html_entity_decode(stripslashes($row_produkt['inkl'])).'';
}

$html .= '
</td>
		<td width="2mm">&nbsp;</td>
		<td width="45mm">';

// überprüfen ob Foto vorhanden.
$filemarke = "images/produkte/gross/".$row_produkt['nummer'].".jpg";

if (file_exists($filemarke))
{
	$html .= '<img src="https://'.$_SERVER['HTTP_HOST'].'/images/produkte/gross/'.$row_produkt['nummer'].'.jpg" border="0" style="border-style: solid; border-width: 0.2mm 0.2mm 0.2mm 0.2mm; border-color: #F92F01;" />';
}
else
{
	$html .= '<img src="https://'.$_SERVER['HTTP_HOST'].'/images/produkte/gross/bild_folgt.jpg" border="0" style="border-style: solid; border-width: 0.2mm 0.2mm 0.2mm 0.2mm; border-color: #F92F01;" />';
}

$html .= '<br /><br />';
		
// überprüfen ob Foto 2 vorhanden.
$filemarke = "images/produkte/2/gross/".$row_produkt['nummer'].".jpg";

if (file_exists($filemarke))
{		
$html .= '<img src="https://'.$_SERVER['HTTP_HOST'].'/images/produkte/2/gross/'.$row_produkt['nummer'].'.jpg" border="0" style="border-style: solid; border-width: 0.2mm 0.2mm 0.2mm 0.2mm; border-color: #F92F01;" /><br /><br />';
}

// überprüfen ob Foto 3 vorhanden.
$filemarke = "images/produkte/3/gross/".$row_produkt['nummer'].".jpg";

if (file_exists($filemarke))
{		
$html .= '<img src="https://'.$_SERVER['HTTP_HOST'].'/images/produkte/3/gross/'.$row_produkt['nummer'].'.jpg" border="0" style="border-style: solid; border-width: 0.2mm 0.2mm 0.2mm 0.2mm; border-color: #F92F01;" />';
}

$html .= '
		</td>
	</tr>
</table>';

// output the HTML content
$pdf->writeHTMLCell(188, 0, 15, 46, $html, 0, false, 0, false, '');

//Close and output PDF document
$pdf->Output(''.html_entity_decode(stripslashes($row_marke['marke'])).' '.html_entity_decode(stripslashes($row_produkt['produkt'])).'.pdf', 'D');

mysqli_close($dbi);