salix/db/routines/vn/procedures/collection_addWithReservati...

91 lines
2.1 KiB
MySQL
Raw Normal View History

2024-01-31 06:11:03 +00:00
DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`collection_addWithReservation`(
2024-01-31 06:11:03 +00:00
vItemFk INT,
vQuantity INT,
2024-04-25 14:15:53 +00:00
vTicketFk INT,
2024-07-17 14:09:09 +00:00
vSaleGroupFk INT,
vSectorFk INT
2024-01-31 06:11:03 +00:00
)
BEGIN
/**
* En el ámbito de las colecciones se añade una línea de sale a un ticket
* de una colección en caso de tener disponible y se realiza la reserva.
*
* @param vItemFk id of item
* @param vQuantity quantity to be added to the ticket
* @param vTicketFk ticket to which the sales line is added
2024-04-25 14:15:53 +00:00
* @param vSaleGroupFk saleGroupFk id to add saleGroupDetail
2024-01-31 06:11:03 +00:00
*/
2024-04-25 14:15:53 +00:00
2024-01-31 06:11:03 +00:00
DECLARE vWarehouseFk INT;
DECLARE vCacheAvailableFk INT;
DECLARE vAvailable INT;
DECLARE vSaleFk INT;
DECLARE vConcept VARCHAR(50);
DECLARE vItemName VARCHAR(50);
DECLARE vHasThrow BOOLEAN DEFAULT FALSE;
2024-01-31 06:11:03 +00:00
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
SELECT t.warehouseFk INTO vWarehouseFk
FROM ticket t
JOIN ticketCollection tc ON tc.ticketFk = t.id
WHERE t.id = vTicketFk;
CALL cache.available_refresh(
2024-10-22 08:06:59 +00:00
vCacheAvailableFk,
2024-01-31 06:11:03 +00:00
FALSE,
2024-10-22 08:06:59 +00:00
vWarehouseFk,
2024-01-31 06:11:03 +00:00
util.VN_CURDATE());
SELECT available INTO vAvailable
FROM cache.available
2024-10-22 08:06:59 +00:00
WHERE calc_id = vCacheAvailableFk
2024-01-31 06:11:03 +00:00
AND item_id = vItemFk;
2024-10-22 08:06:59 +00:00
2024-01-31 06:11:03 +00:00
IF vAvailable < vQuantity THEN
SET vHasThrow = TRUE;
ELSE
SELECT `name`,
2024-10-22 08:06:59 +00:00
CONCAT(getUser(), ' ', DATE_FORMAT(util.VN_NOW(), '%H:%i'), ' ', name)
2024-01-31 06:11:03 +00:00
INTO vItemName, vConcept
2024-10-22 08:06:59 +00:00
FROM item
2024-01-31 06:11:03 +00:00
WHERE id = vItemFk;
START TRANSACTION;
2024-01-31 06:11:03 +00:00
INSERT INTO sale
SET itemFk = vItemFk,
ticketFk = vTicketFk,
concept = vConcept,
quantity = vQuantity,
isAdded = TRUE;
SELECT LAST_INSERT_ID() INTO vSaleFk;
CALL sale_calculateComponent(vSaleFk, NULL);
2024-07-17 14:09:09 +00:00
CALL itemShelvingSale_addBySale(vSaleFk, vSectorFk);
2024-10-22 08:06:59 +00:00
2024-01-31 06:11:03 +00:00
IF NOT EXISTS (SELECT TRUE FROM itemShelvingSale WHERE saleFk = vSaleFk LIMIT 1) THEN
SET vHasThrow = TRUE;
END IF;
END IF;
IF vHasThrow THEN
CALL util.throw("There is no available for the selected item");
END IF;
2024-10-22 08:06:59 +00:00
IF vSaleGroupFk THEN
INSERT INTO saleGroupDetail
SET saleFk = vSaleFk,
saleGroupFk = vSaleGroupFk;
END IF;
2024-10-22 08:06:59 +00:00
COMMIT;
2024-01-31 06:11:03 +00:00
END$$
DELIMITER ;