<?php
require('fpdf185/fpdf.php');
//require('pdf_barcode.php');
include 'db_connection.php';
$pdf = new FPDF();

$receiptno = $_GET['receiptno'];

//$myarray = array(1,2,3); 
$myarray = array(1); 
$pdf = new FPDF('P','mm',array(100,150));
foreach($myarray as $value){ 
$pdf->SetAutoPageBreak(false);	
$pdf->AddPage();

$sql = "SELECT * FROM mobileorders  WHERE order_id = '$receiptno'  ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) {
		$orid = $row["orid"];
		$pay_option = $row["pay_option"];
		$amount = $row["amount"];
		$discount = $row["discount"];
		$create_at = $row["create_at"];
	}
}



$pdf->SetFont('Arial', '',14);
$pdf->Cell(90, 6, 'ABC Stores', 0, 1, 'C');
$pdf->Cell(90, 6, 'No 43, Main Street, Balangoda', 0, 1, 'C');
$pdf->Cell(90, 6, 'Tel : 077 777 7777', 0, 1, 'C');
$pdf->Cell(90, 6, '-------------------------------------', 0, 1, 'C');
$pdf->SetFont('Arial', '',11);
$pdf->Cell(30, 4, 'Receipt ', 0, 0, 'L');
$pdf->Cell(60, 4, ': '.$orid, 0, 1, 'L');
$pdf->Cell(30, 4, 'Date ', 0, 0, 'L');
$pdf->Cell(60, 4, ': '.$create_at, 0, 1, 'L');

$pdf->Cell(190, 5, '', 0, 1, 'C');

$pdf->SetFont('Arial','B',10);
$pdf->Cell(40, 5, 'Description', 0, 0, 'L');
$pdf->Cell(23, 5, 'Price', 0, 0, 'R');
$pdf->Cell(23, 5, 'Total', 0, 1, 'R');

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

$total_amount = 0;
$sql = "SELECT SUM(pro_qty) AS pro_qty , pro_dis,pro_price,proid,proname,cover  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
		WHERE mobiletemporarycart.orderid='$receiptno' GROUP BY  mobiletemporarycart.proid,mobiletemporarycart.pro_dis   ORDER BY mobiletemporarycart.tempid  DESC";

		$result = $conn->query($sql);
		$rowCount = 0;
		if ($result->num_rows > 0) {
			while($row = $result->fetch_assoc()) {
				$rowCount = $rowCount+1;
				$proid = $row["proid"];
				$proname = $row["proname"];
				$cover = $row["cover"];
				$pro_dis = $row["pro_dis"];
				$pro_price = $row["pro_price"];
				$pro_qty = $row["pro_qty"];

				if($pro_dis>0){
					$dis_txt = '('.$pro_dis.'%)';
				}else{
					$dis_txt = "";
				}

				$discounted_price = ($pro_price/100) * $pro_dis;
				$product_price2 = ($pro_price - $discounted_price)*$pro_qty;
				$total_amount = $total_amount + $product_price2;

				$pdf->Cell(90, 3, $proname, 0, 1, 'L');
				$pdf->Cell(20, 3, $pro_qty , 0, 0, 'L');
				$pdf->Cell(20, 3, $dis_txt , 0, 0, 'L');
$pdf->Cell(23, 3, number_format((float) $pro_price,2), 0, 0, 'R');
$pdf->Cell(23, 3, number_format((float) $product_price2,2), 0, 1, 'R');



	}
}

$pdf->SetFont('Arial','',10);
$pdf->Cell(90, 1, '_____________________________________________', 0, 1, 'C');
$pdf->Cell(90, 3, '', 0, 1, 'C');

$pdf->Cell(63, 4, 'Total Item', 0, 0, 'L');
$pdf->Cell(23, 4, $rowCount, 0, 1, 'R');
$pdf->Cell(90, 1, '_____________________________________________', 0, 1, 'C');
$pdf->Cell(90, 3, '', 0, 1, 'C');

$pdf->Cell(63, 4, 'Total Amount', 0, 0, 'L');
$pdf->Cell(23, 4, number_format((float) $total_amount,2), 0, 1, 'R');

$pdf->Cell(63, 4, 'Discount', 0, 0, 'L');
$pdf->Cell(23, 4, $discount.'%', 0, 1, 'R');

$discounted_amount = $total_amount/100*$discount;

$pdf->Cell(63, 4, 'Discounted Amount', 0, 0, 'L');
$pdf->Cell(23, 4, number_format((float) $discounted_amount,2), 0, 1, 'R');


$pdf->Cell(63, 4, 'Bill Amount', 0, 0, 'L');
$pdf->Cell(23, 4, number_format((float) $total_amount-$discounted_amount,2), 0, 1, 'R');



$pdf->Cell(85, 15, '', 0, 1, 'C');
$pdf->Cell(85, 5, 'Thank You', 0, 1, 'C');
$pdf->Cell(85, 1, '_____________________________________________', 0, 1, 'C');
$pdf->Cell(85, 10, 'Developemnt By SEO PRO Designer ', 0, 1, 'C');


}
$pdf->Output();

?>