2023-10-25 13:48:19 +00:00
|
|
|
DELIMITER $$
|
2024-09-09 13:12:44 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_canAdvance`(vDateFuture DATE, vDateToAdvance DATE, vWarehouseFk INT)
|
2023-10-25 13:48:19 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Devuelve los tickets y la cantidad de lineas de venta que se pueden adelantar.
|
|
|
|
*
|
|
|
|
* @param vDateFuture Fecha de los tickets que se quieren adelantar.
|
|
|
|
* @param vDateToAdvance Fecha a cuando se quiere adelantar.
|
|
|
|
* @param vWarehouseFk Almacén
|
|
|
|
*/
|
2024-09-09 10:08:12 +00:00
|
|
|
|
2024-09-11 08:19:55 +00:00
|
|
|
CALL item_getStock(vWarehouseFk, vDateToAdvance, NULL);
|
|
|
|
CALL item_getMinacum(
|
|
|
|
vWarehouseFk,
|
|
|
|
vDateToAdvance,
|
|
|
|
DATEDIFF(DATE_SUB(vDateFuture, INTERVAL 1 DAY), vDateToAdvance),
|
|
|
|
NULL
|
|
|
|
);
|
2023-10-25 13:48:19 +00:00
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.filter
|
|
|
|
(INDEX (id))
|
2024-09-11 08:19:55 +00:00
|
|
|
SELECT origin.ticketFk futureId,
|
|
|
|
dest.ticketFk id,
|
|
|
|
dest.state,
|
|
|
|
origin.futureState,
|
|
|
|
origin.futureIpt,
|
|
|
|
dest.ipt,
|
|
|
|
origin.workerFk,
|
|
|
|
origin.futureLiters,
|
|
|
|
origin.futureLines,
|
|
|
|
dest.shipped,
|
|
|
|
origin.shipped futureShipped,
|
|
|
|
dest.totalWithVat,
|
|
|
|
origin.totalWithVat futureTotalWithVat,
|
|
|
|
dest.agency,
|
|
|
|
dest.agencyModeFk,
|
|
|
|
origin.futureAgency,
|
|
|
|
origin.agencyModeFk futureAgencyModeFk,
|
|
|
|
dest.lines,
|
|
|
|
dest.liters,
|
|
|
|
origin.futureLines - origin.hasStock AS notMovableLines,
|
2024-09-09 13:13:08 +00:00
|
|
|
(origin.futureLines = origin.hasStock) AS isFullMovable,
|
2024-09-11 08:19:55 +00:00
|
|
|
dest.zoneFk,
|
|
|
|
origin.futureZoneFk,
|
|
|
|
origin.futureZoneName,
|
|
|
|
origin.classColor futureClassColor,
|
|
|
|
dest.classColor,
|
|
|
|
origin.clientFk futureClientFk,
|
|
|
|
origin.addressFk futureAddressFk,
|
|
|
|
origin.warehouseFk futureWarehouseFk,
|
|
|
|
origin.companyFk futureCompanyFk,
|
|
|
|
IFNULL(dest.nickname, origin.nickname) nickname,
|
|
|
|
dest.landed,
|
|
|
|
dest.preparation
|
2023-10-25 13:48:19 +00:00
|
|
|
FROM (
|
2024-09-11 08:19:55 +00:00
|
|
|
SELECT s.ticketFk,
|
|
|
|
c.salesPersonFk workerFk,
|
|
|
|
t.shipped,
|
|
|
|
t.totalWithVat,
|
2024-09-09 10:08:12 +00:00
|
|
|
st.name futureState,
|
|
|
|
am.name futureAgency,
|
|
|
|
count(s.id) futureLines,
|
|
|
|
GROUP_CONCAT(DISTINCT ipt.code ORDER BY ipt.code) futureIpt,
|
|
|
|
CAST(SUM(litros) AS DECIMAL(10,0)) futureLiters,
|
2024-09-11 08:19:55 +00:00
|
|
|
SUM(s.quantity <= (IFNULL(il.stock,0) + IFNULL(im.amount, 0))) hasStock,
|
|
|
|
z.id futureZoneFk,
|
|
|
|
z.name futureZoneName,
|
|
|
|
st.classColor,
|
|
|
|
t.clientFk,
|
|
|
|
t.nickname,
|
|
|
|
t.addressFk,
|
|
|
|
t.warehouseFk,
|
|
|
|
t.companyFk,
|
|
|
|
t.agencyModeFk
|
2024-09-09 10:08:12 +00:00
|
|
|
FROM ticket t
|
2024-09-11 08:19:55 +00:00
|
|
|
JOIN client c ON c.id = t.clientFk
|
2024-09-09 10:08:12 +00:00
|
|
|
JOIN sale s ON s.ticketFk = t.id
|
|
|
|
JOIN saleVolume sv ON sv.saleFk = s.id
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN `state` st ON st.id = ts.stateFk
|
|
|
|
JOIN agencyMode am ON t.agencyModeFk = am.id
|
2024-09-11 08:19:55 +00:00
|
|
|
JOIN `zone` z ON t.zoneFk = z.id
|
|
|
|
LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk
|
2024-09-09 10:08:12 +00:00
|
|
|
LEFT JOIN itemPackingType ipt ON ipt.code = i.itemPackingTypeFk
|
2024-09-11 08:19:55 +00:00
|
|
|
LEFT JOIN tmp.itemMinacum im ON im.itemFk = i.id
|
|
|
|
AND im.warehouseFk = vWarehouseFk
|
|
|
|
LEFT JOIN tmp.itemList il ON il.itemFk = i.id
|
2024-09-09 10:08:12 +00:00
|
|
|
WHERE t.shipped BETWEEN vDateFuture AND util.dayend(vDateFuture)
|
|
|
|
AND t.warehouseFk = vWarehouseFk
|
|
|
|
GROUP BY t.id
|
2024-09-11 08:19:55 +00:00
|
|
|
) origin
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT t.id ticketFk,
|
2023-10-25 13:48:19 +00:00
|
|
|
st.name state,
|
|
|
|
GROUP_CONCAT(DISTINCT ipt.code ORDER BY ipt.code) ipt,
|
|
|
|
t.shipped,
|
|
|
|
t.totalWithVat,
|
|
|
|
am.name agency,
|
|
|
|
CAST(SUM(litros) AS DECIMAL(10,0)) liters,
|
|
|
|
CAST(COUNT(*) AS DECIMAL(10,0)) `lines`,
|
2023-10-27 13:06:20 +00:00
|
|
|
st.classColor,
|
2024-09-11 08:19:55 +00:00
|
|
|
t.clientFk,
|
|
|
|
t.nickname,
|
|
|
|
t.addressFk,
|
|
|
|
t.zoneFk,
|
|
|
|
t.warehouseFk,
|
|
|
|
t.companyFk,
|
|
|
|
t.landed,
|
|
|
|
t.agencyModeFk,
|
|
|
|
SEC_TO_TIME(
|
|
|
|
COALESCE(HOUR(t.shipped), HOUR(zc.hour), HOUR(z.hour)) * 3600 +
|
|
|
|
COALESCE(MINUTE(t.shipped), MINUTE(zc.hour), MINUTE(z.hour)) * 60
|
2024-09-09 13:13:32 +00:00
|
|
|
) preparation
|
2023-10-25 13:48:19 +00:00
|
|
|
FROM ticket t
|
|
|
|
JOIN sale s ON s.ticketFk = t.id
|
|
|
|
JOIN saleVolume sv ON sv.saleFk = s.id
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
2024-09-09 10:08:12 +00:00
|
|
|
JOIN `state` st ON st.id = ts.stateFk
|
2023-10-25 13:48:19 +00:00
|
|
|
JOIN agencyMode am ON t.agencyModeFk = am.id
|
|
|
|
LEFT JOIN itemPackingType ipt ON ipt.code = i.itemPackingTypeFk
|
2024-09-09 10:08:12 +00:00
|
|
|
LEFT JOIN `zone` z ON z.id = t.zoneFk
|
|
|
|
LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk
|
2024-09-11 08:19:55 +00:00
|
|
|
JOIN ticketCanAdvanceConfig tc
|
2023-10-25 13:48:19 +00:00
|
|
|
WHERE t.shipped BETWEEN vDateToAdvance AND util.dayend(vDateToAdvance)
|
|
|
|
AND t.warehouseFk = vWarehouseFk
|
2024-09-11 08:19:55 +00:00
|
|
|
AND st.order <= tc.destinationOrder
|
2023-10-25 13:48:19 +00:00
|
|
|
GROUP BY t.id
|
2024-09-11 08:19:55 +00:00
|
|
|
) dest ON dest.addressFk = origin.addressFk
|
2024-09-09 13:14:01 +00:00
|
|
|
WHERE origin.hasStock;
|
2023-10-25 13:48:19 +00:00
|
|
|
|
2024-09-11 08:19:55 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS
|
|
|
|
tmp.itemList,
|
|
|
|
tmp.itemMinacum;
|
2023-10-25 13:48:19 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|