<?php

include("../../classes/manager.class.php");
$m = new Manager();

// Get quantity and product ID from AJAX request
$required_qty = isset($_POST['qty']) ? (int)$_POST['qty'] : 0;
$product_id = isset($_POST['productId']) ? $_POST['productId'] : '';

$response = [];

if ($required_qty > 0 && !empty($product_id)) {
    // Get the available quantity
    $availableQty = ProducedProducts::GetTotalAvailableQuantityFIFO($product_id);

    // Get the cost price
    $costPrice = ProducedProducts::GetWeightedAverageCostPriceByProductId($product_id, $required_qty);
    
    // Prepare the response
    $response['available_qty'] = $availableQty;
    $response['cost_price'] = number_format($costPrice, 2);  // Format to 2 decimal places
} else {
    $response['available_qty'] = 0;
    $response['cost_price'] = 0;
}

// Return JSON response
echo json_encode($response);

?>

