<?php

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

header('Content-Type: application/json');

$required_qty = isset($_POST['qty'])
    ? (float) $_POST['qty']
    : 0;

$product_id = isset($_POST['productId'])
    ? (int) $_POST['productId']
    : 0;

$response = array(
    'available_qty' => 0,
    'cost_price'    => '0.00'
);

if ($product_id > 0) {

    // Stock availability remains based on produced stock.
    $availableQty =
        ProducedProducts::GetTotalAvailableQuantityFIFO($product_id);

    // Cost price now comes directly from product_setup.
    $product =
        ProductSetup::GetProductSetupById($product_id);

    $actualCostPrice = 0;

    if ($product && isset($product->actual_cost_price)) {
        $actualCostPrice =
            (float) $product->actual_cost_price;
    }

    $response['available_qty'] =
        (float) $availableQty;

    $response['cost_price'] =
        number_format($actualCostPrice, 2, '.', '');
}

echo json_encode($response);
exit;
?>