<?php
require("conexion.php");
$suc = $_POST['suc'];
if($suc == 1){
    $pre = 'CUL-';
    $not = 'NV-';
    $sucn = 'SUCURSAL MATRIZ';
}else if ($suc == 2){
    $pre = 'MAZ-';
    $not = 'NV-';
    $sucn = 'SUCURSAL MAZATLAN';
}else if($suc == 3){
    $pre = 'HER-';
    $not = 'NV-';
    $sucn = 'SUCURSAL HERMOSILLO';
}
$cod = $_POST['dato'];

if($cod == 1){
	if($suc == 1){
    $pre = 'CUL-';
}else if ($suc == 2){
    $pre = 'MAZ-';
}else if($suc == 3){
    $pre = 'HER-';
}
	$fol = mysqli_query($conexion,"SELECT * FROM Ventas ");
	if(mysqli_num_rows($fol)>0){

	    $foli = mysqli_query($conexion,"SELECT FolioVenta from Ventas WHERE TipoFolio = '1' AND Serie = '$pre' order by FolioVenta DESC limit 1");
	    $f = mysqli_fetch_array($foli);
	    $num = $f['FolioVenta']+1;
	    $a = sprintf('%04d',$num);
	    $folio = $pre.$a;
	}else{
	    $num = sprintf('%04d', 1);
	    $folio = $pre.$num;
	}
}else if($cod == 2){
	if($suc == 1){
    $pre = 'NVC-';
    $sucn = 'SUCURSAL MATRIZ';
}
	$fol = mysqli_query($conexion,"SELECT * FROM Ventas ");
	if(mysqli_num_rows($fol)>0){

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