I have a problem with my system since I generate a self-incremented code in Mysql which works well when the system is used by only one user when using it 2 or 3 users this code that I generate automatically is repeated 3 times since its first creation until the last user there generated its sale.
To insert the sale I have the following in my mysql:
BEGIN
INSERT INTO venta(idtipodocumento,idcliente,idempleado,serie,numero,fecha,totalventa,igv,totalpagar,estado,descuento)
VALUES(pidtipodocumento,pidcliente,pidempleado,pserie,pnumero,pfecha,ptotalventa,pigv,ptotalpagar,pestado,pdescuento);
SELECT LAST_INSERT_ID() as idventa;
END
To get the last sales ID
BEGIN
SELECT CONVERT(SUBSTRING( MAX( SUBSTRING(Numero, 2) ),5),UNSIGNED INTEGER ) AS id
FROM venta
WHERE 1;
END
this code in php shows me the last id and generates a new one
public function generarNumVenta($idtipodoc=1) {
$con = new clsConexion;
if($con->conectarse()==true){
$query = "CALL SP_S_UltimoIdVenta($idtipodoc)";
$result = @mysql_query($query);
while($array = mysql_fetch_array($result)){
$ultimo_id_venta=$array['id']+1;
}
}
$strNum_venta=str_pad((int) $ultimo_id_venta,10,"0",STR_PAD_LEFT);
return $strNum_venta;
}
The problem is that when there is more than one user generating sales, this code is repeated and it is not wanted. If there are 3 users generating sales, this automatically generates example 0000000001, 0000000002, 0000000003 in the next sale 3 more users generated 3 sales 0000000004, 0000000005, 0000000006 and so on.
I apologize so much for the entanglement that someone could give a better question to get the best answer in the community, a greeting