<?php 
// Return next invoice number as JSON
header('Content-Type: application/json');

if(file_exists('../../../../'.($_REQUEST['tenant'] ?? '').'/config.php')){
    require_once('../../../../'.$_REQUEST['tenant'].'/config.php'); echo "3";
}
if(file_exists('/config.php')){
    require_once('/config.php');
}

$res = mysqli_query($GLOBALS['con'], "SELECT invNo FROM invoices ORDER BY invNo DESC LIMIT 1");
$next = 1;
if($res && mysqli_num_rows($res) > 0){
    $row = mysqli_fetch_assoc($res);
    $next = (int)$row['invNo'] + 1;
}
echo json_encode(['invoicenumber' => sprintf("%06d", $next)]);
exit;