<?php
// fetch_unit_quantity.php

// Database connection and necessary includes
require_once('includes/load.php');

// Get the item code from the AJAX request
$itemcode = $_POST['itemcode'];

// Fetch unit-wise quantity split-up from the sapfetcharticles table
$query = "SELECT unitname, SUM(quantity) AS unit_quantity FROM sapfetcharticles WHERE itemcode = '{$itemcode}' GROUP BY unitname";
$result = $db->query($query);

// Generate the HTML content for the unit-wise quantity table
$html = '<h4>Unit-wise Quantity Split-up</h4>';
$html .= '<table class="table table-bordered table-striped table-hover">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th class="text-center">Unit Name</th>';
$html .= '<th class="text-center">Quantity</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';

while ($row = $db->fetch_assoc($result)) {
  $html .= '<tr>';
  $html .= '<td class="text-center">' . $row['unitname'] . '</td>';
  $html .= '<td class="text-center">' . $row['unit_quantity'] . '</td>';
  $html .= '</tr>';
}

$html .= '</tbody>';
$html .= '</table>';

// Return the HTML response
echo $html;
?>
