<?php
require("conexion.php");
date_default_timezone_set('America/Mazatlan');

$suc = $_POST['dato'];

if($suc == 1){
    $pre = 'CUL-';
    $not = 'NV';
    $sucn = 'SUCURSAL CULIACAN';
}else if ($suc == 2){
    $pre = 'MAZ-';
    $not = 'NV';
    $sucn = 'SUCURSAL MAZATLAN';
}else if($suc == 3){
    $pre = 'HER-';
    $not = 'NV';
    $sucn = 'SUCURSAL HERMOSILLO';
}
$fol = mysqli_query($conexion,"SELECT * FROM Ventas WHERE IdSucursal = '$suc'");
if(mysqli_num_rows($fol)>0){

    $foli = mysqli_query($conexion,"SELECT FolioVenta from Ventas WHERE IdSucursal = '$suc' AND Serie = '$pre' AND Activo = 1 order by FolioVenta desc limit 1");
    $f = mysqli_fetch_array($foli);
    $num = $f['FolioVenta']+1;
    $num = sprintf('%04d',$num);
    $folio = $pre.$num;
}else{
    $num = sprintf('%04d', 1);
    $folio = $pre.$num;
}

echo $folio;

?>