<?php

include 'load.php';
require_once 'db_connection.php'; 
?>


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
	<meta name="description" content="POS - Eat More Resturant   | Pos & Invetory System | Dashboard

">
	<meta name="keywords" content="Eat More Resturant   | Pos & Invetory System | Dashboard

">
	<meta name="author" content="Eat More Resturant   | Pos & Invetory System | Dashboard

">
	<meta name="robots" content="noindex, nofollow">

	<title>Stock Transfer to Agency | Eat More Resturant   | Pos & Invetory System | Dashboard

</title>


	<?php
	require_once 'headercss.php';
	?>
</head>
<body>
	<div id="global-loader">
		<div class="whirly-loader"> </div>
	</div>

	<div class="main-wrapper">
		<?php
		require_once 'header.php';
		?>

		<?php
		require_once 'navbar.php';
		?>

		<div class="page-wrapper">
			<div class="content">
				<div class="page-header">
					<div class="page-title">
						<h4>Stock Transfer</h4>
						<h6>Stock Transfer to Agency</h6>
					</div>
					<div class="pcredit-btn">
						<a href="agency-stock-transfer-orders.php" class="btn btn-added"><img src="assets/img/icons/search.svg" alt="img" class="me-1">View Stock Transfer Order</a>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-body">

					<div class="row">
						<div class="col-5">
							<div class="row">
								<div class="col-12">
									<label>Select Agency</label>
									<select class="select" id="agency" onchange="loadAgencyOrderItems();">
										<option value="0">Select Agency</option>
										<?php
										$sql = "SELECT *  FROM mobileagency WHERE status=1 ORDER BY agname ASC";
										$result = $conn->query($sql);
										if ($result->num_rows > 0) {
											while($row = $result->fetch_assoc()) {
												$agid = $row["agid"];
												$agname = $row["agname"];
												echo '<option value="'.$agid.'">'.$agname.'</option>';
											}
										}
										?>
									</select>
								</div>




								<div class="col-12 mt-2" id="product_view">
									<label>Select Product</label>
									<select class="select" id="product" onchange="showPrices();">
										<option value="0">Select Product</option>
										<?php
										$sql = "SELECT *  FROM mobileproducts WHERE status=1 ORDER BY proname ASC";
										$result = $conn->query($sql);
										if ($result->num_rows > 0) {
											while($row = $result->fetch_assoc()) {
												$prid = $row["prid"];
												$proname = $row["proname"];
												$prcode = $row["prcode"];
												$qty = $row["qty"];
												$sellprice = $row["sellprice"];
												echo '<option value="'.$prid.'" data-price="'.$sellprice.'" data-qty="'.$qty.'">'.$proname.'</option>';
											}
										}
										?>
									</select>
								</div>

								<div class="col-12 mt-2">
									<label>Whole Sale Price</label>
									<input type="number" id="price" class="form-control"  placeholder="Whole Sale Price" name="price">
								</div>

								<div class="col-12 mt-2">
									<label>Quantity</label>
									<input type="number" id="qty" class="form-control"  placeholder="Quantity" name="qty">
								</div>

								<div class="col-12 mt-2">
									<button class="btn btn-success" onclick="addToOrder();">Add to Order</button>
								</div>

								<div class="col-12 mt-2" id="errormsg" style="color: red;"></div>
							</div></div>

							<div class="col-7" id="msgView"></div>
						</div>
					</div>

				</div>
			</div>
		</div>

		<?php
		require_once 'footerjs.php';
		?>
		<script src="assets/alertify/alertify.js"></script>
		<script src="assets/alertify/alertify.min.js"></script>
	</body>
	</html>

	<script type="text/javascript">
		//	window.onload = loadAgencyOrderItems;
		$(document).ready(function() {
			loadProductDropDown();
			loadAgencyOrderItems();
		});

		function loadProductDropDown() {
			$.ajax({
				url : "agency-stock-transfer-product-load.php",
				type : "POST",
				success: function(data){
					document.getElementById("product_view").innerHTML=data;
				},
				error: function(jXHR, textStatus, errorThrown){
					alert(jXHR.responseText);
				}
			})
		}

		function showPrices() {
			var price = $('#product option:selected').data('price');
			var balanceQty = $('#product option:selected').data('qty');
			document.getElementById("errormsg").innerHTML="Price : "+price+"<br>Available Stock : "+balanceQty;
			document.getElementById("price").value = price;

		}

		function addToOrder() {
			var agency = document.getElementById("agency").value;
			var product = document.getElementById("product").value;
			var qty = document.getElementById("qty").value;
			var price = document.getElementById("price").value;
			var proname = $('#product option:selected').data('proname');
			var prcode = $('#product option:selected').data('prcode');
			var brandname = $('#product option:selected').data('brandname');
			var category = $('#product option:selected').data('category');
			var discount = $('#product option:selected').data('discount');
			var sellprice = $('#product option:selected').data('sellprice');
			var balanceQty = $('#product option:selected').data('qty');

			if(agency>0){
				if(product>0 && qty>0){
					if(balanceQty==qty || balanceQty>qty){

						$.ajax({
							url : "agency-stock-transfer-save.php",
							type : "POST",
							data : "agency="+agency+"& product="+product+"& qty="+qty+"&  price="+price+"&  proname="+proname+"&  prcode="+prcode+"&  brandname="+brandname+"&  category="+category+"&  discount="+discount+"&  sellprice="+sellprice+"& ",
							success: function(data){
								if(data>0){
									document.getElementById("errormsg").innerHTML="Transfered";
									loadProductDropDown();
									loadAgencyOrderItems();
								}else{
									document.getElementById("errormsg").innerHTML= data;
								}
							},
							error: function(jXHR, textStatus, errorThrown){
								alert(jXHR.responseText);
							}
						})

					}else{
						document.getElementById("errormsg").innerHTML="Balance Quantity Not Enough..";
					}

				}else{
					document.getElementById("errormsg").innerHTML="Select Product & Enter Product Quantity..";
				}

			}else{
				document.getElementById("errormsg").innerHTML="Select Agency.";
			}
		}


		function loadAgencyOrderItems() {
			var agency = document.getElementById("agency").value;
			if(agency>0){

				$.ajax({
					url : "agency-stock-transfer-view.php",
					type : "POST",
					data : "agency="+agency+"& ",
					success: function(data){
						document.getElementById("msgView").innerHTML=data;
					},
					error: function(jXHR, textStatus, errorThrown){
						alert(jXHR.responseText);
					}
				})

			}else{
				document.getElementById("errormsg").innerHTML="Select Agency..";
			}
		}

		function deleteCartItem(transfer_id,product_id, quantity, agency,rowCount) {
			var table = document.getElementById('mytable');
			alertify.confirm("Do you want delete this ?", function (e) {
    if (e) {

			$.ajax({
				url : "agency-stock-transfer-delete.php",
				type : "POST",
				data : "transfer_id="+transfer_id+"& product_id="+product_id+"&  quantity="+quantity+"&   agency="+agency+"& ",
				success: function(data){
					loadAgencyOrderItems();
					loadProductDropDown();
					document.getElementById("errormsg").innerHTML="Deleted...!";
				},
				error: function(jXHR, textStatus, errorThrown){
					alert(jXHR.responseText);
				}
			})

			}
});
		}

		function completeTransferNote(total_amount) {
			var agency = document.getElementById("agency").value;
			if(agency>0){

				$.ajax({
					url : "agency-stock-transfer-complete.php",
					type : "POST",
					data : "agency="+agency+"& total_amount="+total_amount+"& ",
					success: function(data){
						if(data>0){
							document.getElementById("msgView").innerHTML="Stock Transfer Note : "+data+"<br>Success..!";
						}

					},
					error: function(jXHR, textStatus, errorThrown){
						alert(jXHR.responseText);
					}
				})

			}else{
				document.getElementById("errormsg").innerHTML="Select Agency..";
			}
		}
	</script>