<?php

/* $Id: SelectCustomer.php 7133 2015-02-07 03:23:51Z daintree $*/

include('includes/session.inc');
$Title = _('Pay Runs');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
  

echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';


echo '<p> Cash Til Reports. Please note this system rounds the amount to nearest FJD dollars and cents to ensure the POS till balances </p>';




	echo '<div class="col-sm-10">';// div class=?
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';
echo '<div>';


echo '<tr>
			<td>' ._('From Date'). ':</td>
			<td><input  id="datetime" type="datetime-local" maxlength="10" size="15" required="required" name="fromdate" pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" /></td>
		</tr>';

echo '</td>';
	echo '<tr>
			<td>' ._('To Date'). ':</td>
			<td><input  id="datetime" type="datetime-local" maxlength="10" size="15" required="required" name="todate" pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" /></td>
		</tr>';

echo '</td>';
	?>
<td>Select Retail Shops </td>
<td><select name="location" required >;
							<?php
	
							$SQL = "SELECT loccode,locationname											
		                            FROM locations 
									";
			
$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="all">' . _('All') . '</option>';
while ($myrow=DB_fetch_array($result)){
	
			echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'].'</option>';
	
	
	
	
}
echo '</select> </div><br>';

echo '<tr>
			<td>' ._('Counter'). ':</td>
			
			<td>
			<select name="counter" required="required">
			
			<option value="all">' . _('All ') . '</option> 
			<option value="counter1">' . _('Counter 1 ') . '</option>
			
			<option value="counter2">' . _('Counter 2') . '</option>
			<option value="counter3">' . _('Counter 3') . '</option>
			<option value="counter4">' . _('Counter 4') . '</option>
			<option value="counter5">' . _('Counter 5') . '</option>
			<option value="counter6">' . _('Counter 6') . '</option>
			<option value="counter7">' . _('Counter 7') . '</option>
			<option value="counter8">' . _('Counter 8') . '</option>
			<option value="counter9">' . _('Counter 9') . '</option>
			<option value="countertest">' . _('Test Counter') . '</option>
			</td>
			
		</tr>';

echo '</td>';

	
?>
<td>Select Cashier </td>
<td><select name="cashier" required >;
							<?php
	
							$SQL = "SELECT username											
		                            FROM pos_users
									";
			
$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="all">' . _('All ') . '</option>';
while ($myrow=DB_fetch_array($result)){
	
			echo '<option value="' . $myrow['username'] . '">' . $myrow['username'].'</option>';
	
	
	
	
}
echo '</select> </div><br>';

	
	
echo '<div><input type="submit"  name="quote" value="' ._('Show report') . '" />
		</div>
		';



echo '</div>';

echo '</div>';
	echo '</table>';


//$jobno = $_POST['Job'];
$dateraw = $_POST['fromdate'] ;



$starttime = date("Y-m-d H:m:s", strtotime($dateraw));


