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

	if(isset($_POST['receiptfocus']))
	{
		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 cash_sale_item WHERE receipt=%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"]);
				print_r($shopdata);
				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 ".$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($shopdata['email'] != "")
						$detailedcontactinfo = $detailedcontactinfo.'<i class="glyphicon glyphicon-envelope"></i>: '.$shopdata['email'].'<br>';
						
					$receipt = DB::queryFirstRow("SELECT personnel_details.display_name, personnel_details.first_name, cash_receipt.customer_id, 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 ", $_POST['receiptfocus']);
					
					$receiptitems = DB::query("SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.alt_barcode, cash_sale_item.sell_price, cash_sale_item.quantity, cash_sale_item.serial_number 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>Sales 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]-->
	</head>
	<body onload="window.print();">
		<div class="wrapper">
			<!-- Main content -->
			<section class="content">
				<?php
					if($err == "")
					{
				?>
				<div class="col-sm-3">
					<table>
						<tr>
							<td style="vertical-align:top; width: 35%">
								
							</td>
							<td style="vertical-align:top; width: 35%">
								<div class="text-center" style="height: 60px width: 60px">  
								<img class="img-thumbnail" id="itemphoto" src="<?php echo ($shopdata['logo_file'] != "") ? "images/shoplogos/3.jpg" : "images/shoplogos/noimage.gif" ?>" alt="..." width="100%"; height="100%" >
								</div>
							</td>
							<td class="pull-right" style="vertical-align:top; width: 30%">
								
							</td>
						</tr>
					</table>
				
					<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">Client <?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;
						 $previous_claims = 0;
						  
						 if($receipt['customer_id'] > 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 class="text-center" colspan="4" >
								<font face="Arial" size="1">
									---------------------------------------------------------------------
								</font>
							</td>
						</tr>
						<tr>
							<td><font face="Arial" size="1"><b>Item</b></font></td>
							<td class="text-center"><font face="Arial" size="1"><b>Qty</b></font></td>
							<td class="text-right"><font face="Arial" size="1"><b>Price</b></font></td>
							<td class="text-right"><font face="Arial" size="1"><b>Amount</b></font></td>
						</tr>
						<tr>
							<td class="text-center" colspan="4" >
								<font face="Arial" size="1">
									---------------------------------------------------------------------
								</font>
							</td>
						</tr>
						<?php
							$itemscount = 0;
							$itemstotalcost = 0;
							foreach($receiptitems as $receiptrow)
							{  
								$userowtotal = ($receiptrow['quantity'] * $receiptrow['sell_price']);
								
								$itemscount += $receiptrow['quantity'];
								$itemstotalcost += $userowtotal;
						?>
						<tr>
							<td colspan="4" ><font face="Arial" size="1"><?php echo substr($receiptrow['item_name'], 0, 30); ?></font></td>
						</tr>
						<tr>
							<td></td>
							<td class="text-center"><font face="Arial" size="1"><?php echo $receiptrow['quantity']; ?></font></td>
							<td class="text-right"><font face="Arial" size="1"><?php echo number_format($receiptrow['sell_price'], 2); ?></font></td>
							<td class="text-right"><font face="Arial" size="1"><?php echo number_format($userowtotal, 2); ?></font></td>
						</tr>
						<?php
								if($receiptrow['serial_number'] != "")
								{
						?>
						<tr>
							<td class="text-center" colspan="4" ><font face="Arial" size="1"><?php echo "Serial: ".$receiptrow['serial_number']; ?></font></td>
						</tr>
						<?php
								}
							}
						?>
						<tr>
							<td class="text-center" colspan="4" >
								<font face="Arial" size="1">
									---------------------------------------------------------------------
								</font>
							</td>
						</tr>
						<tr>
							<td><font face="Arial" size="1"><b>Totals</b></font></td>
							<td class="text-center"><font face="Arial" size="1"><b><?php echo $itemscount; ?></b></font></td>
							<td class="text-right"></td>
							<td class="text-right"><font face="Arial" size="1"><b><?php echo number_format($itemstotalcost, 2); ?></b></font></td>
						</tr>
					</table>
					
					<table style="width: 100%; margin: 0 auto;">
						<tr>
							<td class="lead"><b><font face="Arial" size="1">Paid</font></b></td>
							<td class="text-center lead"><b><font face="Arial" size="1">:</font></b></td>
							<td class="text-right lead"><b><font face="Arial" size="1"><?php echo number_format($receipt['amount'], 2); ?></font></b></td>
						</tr>

						<?php 
						$bal= $receipt['customer_change'];
						if($bal > 1){
							$bal = 0;
						}
						
						?>
						<tr>
							<td class="lead"><b><font face="Arial" size="1">Balalnce</font></b></td>
							<td class="text-center lead"><b><font face="Arial" size="1">:</font></b></td>
							<td class="text-right lead"><b><font face="Arial" size="1"><?php echo number_format($bal, 2); ?></font></b></td>
						</tr>
						<tr>
							<td class="text-center" colspan="3" >
								<font face="Arial" size="1">
									---------------------------------------------------------------------
								</font>
							</td>
						</tr>
					</table>
					
					<div class="text-center">
					<font face="Arial" size="1">Note <?php echo $receipt['memo']; ?></font><br>
						<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><br>
						
						 
					</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>