<?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'];
      }

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

    }}

$date=date("Y-m-d");
$item=htmlspecialchars(trim($_POST['itm_id']), ENT_QUOTES, 'UTF-8');
//$cost=htmlspecialchars(trim($_POST['cost']), ENT_QUOTES, 'UTF-8');
//$cost = mysqli_fetch_array(mysqli_query($conn,"SELECT sales_p from  stock_card WHERE id='$item'"));
$qty=htmlspecialchars(trim($_POST['qty_val']), ENT_QUOTES, 'UTF-8');

$sql6="SELECT * FROM invoice_no_tbl";
$result6=mysqli_query($conn,$sql6);
while ($row6 = mysqli_fetch_array($result6)){
 $stock2=$row6['dmg_stock'];
 $stock=$stock2+1;
 
}

$ref=$stock;

//$sql22=mysqli_query($conn,"UPDATE invoice_no_tbl SET dmg_stock = '$ref'");

if(empty($ref)) {
 echo "error ref not found";
} else{

$sql7=mysqli_query($conn,"INSERT INTO stock_orders SET ref='$ref', invoice='$ref', date = '$date', stock_date = '$date', type='Damage Items'");

$sql77=mysqli_query($conn,"INSERT INTO stock SET stock_card='$item',ref='$ref', date = '$date', cr_qty='$qty'");
  
if ($sql7 && $sql77) {
  echo "ok";
}else{
  echo "error with last";
}


}

?>