2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-10 06:10:34 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(
|
|
|
|
vSelf INT,
|
|
|
|
vUserFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2024-07-10 08:30:24 +00:00
|
|
|
* Confirms an order, creating each of its tickets
|
|
|
|
* on the corresponding date, store and user.
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
|
|
|
* @param vSelf The order identifier
|
|
|
|
* @param vUser The user identifier
|
|
|
|
*/
|
2024-07-30 08:14:20 +00:00
|
|
|
DECLARE vHasRows BOOL;
|
2024-07-10 06:10:34 +00:00
|
|
|
DECLARE vDone BOOL;
|
|
|
|
DECLARE vWarehouseFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vShipment DATE;
|
2024-07-30 08:14:20 +00:00
|
|
|
DECLARE vShipmentDayEnd DATETIME;
|
2024-07-10 06:10:34 +00:00
|
|
|
DECLARE vTicketFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vNotes VARCHAR(255);
|
2024-07-10 06:18:33 +00:00
|
|
|
DECLARE vItemFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vConcept VARCHAR(30);
|
|
|
|
DECLARE vAmount INT;
|
2024-07-29 10:31:44 +00:00
|
|
|
DECLARE vAvailable INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vPrice DECIMAL(10,2);
|
2024-07-10 06:10:34 +00:00
|
|
|
DECLARE vSaleFk INT;
|
|
|
|
DECLARE vRowFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vPriceFixed DECIMAL(10,2);
|
2024-07-10 08:30:24 +00:00
|
|
|
DECLARE vLanded DATE;
|
2024-07-10 06:10:34 +00:00
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vClientFk INT;
|
|
|
|
DECLARE vCompanyFk INT;
|
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vCalcFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vIsTaxDataChecked BOOL;
|
|
|
|
|
2024-07-10 08:30:24 +00:00
|
|
|
DECLARE vDates CURSOR FOR
|
2024-07-10 06:23:43 +00:00
|
|
|
SELECT zgs.shipped, r.warehouseFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM `order` o
|
2024-07-10 06:23:43 +00:00
|
|
|
JOIN orderRow r ON r.orderFk = o.id
|
|
|
|
LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouseFk
|
2024-07-08 08:08:04 +00:00
|
|
|
WHERE o.id = vSelf
|
|
|
|
AND r.amount
|
2024-07-10 06:23:43 +00:00
|
|
|
GROUP BY r.warehouseFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-10 08:30:24 +00:00
|
|
|
DECLARE vRows CURSOR FOR
|
|
|
|
SELECT r.id,
|
|
|
|
r.itemFk,
|
|
|
|
i.name,
|
|
|
|
r.amount,
|
2024-07-30 08:14:20 +00:00
|
|
|
r.price
|
2024-07-10 06:18:33 +00:00
|
|
|
FROM orderRow r
|
|
|
|
JOIN vn.item i ON i.id = r.itemFk
|
2024-07-08 08:08:04 +00:00
|
|
|
WHERE r.amount
|
2024-07-10 06:18:33 +00:00
|
|
|
AND r.warehouseFk = vWarehouseFk
|
|
|
|
AND r.orderFk = vSelf
|
2024-01-15 11:31:03 +00:00
|
|
|
ORDER BY r.rate DESC;
|
|
|
|
|
2024-07-10 06:10:34 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
-- Carga los datos del pedido
|
2024-07-08 08:08:04 +00:00
|
|
|
SELECT o.date_send,
|
|
|
|
o.address_id,
|
|
|
|
o.note,
|
|
|
|
a.clientFk,
|
|
|
|
o.company_id,
|
|
|
|
o.agency_id,
|
|
|
|
c.isTaxDataChecked
|
2024-07-10 08:30:24 +00:00
|
|
|
INTO vLanded,
|
2024-07-10 06:10:34 +00:00
|
|
|
vAddressFk,
|
2024-07-08 08:08:04 +00:00
|
|
|
vNotes,
|
2024-07-10 06:10:34 +00:00
|
|
|
vClientFk,
|
|
|
|
vCompanyFk,
|
|
|
|
vAgencyModeFk,
|
2024-07-08 08:08:04 +00:00
|
|
|
vIsTaxDataChecked
|
2024-07-10 06:10:34 +00:00
|
|
|
FROM `order` o
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn.address a ON a.id = o.address_id
|
|
|
|
JOIN vn.client c ON c.id = a.clientFk
|
|
|
|
WHERE o.id = vSelf;
|
|
|
|
|
|
|
|
-- Verifica si el cliente tiene los datos comprobados
|
|
|
|
IF NOT vIsTaxDataChecked THEN
|
2024-07-08 08:08:04 +00:00
|
|
|
CALL util.throw('clientNotVerified');
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Carga las fechas de salida de cada almacen
|
2024-07-10 08:30:24 +00:00
|
|
|
CALL vn.zone_getShipped(vLanded, vAddressFk, vAgencyModeFk, FALSE);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Trabajador que realiza la accion
|
2024-07-10 06:10:34 +00:00
|
|
|
IF vUserFk IS NULL THEN
|
|
|
|
SELECT employeeFk INTO vUserFk FROM orderConfig;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
CALL order_checkEditable(vSelf);
|
|
|
|
|
|
|
|
-- Check order is not empty
|
2024-07-30 08:14:20 +00:00
|
|
|
SELECT COUNT(*) > 0 INTO vHasRows
|
2024-07-10 06:23:43 +00:00
|
|
|
FROM orderRow
|
|
|
|
WHERE orderFk = vSelf
|
2024-07-08 08:49:39 +00:00
|
|
|
AND amount > 0;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-30 08:14:20 +00:00
|
|
|
IF NOT vHasRows THEN
|
2024-07-08 08:08:04 +00:00
|
|
|
CALL util.throw('ORDER_EMPTY');
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Crea los tickets del pedido
|
2024-07-10 08:30:24 +00:00
|
|
|
OPEN vDates;
|
|
|
|
lDates: LOOP
|
2024-07-10 06:10:34 +00:00
|
|
|
SET vTicketFk = NULL;
|
2024-01-15 11:31:03 +00:00
|
|
|
SET vDone = FALSE;
|
2024-07-10 08:30:24 +00:00
|
|
|
FETCH vDates INTO vShipment, vWarehouseFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE lDates;
|
|
|
|
END IF;
|
|
|
|
|
2024-07-29 10:36:08 +00:00
|
|
|
SET vShipmentDayEnd = util.dayEnd(vShipment);
|
|
|
|
|
2024-07-29 09:46:59 +00:00
|
|
|
-- Busca un ticket libre disponible
|
2024-07-19 08:49:45 +00:00
|
|
|
WITH tPrevia AS (
|
|
|
|
SELECT DISTINCT s.ticketFk
|
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
2024-07-29 10:36:08 +00:00
|
|
|
WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
|
2024-07-29 10:31:44 +00:00
|
|
|
)
|
2024-07-19 08:49:45 +00:00
|
|
|
SELECT t.id INTO vTicketFk
|
|
|
|
FROM vn.ticket t
|
|
|
|
JOIN vn.alertLevel al ON al.code = 'FREE'
|
|
|
|
LEFT JOIN tPrevia tp ON tp.ticketFk = t.id
|
2024-08-02 05:58:45 +00:00
|
|
|
LEFT JOIN vn.ticketState tls ON tls.ticketFk = t.id
|
2024-07-19 08:49:45 +00:00
|
|
|
JOIN hedera.`order` o ON o.address_id = t.addressFk
|
2024-08-02 05:58:45 +00:00
|
|
|
AND t.shipped BETWEEN vShipment AND vShipmentDayEnd
|
2024-07-19 08:49:45 +00:00
|
|
|
AND t.warehouseFk = vWarehouseFk
|
|
|
|
AND o.date_send = t.landed
|
|
|
|
WHERE o.id = vSelf
|
|
|
|
AND t.refFk IS NULL
|
|
|
|
AND tp.ticketFk IS NULL
|
|
|
|
AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id)
|
|
|
|
LIMIT 1;
|
|
|
|
|
2024-07-29 09:46:59 +00:00
|
|
|
-- Comprobamos si hay un ticket de previa disponible
|
|
|
|
IF vTicketFk IS NULL THEN
|
|
|
|
WITH tItemPackingTypeOrder AS (
|
2024-08-02 05:58:45 +00:00
|
|
|
SELECT GROUP_CONCAT(
|
|
|
|
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
|
|
|
|
) distinctItemPackingTypes,
|
2024-07-29 09:46:59 +00:00
|
|
|
o.address_id
|
|
|
|
FROM vn.item i
|
2024-08-02 10:20:50 +00:00
|
|
|
JOIN hedera.orderRow oro ON oro.itemFk = i.id
|
|
|
|
JOIN hedera.`order` o ON o.id = oro.orderFk
|
2024-07-29 09:46:59 +00:00
|
|
|
WHERE oro.orderFk = vSelf
|
|
|
|
),
|
|
|
|
tItemPackingTypeTicket AS (
|
|
|
|
SELECT t.id,
|
2024-08-02 05:58:45 +00:00
|
|
|
GROUP_CONCAT(
|
|
|
|
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
|
|
|
|
) distinctItemPackingTypes
|
2024-07-29 09:46:59 +00:00
|
|
|
FROM vn.ticket t
|
2024-08-02 05:58:45 +00:00
|
|
|
JOIN vn.ticketState tls ON tls.ticketFk = t.id
|
|
|
|
JOIN vn.alertLevel al ON al.id = tls.alertLevel
|
2024-07-29 09:46:59 +00:00
|
|
|
JOIN vn.sale s ON s.ticketFk = t.id
|
|
|
|
JOIN vn.item i ON i.id = s.itemFk
|
|
|
|
JOIN tItemPackingTypeOrder ipto
|
2024-08-02 05:58:45 +00:00
|
|
|
WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
|
|
|
|
AND t.refFk IS NULL
|
2024-07-29 09:46:59 +00:00
|
|
|
AND t.warehouseFk = vWarehouseFk
|
|
|
|
AND t.addressFk = ipto.address_id
|
2024-08-02 05:58:45 +00:00
|
|
|
AND al.code = 'ON_PREVIOUS'
|
2024-07-29 09:46:59 +00:00
|
|
|
GROUP BY t.id
|
|
|
|
)
|
|
|
|
SELECT iptt.id INTO vTicketFk
|
|
|
|
FROM tItemPackingTypeTicket iptt
|
|
|
|
JOIN tItemPackingTypeOrder ipto
|
|
|
|
WHERE INSTR(iptt.distinctItemPackingTypes, ipto.distinctItemPackingTypes)
|
2024-07-29 10:31:44 +00:00
|
|
|
LIMIT 1;
|
2024-07-29 09:46:59 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-07-19 08:49:45 +00:00
|
|
|
-- Crea el ticket en el caso de no existir uno adecuado
|
|
|
|
IF vTicketFk IS NULL THEN
|
|
|
|
SET vShipment = IFNULL(vShipment, util.VN_CURDATE());
|
|
|
|
CALL vn.ticket_add(
|
|
|
|
vClientFk,
|
|
|
|
vShipment,
|
|
|
|
vWarehouseFk,
|
|
|
|
vCompanyFk,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
NULL,
|
|
|
|
vLanded,
|
|
|
|
vUserFk,
|
|
|
|
TRUE,
|
|
|
|
vTicketFk
|
|
|
|
);
|
|
|
|
ELSE
|
|
|
|
INSERT INTO vn.ticketTracking
|
|
|
|
SET ticketFk = vTicketFk,
|
|
|
|
userFk = vUserFk,
|
|
|
|
stateFk = (SELECT id FROM vn.state WHERE code = 'FREE');
|
|
|
|
END IF;
|
2024-07-10 08:30:24 +00:00
|
|
|
|
2024-07-19 08:49:45 +00:00
|
|
|
INSERT IGNORE INTO vn.orderTicket
|
|
|
|
SET orderFk = vSelf,
|
|
|
|
ticketFk = vTicketFk;
|
|
|
|
|
|
|
|
-- Añade las notas
|
|
|
|
IF vNotes IS NOT NULL AND vNotes <> '' THEN
|
2024-07-30 08:14:20 +00:00
|
|
|
INSERT INTO vn.ticketObservation
|
|
|
|
SET ticketFk = vTicketFk,
|
2024-07-19 08:49:45 +00:00
|
|
|
observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
|
|
|
|
`description` = vNotes
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
`description` = CONCAT(VALUES(`description`),'. ', `description`);
|
|
|
|
END IF;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Añade los movimientos y sus componentes
|
2024-07-10 08:30:24 +00:00
|
|
|
OPEN vRows;
|
2024-01-15 11:31:03 +00:00
|
|
|
lRows: LOOP
|
2024-07-10 08:30:24 +00:00
|
|
|
SET vSaleFk = NULL;
|
2024-01-15 11:31:03 +00:00
|
|
|
SET vDone = FALSE;
|
2024-07-30 08:14:20 +00:00
|
|
|
FETCH vRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE lRows;
|
|
|
|
END IF;
|
|
|
|
|
2024-07-10 06:10:34 +00:00
|
|
|
SELECT s.id INTO vSaleFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM vn.sale s
|
2024-07-10 06:10:34 +00:00
|
|
|
WHERE ticketFk = vTicketFk
|
2024-01-15 11:31:03 +00:00
|
|
|
AND price = vPrice
|
2024-07-10 06:18:33 +00:00
|
|
|
AND itemFk = vItemFk
|
2024-01-15 11:31:03 +00:00
|
|
|
AND discount = 0
|
|
|
|
LIMIT 1;
|
|
|
|
|
2024-07-10 06:10:34 +00:00
|
|
|
IF vSaleFk THEN
|
2024-01-15 11:31:03 +00:00
|
|
|
UPDATE vn.sale
|
|
|
|
SET quantity = quantity + vAmount,
|
|
|
|
originalQuantity = quantity
|
2024-07-10 06:10:34 +00:00
|
|
|
WHERE id = vSaleFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
ELSE
|
|
|
|
-- Obtiene el coste
|
|
|
|
SELECT SUM(rc.`price`) valueSum INTO vPriceFixed
|
|
|
|
FROM orderRowComponent rc
|
|
|
|
JOIN vn.component c ON c.id = rc.componentFk
|
2024-07-10 08:30:24 +00:00
|
|
|
JOIN vn.componentType ct ON ct.id = c.typeFk
|
|
|
|
AND ct.isBase
|
2024-07-10 06:10:34 +00:00
|
|
|
WHERE rc.rowFk = vRowFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
INSERT INTO vn.sale
|
2024-07-10 06:18:33 +00:00
|
|
|
SET itemFk = vItemFk,
|
2024-07-10 06:10:34 +00:00
|
|
|
ticketFk = vTicketFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
concept = vConcept,
|
|
|
|
quantity = vAmount,
|
|
|
|
price = vPrice,
|
|
|
|
priceFixed = vPriceFixed,
|
|
|
|
isPriceFixed = TRUE;
|
|
|
|
|
2024-07-10 06:10:34 +00:00
|
|
|
SET vSaleFk = LAST_INSERT_ID();
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-10 06:10:34 +00:00
|
|
|
INSERT INTO vn.saleComponent (saleFk, componentFk, `value`)
|
|
|
|
SELECT vSaleFk, rc.componentFk, rc.price
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM orderRowComponent rc
|
|
|
|
JOIN vn.component c ON c.id = rc.componentFk
|
2024-07-10 06:10:34 +00:00
|
|
|
WHERE rc.rowFk = vRowFk
|
|
|
|
GROUP BY vSaleFk, rc.componentFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-07-10 06:23:43 +00:00
|
|
|
UPDATE orderRow
|
|
|
|
SET saleFk = vSaleFk
|
2024-07-10 06:10:34 +00:00
|
|
|
WHERE id = vRowFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
END LOOP;
|
2024-07-10 08:30:24 +00:00
|
|
|
CLOSE vRows;
|
2024-01-15 11:31:03 +00:00
|
|
|
END LOOP;
|
2024-07-10 08:30:24 +00:00
|
|
|
CLOSE vDates;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 08:08:04 +00:00
|
|
|
UPDATE `order`
|
|
|
|
SET confirmed = TRUE,
|
|
|
|
confirm_date = util.VN_NOW()
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE id = vSelf;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|