<?php
include '../db/db.php';
session_start();
$log= htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
if (empty($log)) {
	 
  $redirectUrl = "../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";
	 
}else{

  $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
  $resultu=mysqli_query($conn, $sqlu);
  $countu=mysqli_num_rows($resultu);
  
	if($countu==1){
	
	  $sqlc="SELECT * FROM details_of_users where user='$log'";
	  $resultc=mysqli_query($conn, $sqlc);
	  while ($rowc = mysqli_fetch_array($resultc)){
	  $name=$rowc['id'];
	  }
		
	  $sup_id = htmlspecialchars(trim($_GET['sup']), ENT_QUOTES, 'UTF-8');
	  $inv_no = htmlspecialchars(trim($_GET['inv_no']), ENT_QUOTES, 'UTF-8');
	  
	  $date_t = date('Y-m-d');
	  
	  if(empty($sup_id) || empty($inv_no)) {
		  $redirectUrl = "../add_GRN_details.php?inv_no=$inv_no&&sup=$sup_id";
		  echo "<script type=\"text/javascript\">";
		  echo "window.location.href = '$redirectUrl'";
		  echo "</script>";
	  
	  } else {
	  
		  $sql6 = mysqli_fetch_assoc(mysqli_query($conn, "SELECT stock FROM invoice_no_tbl"));
		  $stock = $sql6['stock'];
		  $stock = $stock + 1;
		  
		  $ref = $stock;
		  
		  mysqli_query($conn, "UPDATE invoice_no_tbl SET stock='$stock'");
		   
		  $sqlsup = mysqli_fetch_assoc(mysqli_query($conn, "SELECT
grn_tkn_order.id,
grn_tkn_order.order_no,
grn_tkn_order.`date`,
supplier.acc_no
FROM
grn_tkn_order
Inner Join supplier ON grn_tkn_order.sup_id = supplier.id
WHERE
grn_tkn_order.inv_no = '$inv_no' AND
grn_tkn_order.sup_id = '$sup_id'"));
		 
			$date = $sqlsup['date'];
			$order_no = $sqlsup['order_no'];
			$acc_no = $sqlsup['acc_no'];
			$id_g = $sqlsup['id'];
			
			mysqli_query($conn, "INSERT INTO stock_orders SET p_order='$order_no', ref='$ref', sup_acc='$acc_no', invoice='$inv_no', date='$date_t', type='GRN', stock_date='$date'");
			
			mysqli_query($conn, "DELETE FROM grn_tkn_order WHERE id='$id_g'");		  
		  
		  $sqls = mysqli_query($conn, "SELECT * FROM grn_tkn_stock WHERE inv_no='$inv_no' and sup_id='$sup_id'");
		  while($rows = mysqli_fetch_array($sqls)) {
			$item = $rows['item'];
			$cost = $rows['cost'];
			$qty = $rows['qty'];
			$id_gr = $rows['id'];
			
			$row_bal = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(`dr_qty`) - SUM(`cr_qty`) AS bal FROM `stock` WHERE stock_card='$item'"));
			$old_qty = $row_bal['bal'];
			
			$avg = mysqli_query($conn, "SELECT avg_cost FROM stock_cost WHERE item='$item'");
			$sql_avg = mysqli_fetch_array($avg);
			$old_avg_cost = $sql_avg['avg_cost'];
			
			$new_qty = $qty;
			$new_cost = $cost;
			
			$avg_cost = ((($old_qty*$old_avg_cost)+($new_qty*$new_cost))/($old_qty+$new_qty));
			
			if(mysqli_num_rows($avg) == 0) {
				mysqli_query($conn, "INSERT INTO stock_cost SET avg_cost='$avg_cost', item='$item'");
			
			} else {
				mysqli_query($conn, "UPDATE stock_cost SET avg_cost='$avg_cost' WHERE item='$item'");
			}
			
			mysqli_query($conn, "INSERT INTO stock SET stock_card='$item',ref='$ref', unit_cost='$cost', dr_qty='$qty', date='$date'");
			mysqli_query($conn, "DELETE FROM grn_tkn_stock WHERE id='$id_gr'");
			  
		  }
		  
			echo "<script>alert('Successfully Finalized!')</script>";
			$redirectUrl = "../add_GRN.php";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
	  
	  }

	
	} else{
	  $redirectUrl = "../index.php";
	  echo "<script type=\"text/javascript\">";
	  echo "window.location.href = '$redirectUrl'";
	  echo "</script>";
	
	}
}

?>