salix/db/routines/vn/procedures/item_setVisibleDiscard.sql

80 lines
1.8 KiB
MySQL
Raw Normal View History

2023-11-07 15:13:21 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_setVisibleDiscard`(
vItemFk INT,
vWarehouseFk INT,
vQuantity INT,
vAddressFk INT)
BEGIN
/**
* Procedimiento para dar dar de baja/alta un item, si vAddressFk es NULL se entiende que se da de alta y se toma el addressFk de la configuración
*
* @param vItemFk Identificador del ítem
* @param vWarehouseFk id del warehouse
* @param vQuantity a dar de alta/baja
* @param vAddressFk id address
*/
2024-04-03 07:02:34 +00:00
2023-11-07 15:13:21 +00:00
DECLARE vTicketFk INT;
DECLARE vClientFk INT;
2023-11-08 10:50:04 +00:00
DECLARE vDefaultCompanyFk INT;
2023-11-07 15:13:21 +00:00
DECLARE vCalc INT;
DECLARE vAddressShortage INT;
SELECT barcodeToItem(vItemFk) INTO vItemFk;
2023-11-08 10:50:04 +00:00
SELECT DEFAULT(companyFk) INTO vDefaultCompanyFk
2023-11-07 15:13:21 +00:00
FROM vn.ticket LIMIT 1;
2024-04-03 05:09:07 +00:00
IF vAddressFk IS NULL THEN
2023-11-08 10:50:04 +00:00
SELECT pc.shortageAddressFk INTO vAddressShortage
FROM productionConfig pc ;
2024-04-03 05:09:07 +00:00
ELSE
2023-11-07 15:13:21 +00:00
SET vAddressShortage = vAddressFk;
END IF;
SELECT a.clientFk INTO vClientFk
FROM address a
WHERE a.id = vAddressFk;
SELECT t.id INTO vTicketFk
FROM ticket t
JOIN address a ON a.id = t.addressFk
JOIN ticketState ts ON ts.ticketFk = t.id
WHERE t.warehouseFk = vWarehouseFk
AND a.id = vAddressShortage
AND DATE(t.shipped) = util.VN_CURDATE()
AND ts.code = 'DELIVERED'
LIMIT 1;
CALL cache.visible_refresh(vCalc, TRUE, vWarehouseFk);
IF vTicketFk IS NULL THEN
CALL ticket_add(
vClientFk,
util.VN_CURDATE(),
vWarehouseFk,
2023-11-08 10:50:04 +00:00
vDefaultCompanyFk,
2023-11-07 15:13:21 +00:00
vAddressFk,
NULL,
NULL,
util.VN_CURDATE(),
account.myUser_getId(),
FALSE,
vTicketFk);
END IF;
INSERT INTO sale(ticketFk, itemFk, concept, quantity)
SELECT vTicketFk,
vItemFk,
2024-04-03 05:09:07 +00:00
longName,
2023-11-07 15:13:21 +00:00
vQuantity
FROM item
WHERE id = vItemFk;
UPDATE cache.visible
SET visible = visible - vQuantity
WHERE calc_id = vCalc
AND item_id = vItemFk;
END$$
DELIMITER ;