<?php
function getDishCost($mysqli, $dish_id) {
    $sql = "SELECT dc.quantity, dc.unit, p.price 
            FROM dish_calc dc 
            JOIN products p ON dc.product_id = p.id 
            WHERE dc.dish_id = ?";
    
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param("i", $dish_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $total = 0;
    while ($row = $result->fetch_assoc()) {
        $qty = $row['quantity'];
        if ($row['unit'] === 'გრ') {
            $qty /= 1000;
        }
        $total += $qty * $row['price'];
    }
    return $total;
}