2022-05-26 05:41:00 +00:00
|
|
|
DROP PROCEDURE IF EXISTS `vn`.`ticket_doRefund`;
|
2022-04-04 11:26:53 +00:00
|
|
|
|
|
|
|
DELIMITER $$
|
|
|
|
$$
|
2022-04-21 12:59:41 +00:00
|
|
|
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doRefund`(OUT vNewTicket INT)
|
2022-04-04 11:26:53 +00:00
|
|
|
BEGIN
|
2022-04-21 12:59:41 +00:00
|
|
|
/**
|
|
|
|
* Crea un ticket de abono a partir de tmp.sale y/o tmp.ticketService
|
|
|
|
*
|
|
|
|
* @return vNewTicket
|
|
|
|
*/
|
2022-04-04 11:26:53 +00:00
|
|
|
DECLARE vDone BIT DEFAULT 0;
|
2022-04-21 12:59:41 +00:00
|
|
|
DECLARE vClientFk MEDIUMINT;
|
2022-04-04 11:26:53 +00:00
|
|
|
DECLARE vWarehouse TINYINT;
|
|
|
|
DECLARE vCompany MEDIUMINT;
|
|
|
|
DECLARE vAddress MEDIUMINT;
|
2022-04-21 12:59:41 +00:00
|
|
|
DECLARE vRefundAgencyMode INT;
|
|
|
|
DECLARE vItemFk INT;
|
|
|
|
DECLARE vQuantity DECIMAL (10,2);
|
|
|
|
DECLARE vConcept VARCHAR(50);
|
|
|
|
DECLARE vPrice DECIMAL (10,2);
|
|
|
|
DECLARE vDiscount TINYINT;
|
2022-04-04 11:26:53 +00:00
|
|
|
DECLARE vSaleNew INT;
|
2022-04-21 12:59:41 +00:00
|
|
|
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;
|
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
DECLARE cTicketServices CURSOR FOR
|
2022-04-21 12:59:41 +00:00
|
|
|
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
|
2022-04-22 09:45:14 +00:00
|
|
|
) sub
|
|
|
|
LIMIT 1;
|
2022-04-21 12:59:41 +00:00
|
|
|
|
|
|
|
SELECT id INTO vRefundAgencyMode
|
2022-04-04 11:26:53 +00:00
|
|
|
FROM agencyMode WHERE `name` = 'ABONO';
|
|
|
|
|
|
|
|
SELECT clientFk, warehouseFk, companyFk, addressFk
|
2022-04-21 12:59:41 +00:00
|
|
|
INTO vClientFk, vWarehouse, vCompany, vAddress
|
|
|
|
FROM ticket
|
|
|
|
WHERE id = vOriginTicket;
|
|
|
|
|
|
|
|
SELECT id INTO vZoneFk
|
2022-04-04 11:26:53 +00:00
|
|
|
FROM zone WHERE agencyModeFk = vRefundAgencyMode
|
2022-04-21 12:59:41 +00:00
|
|
|
LIMIT 1;
|
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
INSERT INTO vn.ticket (
|
|
|
|
clientFk,
|
|
|
|
shipped,
|
|
|
|
addressFk,
|
|
|
|
agencyModeFk,
|
|
|
|
nickname,
|
|
|
|
warehouseFk,
|
|
|
|
companyFk,
|
|
|
|
landed,
|
2022-04-21 12:59:41 +00:00
|
|
|
zoneFk
|
2022-04-04 11:26:53 +00:00
|
|
|
)
|
|
|
|
SELECT
|
2022-04-21 12:59:41 +00:00
|
|
|
vClientFk,
|
2022-04-04 11:26:53 +00:00
|
|
|
CURDATE(),
|
|
|
|
vAddress,
|
|
|
|
vRefundAgencyMode,
|
|
|
|
a.nickname,
|
|
|
|
vWarehouse,
|
|
|
|
vCompany,
|
|
|
|
CURDATE(),
|
2022-04-21 12:59:41 +00:00
|
|
|
vZoneFk
|
2022-04-04 11:26:53 +00:00
|
|
|
FROM address a
|
|
|
|
WHERE a.id = vAddress;
|
|
|
|
|
|
|
|
SET vNewTicket = LAST_INSERT_ID();
|
|
|
|
|
2022-04-21 12:59:41 +00:00
|
|
|
SET vDone := FALSE;
|
2022-04-04 11:26:53 +00:00
|
|
|
OPEN cSales;
|
2022-04-21 12:59:41 +00:00
|
|
|
FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;
|
2022-04-04 11:26:53 +00:00
|
|
|
|
|
|
|
WHILE NOT vDone DO
|
2022-04-21 12:59:41 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount)
|
|
|
|
VALUES( vNewTicket, vItemFk, vQuantity, vConcept, vPrice, vDiscount );
|
2022-04-21 12:59:41 +00:00
|
|
|
|
|
|
|
SET vSaleNew = LAST_INSERT_ID();
|
|
|
|
|
|
|
|
INSERT INTO vn.saleComponent(saleFk,componentFk,`value`)
|
|
|
|
SELECT vSaleNew,componentFk,`value`
|
|
|
|
FROM vn.saleComponent
|
|
|
|
WHERE saleFk = vSaleMain;
|
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;
|
2022-04-21 12:59:41 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
END WHILE;
|
|
|
|
CLOSE cSales;
|
|
|
|
|
2022-04-21 12:59:41 +00:00
|
|
|
SET vDone := FALSE;
|
2022-04-04 11:26:53 +00:00
|
|
|
OPEN cTicketServices;
|
2022-04-21 12:59:41 +00:00
|
|
|
FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;
|
2022-04-04 11:26:53 +00:00
|
|
|
|
|
|
|
WHILE NOT vDone DO
|
2022-04-21 12:59:41 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
INSERT INTO vn.ticketService(description, quantity, price, taxClassFk, ticketFk, ticketServiceTypeFk)
|
|
|
|
VALUES(vDescription, vQuantity, vPrice, vTaxClassFk, vNewTicket, vTicketServiceTypeFk);
|
2022-04-21 12:59:41 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;
|
2022-04-21 12:59:41 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
END WHILE;
|
|
|
|
CLOSE cTicketServices;
|
|
|
|
|
|
|
|
INSERT INTO vn.ticketRefund(refundTicketFk, originalTicketFk)
|
|
|
|
VALUES(vNewTicket, vOriginTicket);
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|