salix/db/changes/225201/00-ticket_canAdvance.sql

105 lines
3.3 KiB
MySQL
Raw Normal View History

2022-11-29 09:29:18 +00:00
DROP PROCEDURE IF EXISTS `vn`.`ticket_canAdvance`;
2022-11-14 13:30:35 +00:00
DELIMITER $$
$$
2022-12-20 10:36:37 +00:00
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_canAdvance`(vDateFuture DATE, vDateToAdvance DATE, vWarehouseFk INT)
2022-11-14 13:30:35 +00:00
BEGIN
/**
* Devuelve los tickets y la cantidad de lineas de venta que se pueden adelantar.
*
2022-12-20 10:36:37 +00:00
* @param vDateFuture Fecha de los tickets que se quieren adelantar.
* @param vDateToAdvance Fecha a cuando se quiere adelantar.
2022-11-14 13:30:35 +00:00
* @param vWarehouseFk Almacén
*/
DECLARE vDateInventory DATE;
SELECT inventoried INTO vDateInventory FROM vn.config;
DROP TEMPORARY TABLE IF EXISTS tmp.stock;
CREATE TEMPORARY TABLE tmp.stock
(itemFk INT PRIMARY KEY,
amount INT)
ENGINE = MEMORY;
INSERT INTO tmp.stock(itemFk, amount)
SELECT itemFk, SUM(quantity) amount FROM
(
SELECT itemFk, quantity
FROM vn.itemTicketOut
WHERE shipped >= vDateInventory
2022-12-20 10:36:37 +00:00
AND shipped < vDateFuture
2022-11-14 13:30:35 +00:00
AND warehouseFk = vWarehouseFk
UNION ALL
SELECT itemFk, quantity
FROM vn.itemEntryIn
WHERE landed >= vDateInventory
2022-12-20 10:36:37 +00:00
AND landed < vDateFuture
2022-11-14 13:30:35 +00:00
AND isVirtualStock = FALSE
AND warehouseInFk = vWarehouseFk
UNION ALL
SELECT itemFk, quantity
FROM vn.itemEntryOut
WHERE shipped >= vDateInventory
2022-12-20 10:36:37 +00:00
AND shipped < vDateFuture
2022-11-14 13:30:35 +00:00
AND warehouseOutFk = vWarehouseFk
) t
GROUP BY itemFk HAVING amount != 0;
DROP TEMPORARY TABLE IF EXISTS tmp.filter;
CREATE TEMPORARY TABLE tmp.filter
(INDEX (id))
2022-12-20 10:36:58 +00:00
SELECT s.ticketFk futureId,
t2.ticketFk id,
2022-11-14 13:30:35 +00:00
sum((s.quantity <= IFNULL(st.amount,0))) hasStock,
count(DISTINCT s.id) saleCount,
t2.state,
2022-12-20 10:36:58 +00:00
t2.stateCode,
st.name futureState,
st.code futureStateCode,
GROUP_CONCAT(DISTINCT ipt.code ORDER BY ipt.code) futureIpt,
2022-11-14 13:30:35 +00:00
t2.ipt,
t.workerFk,
CAST(sum(litros) AS DECIMAL(10,0)) liters,
CAST(count(*) AS DECIMAL(10,0)) `lines`,
t2.shipped,
2022-12-20 10:36:58 +00:00
t.shipped futureShipped,
2022-11-14 13:30:35 +00:00
t2.totalWithVat,
2022-12-20 10:36:58 +00:00
t.totalWithVat futureTotalWithVat
2022-11-14 13:30:35 +00:00
FROM vn.ticket t
JOIN vn.ticketState ts ON ts.ticketFk = t.id
JOIN vn.state st ON st.id = ts.stateFk
JOIN vn.saleVolume sv ON t.id = sv.ticketFk
2022-11-15 14:30:00 +00:00
JOIN (SELECT
2022-11-14 13:30:35 +00:00
t2.id ticketFk,
t2.addressFk,
st.name state,
2022-12-20 10:36:58 +00:00
st.code stateCode,
2022-12-20 10:36:37 +00:00
GROUP_CONCAT(DISTINCT ipt.code ORDER BY ipt.code) ipt,
2022-11-14 13:30:35 +00:00
t2.shipped,
t2.totalWithVat
FROM vn.ticket t2
JOIN vn.sale s ON s.ticketFk = t2.id
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.ticketState ts ON ts.ticketFk = t2.id
JOIN vn.state st ON st.id = ts.stateFk
2022-11-15 14:30:00 +00:00
LEFT JOIN vn.itemPackingType ipt ON ipt.code = i.itemPackingTypeFk
2022-11-14 13:30:35 +00:00
WHERE t2.shipped BETWEEN vDateToAdvance AND util.dayend(vDateToAdvance)
AND t2.warehouseFk = vWarehouseFk
GROUP BY t2.id) t2 ON t2.addressFk = t.addressFk
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
2022-11-15 14:30:00 +00:00
LEFT JOIN vn.itemPackingType ipt ON ipt.code = i.itemPackingTypeFk
2022-11-14 13:30:35 +00:00
LEFT JOIN tmp.stock st ON st.itemFk = s.itemFk
2022-12-20 10:36:37 +00:00
WHERE t.shipped BETWEEN vDateFuture AND util.dayend(vDateFuture)
2022-11-14 13:30:35 +00:00
AND t.warehouseFk = vWarehouseFk
GROUP BY t.id;
DROP TEMPORARY TABLE tmp.stock;
END$$
DELIMITER ;
2022-11-21 10:48:18 +00:00
INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId)
VALUES
('Ticket', 'getTicketsAdvance', 'READ', 'ALLOW', 'ROLE', 'employee');