diff --git a/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql b/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql index 4c7edd49c..3ccb04726 100644 --- a/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql +++ b/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql @@ -1,8 +1,8 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_doSelfConsumptionPackaging`( - vSelf DATE, + vSelf INT ) -BEGIN +proc: BEGIN /** * Comprueba si existe un ticket de autoconsumo ese mismo día * si no lo está lo crea, y añade la caja utilizada. @@ -15,7 +15,8 @@ BEGIN DECLARE vTicketFk INT; DECLARE vItemFk INT; DECLARE vItemName VARCHAR(50); - DECLARE vExists BOOL; + DECLARE vWarehouseFk INT; + DECLARE vSaleFk INT; SET vCurdate = util.VN_CURDATE(); @@ -27,8 +28,8 @@ BEGIN CALL util.throw('Some config parameters are not set'); END IF; - SELECT i.id, t.warehouseFk, i.name - INTO vItemFk, vWarehouseFk, vItemName + SELECT i.id, i.name, t.warehouseFk + INTO vItemFk, vItemName, vWarehouseFk FROM expedition e JOIN packaging p ON p.id = e.packagingFk JOIN item i ON i.id = p.itemFk @@ -47,17 +48,21 @@ BEGIN SELECT vClientFk, vWarehouseFk, vCurdate, 'CAJAS AUTOCONSUMO', vAddressFk; SET vTicketFk = LAST_INSERT_ID(); - - INSERT INTO sale(itemFk, ticketFk, concept, quantity) - SELECT vItemFk, vTicketFk, vItemName, 1; ELSE - SELECT id INTO vExists + SELECT id INTO vSaleFk FROM sale - WHERE itemFk = vItemFk - AND ticketFk = vTicketFk; - - -- Comprobar si existe o no y si existe, sumar +1 la quantity, si no, lo creamos + WHERE itemFk = vItemFk + AND ticketFk = vTicketFk; + IF vSaleFk NOT IS NULL THEN + UPDATE sale + SET quantity = quantity + 1 + WHERE id = vSaleFk; + LEAVE proc; + END IF; END IF; + + INSERT INTO sale(itemFk, ticketFk, concept, quantity) + SELECT vItemFk, vTicketFk, vItemName, 1; END$$ DELIMITER ;