salix/db/routines/floranet/procedures/order_confirm.sql

167 lines
3.7 KiB
MySQL
Raw Normal View History

2024-02-15 12:13:22 +00:00
DELIMITER $$
$$
2024-05-14 12:01:13 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost`PROCEDURE floranet.order_confirm(vCatalogueFk INT)
2024-02-15 12:13:22 +00:00
READS SQL DATA
2024-05-24 09:44:58 +00:00
proc:BEGIN
2024-05-14 12:01:13 +00:00
/** Update order.isPaid field, and makes the ticket
2024-02-15 12:13:22 +00:00
*
* @param vCatalogueFk floranet.catalogue.id
*
* @returns floranet.order.isPaid
*/
2024-05-14 12:01:13 +00:00
DECLARE vNewTicketFk INT;
2024-05-24 09:44:58 +00:00
DECLARE vCustomerEmail VARCHAR(255);
DECLARE vFloranetEmail VARCHAR(255);
DECLARE vSubjectEmail VARCHAR(100);
DECLARE vBodyEmail TEXT;
2024-05-14 12:01:13 +00:00
DECLARE vZoneFk INT;
DECLARE exit handler FOR SQLEXCEPTION
BEGIN
ROLLBACK;
GET DIAGNOSTICS CONDITION 2 @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
SELECT CONCAT('ERROR ', IFNULL(@errno,0), ': ', ifnull(@text,'texto')) AS `SQLEXCEPTION`;
CALL vn.mail_insert(
'floranet@verdnatura.es,pako@verdnatura.es',
'noreply@verdnatura.es',
'Floranet.order_confirm failure',
CONCAT('CatalogueFk: ', vCatalogueFk, '\n','ERROR ', IFNULL(@errno, 0), ': ', ifnull(@text, 'texto'))
);
END;
2024-05-24 09:44:58 +00:00
IF (SELECT isPaid FROM `order` WHERE catalogueFk = vCatalogueFk) THEN
SELECT CONCAT('CatalogueFk: ', vCatalogueFk, ' Esta orden ya está confirmada') AS `ERROR`;
LEAVE proc;
2024-05-24 09:44:58 +00:00
END IF;
START TRANSACTION;
2024-02-15 12:13:22 +00:00
UPDATE `order`
SET isPaid = TRUE,
payed = NOW()
WHERE catalogueFk = vCatalogueFk;
2024-05-14 12:01:13 +00:00
SELECT zoneFk
INTO vZoneFk
FROM (
SELECT zoneFk, COUNT(*) totalCount
FROM vn.ticket t
JOIN catalogue c ON c.id = vCatalogueFk
WHERE t.shipped > util.VN_CURDATE() - INTERVAL 1 YEAR
AND t.addressFk = c.addressFk
GROUP BY zoneFk
ORDER BY totalCount DESC
LIMIT 10000000000000000000
) sub
LIMIT 1;
2024-05-24 09:44:58 +00:00
INSERT INTO vn.ticket (
2024-05-14 12:01:13 +00:00
clientFk,
shipped,
addressFk,
agencyModeFk,
nickname,
warehouseFk,
routeFk,
companyFk,
landed,
zoneFk
)
SELECT a.clientFk,
c.dated - INTERVAL 1 DAY,
c.addressFk,
a.agencyModeFk,
a.nickname,
ag.warehouseFk,
NULL,
co.id,
c.dated,
vZoneFk
FROM vn.address a
JOIN vn.agencyMode am ON am.id = a.agencyModeFk
2024-05-24 09:44:58 +00:00
JOIN vn.agency ag ON ag.id = am.agencyFk
2024-05-14 12:01:13 +00:00
JOIN catalogue c ON c.addressFk = a.id
JOIN vn.company co ON co.code = 'VNL'
WHERE c.id = vCatalogueFk;
SET vNewTicketFk = LAST_INSERT_ID();
INSERT INTO vn.sale(
ticketFk,
itemFk,
concept,
price,
quantity)
SELECT
vNewTicketFk,
c.itemFk,
CONCAT('Entrega: ',c.name),
2024-06-25 10:51:14 +00:00
- apc.deliveryCost,
2024-05-14 12:01:13 +00:00
1
FROM catalogue c
JOIN addressPostCode apc
ON apc.addressFk = c.addressFk
2024-05-24 09:44:58 +00:00
AND apc.dayOfWeek = dayOfWeek(c.dated)
2024-05-14 12:01:13 +00:00
WHERE c.id = vCatalogueFk;
INSERT INTO vn.sale(
ticketFk,
itemFk,
concept,
price,
quantity)
SELECT
vNewTicketFk,
r.elementFk,
i.longName,
2024-06-25 10:51:14 +00:00
0,
r.quantity
FROM catalogue c
JOIN recipe r ON r.itemFk = c.itemFk
JOIN vn.item i ON i.id = r.elementFk
WHERE c.id = vCatalogueFk;
2024-05-24 09:44:58 +00:00
SELECT cl.email,
cf.email,
CONCAT('Nuevo pedido FLORANET para entrega el ',c.dated),
CONCAT_WS('\n',
CONCAT('Producto: ', c.name),
CONCAT('Fecha de entrega: ',c.dated),
CONCAT('Destinatario: ', o.deliveryName),
CONCAT('Dirección: ', o.address),
CONCAT('CP: ', c.postalCode),
CONCAT('Foto: ', c.image),
CONCAT('Mensaje: ', IFNULL(o.message,"Ninguno.")),
CONCAT('Teléfono: ',IFNULL(o.deliveryPhone,"--")),
CONCAT('Observaciones: ', IFNULL(o.observations,"No hay."))
)
INTO vCustomerEmail,
vFloranetEmail,
vSubjectEmail,
vBodyEmail
FROM vn.client cl
JOIN vn.address a ON a.clientFk = cl.id
JOIN catalogue c ON c.addressFk = a.id
JOIN `order` o ON o.catalogueFk = c.id
JOIN config cf
WHERE c.id = vCatalogueFk;
2024-05-24 09:44:58 +00:00
CALL vn.mail_insert(
vCustomerEmail,
vFloranetEmail,
vSubjectEmail,
vBodyEmail);
2024-05-14 12:01:13 +00:00
2024-05-24 09:44:58 +00:00
SELECT isPaid, vNewTicketFk
2024-02-15 12:13:22 +00:00
FROM `order`
WHERE catalogueFk = vCatalogueFk;
COMMIT;
2024-02-15 12:13:22 +00:00
END$$
DELIMITER ;