<?php
	include("includes/dbconfig.php");
	
    $err= "";
    $product_name = '';

	if(isset($_POST['receiptfocus']))
	{
        $product_name =  $_POST['product_name'];
        
		if(DB::queryFirstField("SELECT COUNT(id) FROM cash_receipt WHERE id=%i", $_POST['receiptfocus']) != 1)
		{
			$err = $err."Receipt Not Identified!<br>";
		}
		else
		{
			if(DB::queryFirstField("SELECT COUNT(id) FROM customer_claims WHERE receipt_id=%i", $_POST['receiptfocus']) == 0)
			{
				$err = $err."Receipt Items Not Identified!<br>";
			}
			else
			{
				$shopdata = DB::queryFirstRow("SELECT full_name, logo_file, address_line_1, address_line_2, address_line_3, zip_code, city, country, tin, phone, fax, email FROM shop WHERE id=%i", $_SESSION["connecteduserbranch"]);
				
				if(DB::count() != 1)
				{
					$err = $err."Shop Not Identified!<br>";
				}
				else
				{
					$detailedreceiptaddress = "";
					$detailedcontactinfo = "";
					
					if(trim($shopdata['address_line_1']) != "")
						$detailedreceiptaddress = $detailedreceiptaddress.trim($shopdata['address_line_1']);
						
					if(trim($shopdata['address_line_2']) != "" && $detailedreceiptaddress != "")
					{
						$detailedreceiptaddress = $detailedreceiptaddress.", ".trim($shopdata['address_line_2']);
					}
					elseif(trim($shopdata['address_line_2']) != "" && $detailedreceiptaddress == "")
					{
						$detailedreceiptaddress = $detailedreceiptaddress.trim($shopdata['address_line_2']);
					}
					
					if(trim($shopdata['address_line_3']) != "" && $detailedreceiptaddress != "")
					{
						$detailedreceiptaddress = $detailedreceiptaddress.", ".trim($shopdata['address_line_3']);
					}
					elseif(trim($shopdata['address_line_3']) != "" && $detailedreceiptaddress == "")
					{
						$detailedreceiptaddress = $detailedreceiptaddress.trim($shopdata['address_line_3']);
					}
					
					if($detailedreceiptaddress != "")
						$detailedreceiptaddress = $detailedreceiptaddress."<br>";
					
					if(trim($shopdata['zip_code']) != "")
						$detailedcontactinfo = $detailedcontactinfo."P.O Box ".trim($shopdata['zip_code']);
					if(trim($shopdata['city']) != "")
						$detailedcontactinfo = $detailedcontactinfo." ".trim($shopdata['city']);
					if(trim($shopdata['country']) != "")
						$detailedcontactinfo = $detailedcontactinfo." - ".trim($shopdata['country'])."<br>";
					
					if(trim($shopdata['phone']) != "")
						$detailedcontactinfo = $detailedcontactinfo.'<i class="glyphicon glyphicon-earphone"></i>: '.trim($shopdata['phone']).'<br>';
					if(trim($shopdata['fax']) != "")
						$detailedcontactinfo = $detailedcontactinfo.'<i class="glyphicon glyphicon-print"></i>: '.trim($shopdata['fax']).'<br>';
					if(trim($shopdata['email']) != "")
						$detailedcontactinfo = $detailedcontactinfo.'<i class="glyphicon glyphicon-envelope"></i>: '.trim($shopdata['email']).'<br>';
						
					$receipt = DB::queryFirstRow("SELECT personnel_details.display_name, personnel_details.first_name,  cash_receipt.customer_id,    cash_receipt.receipt_mode, cash_receipt.created_date, cash_receipt.value_date, cash_receipt.receipt_number, cash_receipt.customer_name, cash_receipt.amount, cash_receipt.customer_change, cash_receipt.memo, cash_receipt.is_cancelled FROM cash_receipt LEFT JOIN  personnel_details ON cash_receipt.created_by = personnel_details.id WHERE cash_receipt.id=%i AND cash_receipt.shop=%i", $_POST['receiptfocus'], $_SESSION["connecteduserbranch"]);
					
					#$receiptitems = DB::query("SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.description, item_batch.batch_code, item_batch.manufacture_date, item_batch.expiry_date, cash_sale_item.sell_price, cash_sale_item.quantity FROM cash_sale_item INNER JOIN item_price ON cash_sale_item.price_id = item_price.id INNER JOIN stock_item ON item_price.item = stock_item.id LEFT JOIN item_batch ON item_price.batch = item_batch.id WHERE cash_sale_item.shop=%i AND cash_sale_item.receipt=%i", $_SESSION["connecteduserbranch"], $_POST['receiptfocus']);
					#$receiptitems = DB::query("SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.description, item_batch.batch_code, item_batch.manufacture_date, item_batch.expiry_date, customer_claims.sell_price, customer_claims.quantity FROM customer_claims INNER JOIN item_price ON customer_claims.price_id = item_price.id INNER JOIN stock_item ON item_price.item = stock_item.id LEFT JOIN item_batch ON item_price.batch = item_batch.id WHERE customer_claims.shop_id=%i AND customer_claims.receipt_id=%i", $_SESSION["connecteduserbranch"], $_POST['receiptfocus']);
					
					$receiptitemscompact = DB::query("SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.description,   cash_sale_item.sell_price, cash_sale_item.quantity FROM cash_sale_item INNER JOIN item_price ON cash_sale_item.price_id = item_price.id INNER JOIN stock_item ON item_price.item = stock_item.id   WHERE cash_sale_item.shop=%i AND cash_sale_item.receipt=%i", $_SESSION["connecteduserbranch"], $_POST['receiptfocus']);

				}
			}
		}
	}
	else
	{
		$err= $err."Receipt Identification Required!<br>";
	}
