2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemShelving_selfConsumption`(
|
2024-11-13 08:03:44 +00:00
|
|
|
vShelvingCode VARCHAR(10) COLLATE utf8_general_ci,
|
2024-01-15 11:31:03 +00:00
|
|
|
vItemFk INT,
|
|
|
|
vQuantity INT
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
2024-09-24 14:48:48 +00:00
|
|
|
* Leave the indicated amount on the shelve
|
2024-01-15 11:31:03 +00:00
|
|
|
* and create a ticket with the difference.
|
|
|
|
*
|
2024-11-13 08:03:44 +00:00
|
|
|
* @param vShelvingCode Code of the shelve where the item is located
|
|
|
|
* @param vItemFk Item of which the self-consumption ticket is to be created
|
|
|
|
* @param vQuantity Amount that will stay on the shelve
|
2024-01-15 11:31:03 +00:00
|
|
|
*/
|
|
|
|
DECLARE vVisible INT;
|
|
|
|
DECLARE vClientFk INT;
|
|
|
|
DECLARE vTicketFk INT;
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
DECLARE vCompanyFk INT;
|
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vItemShelvingFk INT;
|
2024-02-15 09:04:15 +00:00
|
|
|
DECLARE vAddressFk INT;
|
2024-11-13 08:03:44 +00:00
|
|
|
DECLARE vShelvingFk INT;
|
|
|
|
|
|
|
|
SELECT id INTO vShelvingFk
|
|
|
|
FROM shelving
|
|
|
|
WHERE code = vShelvingCode;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SELECT c.id,
|
|
|
|
pc.clientSelfConsumptionFk,
|
2024-02-15 09:04:15 +00:00
|
|
|
s.warehouseFk,
|
|
|
|
pc.addressSelfConsumptionFk
|
2024-01-15 11:31:03 +00:00
|
|
|
INTO vCompanyFk,
|
|
|
|
vClientFk,
|
2024-02-15 09:04:15 +00:00
|
|
|
vWarehouseFk,
|
|
|
|
vAddressFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM company c
|
|
|
|
JOIN address a ON a.clientFk = c.clientFk
|
|
|
|
JOIN warehouse w ON w.addressFk = a.id
|
|
|
|
JOIN sector s ON s.warehouseFk = w.id
|
|
|
|
JOIN parking p ON p.sectorFk = s.id
|
|
|
|
JOIN shelving s2 ON s2.parkingFk = p.id
|
2024-02-15 09:04:15 +00:00
|
|
|
JOIN productionConfig pc
|
2024-11-13 08:03:44 +00:00
|
|
|
WHERE s2.id = vShelvingFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vClientFk IS NULL THEN
|
|
|
|
CALL util.throw('The company does not have a customer assigned');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vQuantity IS NULL OR vQuantity < 0 THEN
|
|
|
|
CALL util.throw('The shelf cannot have NULL or negative quantities');
|
|
|
|
END IF;
|
|
|
|
|
2024-11-13 08:03:44 +00:00
|
|
|
IF vShelvingCode IS NULL THEN
|
2024-01-15 11:31:03 +00:00
|
|
|
CALL util.throw('The shelf is necessary');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vItemFk IS NULL THEN
|
|
|
|
CALL util.throw('The article is required to create the ticket.');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT SUM(visible), id INTO vVisible, vItemShelvingFk
|
|
|
|
FROM itemShelving
|
|
|
|
WHERE shelvingFk = vShelvingFk
|
|
|
|
AND itemFk = vItemFk;
|
|
|
|
|
|
|
|
IF vVisible IS NULL THEN
|
|
|
|
CALL util.throw('The item is not on the shelf.');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL ticket_getWithParameters(
|
|
|
|
vClientFk,
|
|
|
|
vWarehouseFk,
|
|
|
|
CURDATE(),
|
2024-02-15 09:04:15 +00:00
|
|
|
vAddressFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
vCompanyFk,
|
|
|
|
NULL,
|
|
|
|
vTicketFk
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO sale (itemFk, ticketFk, quantity, concept)
|
|
|
|
SELECT vItemFk, vTicketFk, (vVisible - vQuantity), longName
|
|
|
|
FROM item
|
|
|
|
WHERE id = vItemFk;
|
|
|
|
|
|
|
|
UPDATE itemShelving
|
2024-09-24 14:48:48 +00:00
|
|
|
SET visible = IF(id = vItemShelvingFk, vQuantity, 0),
|
|
|
|
available = IF(id = vItemShelvingFk, vQuantity, 0)
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE shelvingFk = vShelvingFk
|
|
|
|
AND itemFk = vItemFk;
|
|
|
|
|
|
|
|
CALL vn.ticket_setState(vTicketFk, 'DELIVERED');
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|