<?php 
include_once("../connect.php");
global $con;
include_once("../session.php");


$fromDate = $_REQUEST['fdate'];
$toDate = $_REQUEST['tdate'];

$query = "SELECT MIN(Sale)MAX(SaleDate) FROM SALES_HISTORY_HEADER";

 ?>
 
  <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for 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/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="../js/bootstrap.min.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
       	 <script src="../js/excellentexport.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
       //        $('.grpline').css("width", "100px");
				
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>
<?php 
	include("navbar.php");

?>
        
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-8">
				<h3 class="page-header">Tax Summary</h3>
				<span>From: <?= date("m/d/Y", strtotime($fromDate)) ?> - To: <?= date("m/d/Y", strtotime($toDate)) ?></span>
			</div>
			<div class="col-xs-4">
				<div class="panel panel-default">
					
				</div>
			</div>
		</div>
		
		<div class="row">
			<div class="col-xs-12">
				<h4 class="page-header">Sales <small>All sales, regardless of payment status</small></h4>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<table class="table" id="TaxSummarySales">
					<thead>
						<tr>
							<th>Sales</th>
							<th>Tax 1 (<?= (getSetting('Tax1Name')['value']) ?>)</th>
							<th>Tax 2 (<?= (getSetting('Tax2Name')['value']) ?>)</th>
							<th>Total Tax</th>
							<th>Total</th>
							<th>Download</th>
						</tr>
						
					</thead>
					<tbody>
					<?php
						
						$query = "SELECT round(SUM(Subtotal),2) as TotalSales, SUM(Tax1Total+Tax2Total) as TotalTaxAmount, SUM(Tax1Total) as Tax1Total, SUM(Tax2Total) as Tax2Total FROM `INVOICE_HEAD` WHERE InvoiceDate BETWEEN '" . $fromDate . "' AND '" . $toDate . " 23:59:59'";
						$result = mysqli_query($con, $query);
						while($row = mysqli_fetch_array($result)){
							
					?>
						
						<tr>
							<td><?= moneyFormat($row['TotalSales']); ?></td>
							
							<td><?= moneyFormat($row['Tax1Total']); ?></td>
							<td><?= moneyFormat($row['Tax2Total']); ?></td>
							<td><?= moneyFormat($row['TotalTaxAmount']); ?></td>
							<td><?= moneyFormat($row['TotalSales'] + $row['TotalTaxAmount']); ?></td>
							<td><a class="btn btn-success" href="mod.php?p=taxreports&a=functions&f=downloadSalesTaxReport&fdate=<?= $fromDate ?>&tdate=<?= $toDate ?>&st=<?= $row['ST'] ?>">Download Detail</a></td>
							
						</tr>
					<?php
						}
					?>
					</tbody>
				</table>
			</div>
			
		</div>
		
		
		<div class="row">
			<div class="col-xs-12">
				<h4 class="page-header">Payments <small>Payments tax report.</small></h4>
			</div>
		</div>
		
		
		<div class="row">
			<div class="col-xs-12">
				<table class="table" id="TaxSummaryPayments">
					<thead>
						<tr>
							
							<th>Payment</th>
							<th>Total Discount</th>
							
							<th>Tax 1 (<?= (getSetting('Tax1Name')['value']) ?>)</th>
							<th>Tax 2 (<?= (getSetting('Tax2Name')['value']) ?>)</th>
							<th>Total Tax</th>
							<th>Total Payments</th>
							<th>Download</th>
						</tr>
						
					</thead>
					<tbody>
					<?php
						
						
						$query = "SELECT ROUND(SUM(PaymentAmount),2) as TotalPayment, ROUND(SUM(PaymentAmount),2)-sum(IF(PartialPayment, 0, TaxAmount)) TotalSales, ROUND(SUM(DiscountAmount),2) as TotalDiscount, sum(IF(PartialPayment, 0, TaxAmount)) as TotalTax, SUM(IF(PartialPayment,0,Tax1Total)) Tax1Total, SUM(IF(PartialPayment,0,Tax2Total)) Tax2Total FROM DETAILED_PAYMENT LEFT JOIN INVOICE_HEAD USING(InvoiceID) WHERE PaymentDate BETWEEN '$fromDate' AND '$toDate 23:59:59' AND DETAILED_PAYMENT.Void = 0";
						$result = mysqli_query($con, $query);
						while($row = mysqli_fetch_array($result)){
							
					?>
						
						<tr>
							
							
							<td><?= moneyFormat($row['TotalSales']); ?></td>
							<td><?= moneyFormat($row['TotalDiscount']); ?></td>
							
							
							<td><?= moneyFormat($row['Tax1Total']); ?></td>
							<td><?= moneyFormat($row['Tax2Total']); ?></td>
							<td><?= moneyFormat($row['TotalTax']); ?></td>
							<td><?= moneyFormat($row['TotalPayment']); ?></td>
							<td><a class="btn btn-success" href="mod.php?p=taxreports&a=functions&f=downloadPaymentsTaxReport&fdate=<?= $fromDate ?>&tdate=<?= $toDate ?>&st=<?= $row['ST'] ?>">Download Detail</a></td>
							
						</tr>
					<?php
						}
					?>
					</tbody>
				</table>
			</div>
			
			
			
		</div>
	</div>
	

     <script type="text/javascript">
			function processRegular(){
				alert('The report will begin to process now... the page will reload automatically');
				$.get('mod.php?p=taxreports&a=functions&f=processPaymentTaxRegular&fdate=<?= $fromDate ?>&tdate=<?= $toDate ?>', function(data){
					if(data == "true"){
						location.reload();		
					}else{
						alert("Unexpected result returned from server:\n\n" . data);
					}
						
				});
			}
			function processExceptions(){
				alert('The report will begin to process now... the page will reload automatically');
				$.get('mod.php?p=taxreports&a=functions&f=processPaymentTaxExceptions&fdate=<?= $fromDate ?>&tdate=<?= $toDate ?>', function(data){
					if(data == "true"){
						location.reload();		
					}else{
						alert("Unexpected result returned from server:\n\n" . data);
					}
					
						
				});
			}
     </script>
		 	<style>
				#iFrameModalUrl{
					width: 100%;
					height: 500px;
					border: none;
				}
				
			</style>
			
		 
   </body>
 </html>