?>
<!DOCTYPE html>
<html>
	<head>
	<meta charset="UTF-8">
	<title>Cash Receipt</title>
	<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
	<!-- Bootstrap 3.3.2 -->
	<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
	<!-- Font Awesome Icons -->
	<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
	<!-- Ionicons -->
	<link href="http://code.ionicframework.com/ionicons/2.0.0/css/ionicons.min.css" rel="stylesheet" type="text/css" />
	<!-- Theme style -->
	<link href="dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />

	<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries  -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
	<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
	<![endif]
	<body onload=" window.print(); location.href='cashsalesregister.php';">

-->  
	</head>
	<body onload=" window.print(); ">
		<div class="wrapper">
			<!-- Main content -->
			<section class="content">
				<?php
					if($err == "")
					{
						 
						 
				?>
				<div class="col-sm-3">				
					<div class="text-center">
						<b><font face="Arial" size="1"><?php echo $shopdata['full_name']; ?></font></b><br>
						<font face="Arial" size="1"><?php echo $detailedreceiptaddress; ?></font>
						<font face="Arial" size="1"><?php echo $detailedcontactinfo; ?></font>
						<font face="Arial" size="1">TIN: <?php echo $shopdata['tin']; ?></font><br>
						<font face="Arial" size="1"> POINTS SALE: <?php echo $receipt['customer_name']; ?></font><br>
						<font face="Arial" size="1">Receipt No:<?php echo $receipt['receipt_number']; ?></font><br>
						<font face="Arial" size="1">Date: <?php echo date("d-m-Y H:i", strtotime($receipt['created_date'])); ?></font>
					</div>
					<?php
						 
						 $items = DB::query("SELECT id, customer_name, value_date, bill, claim FROM cash_receipt where customer_id = '".$receipt['customer_id']."'");
																				
						 $total_bills = 0;
						 $total_claims = 0;
						 
						 foreach ($items as $item)
						 {
							 $total_claims += ($item['claim']/100)*($item['bill']);
						 }

						 
						 
						 //get previous claims
						 $claims = DB::query("SELECT id, customer_id, claim from customer_claims where customer_id = '".$receipt['customer_id']."'");
						 $previous_claims = 0;
						 
						 foreach ($claims as $claim)
						 {
							 $previous_claims +=   $claim['claim'];
							 
						 }

						 $points_balance =  $total_claims - $previous_claims;

					?>
				
					<table style="width: 100%; margin: 0 auto;">
						<tr>
							<td><font face="Arial" size="1">Item</font></td>
							<td class="text-center"><font face="Arial" size="1">Qty</font></td>
							<td class="text-right"><font face="Arial" size="1">Price</font></td>
							<td class="text-right"><font face="Arial" size="1">Points</font></td>
							
                        </tr>
                        <?php

                            $claims = DB::query("SELECT *from customer_claims WHERE receipt_id=%i", $_POST['receiptfocus']);
                            $previous_claims = 0;
                            $itemscount = 0;
							$itemstotalcost = 0;

                            foreach ($claims as $claim)
                            {
                                $previous_claims +=   $claim['claim'];
                                $userowtotal = (1 * $claim['sell_price']);
								
								$itemscount += 1;
                                $itemstotalcost += $userowtotal;
                                
                                echo '<tr><td>'.$product_name.'</td><td>1</td><td>'.$claim['sell_price'].'</td> <td>'.$claim['sell_price'].'</td> </tr>';
                                
                            }

                            //$points_balance =  $total_claims - $previous_claims;


                        ?>
                        
 
					 
					</table>
					
					<table style="width: 100%; margin: 0 auto;">
					 
					  <tr>
							<td><font face="Arial" size="1"><b>Total</b></font></td>
							<td class="text-center"><font face="Arial" size="1"><b> : </b></font></td>
							 
							<td class="text-right lead"><font face="Arial" size="1"><b><?php echo number_format(($itemstotalcost  ), 2) ; ?></b></font></td>
						</tr>  


						<tr>
							<td class="lead"><font face="Arial" size="1">Used</font></td>
							<td class="text-center lead"><font face="Arial" size="1">:</font></td>
							<td class="text-right lead"><font face="Arial" size="1"><?php echo number_format(($itemstotalcost ), 2) ; ?> Points</font></td>
						</tr>

						 
                        
                        <tr>
							<td class="lead"><font face="Arial" size="1">Balance</font></td>
							<td class="text-center lead"><font face="Arial" size="1">:</font></td>
							<td class="text-right lead"><font face="Arial" size="1"><?php echo  number_format(  $points_balance, 2);?> Points</font></td>
						</tr>
					</table>
					
					<div class="text-center">
						<font face="Arial" size="1">Goods once sold will not be taken back or exchanged</font><br>
						<font face="Arial" size="1">Thank you. Please come again.</font><br>
						<font face="Arial" size="1">Served By: <?php echo $receipt['display_name']; ?></font>
						 
					</div>
				</div>
				<div class="col-sm-9">
				</div>
				<?php
					}
					else
					{
				?>
				<div class="alert alert-danger alert-dismissable">
					<i class="glyphicon glyphicon-ban-circle"></i><br>
					<?php 
						echo $err; 
					?>
				</div>
				<?php
					}
				?>
			</section><!-- /.content -->
		</div><!-- ./wrapper -->
		<!-- AdminLTE App -->
		<script src="dist/js/app.min.js" type="text/javascript"></script>
	</body>
</html>