<?php
include $_SERVER['DOCUMENT_ROOT'] . "/classes/autoload.php";

if (!isset($_POST['supplier_code']) || empty($_POST['supplier_code'])) {
    echo 'supplier_code is empty';
    exit;
}

$supplier_code = trim($_POST['supplier_code']);

$db = new db();

$res = $db->query("supplier_orders", "SELECT supplier_order_no FROM supplier_orders WHERE supplier_order_no LIKE '{$supplier_code}-%' ORDER BY record_id DESC LIMIT 1");

if ($res->num_rows == 0) {
    echo $supplier_code . '-1';
} else {
    $last = $res->fetch_assoc()['supplier_order_no'];
    $num  = (int) str_replace($supplier_code . '-', '', $last);
    echo $supplier_code . '-' . ($num + 1);
}