<?php
	include("includes/dbconfig.php");
	include('Aira_settings.php');
	$pagetitle = "Cash Sales Register";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Point Of Sale</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default box -->
					<?php
						if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] == 4 ||  $_SESSION["connectedusergroup"] == 1)
						{
					?>
					<div class="row">
						<div class="col-md-12">
							<div id="servererrormessage">
							</div>
						</div>
					</div>
				
					<div class="row">
						<div class="col-md-8">
						 
						<?php 
						$web_order =  $_SESSION["web_order"];
						 
					 
						$order = web_order_products_id($web_order  );
						 echo $order[1];
						 $web_orders = web_orders($order);
						 $cus_tomer = $web_orders[2];
						 //echo $order[2];
						 //print_r($web_orders);
					 
						
						
						?>
							<div class="card card-primary" id="saledetails" style="height: 500px;">
								<div class="card-body">
									<div class="row">
										<div class="form-group col-sm-8">
											<input type="text" name="itemcode" id="itemcode" class="form-control" placeholder="Scan Item Barcode" >
										</div><!-- form-group -->
										<div class="form-group col-sm-4">
											<button id="searchitembtn" onclick="loadsearchform()" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i> Open Item Search</button>
										</div>
									</div>
									
									
									 
										<div id="cartitemslist">
											<table class="table table-hover"  id="cartitemstable">
												<thead>
													<tr>
														<th style="display:none;"></th>
														<th>Name</th>
														<th>Srial Number(s)</th>
														<th class="text-right">Price</th>
														<th class="text-right">Quantity</th>
														<th class="text-right">Total</th>
														<th class="text-right">Action</th>
													</tr>
												</thead>
												<tbody>
													
												</tbody>
											</table>
										</div>
									 
								</div><!-- /.box-body -->
							</div><!-- /.box -->
						</div>
						
						<div class="col-md-4">
							<div class="card card-success" id="salesummary" style="height: 700px;">
								<div class="card-body">
									 
									
									 
										 
											 
											<input type="hidden" name="clientname" id="clientname"  value='<?php echo $_SESSION['webclient_id'];?>'/>
											<input type="hidden"  name="attendant" id="attendant" value='0' />
											<input type="hidden" name="mechanic" id="mechanic" value='0' />
										 
										 
								 
										 
										  
										  
										  
										  
									
									<div class="form-group col-md-12">
										
										
									
									<label class="col-md-6">Items Count</label><i class="col-md-6" id="itemscount"><b>0</b></i>
									
									<div class="col-md-12" style="margin-top: 25px"></div>
									
									<label class="col-md-6">Amount Due</label><i class="col-md-6" id="cost"><b>0 UGX</b></i>
									
									<div class="col-md-12" style="margin-top: 25px"></div>
									
									<div class="form-group col-md-12">
										<label for="cashpaidin">Paid Amount</label>
										<input type="text" name="cashpaidin" id="cashpaidin" maxlength="100" class="form-control" onfocus="this.select();" onmouseup="return false;">
									</div><!-- form-group -->
									
									<div class="form-group col-md-12">
										<label for="receiptmode">Payment Type</label>
										<select  class="form-control" name="receiptmode" id="receiptmode">
											<?php
												$paymentmodedata = DB::query("SELECT id, payment_name FROM payment_type");
												
												foreach ($paymentmodedata as $paymentmode)
												{
											?>
											<option value="<?php echo $paymentmode['id']; ?>"><?php echo $paymentmode['payment_name']; ?></option>
											<?php
												}
											?>
										</select>
									</div><!-- form-group -->
										
									<label class="col-md-6">Customer Change</label><i class="col-md-6" id="clientbalance"><b>0 UGX</b></i>
									
									<div class="col-md-12" style="margin-top: 25px"></div>
									
									<div class="form-group col-md-12">
										<textarea name="memo" id="memo" rows="3" placeholder="Additional Notes ... " maxlength="100" class="form-control"></textarea>
									</div><!-- form-group -->
									
									<div class="btn-list col-md-12 text-center">
										<button onclick="cancelsale()" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancel Sale</button>
										
										<button onclick="processcashsale()" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Process Sale</button>
									</div>
							
								</div><!-- /.box-body -->
							</div><!-- /.box -->
						</div>
					</div>
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="glyphicon glyphicon-ban-circle"></i>
						Limited Access
					</div>
					<?php
						}
					?>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->
 
		</div><!-- ./wrapper -->
		
		<div class="modal" id="waitdialogmodal"  data-backdrop="static">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                  <div class="modal-body">
					<div class="text-center">
						<img src="<?php echo $base_url . $ajax_loading_image; ?>" />
					</div>
				  </div>
              </div>
            </div>
        </div>
		
		<div class="modal" id="mediummodal">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title text-center">Cart Item Update</h4>
					</div>
					<div class="modal-body">					
						<div id="itemediterrormessage">
						</div>
						
						<div id="itemrowindex" style="display:none;">
						</div>
						
						<div id="itemnamedisplay" class="text-center" >
							<b>Sample Item Name</b>
						</div>
						
						<div class="col-md-12" style="margin-top: 15px"></div>
						
						<div class="row">
							<div class="col-sm-6">
								<div class="form-group">
									<label for="itemquantity">Item Quantity</label>
									<input type="text" name="itemquantity" id="itemquantity" class="form-control" >
								</div><!-- form-group -->
							</div>
							
							<div class="col-sm-6">
								<div class="form-group">
									<label for="itemsellprice">Item Sell Price</label>
									<input type="text" name="itemsellprice" id="itemsellprice" class="form-control" >
								</div><!-- form-group -->
							</div>
						</div>
						
						<div class="row">
							<div class="col-sm-12">
								<div class="form-group">
									<label for="serialnumbers">Serial Number(s)</label>
									<input type="text" name="serialnumbers" id="serialnumbers" maxlength="195" class="form-control" >
								</div><!-- form-group -->
							</div>
						</div>
						
						<div id="notesfooter" class="modal-footer">
							<a href="" data-dismiss="modal" class="btn btn-danger"><i class="fa fa-times"></i> Close</a>
							<button name="updateitembtn" id="updateitembtn" onclick="cartitemrowupdate()" class="btn btn-primary"><i class="fa fa-save"></i> Save</button>
							
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="modal" id="largemodal">
			<div class="modal-dialog modal-lg">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title text-center">Item Search Form</h4>
					</div>
					<div class="modal-body">
						<div id="searchitemerrormessage">
						</div>
						
						<div class="form-group">
							<div class="input-group">
								<input type="text" name="itemsearchtext" id="itemsearchtext" class="form-control" placeholder="Enter Item Name Or Code" />
								<span class="input-group-btn">
									<button class="btn btn-primary btn-flat" name="getitemsbtn" id="getitemsbtn">Go!</button>
								</span>
							</div>
						</div><!-- form-group -->
						
						<div id="searchresultsdisplay">
						
						</div>
					</div>
					<div id="notesfooter" class="modal-footer">
						<a href="" data-dismiss="modal" class="btn btn-danger"><i class="fa fa-times"></i> Close</a>
					</div>
				</div>
			</div>
		</div>
		
			<?php include ('includes/general_footer.php')?>
		
		<script src="<?php echo $base_url; ?>plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="<?php echo $base_url; ?>plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="<?php echo $base_url; ?>plugins/bert/onlineregister.js" type="text/javascript"></script>
		<script src="<?php echo $base_url; ?>plugins/js/select2.full.min.js" type="text/javascript"></script>
		
		<script>
		$('.process_orderx').click(function() {
    
});

