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

//$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();

$getReturnDetailsOngoingIdQuery=$conn->query("SELECT * FROM tbl_return WHERE return_id='$ReturnId' ");
if ($GRETDOrs=$getReturnDetailsOngoingIdQuery->fetch_array()) {
    //$ReturnId=$GRETDOrs[0];
    $OrderId=$GRETDOrs[1];
    $ReturnDateTime=$GRETDOrs[3];

    $getOrderDetailsQuery=$conn->query("SELECT * FROM mobileorders mor INNER JOIN invoice_other_details iod ON mor.order_id=iod.order_id WHERE mor.order_id='$OrderId' ");
	if ($GODrs=$getOrderDetailsQuery->fetch_array()) {
	    $ORID=$GODrs[1];
	    $Amount=$GODrs[3];
	    $BranchId=$GODrs[19];
	}

}


$pdf->SetFont('Arial', '',14);
$pdf->Cell(80, 7, 'Kumara Enterprises', 0, 1, 'C');
$pdf->Cell(80, 4, 'No.58K,Gangabada Road', 0, 1, 'C');
$pdf->Cell(80, 6, 'Tel : 077815955 / 0112613606', 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, ': '.$ReturnDateTime, 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_bill_amount = 0;
	$total_bill_return = 0;
	$getOrderItemsQuery=$conn->query("SELECT * FROM mobiletemporarycart WHERE orderid='$OrderId' ");
	while($GOIrs=$getOrderItemsQuery->fetch_array()) {
		$TempId=$GOIrs[0];
		$ProId=$GOIrs[2];
		$ProPrice=$GOIrs[3];
		$ProDis=$GOIrs[4];
		$ProQty=$GOIrs[5];
		$ReturnQty=$GOIrs[7];

		$discounted_amount = $ProPrice/100 * $ProDis;
		$sub_total_one_item = $ProPrice - $discounted_amount;
		$sub_total = $sub_total_one_item*$ProQty;
		$total_bill_amount = $total_bill_amount+$sub_total;

		$return_sub_total = $sub_total_one_item * $ReturnQty;
		$total_bill_return = $total_bill_return+$return_sub_total;
		$available_to_return = $ProQty - $ReturnQty;

		$getProductDetailsQuery=$conn->query("SELECT * FROM mobileproducts WHERE prid='$ProId' ");
		if ($GPDrs=$getProductDetailsQuery->fetch_array()) {
			$ProductName=$GPDrs[3];
			$Cover=$GPDrs[14];
		}


			$pdf->Cell(90, 3, $ProductName, 0, 1, 'L');
			$pdf->Cell(20, 3, $ReturnQty , 0, 0, 'L');
			$pdf->Cell(20, 3, "" , 0, 0, 'L');
			$pdf->Cell(23, 3, number_format((float) $ProPrice,2), 0, 0, 'R');
			$pdf->Cell(23, 3, number_format((float) $return_sub_total,2), 0, 1, 'R');

	}

// $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->SetFont('Arial','',10);
$pdf->Cell(90, 1, '_____________________________________________', 0, 1, 'C');
$pdf->Cell(90, 3, '', 0, 1, 'C');

$pdf->Cell(63, 4, 'Return Amount', 0, 0, 'L');
$pdf->Cell(23, 4, number_format((float) $total_bill_return,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 VVH Solutions', 0, 1, 'C');

}
$pdf->Output();

?>