feat: refs #6822 create entryTransfer
This commit is contained in:
parent
1730276844
commit
9585af8e77
|
@ -0,0 +1,113 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`entryTransfer`(vOriginalEntry INT)
|
||||
BEGIN
|
||||
/**
|
||||
* If the bic length is Incorrect throw exception
|
||||
*
|
||||
* @param vBic bic code
|
||||
*/
|
||||
|
||||
DECLARE vNewEntryFk INT;
|
||||
DECLARE vTravelFk INT;
|
||||
DECLARE vWarehouseFk INT;
|
||||
|
||||
-- Clonar la entrada
|
||||
CALL entry_clone(vOriginalEntry);
|
||||
|
||||
SELECT id INTO vNewEntryFk
|
||||
FROM entry
|
||||
WHERE clonedFrom = vOriginalEntry
|
||||
ORDER BY dated DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Hay que crear un nuevo travel, con salida hoy y llegada mañana y asignar la entrada nueva al nuevo travel.
|
||||
INSERT INTO travel(
|
||||
shipped,
|
||||
landed,
|
||||
warehouseInFk,
|
||||
warehouseOutFk,
|
||||
`ref`,
|
||||
isReceived,
|
||||
agencyModeFk)
|
||||
SELECT util.VN_CURDATE(),
|
||||
util.VN_CURDATE() + INTERVAL 1 DAY,
|
||||
t.warehouseInFk,
|
||||
t.warehouseOutFk,
|
||||
t.`ref`,
|
||||
t.isReceived,
|
||||
t.agencyModeFk
|
||||
FROM travel t
|
||||
JOIN entry e ON e.travelFk = t.id
|
||||
WHERE e.id = vOriginalEntry;
|
||||
|
||||
SET vTravelFk = LAST_INSERT_ID();
|
||||
|
||||
UPDATE entry
|
||||
SET travelFk = vTravelFk
|
||||
WHERE id = vNewEntryFk;
|
||||
|
||||
-- Poner a 0 las cantidades
|
||||
UPDATE buy b
|
||||
SET b.quantity = 0, b.stickers = 0
|
||||
WHERE b.entryFk = vNewEntryFk;
|
||||
|
||||
-- Eliminar duplicados
|
||||
DELETE b.*
|
||||
FROM buy b
|
||||
LEFT JOIN (SELECT b.id, b.itemFk
|
||||
FROM buy b
|
||||
WHERE b.entryFk = vNewEntryFk
|
||||
GROUP BY b.itemFk) tBuy ON tBuy.id = b.id
|
||||
WHERE b.entryFk = vNewEntryFk
|
||||
AND tBuy.id IS NULL;
|
||||
|
||||
SELECT t.warehouseInFk INTO vWarehouseFk
|
||||
FROM travel t
|
||||
JOIN entry e ON e.travelFk = t.id
|
||||
WHERE e.id = vOriginalEntry;
|
||||
|
||||
-- Actualizar la nueva entrada con lo que no está ubicado HOY, descontando lo vendido HOY de esas ubicaciones
|
||||
UPDATE buy b
|
||||
JOIN (
|
||||
SELECT tBuy.itemFk, IFNULL(iss.visible,0) visible, tBuy.totalQuantity, IFNULL(sales.sold,0) sold
|
||||
FROM (SELECT b.itemFk, SUM(b.quantity) totalQuantity
|
||||
FROM buy b
|
||||
WHERE b.entryFk = vOriginalEntry
|
||||
GROUP BY b.itemFk
|
||||
) tBuy
|
||||
LEFT JOIN (
|
||||
SELECT ish.itemFk, SUM(visible) visible
|
||||
FROM itemShelving ish
|
||||
JOIN shelving sh ON sh.code = ish.shelvingFk
|
||||
JOIN parking p ON p.id = sh.parkingFk
|
||||
JOIN sector s ON s.id = p.sectorFk
|
||||
WHERE s.warehouseFk = vWarehouseFk
|
||||
AND sh.parked = util.VN_CURDATE()
|
||||
GROUP BY ish.itemFk) iss ON tBuy.itemFk = iss.itemFk
|
||||
LEFT JOIN (
|
||||
SELECT s.itemFk, SUM(s.quantity) sold
|
||||
FROM ticket t
|
||||
JOIN sale s ON s.ticketFk = t.id
|
||||
JOIN itemShelvingSale iss ON iss.saleFk = s.id
|
||||
JOIN itemShelving is2 ON is2.id = iss.itemShelvingFk
|
||||
JOIN shelving s2 ON s2.code = is2.shelvingFk
|
||||
WHERE t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE())
|
||||
AND s2.parked = util.VN_CURDATE()
|
||||
GROUP BY s.itemFk) sales ON sales.itemFk = tBuy.itemFk
|
||||
WHERE visible = tBuy.totalQuantity
|
||||
OR iss.itemFk IS NULL
|
||||
) sub ON sub.itemFk = b.itemFk
|
||||
SET b.quantity = sub.totalQuantity - sub.visible - sub.sold
|
||||
WHERE b.entryFk = vNewEntryFk;
|
||||
|
||||
-- Limpia la nueva entrada
|
||||
DELETE b.*
|
||||
FROM buy b
|
||||
WHERE b.entryFk = vNewEntryFk
|
||||
AND b.quantity = 0;
|
||||
|
||||
CALL cache.visible_refresh(@c,TRUE,7);
|
||||
CALL cache.available_refresh(@c, TRUE, 7, util.VN_CURDATE());
|
||||
|
||||
END$$
|
||||
DELIMITER ;
|
|
@ -1,5 +1,5 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`entry_clone`(vSelf INT)
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`entry_clone`(IN vSelf INT, OUT vNewEntryFk INT)
|
||||
BEGIN
|
||||
/**
|
||||
* clones an entry.
|
||||
|
@ -8,6 +8,12 @@ BEGIN
|
|||
*/
|
||||
DECLARE vNewEntryFk INT;
|
||||
|
||||
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||
BEGIN
|
||||
ROLLBACK;
|
||||
RESIGNAL;
|
||||
END;
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
CALL entry_cloneHeader(vSelf, vNewEntryFk, NULL);
|
||||
|
@ -15,6 +21,5 @@ BEGIN
|
|||
|
||||
COMMIT;
|
||||
|
||||
SELECT vNewEntryFk;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
|
Loading…
Reference in New Issue