$('.process_order').click(function() {
  
	 
	$(this).prop("disabled",true);
	 
	 
	
	
	$.ajax
				(
				 
					{
						url: "process_order.php",
						async: false, 
						type: "post",
						dataType: "json",
						data: {itemcode: $(this).val().trim()},
						success: function(json)
						{
							if(json[0].isdataobtained == 0)
							{
								$("#servererrormessage").html(json[0].message);
								$('html, body').animate({ scrollTop: 0}, 'slow');
								$("#itemcode").select();
							}
							else
							{
								var itemrow = '<tr><td style="display:none;">'+json[0].priceid+'</td><td>'+json[0].name+'</td><td></td><td class="text-right">'+json[0].price+'</td><td class="text-right">1</td><td class="text-right">'+json[0].price+'</td><td class="text-right"><a href="javascript:void(0);" onclick="itemupdate(this)" data-toggle="tooltip" title="Edit Cart Item" class="tooltips"><span class="label label-primary" ><i class="glyphicon glyphicon-edit"></i></span></a> <a href="javascript:void(0);" onclick="itemdelete(this)" data-toggle="tooltip" title="Delete Cart Item" class="tooltips"><span class="label label-danger" ><i class="glyphicon glyphicon-trash"></i></span></a></td></tr>';
								
								$('#cartitemstable > tbody:last-child').append(itemrow);
								document.getElementById("itemcode").value = "";
								
								$('#cartitemslist').slimscroll({scrollTo: '350px'});
								
								var numberofcartitems = 0.00;
								var totalbill = 0.00;
									
								$("#cartitemstable").children("tbody").children("tr").each(function(i) 
								{
									$tr = $(this);
									var useindex = 0;
				
									$("td", $tr).each(function() 
									{
										if(useindex == 4)
										{
											numberofcartitems += parseFloat($(this).text().replace(/[^0-9\.]/g,""));
										}
										
										if(useindex == 5)
										{
											totalbill += parseFloat($(this).text().replace(/[^0-9\.]/g,""));
										}
										
										useindex +=1;
									});
								});
								
								$("#itemscount").html('<b>'+numberofcartitems+'</b>');
								$("#cost").html('<b>'+totalbill.toLocaleString()+' UGX</b>');
								document.getElementById("cashpaidin").value = "";
								$("#clientbalance").html('<b>0 UGX</b>');
							}
						},
						error: function(jqXHR, exception)
						{	
							if (jqXHR.status === 0) 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Not Connected Verify Network.</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else if (jqXHR.status == 404) 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Requested Operation Not Found.</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else if (jqXHR.status == 500) 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Internal Server Error.</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else if (exception === "parsererror") 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Requested JSON Parse Failed" + jqXHR.responseText + ".</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else if (exception === "timeout") 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Time Out Error.</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else if (exception === "abort") 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Ajax Request Aborted.</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							} 
							else 
							{
								$("#servererrormessage").html("<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Uncaught Error " + jqXHR.responseText + ".</div>");
								$('html, body').animate({ scrollTop: 0}, 'slow');
							}
						}
					}
				);
				 
  
});
</script>
	<script>
	
	
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2();

	//SelectElement("receiptmode", valueToSelect);
	//alert();
	function test(){
		 
	}
	$('#change_display').hide();

	$("#receiptmode").change(function()
    {
		//$('#change_display').hide();
		var id=$(this).val();
		
		var totalbill = 0.00;
				
			$("#cartitemstable").children("tbody").children("tr").each(function(i) 
			{
				$tr = $(this);
				var useindex = 0;

				$("td", $tr).each(function() 
				{
					 
					
					if(useindex == 4)
					{
						totalbill += parseFloat($(this).text().replace(/[^0-9\.]/g,""));
					}
					
					useindex +=1;
				});
			});

		 
         
         return false;
    });
	/*
	//$("#div1").load("demo_test.txt");
	$.get("process_order.php", function(data, status){
    alert();
  }); */
	
	
 
  })
  
  /*
  // AJAX call for autocomplete 
$(document).ready(function(){
	$("#search-box").css("background","#ffe6e6");
	
	$("#search-box").keyup(function(){
		 
		$.ajax({
		type: "POST",
		url: "get_customers.php",
		data:'keyword='+$(this).val(),
		beforeSend: function(){
			 
			$("#clientname").css("background","#FF0");
		},
		success: function(data){ 
			$("#clientname").html(data); 
			 
			$("#clientname").css("background","#ffcccc");
			$("#search-box").css("background","#ffffff");
		}
		});
	});
});*/
//To select country name
 
</script>
	</body>
</html>