<!doctype html>
<html class="no-js" lang="zxx">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Sudila Printers - Kurunegala Sri Lanka</title>
<meta name="description" content="Buddist books online; best online Buddist book store in Sri Lanka. Our collection include Sri Lankan Buddhist books, Buddhist Information and Online Books from Sri Lanka.">
<meta name="keywords" content="Sudila Printers">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Favicons -->
<link rel="shortcut icon" href="images/favicon.ico">

<!-- Google font (font-family: 'Roboto', sans-serif; Poppins ; Satisfy) -->
<link type="text/css" href="css/font_opensans.css" rel="stylesheet">
<link type="text/css" href="css/font_poppins.css" rel="stylesheet">
<link type="text/css" href="css/font_roboto.css" rel="stylesheet">
<!-- Stylesheets -->
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="css/plugins.css">
<link type="text/css" rel="stylesheet" href="css/style.css">
<!-- Cusom css -->
<link type="text/css" rel="stylesheet" href="css/custom.css">
<!-- Modernizer js -->
<script type="text/javascript" src="js/vendor/modernizr-3.5.0.min.js"></script>
<style type="text/css">
.table-content table td {
    text-align: right;
    text-transform: uppercase;
    line-height: 20px;
}
.table-content table td.bname {
    text-align: left;
}
.total__amount li {
    font-size: 22px;
}
</style>
</head>
<body>
<!-- Main wrapper -->
<div class="wrapper" id="wrapper">
  <div id="pre_loader"></div>
  <!-- Header -->
  <?php include 'common/header.php';
	if (isset($_SESSION['temp_user_bb']) && isset($_SESSION['guest_user_bb'])) {
		$user_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_bb'])), ENT_QUOTES, 'UTF-8');
		$whr = 'temp_id';

		if(empty($user_id)) {
			$redirectUrl = "index";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
		
		} else {
			$sqlt = mysqli_query($conn, "SELECT * FROM live_cart WHERE temp_id=$user_id");
			if(mysqli_num_rows($sqlt) > 0) {
				$row_inv = mysqli_fetch_assoc($sqlt);
				$g_inv = $row_inv['inv_no'];
				$sql_up = mysqli_query($conn, "UPDATE live_cart SET User_ID='G$g_inv', inv_no='$g_inv' WHERE temp_id=$user_id");
			}
			$guest = 1;
		}

	} else {
		if (!isset($_SESSION['normal_user_bb']) || empty($_SESSION['normal_user_bb'])) {
			$redirectUrl = "sign-in";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
		
		} else {
			$user_id = htmlspecialchars(strip_tags(trim($_SESSION['normal_user_bb'])), ENT_QUOTES, 'UTF-8');
			$whr = 'User_ID';
			$guest = 0;
		}
	}

	$sql = mysqli_query($conn, "SELECT inv_no, SUM(price*Qty) AS sub_tot, SUM(item_price*Qty) AS item_tot, SUM(weight*Qty) AS tot_weight FROM live_cart WHERE $whr=$user_id");
	if(mysqli_num_rows($sql) == 0) {
		$redirectUrl = "index";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	

	} else {
		$row = mysqli_fetch_assoc($sql);
		$item_tot = $row['item_tot'];
		$sub_tot = $row['sub_tot'];
		$tot_discount = $item_tot-$sub_tot;
		$tot_weight = $row['tot_weight'];
		$inv_no = $row['inv_no'];
		
		if(empty($inv_no)) {
			$redirectUrl = "index";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
		
		} else {
			$sqlu = "SELECT * FROM order_details_temp WHERE user_id=$user_id AND inv_no='$inv_no' AND guest=$guest";
			$resultu = mysqli_query($conn, $sqlu);
			$countu = mysqli_num_rows($resultu);

			if($countu == 0) {
				$redirectUrl = "index";
				echo "<script type=\"text/javascript\">";
				echo "window.location.href = '$redirectUrl'";
				echo "</script>";		
				
			} else {
				$sqltmp = mysqli_fetch_assoc($resultu);
				$del_fee = $flag = 0;
				$country = $sqltmp['country'];
				
				$sql_cou = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM courier_charges"));
				
				if($tot_weight > 0) {
					if($sqltmp['delivery_type'] == 1 && $country == 'Sri Lanka') { // Postal
						$sql_del_chg = mysqli_query($conn, "SELECT fee FROM postal_charges WHERE low_w < '$tot_weight' AND high_w >='$tot_weight'");	
						
						if($tot_weight > 20000) { // Max Out 2kg
							$flag = 1;
							
						} else {
							if(mysqli_num_rows($sql_del_chg) == 0) {
								$flag = 1;
								
							} else {
								$row_del_chg = mysqli_fetch_assoc($sql_del_chg);
								$del_fee = $row_del_chg['fee'];
							}
						}
					
					} else if($sqltmp['delivery_type'] == 2 && $country == 'Sri Lanka') { // Courier
						$a = floor($tot_weight/1000);
						
						if($tot_weight > 0 && $tot_weight <= 1000) {
							$del_fee = $sql_cou['courier_fee'];
						
						} else {
							$del_fee = $sql_cou['courier_fee'] + ($a * $sql_cou['courier_1kg']);
						}
						
					} else if($sqltmp['delivery_type'] == 4 && $country == 'Sri Lanka') { // Cash on Delivery
						$a = floor($tot_weight/1000);

						if($tot_weight >= 0 && $tot_weight <= 1000) {
							$del_fee = $sql_cou['cash_on_del'];

						} else {
							$del_fee = $sql_cou['cash_on_del'] + ($a * $sql_cou['cash_on_1kg']);
						}
					
					} else if($sqltmp['delivery_type'] == 3 && $country != 'Sri Lanka') { // EMS
						$sql_wgt = mysqli_query($conn, "SELECT * FROM ems_charges WHERE country='$country'");
						if(mysqli_num_rows($sql_wgt) == 0) {
							$del_fee = 0;
							$flag = 1;
								
						} else {
							$row_wgt = mysqli_fetch_assoc($sql_wgt);
							$max_w = $row_wgt['max_kg']*1000;
							
							if($row_wgt['max_kg'] == 20) {
								if($tot_weight > $max_w) {
									$del_fee = 0;
									$flag = 1;
								
								} else {
									if($tot_weight > 10000) { // 10001-20000 Grams
										$f = $row_wgt['kg_20'];
										
										$r = $tot_weight - 1000;
										$w = ceil($r/500);
										$del_fee = $row_wgt['kg_1'] + ($w * $f);
									
									} else if($tot_weight > 1000) { // 1001-10000 Grams
										$f = $row_wgt['kg_10'];
										
										$r = $tot_weight - 1000;
										$w = ceil($r/500);
										$del_fee = $row_wgt['kg_1'] + ($w * $f);
									
									} else if($tot_weight > 500) { // 501-1000 Grams
										$del_fee = $row_wgt['kg_1'];
									
									} else if($tot_weight > 250) { // 251-500 Grams
										$del_fee = $row_wgt['g_500'];
									
									} else { // 0-250 Grams
										$del_fee = $row_wgt['g_250'];
									}
									
									$del_fee += 250; // Packing Charges
								}
							
							} else if($row_wgt['max_kg'] == 30) {
								if($tot_weight > $max_w) {
									$del_fee = 0;
									$flag = 1;
								
								} else {
									if($tot_weight > 20000) { // 20001-30000 Grams
										$f = $row_wgt['kg_30'];
										
										$r = $tot_weight - 1000;
										$w = ceil($r/500);
										$del_fee = $row_wgt['kg_1'] + ($w * $f);
									
									} else if($tot_weight > 10000) { // 10001-20000 Grams
										$f = $row_wgt['kg_20'];
										
										$r = $tot_weight - 1000;
										$w = ceil($r/500);
										$del_fee = $row_wgt['kg_1'] + ($w * $f);
									
									} else if($tot_weight > 1000) { // 1001-10000 Grams
										$f = $row_wgt['kg_10'];
										
										$r = $tot_weight - 1000;
										$w = ceil($r/500);
										$del_fee = $row_wgt['kg_1'] + ($w * $f);
									
									} else if($tot_weight > 500) { // 501-1000 Grams
										$del_fee = $row_wgt['kg_1'];
									
									} else if($tot_weight > 250) { // 251-500 Grams
										$del_fee = $row_wgt['g_500'];
									
									} else { // 0-250 Grams
										$del_fee = $row_wgt['g_250'];
									}
									
									$del_fee += 250; // Packing Charges
								}
								
							} else {
								$del_fee = 0;
								$flag = 1;
							}
						}			
					
					} else {
						$del_fee = 0;
						$flag = 1;
					}
				
				} else {
					$del_fee = 0;
					$flag = 1;
				}
			}
		}
	}
  ?>
  <!-- //Header --> 
  <!-- Start Bradcaump area -->
  <div class="ht__bradcaump__area bg-image--4">
    <div class="container">
      <div class="row">
        <div class="col-lg-12">
          <div class="bradcaump__inner text-center">
            <h2 class="bradcaump-title">order summary</h2>
            <nav class="bradcaump-content"> <a class="breadcrumb_item" href="index">Home</a> <span class="brd-separetor">/</span> <a class="breadcrumb_item" href="my-cart">My Cart</a><span class="brd-separetor">/</span><a class="breadcrumb_item" href="checkout">Checkout Details</a><span class="brd-separetor">/</span> <span class="breadcrumb_item active">Order Summary</span> </nav>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- End Bradcaump area --> 
        <!-- Start Checkout Area -->
        <section class="wn__checkout__area pt--30 pb--30 bg__white">
        	<div class="container">
				<div class="row">
        			<div class="col-lg-12 col-12">
        				<div class="account__form">
        					<h4><u>RECIPIENT INFORMATION</u></h4>
        					<div class="customar__field mt--30">
							  <table width="100%" border="0">
								<tr>
								  <td width="25%"><b>Name</b></td>
								  <td width="75%"><?php echo $sqltmp['f_name'].' '.$sqltmp['l_name']; ?></td>
								</tr>
								<tr>
								  <td><b>Contact Number</b></td>
								  <td><?php echo $sqltmp['tel_no']; ?></td>
								</tr>
								<tr>
								  <td><b>Email Address</b></td>
								  <td><?php echo $sqltmp['email']; ?></td>
								</tr>
								<tr>
								  <td><b>Delivery Address</b></td>
								  <td><?php echo $sqltmp['address_1'].' '.$sqltmp['address_2'].' '.$sqltmp['city']; ?></td>
								</tr>
								<tr>
								  <td><b>Country</b></td>
								  <td><?php echo $country; ?></td>
								</tr>
								<tr>
								  <td><b>Postal Code</b></td>
								  <td><?php echo $sqltmp['postal_code']; ?></td>
								</tr>
								<tr>
								  <td><b>Special Instructions</b></td>
								  <td><?php echo $sqltmp['special_msg']; ?></td>
								</tr>
								<tr>
								  <td><b>Delivery Method</b></td>
								  <td><?php if($sqltmp['delivery_type'] == 1) { echo 'Postal'; } else if($sqltmp['delivery_type'] == 2) { echo 'Courier Service'; } else if($sqltmp['delivery_type'] == 3) { echo 'Expedited Mail Service (EMS)'; } else if($sqltmp['delivery_type'] == 4) { echo 'Cash on Delivery'; } ?></td>
								</tr>
								<tr>
								  <td><b>Sub Total</b></td>
								  <td>LKR <?php echo number_format($sub_tot, 2); ?></td>
								</tr>
								<tr>
								  <td><b>Delivery Charges</b></td>
								  <td>LKR <?php echo number_format($del_fee, 2); ?> &nbsp;<b>(Total Weight: <?php echo $tot_weight; ?>g)</b></td>
								</tr>
								<tr>
								  <td><h4>Grand Total</h4></td>
								  <td><h4>LKR
									<?php $g_tot = $sub_tot + $del_fee; echo number_format($g_tot, 2); ?>
									</h4></td>
								</tr>
							  </table>
        					</div>
        				</div>
        			</div>
        		</div>
        		<div class="row">
        			<div class="col-lg-12">
        				<div class="wn__order__box">
        					<h3 class="onder__title">order summary - Invoice ID: <?php echo $inv_no; ?></h3>
							<div class="table-content wnro__table table-responsive">
							  <table>
								<tr class="title-top">
								  <th width="25%">Product Name</th>
								  <th width="15%">Weight</th>
								  <th width="15%">Unit Price (lkr)</th>
								  <th width="15%">Discounted Price (lkr)</th>
								  <th width="12%">Quantity</th>
								  <th width="18%">Subtotal (lkr)</th>
								</tr>
								<?php $sql_c = mysqli_query($conn, "SELECT * FROM live_cart WHERE live_cart.$whr =  $user_id");
								while($row_c = mysqli_fetch_array($sql_c)) { ?>
								<tr>
								  <td class="bname"><?php echo $row_c['name']; ?></td>
								  <td><?php echo $row_c['weight']; ?>g</td>
								  <td><?php echo $row_c['item_price']; ?></td>
								  <td><?php echo $row_c['price']; ?></td>
								  <td><?php echo $row_c['Qty']; ?></td>
								  <td><?php echo number_format($row_c['price']*$row_c['Qty'], 2); ?></td>
								</tr>
								<?php }
								// Update Grand Total
								mysqli_query($conn, "UPDATE order_details_temp SET price_total='$item_tot', tot_discount='$tot_discount', sub_total='$sub_tot', delivery_chg='$del_fee', grand_total='$g_tot' WHERE user_id='$user_id' AND inv_no='$inv_no' AND guest=$guest");
								?>
							  </table>
							</div>
							<ul class="shipping__method">
        						<li>Cart Total <span>LKR <?php echo number_format($item_tot, 2); ?></span></li>
        						<li>Total Discount <span>LKR <?php echo number_format($tot_discount, 2); ?></span></li>
        						<li>Total Subtotal <span>LKR <?php echo number_format($item_tot-$tot_discount, 2); ?></span></li>
        						<li>Delivery Charges (<?php echo $tot_weight; ?>g) <span>LKR <?php echo number_format($del_fee, 2); ?></span></li>
        					</ul>
        					<ul class="total__amount">
        						<li>GRAND Total <span>LKR <?php echo number_format($g_tot, 2); ?></span></li>
        					</ul>
        				</div>
						<div class="account__form" style="padding: 0;border: none;">
							<div class="row">
								<div class="col-md-12 d-flex justify-content-between">
									<div class="submite__btn">
										<a href="checkout">BACK</a>
									</div>
									<?php if($flag == 0 && $del_fee > 0) { ?>
									<div class="product-add-to-cart chkt">
										<a href="payment-confirm">Pay Now </a>
									</div>
									<?php } else { echo '<div class="alert alert-danger">Total Weight is Exceeded or Invalid Delivery Method. <a href="checkout" class="text-primary">Please Edit Your Checkout Details.</a></div>'; }  ?>
								</div>
							</div>
					    </div>
        			</div>
        		</div>
        	</div>
        </section>

  <!-- Footer Area -->
  <?php include 'common/footer.php'; ?>
  <!-- //Footer Area --> 
</div>
<!-- //Main wrapper -->
<!-- JS Files --> 
<script type="text/javascript" src="js/vendor/jquery-3.2.1.min.js"></script> 
<script type="text/javascript" src="js/popper.min.js"></script> 
<script type="text/javascript" src="js/bootstrap.min.js"></script> 
<script type="text/javascript" src="js/plugins.js"></script> 
<script type="text/javascript" src="js/active.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/add-to-cart.js"></script>
</body>
</html>