<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
//include ('includes/function.php');

$array    = '';
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$source = $_REQUEST['source'];

if ($source == 'PU') {
    $maindb = "purmain";
} else if ($source == 'SL') {
    $maindb = "salemain";
} else if ($source == 'PR') {
    $maindb = "debmain";
} else if ($source == 'SR') {
    $maindb = "credmain";
}

if (isset($_REQUEST['company'])) {
    $company = $_REQUEST['company'];

    $result = mysqli_query($conn, "SELECT max(invno) as maxbillno FROM `$maindb` WHERE `vouchno`!='' and `company`='$company'") or die(mysqli_error($conn));
    $getres  = mysqli_num_rows($result);
    $fetres  = mysqli_fetch_array($result);
    if ($fetres && $fetres['maxbillno'] != '') {
        $newbillno = $fetres['maxbillno'] + 1;
        echo $newbillno ;
    } else {
        echo "1";
    }
}
