<?php
// Include database connection and any other necessary files
include('../includes/dbcon.php');

// Check if rtype is provided
if (isset($_POST['rtype'])) {
    $rtype = $_POST['rtype'];

    // Fetch rno from the sequence table based on rtype
    $query = "SELECT cno,prefix FROM sequence WHERE rtype = '$rtype'";
    $result = mysqli_query($con, $query);
    if ($result) {
        $row = mysqli_fetch_assoc($result);
       // $rno = $row['cno'];

        $prefix=$row['prefix'];
//$inv_new_no=$prefix . $row['cno'];
$numberOfDigits = 6;
$cno=$row['cno'];
$rno = $prefix . sprintf('%0' . $numberOfDigits . 'd', $cno);



        echo json_encode(['success' => true, 'rno' => $rno]);
    } else {
        echo json_encode(['success' => false, 'error' => 'Failed to fetch rno from database']);
    }
} else {
    echo json_encode(['success' => false, 'error' => 'rtype is not provided']);
}
?>
