DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceFromAddress`(vMaxTicketDate DATETIME,vAddress INT,vCompany INT)
BEGIN

	DECLARE vMinDateTicket DATE DEFAULT TIMESTAMPADD(MONTH, -3, util.VN_CURDATE());

	SET vMaxTicketDate = util.dayEnd(vMaxTicketDate);

	DROP TEMPORARY TABLE IF EXISTS `tmp`.`ticketToInvoice`;

	CREATE TEMPORARY TABLE `tmp.``ticketToInvoice`
		(PRIMARY KEY  (`id`))
        ENGINE = MEMORY
		SELECT Id_Ticket id FROM vn2008.Tickets WHERE (Fecha BETWEEN vMinDateTicket
			AND vMaxTicketDate) AND Id_Consigna = vAddress
            AND Factura IS NULL AND empresa_id = vCompany;

END$$
DELIMITER ;
