from the programming language in a method you send the header, with the stored procedure ups_venta_insertar, this will return the idventa and then you go through the grid where you have the lines that make up the detail of the sale and send it to the other stored procedure ups_venta_detalle_insertar:
-- CABECERA VENTA (tabla venta)
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE 'usp_venta_insertar'(
IN 'p_fecha' datetime,
IN 'p_id_cliente' int(10) UNSIGNED,
IN 'p_subtotal' decimal,
IN 'p_descuentos' decimal,
IN 'p_total' decimal,
OUT 'p_idventa' int(11) UNSIGNED,
OUT 'p_mierror_cod' int(11),
OUT 'p_mierror_msg' text)
MODIFIES SQL DATA
COMMENT 'Insertar Venta'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
GET DIAGNOSTICS CONDITION 1 @RETURNED_SQLSTATE = RETURNED_SQLSTATE, @MYSQL_ERRNO = MYSQL_ERRNO, @MESSAGE_TEXT = MESSAGE_TEXT;
SET p_mierror_cod = -10;
SET p_mierror_msg = CONCAT("ERRNO: ",@MYSQL_ERRNO, ", SQLSTATE: ", @RETURNED_SQLSTATE, ", TEXT: ", @MESSAGE_TEXT);
END;
START TRANSACTION;
SET p_idventa = 0;
INSERT INTO 'venta' ('id_cliente', 'subtotal', 'descuentos', 'total')
VALUES ('p_id_cliente', 'p_subtotal', 'p_descuentos', 'p_total');
SELECT LAST_INSERT_ID() INTO p_idventa;
-- Exitosamente
SET p_mierror_cod = 1;
SET p_mierror_msg = 'Venta insertado correctamente';
COMMIT;
END$$
DELIMITER ;
-- LINEA VENTA (TABLA venta_detalle)
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE 'usp_venta_detalle_insertar'(
IN 'p_id_venta' int(10) UNSIGNED,
IN 'p_id_producto' int(10) UNSIGNED,
IN 'p_cantidad' int,
IN 'p_precio_unitario' decimal,
IN 'p_importe' decimal,
OUT 'p_idventa_detalle' int(11) UNSIGNED,
OUT 'p_mierror_cod' int(11),
OUT 'p_mierror_msg' text)
MODIFIES SQL DATA
COMMENT 'Insertar Venta Detalle (linea)'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
GET DIAGNOSTICS CONDITION 1 @RETURNED_SQLSTATE = RETURNED_SQLSTATE, @MYSQL_ERRNO = MYSQL_ERRNO, @MESSAGE_TEXT = MESSAGE_TEXT;
SET p_mierror_cod = -10;
SET p_mierror_msg = CONCAT("ERRNO: ",@MYSQL_ERRNO, ", SQLSTATE: ", @RETURNED_SQLSTATE, ", TEXT: ", @MESSAGE_TEXT);
END;
START TRANSACTION;
SET p_idventa_detalle = 0;
INSERT INTO 'venta_detalle' ('id_venta', 'id_producto','cantidad','precio_unitario','importe')
VALUES ('p_id_venta','p_id_producto','p_cantidad','p_precio_unitario','p_importe');
SELECT LAST_INSERT_ID() INTO p_idventa_detalle;
-- Exitosamente
SET p_mierror_cod = 1;
SET p_mierror_msg = 'Venta Detalle (Linea) insertado correctamente';
COMMIT;
END$$
DELIMITER ;