<?php
//============================================================+
// File name   : example_006.php
// Begin       : 2008-03-04
// Last Update : 2013-05-14
//
// Description : Example 006 for TCPDF class
//               WriteHTML and RTL support
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: WriteHTML and RTL support
 * @author Nicola Asuni
 * @since 2008-03-04
 */

include "../conf/conn.php";

$record = $_GET["record"];

$query = "SELECT * FROM `receipt` WHERE paypal_id = '$record'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 0) {
	$query = "INSERT INTO `receipt` (`record`, `paypal_id`) VALUES (NULL, '$record')";
	$result = mysqli_query($conn, $query);}

$query = "SELECT * FROM `receipt` WHERE paypal_id = '$record'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$receipt = sprintf("%'05d", $row["record"]);

$sql = "UPDATE `orders` SET `status` = 'bank doing',`receipt_number` = '" . "SRR" . date('y') . "-" . $receipt . "' WHERE `orders`.`record` = " . $_GET["record"];
$query = mysqli_query($conn, $sql);

$sql = "select * from orders WHERE `orders`.`record` = " . $_GET["record"];
$query = mysqli_query($conn, $sql);
$row = $query->fetch_assoc();

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

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
	//Page header
	public function Header() {
		$html = '<div style="background-color:#c2e3e3;padding-bottom:40px" align="center">
			<img style="height:auto;width:180px;" src="../img/weblogo.png" width="217">
		</div>';
		$this->writeHTML($html, true, false, true, false, '');
	}

	// Page footer
	public function Footer() {

		include "../conf/conn.php";

		$sql = "select * from orders WHERE `orders`.`record` = " . $_GET["record"];
		$query = mysqli_query($conn, $sql);
		$row = $query->fetch_assoc();

		// Position at 15 mm from bottom
		$this->SetY(-15);
		// Set font
		$this->SetFont('helvetica', 'I', 8);
		// Page number
		$this->Cell(0, 10, $row['receipt_number'] . ' Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}
}

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

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Sayvour');
$pdf->SetTitle('Receipt');
$pdf->SetSubject('Receipt');
$pdf->SetKeywords('Receipt');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins('', 40, '');
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {
	require_once dirname(__FILE__) . '/lang/eng.php';
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------
// set font

$pdf->SetFont('droidsansfallback');

// writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)

// create some HTML content

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

$html = '<table border="0" cellspacing="3" cellpadding="4">
		<tr style="font-family:arialbd;">
		<td style="width:80px">
		</td>
		<td>
			<H3>Order Summary and Receipt :</H3>
		</td>
		</tr>
		<tr>
		<td>
		</td>
		</tr>
		</table>

		<table rules="rows" cellspacing="3" cellpadding="4">
		<tr style="font-family:arialbd;">
			<td style="width:80px">
			</td>
			<th>
				<b>Username</b>
			</th>
			<th>
				<b>Order no</b>
			</th>
			<th>
				<b>Receipt no</b>
			</th>
			<th>
				<b>Order date</b>
			</th>
		</tr>
		<tr>
			<td style="width:80px">
			</td>
			<td>
				' . $row['username'] . '
			</td>
			<td>
				' . $row['paypal id'] . '
			</td>
			<td>
				' . $row['receipt_number'] . '
			</td>
			<td>
				' . substr($row['time'], 0, 10) . '
			</td>
		</tr>
		<tr>
		<td style="width:70px">
		</td>
		<td colspan="4">
			<img style="width:560px;height:2px;" src="../img/border.png">
		</td>
		</tr>
		<tr style="font-family:arialbd;">
			<td style="width:80px">
			</td>
			<th>
				<b>Product Name</b>
			</th>
			<th>
				<b>Unit Price</b>
			</th>
			<th>
				<b>Quantity</b>
			</th>
			<th>
				<b>Amount</b>
			</th>
		</tr>';

$query = 'SELECT * FROM `bought_item` WHERE paypal_id = "' . $row['paypal id'] . '" and username = "' . $row["username"] . '"';
$result = mysqli_query($conn, $query);
$count = 0;

while ($row1 = mysqli_fetch_assoc($result)) {
	$total_price = $row1['price'] * $row1['amount'];
	$count = $count + $total_price;

	$product_name = str_replace('<div class="Dark">', '<img width="25" height="25" src ="../img/letter/d_', $row1["product_name"]);
	$product_name = str_replace('<div class="White">', '<img width="25" height="25" src ="../img/letter/w_', $product_name);
	$product_name = str_replace('<div class="Pink">', '<img width="25" height="25" src ="../img/letter/p_', $product_name);
	$product_name = str_replace('<div class="Brown">', '<img width="25" height="25" src ="../img/letter/b_', $product_name);
	$product_name = str_replace('</div>', '.png"/>', $product_name);
	$product_name = str_replace('_?.', '_s1.', $product_name);
	$product_name = str_replace('_/.', '_s2.', $product_name);
	$product_name = str_replace('_:.', '_s3.', $product_name);
	$product_name = str_replace('_☺.', '_smile.', $product_name);
	$product_name = str_replace('_♥.', '_heart.', $product_name);

	$html = $html . '
		<tr>
			<td style="width:80px">
			</td>
			<td>
				' . $product_name . '
			</td>
			<td>
				HK$' . $row1['price'] . '
			</td>
			<td>
				' . $row1['amount'] . '
			</td>
			<td>
				HK$' . $total_price . '
			</td>
		</tr>';
}
/*if($count<=800){
$html=$html.'
<tr>
<td style="width:80px">
</td>
<td>
Delivery Fee
</td>
<td>
HK$38
</td>
<td>
1
</td>
<td>
HK$38
</td>
</tr>';
}*/
$html = $html . '
	<tr>
	<td style="width:70px">
	</td>
	<td colspan="4">
		<img style="width:560px;height:2px;" src="../img/border.png">
	</td>
	</tr>
	<tr>
		<td style="width:80px">
		</td>
		<td style="font-family:arialbd;" colspan="3">
			<b>Total</b>
		</td>
		<td>
			HK$' . $row['amount'] . '
		</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	</table>

	<table border="0" cellspacing="3" cellpadding="4">
	<tr style="font-family:arialbd;">
	<td style="width:80px">
	</td>
	<td style="width:250px">
		<H4>Order Details :</H4>
	</td>
	<td style="width:250px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td style="width:250px">
		Name :
	</td>
	<td>
		' . $row['customer Fname'] . ' ' . $row['customer Lname'] . '
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Email :
	</td>
	<td>
		' . $row['customer email'] . '
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Phone :
	</td>
	<td>
		' . $row['customer phone'] . '
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Company Name :
	</td>
	<td>
		' . $row['company name'] . '
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	</table>

	<table border="0" cellspacing="3" cellpadding="4">
	<tr style="font-family:arialbd;">
	<td style="width:80px">
	</td>
	<td>
		<H4>Delivery Details:</H4>
	</td>
	<td style="width:250px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td style="width:250px">
		Name :
	</td>
	<td>
		' . $row['recipient Fname'] . ' ' . $row['recipient Lname'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Email :
	</td>
	<td>
		' . $row['email'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Phone :
	</td>
	<td>
		' . $row['phone'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Address :
	</td>
	<td>
		' . $row['address'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Delivery Time :
	</td>
	<td>
		' . $row['delivery date'] . ' ' . $row['delivery time'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	<tr>
	<td style="width:80px">
	</td>
	<td>
		Remarks :
	</td>
	<td>
		' . $row['remarks'] . '
	</td>
	<td style="width:80px">
	</td>
	</tr>
	</table>';

// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');

// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document

$pdf->Output('receipt_' . $_GET["record"] . '.pdf', 'D');

/*$local_file = 'receipt_'.$_GET["record"].'.pdf';
$download_file = 'receipt_'.$_GET["record"].'.pdf';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);

print fread(fopen($local_file, "r"), filesize($local_file));

fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}*/

//============================================================+
// END OF FILE
//============================================================+
?>