$dateraw1 = $_POST['todate'] ;  
$endtime = date("Y-m-d H:m:s", strtotime($dateraw1)); 

	
$cashier = $_POST['cashier'];
	$counter =$_POST['counter'] ;
	$loccode = $_POST['location'];


	if (isset($_POST['quote']) && $loccode == 'all' && $counter == 'all' && $cashier == 'all'){
	
			//echo ' <div class="row"> ';

		
		$sql4 =  "SELECT sales_retailshops.id,
	               sales_retailshops.sale_id,
				   sales_retailshops.sale_time,
				   
				   locations.locationname,
				   sales_retailshops.counter,
				  sales_retailshops.username
			FROM sales_retailshops INNER JOIN locations
				ON sales_retailshops.loccode=locations.loccode WHERE   sales_retailshops.sale_time between '" . $starttime. "' and '" . $endtime. "' 
			Order by sales_retailshops.id ASC;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
		
echo '<div class="table-responsive"><table id="projectreport" class="table">';
	echo '<thead> <tr class="info">
			<th>' . _('Sale Time') . '</th>
			<th>' . _('ID') . '</th>
			<th>' . _('Receipt/Sale ID') . '</th>
			<th>' . _('Location') . '</th>
	       	<th>' . _('Counter') . '</th>
			<th>' . _('Employee/Cashier') . '</th>
			<th>' . _('Amount') . '</th>
			
	
			</thead></tr>';
		$grandtotal = 0;
	
	while ($myrow4 = DB_fetch_array($result4)) {
		  
		//getbudgeted totals
		
		 $query = "SELECT SUM(quantity_purchased*item_unit_price) as 'total'
						 
						 
				   			  
		          FROM salesitems_retailshops where id ='" . $myrow4['id']. "'";
		
		$result5 = DB_query($query);
		
		$myrow5 = DB_fetch_array($result5);
		//get actual cost
		
		$subtotal = $myrow5['total'];
		
		
		
		$valueInString = strval(round($subtotal,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $subtotal-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $subtotal-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($subtotal);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$total = $newvalue;
		
		echo 		
					'<tr><td>' . $myrow4['sale_time'] . '</td>
					 <td>' . $myrow4['id'] . '</td>
					<td>' . $myrow4['sale_id']. '</td>
					<td>' . $myrow4['locationname']. '</td>
					<td>' . $myrow4['counter']. '</td>
					<td>' . $myrow4['username']. '</td>
					<td>' . round($total,2). '</td>
			
				
					</tr>';
		
		$grandtotal += $total;
		
	}
echo 'Total Sales:' . round($grandtotal,2) ;
	
echo '</div>';
echo '</div>';

		
		
	}

	
	elseif (isset($_POST['quote']) && $loccode !=='all' && $counter == 'all'  && $cashier == 'all'){  
	
			//echo ' <div class="row"> ';



		
		$sql4 =  "SELECT sales_retailshops.id,
	               sales_retailshops.sale_id,
				   sales_retailshops.sale_time,
				   locations.locationname,
				   sales_retailshops.counter,
				  sales_retailshops.username
			FROM sales_retailshops INNER JOIN locations
				ON sales_retailshops.loccode=locations.loccode WHERE   sales_retailshops.sale_time between '" . $starttime. "' and '" . $endtime. "' AND sales_retailshops.loccode ='" . $loccode. "' 
			Order by sales_retailshops.id ASC;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
		
	
echo '<div class="table-responsive"><table id="projectreport" class="table">';
	echo '<thead> <tr class="info">
			<th >' . _('Sale Time') . '</th>
			<th >' . _('ID') . '</th>
			<th >' . _('Receipt/Sale ID') . '</th>
			<th >' . _('Location') . '</th>
	       	<th >' . _('Counter') . '</th>
			<th >' . _('Employee/Cashier') . '</th>
			<th >' . _('Amount') . '</th>
			
	
			</tr></thead>';
		$grandtotal = 0;
	
	while ($myrow4 = DB_fetch_array($result4)) {
		
		//getbudgeted totals
		
		 $query = "SELECT SUM(quantity_purchased*item_unit_price) as 'total'
						 
						 
				   			  
		          FROM salesitems_retailshops where id ='" . $myrow4['id']. "'";
		
		$result5 = DB_query($query);
		
		$myrow5 = DB_fetch_array($result5);
		//get actual cost
		
		$subtotal = $myrow5['total'];
		
		
		
		$valueInString = strval(round($subtotal,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $subtotal-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $subtotal-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($subtotal);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$total = $newvalue;
		
		echo 		
					'<td>' . $myrow4['sale_time'] . '</td>
					 <td>' . $myrow4['id'] . '</td>
					<td>' . $myrow4['sale_id']. '</td>
					<td>' . $myrow4['locationname']. '</td>
					<td>' . $myrow4['counter']. '</td>
					<td>' . $myrow4['username']. '</td>
					<td>$' . round($total,2). '</td>
			
				
					</tr>';
		
		$grandtotal += $total;
		
	}
echo '<tr> <td></td>
					 <td></td>
					<td></td>
					<td></td>
					<td></td><td>Total Sales:' . round($grandtotal,2) . '</td> </tr> ';
	
echo '</div>';
echo '</div>';

		
		
	}
	
	
		elseif (isset($_POST['quote']) && $loccode !=='all' && $counter !=='all' && $cashier =='all' ){  
	
			//echo ' <div class="row"> ';


		
		$sql4 =  "SELECT sales_retailshops.id,
	               sales_retailshops.sale_id,
				   sales_retailshops.sale_time,
				   locations.locationname,
				   sales_retailshops.counter,
				  sales_retailshops.username
			FROM sales_retailshops INNER JOIN locations
				ON sales_retailshops.loccode=locations.loccode WHERE   sales_retailshops.sale_time between '" . $starttime. "' and '" . $endtime. "' AND sales_retailshops.loccode ='" . $loccode. "' AND sales_retailshops.counter='" . $counter. "'
			Order by sales_retailshops.id ASC;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
		
	
echo '<div class="table-responsive"><table id="projectreport" class="table">';
	echo '<thead> <tr class="info">
			<th >' . _('Sale Time') . '</th>
			<th >' . _('ID') . '</th>
			<th >' . _('Receipt/Sale ID') . '</th>
			<th >' . _('Location') . '</th>
	       	<th >' . _('Counter') . '</th>
			<th >' . _('Employee/Cashier') . '</th>
			<th >' . _('Amount') . '</th>
			
	
			</tr></thead>';
		$grandtotal = 0;
	
	while ($myrow4 = DB_fetch_array($result4)) {
		
		//getbudgeted totals
		
		 $query = "SELECT SUM(quantity_purchased*item_unit_price) as 'total'
						 
						 
				   			  
		          FROM salesitems_retailshops where id ='" . $myrow4['id']. "'";
		
		$result5 = DB_query($query);
		
		$myrow5 = DB_fetch_array($result5);
		//get actual cost
		
		$subtotal = $myrow5['total'];
		
		
		
		$valueInString = strval(round($subtotal,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $subtotal-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $subtotal-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($subtotal);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$total = $newvalue;
		
		echo 		
					'<td>' . $myrow4['sale_time'] . '</td>
					 <td>' . $myrow4['id'] . '</td>
					<td>' . $myrow4['sale_id']. '</td>
					<td>' . $myrow4['locationname']. '</td>
					<td>' . $myrow4['counter']. '</td>
					<td>' . $myrow4['username']. '</td>
					<td>$' . round($total,2). '</td>
			
				
					</tr>';
		
		$grandtotal += $total;
		
	}
echo '<tr> <td></td>
					 <td></td>
					<td></td>
					<td></td>
					<td></td><td>Total Sales:' . round($grandtotal,2) . '</td> </tr> ';
	
echo '</div>';
echo '</div>';

		
		
	}
	
	elseif (isset($_POST['quote'])&& $loccode !== 'all' && $counter !== 'all' && $cashier !== 'all'){  
		
	//echo ' <div class="row"> ';


		
		$sql4 =  "SELECT sales_retailshops.id,
	               sales_retailshops.sale_id,
				   sales_retailshops.sale_time,
				    locations.locationname,
				   sales_retailshops.counter,
				  sales_retailshops.username
			FROM sales_retailshops INNER JOIN locations
				ON sales_retailshops.loccode=locations.loccode WHERE  sales_retailshops.loccode ='" . $loccode. "' AND sales_retailshops.counter ='" . $counter. "' AND sales_retailshops.username='" . $cashier. "'AND  sales_retailshops.sale_time between '" . $starttime. "' and '" . $endtime. "' 
			Order by sales_retailshops.id ASC;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
		
	
echo '<div class="table-responsive"><table id="projectreport" class="table">';
	echo '<thead> <tr class="info">
			<th >' . _('Sale Time') . '</th>
			<th >' . _('ID') . '</th>
			<th >' . _('Receipt/Sale ID') . '</th>
			<th >' . _('Location') . '</th>
	       	<th >' . _('Counter') . '</th>
			<th >' . _('Employee/Cashier') . '</th>
			<th >' . _('Amount') . '</th>
			
	
			</tr></thead>';
		$grandtotal = 0;
	
	while ($myrow4 = DB_fetch_array($result4)) {
		
		//getbudgeted totals
		
		 $query = "SELECT SUM(quantity_purchased*item_unit_price) as 'total'
						 
						 
				   			  
		          FROM salesitems_retailshops where id ='" . $myrow4['id']. "'";
		
		$result5 = DB_query($query);
		
		$myrow5 = DB_fetch_array($result5);
		//get actual cost
		
		$subtotal = $myrow5['total'];
		
		
		
		$valueInString = strval(round($subtotal,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $subtotal-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $subtotal-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($subtotal);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$total = $newvalue;
		
		echo 		
					'<td>' . $myrow4['sale_time'] . '</td>
					 <td>' . $myrow4['id'] . '</td>
					<td>' . $myrow4['sale_id']. '</td>
					<td>' . $myrow4['locationname']. '</td>
					<td>' . $myrow4['counter']. '</td>
					<td>' . $myrow4['username']. '</td>
					<td>$' . round($total,2). '</td>
			
				
					</tr>';
		
		$grandtotal += $total;
		
	}
echo '<tr> <td></td>
					 <td></td>
					<td></td>
					<td></td>
					<td></td><td>Total Sales:' . round($grandtotal,2) . '</td> </tr> ';
	
echo '</div>';
echo '</div>';


	
}
		elseif (isset($_POST['quote'])&& $loccode !== 'all' && $counter == 'all' && $cashier !== 'all'){  
		
	//echo ' <div class="row"> ';


		
		$sql4 =  "SELECT sales_retailshops.id,
	               sales_retailshops.sale_id,
				   sales_retailshops.sale_time,
				    locations.locationname,
				   sales_retailshops.counter,
				  sales_retailshops.username
			FROM sales_retailshops INNER JOIN locations
				ON sales_retailshops.loccode=locations.loccode WHERE  sales_retailshops.loccode ='" . $loccode. "' AND sales_retailshops.username='" . $cashier. "'AND  sales_retailshops.sale_time between '" . $starttime. "' and '" . $endtime. "' 
			Order by sales_retailshops.id ASC;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
		
	
echo '<div class="table-responsive"><table id="projectreport" class="table">';
	echo '<thead> <tr class="info">
			<th >' . _('Sale Time') . '</th>
			<th >' . _('ID') . '</th>
			<th >' . _('Receipt/Sale ID') . '</th>
			<th >' . _('Location') . '</th>
	       	<th >' . _('Counter') . '</th>
			<th >' . _('Employee/Cashier') . '</th>
			<th >' . _('Amount') . '</th>
			
	
			</tr></thead>';
		$grandtotal = 0;
	
	while ($myrow4 = DB_fetch_array($result4)) {
		
		//getbudgeted totals
		
		 $query = "SELECT SUM(quantity_purchased*item_unit_price) as 'total'
						 
						 
				   			  
		          FROM salesitems_retailshops where id ='" . $myrow4['id']. "'";
		
		$result5 = DB_query($query);
		
		$myrow5 = DB_fetch_array($result5);
		//get actual cost
		
		$subtotal = $myrow5['total'];
		
		
		
		$valueInString = strval(round($subtotal,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $subtotal-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $subtotal-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($subtotal);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$total = $newvalue;
		
		echo 		
					'<td>' . $myrow4['sale_time'] . '</td>
					 <td>' . $myrow4['id'] . '</td>
					<td>' . $myrow4['sale_id']. '</td>
					<td>' . $myrow4['locationname']. '</td>
					<td>' . $myrow4['counter']. '</td>
					<td>' . $myrow4['username']. '</td>
					<td>$' . round($total,2). '</td>
			
				
					</tr>';
		
		$grandtotal += $total;
		
	}
echo '<tr> <td></td>
					 <td></td>
					<td></td>
					<td></td>
					<td></td><td>Total Sales:' . round($grandtotal,2) . '</td> </tr> ';
	
echo '</div>';
echo '</div>';


	
}
?>

<script>$(document).ready(function() {
    $('#projectreport').DataTable( {
        dom: 'Bfrtip',
        buttons: [
           
		{
                extend: 'pdfHtml5',
                orientation: 'portrait',
			pageSize: 'A4',
			
			
                customize: function(doc) {
      doc.defaultStyle.fontSize = 12; //<-- set fontsize to 16 instead of 10 
					  
   }  
            },
			{
                extend: 'excelHtml5'},
			
			{
                extend: 'copyHtml5'}
			
			
		]
    } );
} );
	
</script>	