drop procedure if EXISTS register_Send_invoice;
DELIMITER $$
CREATE  PROCEDURE `register_Send_invoice`(IN `issue_dateP` VARCHAR(50), IN `file_nameP` VARCHAR(50), IN `reference_dateP` VARCHAR(50), IN `processed_dateP` VARCHAR(50), IN `typeP` VARCHAR(50), IN `flg_responseP` INT, IN `error_codeP` VARCHAR(15), IN `response_descripP` VARCHAR(1000), IN `statusP` INT, IN `uCreaP` INT, IN `fCreaP` VARCHAR(50), IN `estadoP` INT, IN `sale_idP` INT)
    NO SQL
BEGIN

DECLARE resultadoID INT;

INSERT INTO `tec_send_invoice`(
    `issue_date`, `number`, `file_name`, 
    `ticket`, `reference_date`, `processed_date`, 
    `type`, `flg_response`, `error_code`, 
    `response_descrip`, `observations`, `status`, 
    `uCrea`, `fCrea`, `uActualiza`, 
    `fActualiza`, `estado`) VALUES (
        issue_dateP, null, file_nameP, 
        null, reference_dateP, processed_dateP,
        typeP, flg_responseP, error_codeP,
        response_descripP, null, statusP,
        uCreaP, fCreaP, null,
        null, estadoP
    );
    
    SET resultadoID := (SELECT LAST_INSERT_ID());
    
    INSERT INTO `tec_send_invoice_items`(`send_invoice_id`, `sale_id`, `uCrea`, 
                                         `fCrea`, `uActualiza`, `fActualiza`, 
                                         `estado`) VALUES (
                                             resultadoID, sale_idP, uCreaP, 
                                             fCreaP, null, null, 
                                             estadoP);

END$$
DELIMITER ;