DROP PROCEDURE IF EXISTS vn.ticket_doRefund;

DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doRefund`(OUT vNewTicket INT)
BEGIN
/**
 * Crea un ticket de abono a partir de tmp.sale y/o tmp.ticketService
 * 
 * @return vNewTicket
 */  
	DECLARE vDone BIT DEFAULT 0;
	DECLARE vClientFk MEDIUMINT;
	DECLARE vWarehouse TINYINT;
	DECLARE vCompany MEDIUMINT;
	DECLARE vAddress MEDIUMINT;
	DECLARE vRefundAgencyMode INT;
	DECLARE vItemFk INT;
	DECLARE vQuantity DECIMAL (10,2);
	DECLARE vConcept VARCHAR(50);
	DECLARE vPrice DECIMAL (10,2);
	DECLARE vDiscount TINYINT;
	DECLARE vSaleNew INT;
	DECLARE vSaleMain INT;
	DECLARE vZoneFk INT;
	DECLARE vDescription VARCHAR(50);
	DECLARE vTaxClassFk INT;
	DECLARE vTicketServiceTypeFk INT;
	DECLARE vOriginTicket INT;

	DECLARE cSales CURSOR FOR
		SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
			FROM tmp.sale s;

	DECLARE cTicketServices CURSOR FOR
		SELECT ts.description, - ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk
			FROM tmp.ticketService ts;

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;

	SELECT sub.ticketFk INTO vOriginTicket
		FROM (
			SELECT s.ticketFk
				FROM tmp.sale s
			UNION ALL
			SELECT ts.ticketFk
				FROM tmp.ticketService ts
		) sub
	LIMIT 1;

	SELECT id INTO vRefundAgencyMode
		FROM agencyMode WHERE `name` = 'ABONO';

	SELECT clientFk, warehouseFk, companyFk, addressFk 
			INTO vClientFk, vWarehouse, vCompany, vAddress
		FROM ticket
		WHERE id = vOriginTicket;

	SELECT id INTO vZoneFk
		FROM zone WHERE agencyModeFk = vRefundAgencyMode
		LIMIT 1;

	INSERT INTO vn.ticket (
			clientFk,
			shipped,
			addressFk,
			agencyModeFk,
			nickname,
			warehouseFk,
			companyFk,
			landed,
			zoneFk
		)
		SELECT
					vClientFk,
					CURDATE(),
					vAddress,
					vRefundAgencyMode,
					a.nickname,
					vWarehouse,
					vCompany,
					CURDATE(),
					vZoneFk
			FROM address a
			WHERE a.id = vAddress;

	SET vNewTicket = LAST_INSERT_ID();

	SET vDone := FALSE;
	OPEN cSales;
	FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;

	WHILE NOT vDone DO

		INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount)
			VALUES( vNewTicket, vItemFk, vQuantity, vConcept, vPrice, vDiscount );

	SET vSaleNew = LAST_INSERT_ID();

	INSERT INTO vn.saleComponent(saleFk,componentFk,`value`) 
		SELECT vSaleNew,componentFk,`value`
			FROM vn.saleComponent
			WHERE saleFk = vSaleMain;

		FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;

	END WHILE;
	CLOSE cSales;

	SET vDone := FALSE;
	OPEN cTicketServices;
	FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;

	WHILE NOT vDone DO

		INSERT INTO vn.ticketService(description, quantity, price, taxClassFk, ticketFk, ticketServiceTypeFk)
			VALUES(vDescription, vQuantity, vPrice, vTaxClassFk, vNewTicket, vTicketServiceTypeFk);

		FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;

	END WHILE;
	CLOSE cTicketServices;

	INSERT INTO vn.ticketRefund(refundTicketFk, originalTicketFk)
		VALUES(vNewTicket, vOriginTicket);
END$$
DELIMITER ;
