<?php
include 'connect.php';

$product_id = $_POST['product_id'];
$adjust_qty = (int)$_POST['adjust_qty']; // can be negative
$reason = $_POST['reason'];

// 1. Save to adjustments table
$sqlAdj = "INSERT INTO stock_adjustments (product_id, adjust_qty, reason) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sqlAdj);
$stmt->bind_param("iis", $product_id, $adjust_qty, $reason);
$stmt->execute();

// 2. Update the products_table stock directly
$sqlUpd = "UPDATE products_table SET qty_Onhand = qty_Onhand + ? WHERE product_id = ?";
$stmt2 = $conn->prepare($sqlUpd);
$stmt2->bind_param("ii", $adjust_qty, $product_id);
$stmt2->execute();

header("Location: list_adjustment.php");
exit();
