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`.`ticket_cloneWeekly`(
|
2024-06-25 17:47:22 +00:00
|
|
|
vDateFrom DATE,
|
|
|
|
vDateTo DATE
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
DECLARE vLanding DATE;
|
|
|
|
DECLARE vShipment DATE;
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
DECLARE vTicketFk INT;
|
|
|
|
DECLARE vClientFk INT;
|
|
|
|
DECLARE vCompanyFk INT;
|
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vNewTicket INT;
|
|
|
|
DECLARE vYear INT;
|
2024-08-12 07:02:38 +00:00
|
|
|
DECLARE vObservationSalesPersonFk INT
|
|
|
|
DEFAULT (SELECT id FROM observationType WHERE code = 'salesPerson');
|
|
|
|
DECLARE vObservationItemPickerFk INT
|
|
|
|
DEFAULT (SELECT id FROM observationType WHERE code = 'itemPicker');
|
|
|
|
DECLARE vEmail VARCHAR(255);
|
2024-08-12 06:53:24 +00:00
|
|
|
DECLARE vIsDuplicateMail BOOL;
|
2024-08-12 07:02:38 +00:00
|
|
|
DECLARE vSubject VARCHAR(100);
|
2024-08-12 06:53:24 +00:00
|
|
|
DECLARE vMessage TEXT;
|
2024-08-12 07:02:38 +00:00
|
|
|
DECLARE vDone BOOL;
|
2024-08-12 06:53:24 +00:00
|
|
|
|
|
|
|
DECLARE vTickets CURSOR FOR
|
|
|
|
SELECT tt.ticketFk,
|
|
|
|
t.clientFk,
|
|
|
|
t.warehouseFk,
|
|
|
|
t.companyFk,
|
|
|
|
t.addressFk,
|
|
|
|
tt.agencyModeFk,
|
|
|
|
ti.dated
|
|
|
|
FROM ticketWeekly tt
|
|
|
|
JOIN ticket t ON tt.ticketFk = t.id
|
|
|
|
JOIN tmp.time ti
|
|
|
|
WHERE WEEKDAY(ti.dated) = tt.weekDay;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-08-12 07:02:38 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-08-12 06:53:24 +00:00
|
|
|
|
|
|
|
CALL `util`.`time_generate`(vDateFrom, vDateTo);
|
|
|
|
|
|
|
|
OPEN vTickets;
|
|
|
|
l: LOOP
|
2024-08-12 07:02:38 +00:00
|
|
|
SET vDone = FALSE;
|
2024-08-12 06:53:24 +00:00
|
|
|
FETCH vTickets INTO
|
2024-06-25 17:47:22 +00:00
|
|
|
vTicketFk,
|
|
|
|
vClientFk,
|
|
|
|
vWarehouseFk,
|
|
|
|
vCompanyFk,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
vShipment;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-08-12 07:02:38 +00:00
|
|
|
IF vDone THEN
|
2024-08-12 06:53:24 +00:00
|
|
|
LEAVE l;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-08-12 06:53:24 +00:00
|
|
|
-- Busca si el ticket ya ha sido clonado
|
2024-01-15 11:31:03 +00:00
|
|
|
IF EXISTS (SELECT TRUE FROM ticket tOrig
|
|
|
|
JOIN sale saleOrig ON tOrig.id = saleOrig.ticketFk
|
|
|
|
JOIN saleCloned sc ON sc.saleOriginalFk = saleOrig.id
|
|
|
|
JOIN sale saleClon ON saleClon.id = sc.saleClonedFk
|
|
|
|
JOIN ticket tClon ON tClon.id = saleClon.ticketFk
|
|
|
|
WHERE tOrig.id = vTicketFk
|
|
|
|
AND tClon.isDeleted = FALSE
|
|
|
|
AND DATE(tClon.shipped) = vShipment)
|
|
|
|
THEN
|
2024-08-12 06:53:24 +00:00
|
|
|
ITERATE l;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vAgencyModeFk IS NULL THEN
|
|
|
|
SELECT agencyModeFk INTO vAgencyModeFk
|
|
|
|
FROM address
|
|
|
|
WHERE clientFk = vClientFk
|
|
|
|
AND isDefaultAddress;
|
|
|
|
END IF;
|
|
|
|
|
2024-06-25 17:47:22 +00:00
|
|
|
CALL zone_getLanded(vShipment, vAddressFk, vAgencyModeFk, vWarehouseFk, FALSE);
|
2024-01-15 11:31:03 +00:00
|
|
|
SET vLanding = NULL;
|
|
|
|
SELECT landed INTO vLanding FROM tmp.zoneGetLanded LIMIT 1;
|
|
|
|
|
|
|
|
CALL ticket_add(
|
|
|
|
vClientFk,
|
|
|
|
vShipment,
|
|
|
|
vWarehouseFk,
|
|
|
|
vCompanyFk,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
NULL,
|
|
|
|
vLanding,
|
|
|
|
account.myUser_getId(),
|
|
|
|
FALSE,
|
|
|
|
vNewTicket);
|
|
|
|
|
|
|
|
UPDATE ticket
|
|
|
|
SET clonedFrom = vTicketFk
|
|
|
|
WHERE id = vNewTicket;
|
|
|
|
|
2024-06-25 17:47:22 +00:00
|
|
|
INSERT INTO sale (ticketFk,
|
|
|
|
itemFk,
|
|
|
|
concept,
|
|
|
|
quantity,
|
|
|
|
price,
|
|
|
|
discount,
|
|
|
|
priceFixed,
|
|
|
|
isPriceFixed)
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT vNewTicket,
|
2024-08-12 07:02:38 +00:00
|
|
|
itemFk,
|
|
|
|
concept,
|
|
|
|
quantity,
|
|
|
|
price,
|
|
|
|
discount,
|
|
|
|
priceFixed,
|
|
|
|
isPriceFixed
|
|
|
|
FROM sale
|
|
|
|
WHERE ticketFk = vTicketFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
INSERT IGNORE INTO saleCloned(saleOriginalFk, saleClonedFk)
|
|
|
|
SELECT saleOriginal.id, saleClon.id
|
|
|
|
FROM sale saleOriginal
|
|
|
|
JOIN sale saleClon ON saleOriginal.itemFk = saleClon.itemFk
|
|
|
|
AND saleOriginal.quantity = saleClon.quantity
|
|
|
|
WHERE saleOriginal.ticketFk = vTicketFk
|
|
|
|
AND saleClon.ticketFk = vNewTicket;
|
|
|
|
|
|
|
|
INSERT INTO ticketRequest (description,
|
|
|
|
ordered,
|
|
|
|
shipped,
|
|
|
|
quantity,
|
|
|
|
price,
|
|
|
|
itemFk,
|
|
|
|
clientFk,
|
|
|
|
response,
|
|
|
|
total,
|
|
|
|
buyed,
|
|
|
|
requesterFk
|
|
|
|
,attenderFk,
|
|
|
|
ticketFk)
|
|
|
|
SELECT description,
|
2024-06-25 17:47:22 +00:00
|
|
|
ordered,
|
|
|
|
shipped,
|
|
|
|
quantity,
|
|
|
|
price,
|
|
|
|
itemFk,
|
|
|
|
clientFk,
|
|
|
|
response,
|
|
|
|
total,
|
|
|
|
buyed,
|
|
|
|
requesterFk,
|
|
|
|
attenderFk,
|
|
|
|
vNewTicket
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM ticketRequest
|
2024-08-12 07:02:38 +00:00
|
|
|
WHERE ticketFk = vTicketFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
INSERT INTO ticketObservation(
|
|
|
|
ticketFk,
|
|
|
|
observationTypeFk,
|
|
|
|
description)
|
|
|
|
VALUES(
|
|
|
|
vNewTicket,
|
2024-08-12 07:02:38 +00:00
|
|
|
vObservationSalesPersonFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
CONCAT('turno desde ticket: ',vTicketFk))
|
|
|
|
ON DUPLICATE KEY UPDATE description =
|
|
|
|
CONCAT(ticketObservation.description,VALUES(description),' ');
|
|
|
|
|
|
|
|
INSERT INTO ticketObservation(ticketFk,
|
|
|
|
observationTypeFk,
|
|
|
|
description)
|
|
|
|
VALUES(
|
|
|
|
vNewTicket,
|
2024-08-12 07:02:38 +00:00
|
|
|
vObservationItemPickerFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
'ATENCION: Contiene lineas de TURNO')
|
|
|
|
ON DUPLICATE KEY UPDATE description =
|
|
|
|
CONCAT(ticketObservation.description,VALUES(description),' ');
|
|
|
|
|
2024-08-12 06:53:24 +00:00
|
|
|
IF vLanding IS NULL THEN
|
|
|
|
SELECT IFNULL(d.notificationEmail, e.email) INTO vEmail
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM client c
|
|
|
|
JOIN account.emailUser e ON e.userFk = c.salesPersonFk
|
2024-08-12 07:06:24 +00:00
|
|
|
LEFT JOIN workerDepartment wd ON wd.workerFk = c.salesPersonFk
|
|
|
|
LEFT JOIN department d ON d.id = wd.departmentFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE c.id = vClientFk;
|
|
|
|
|
|
|
|
SET vSubject = CONCAT('Turnos - No se ha podido clonar correctamente el ticket ',
|
|
|
|
vTicketFk,' para el dia: ', vShipment);
|
|
|
|
SET vMessage = CONCAT('No se ha podido clonar el ticket ', vTicketFk,
|
|
|
|
' para el dia: ', vShipment,
|
|
|
|
' porque no hay una zona de envío disponible. Se ha creado el ticket: ',
|
|
|
|
vNewTicket, ' pero ha que revisar las fechas y la agencia');
|
|
|
|
|
|
|
|
SELECT COUNT(*) INTO vIsDuplicateMail
|
|
|
|
FROM mail
|
2024-08-12 06:53:24 +00:00
|
|
|
WHERE receiver = vEmail
|
2024-01-15 11:31:03 +00:00
|
|
|
AND subject = vSubject;
|
|
|
|
|
|
|
|
IF NOT vIsDuplicateMail THEN
|
2024-08-12 06:53:24 +00:00
|
|
|
CALL mail_insert(vEmail, NULL, vSubject, vMessage);
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
2024-06-25 17:30:59 +00:00
|
|
|
CALL ticket_setState(vNewTicket, 'FIXING');
|
2024-01-15 11:31:03 +00:00
|
|
|
ELSE
|
|
|
|
CALL ticketCalculateClon(vNewTicket, vTicketFk);
|
|
|
|
END IF;
|
|
|
|
END LOOP;
|
2024-08-12 06:53:24 +00:00
|
|
|
CLOSE vTickets;
|
2024-07-24 05:25:43 +00:00
|
|
|
|
2024-08-12 06:53:24 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS
|
|
|
|
tmp.time,
|
|
|
|
tmp.zoneGetLanded;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|