refactor: refs #6453 order_confirmWithUser #2694

Merged
guillermo merged 16 commits from 6453-orderConfirm into dev 2024-08-02 10:15:00 +00:00
1 changed files with 162 additions and 168 deletions

View File

@ -1,59 +1,62 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(vSelf INT, vUserId INT) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(
vSelf INT,
vUserFk INT
)
BEGIN BEGIN
/** /**
* Confirms an order, creating each of its tickets on the corresponding * Confirms an order, creating each of its tickets
* date, store and user. * on the corresponding date, store and user.
* *
* @param vSelf The order identifier * @param vSelf The order identifier
* @param vUser The user identifier * @param vUser The user identifier
*/ */
DECLARE vOk BOOL; DECLARE vHasRows BOOL;
DECLARE vDone BOOL DEFAULT FALSE; DECLARE vDone BOOL;
DECLARE vWarehouse INT; DECLARE vWarehouseFk INT;
DECLARE vShipment DATE; DECLARE vShipment DATE;
DECLARE vTicket INT; DECLARE vShipmentDayEnd DATETIME;
DECLARE vTicketFk INT;
DECLARE vNotes VARCHAR(255); DECLARE vNotes VARCHAR(255);
DECLARE vItem INT; DECLARE vItemFk INT;
DECLARE vConcept VARCHAR(30); DECLARE vConcept VARCHAR(30);
DECLARE vAmount INT; DECLARE vAmount INT;
DECLARE vAvailable INT;
DECLARE vPrice DECIMAL(10,2); DECLARE vPrice DECIMAL(10,2);
DECLARE vSale INT; DECLARE vSaleFk INT;
DECLARE vRate INT; DECLARE vRowFk INT;
DECLARE vRowId INT;
DECLARE vPriceFixed DECIMAL(10,2); DECLARE vPriceFixed DECIMAL(10,2);
DECLARE vDelivery DATE; DECLARE vLanded DATE;
DECLARE vAddress INT; DECLARE vAddressFk INT;
DECLARE vIsConfirmed BOOL; DECLARE vClientFk INT;
DECLARE vClientId INT; DECLARE vCompanyFk INT;
DECLARE vCompanyId INT; DECLARE vAgencyModeFk INT;
DECLARE vAgencyModeId INT; DECLARE vCalcFk INT;
DECLARE TICKET_FREE INT DEFAULT 2;
DECLARE vCalc INT;
DECLARE vIsLogifloraItem BOOL;
DECLARE vOldQuantity INT;
DECLARE vNewQuantity INT;
DECLARE vIsTaxDataChecked BOOL; DECLARE vIsTaxDataChecked BOOL;
DECLARE cDates CURSOR FOR DECLARE vDates CURSOR FOR
SELECT zgs.shipped, r.warehouse_id SELECT zgs.shipped, r.warehouseFk
FROM `order` o FROM `order` o
JOIN order_row r ON r.order_id = o.id JOIN orderRow r ON r.orderFk = o.id
LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouse_id LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouseFk
WHERE o.id = vSelf AND r.amount != 0 WHERE o.id = vSelf
GROUP BY r.warehouse_id; AND r.amount
GROUP BY r.warehouseFk;
DECLARE cRows CURSOR FOR DECLARE vRows CURSOR FOR
SELECT r.id, r.item_id, i.name, r.amount, r.price, r.rate, i.isFloramondo SELECT r.id,
FROM order_row r r.itemFk,
JOIN vn.item i ON i.id = r.item_id i.name,
WHERE r.amount != 0 r.amount,
AND r.warehouse_id = vWarehouse r.price
AND r.order_id = vSelf FROM orderRow r
JOIN vn.item i ON i.id = r.itemFk
WHERE r.amount
AND r.warehouseFk = vWarehouseFk
AND r.orderFk = vSelf
ORDER BY r.rate DESC; ORDER BY r.rate DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN
@ -62,26 +65,36 @@ BEGIN
END; END;
-- Carga los datos del pedido -- Carga los datos del pedido
SELECT o.date_send, o.address_id, o.note, a.clientFk, SELECT o.date_send,
o.company_id, o.agency_id, c.isTaxDataChecked o.address_id,
INTO vDelivery, vAddress, vNotes, vClientId, o.note,
vCompanyId, vAgencyModeId, vIsTaxDataChecked a.clientFk,
FROM hedera.`order` o o.company_id,
o.agency_id,
c.isTaxDataChecked
INTO vLanded,
vAddressFk,
vNotes,
vClientFk,
vCompanyFk,
vAgencyModeFk,
vIsTaxDataChecked
FROM `order` o
JOIN vn.address a ON a.id = o.address_id JOIN vn.address a ON a.id = o.address_id
JOIN vn.client c ON c.id = a.clientFk JOIN vn.client c ON c.id = a.clientFk
WHERE o.id = vSelf; WHERE o.id = vSelf;
-- Verifica si el cliente tiene los datos comprobados -- Verifica si el cliente tiene los datos comprobados
IF NOT vIsTaxDataChecked THEN IF NOT vIsTaxDataChecked THEN
CALL util.throw ('clientNotVerified'); CALL util.throw('clientNotVerified');
END IF; END IF;
-- Carga las fechas de salida de cada almacen -- Carga las fechas de salida de cada almacen
CALL vn.zone_getShipped (vDelivery, vAddress, vAgencyModeId, FALSE); CALL vn.zone_getShipped(vLanded, vAddressFk, vAgencyModeFk, FALSE);
-- Trabajador que realiza la accion -- Trabajador que realiza la accion
IF vUserId IS NULL THEN IF vUserFk IS NULL THEN
SELECT employeeFk INTO vUserId FROM orderConfig; SELECT employeeFk INTO vUserFk FROM orderConfig;
END IF; END IF;
START TRANSACTION; START TRANSACTION;
@ -89,207 +102,188 @@ BEGIN
CALL order_checkEditable(vSelf); CALL order_checkEditable(vSelf);
-- Check order is not empty -- Check order is not empty
SELECT COUNT(*) > 0 INTO vHasRows
FROM orderRow
WHERE orderFk = vSelf
AND amount > 0;
SELECT COUNT(*) > 0 INTO vOk IF NOT vHasRows THEN
FROM order_row WHERE order_id = vSelf AND amount > 0; CALL util.throw('ORDER_EMPTY');
IF NOT vOk THEN
CALL util.throw ('ORDER_EMPTY');
END IF; END IF;
-- Crea los tickets del pedido -- Crea los tickets del pedido
OPEN vDates;
OPEN cDates; lDates: LOOP
SET vTicketFk = NULL;
lDates:
LOOP
SET vTicket = NULL;
SET vDone = FALSE; SET vDone = FALSE;
FETCH cDates INTO vShipment, vWarehouse; FETCH vDates INTO vShipment, vWarehouseFk;
IF vDone THEN IF vDone THEN
LEAVE lDates; LEAVE lDates;
END IF; END IF;
-- Busca un ticket existente que coincida con los parametros SET vShipmentDayEnd = util.dayEnd(vShipment);
WITH tPrevia AS
(SELECT DISTINCT s.ticketFk -- Busca un ticket libre disponible
WITH tPrevia AS (
SELECT DISTINCT s.ticketFk
FROM vn.sale s FROM vn.sale s
JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id
JOIN vn.ticket t ON t.id = s.ticketFk JOIN vn.ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
) )
SELECT t.id INTO vTicket SELECT t.id INTO vTicketFk
FROM vn.ticket t FROM vn.ticket t
JOIN vn.alertLevel al ON al.code = 'FREE' JOIN vn.alertLevel al ON al.code = 'FREE'
LEFT JOIN tPrevia tp ON tp.ticketFk = t.id LEFT JOIN tPrevia tp ON tp.ticketFk = t.id
LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id LEFT JOIN vn.ticketState tls ON tls.ticketFk = t.id
JOIN hedera.`order` o JOIN hedera.`order` o ON o.address_id = t.addressFk
ON o.address_id = t.addressFk AND t.shipped BETWEEN vShipment AND vShipmentDayEnd
AND vWarehouse = t.warehouseFk AND t.warehouseFk = vWarehouseFk
AND o.date_send = t.landed AND o.date_send = t.landed
AND DATE(t.shipped) = vShipment
WHERE o.id = vSelf WHERE o.id = vSelf
AND t.refFk IS NULL AND t.refFk IS NULL
AND tp.ticketFk IS NULL AND tp.ticketFk IS NULL
AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id)
LIMIT 1; LIMIT 1;
-- Comprobamos si hay un ticket de previa disponible
IF vTicketFk IS NULL THEN
WITH tItemPackingTypeOrder AS (
SELECT GROUP_CONCAT(
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
) distinctItemPackingTypes,
o.address_id
FROM vn.item i
JOIN orderRow oro ON oro.itemFk = i.id
JOIN `order` o ON o.id = oro.orderFk
WHERE oro.orderFk = vSelf
),
tItemPackingTypeTicket AS (
SELECT t.id,
GROUP_CONCAT(
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
) distinctItemPackingTypes
FROM vn.ticket t
JOIN vn.ticketState tls ON tls.ticketFk = t.id
JOIN vn.alertLevel al ON al.id = tls.alertLevel
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
JOIN tItemPackingTypeOrder ipto
WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
AND t.refFk IS NULL
AND t.warehouseFk = vWarehouseFk
AND t.addressFk = ipto.address_id
AND al.code = 'ON_PREVIOUS'
GROUP BY t.id
)
SELECT iptt.id INTO vTicketFk
FROM tItemPackingTypeTicket iptt
JOIN tItemPackingTypeOrder ipto
WHERE INSTR(iptt.distinctItemPackingTypes, ipto.distinctItemPackingTypes)
LIMIT 1;
END IF;
-- Crea el ticket en el caso de no existir uno adecuado -- Crea el ticket en el caso de no existir uno adecuado
IF vTicket IS NULL IF vTicketFk IS NULL THEN
THEN
SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); SET vShipment = IFNULL(vShipment, util.VN_CURDATE());
CALL vn.ticket_add( CALL vn.ticket_add(
vClientId, vClientFk,
vShipment, vShipment,
vWarehouse, vWarehouseFk,
vCompanyId, vCompanyFk,
vAddress, vAddressFk,
vAgencyModeId, vAgencyModeFk,
NULL, NULL,
vDelivery, vLanded,
vUserId, vUserFk,
TRUE, TRUE,
vTicket vTicketFk
); );
ELSE ELSE
INSERT INTO vn.ticketTracking INSERT INTO vn.ticketTracking
SET ticketFk = vTicket, SET ticketFk = vTicketFk,
userFk = vUserId, userFk = vUserFk,
stateFk = TICKET_FREE; stateFk = (SELECT id FROM vn.state WHERE code = 'FREE');
END IF; END IF;
INSERT IGNORE INTO vn.orderTicket INSERT IGNORE INTO vn.orderTicket
SET orderFk = vSelf, SET orderFk = vSelf,
ticketFk = vTicket; ticketFk = vTicketFk;
guillermo marked this conversation as resolved
Review

ticket_add que es crida dalt ja fa insert en la taula ticketObservation y es el que realment deuria fer-ho. Fes una proba per confirmar si este codi esta duplicant les observacions i si es així ho lleves.

ticket_add que es crida dalt ja fa insert en la taula ticketObservation y es el que realment deuria fer-ho. Fes una proba per confirmar si este codi esta duplicant les observacions i si es així ho lleves.
Review

He revisat el codi, i ahí soles se inserta la observació del order, per lo tant, es correcte, no es duplica res

He revisat el codi, i ahí soles se inserta la observació del order, per lo tant, es correcte, no es duplica res
-- Añade las notas -- Añade las notas
IF vNotes IS NOT NULL AND vNotes <> '' THEN
IF vNotes IS NOT NULL AND vNotes != '' INSERT INTO vn.ticketObservation
THEN SET ticketFk = vTicketFk,
INSERT INTO vn.ticketObservation SET observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
ticketFk = vTicket,
observationTypeFk = 4 /* salesperson */ ,
`description` = vNotes `description` = vNotes
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
`description` = CONCAT(VALUES(`description`),'. ', `description`); `description` = CONCAT(VALUES(`description`),'. ', `description`);
END IF; END IF;
-- Añade los movimientos y sus componentes -- Añade los movimientos y sus componentes
OPEN vRows;
OPEN cRows;
lRows: LOOP lRows: LOOP
SET vSaleFk = NULL;
SET vDone = FALSE; SET vDone = FALSE;
FETCH cRows INTO vRowId, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; FETCH vRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice;
IF vDone THEN IF vDone THEN
LEAVE lRows; LEAVE lRows;
END IF; END IF;
SET vSale = NULL; SELECT s.id INTO vSaleFk
SELECT s.id, s.quantity INTO vSale, vOldQuantity
FROM vn.sale s FROM vn.sale s
WHERE ticketFk = vTicket WHERE ticketFk = vTicketFk
AND price = vPrice AND price = vPrice
AND itemFk = vItem AND itemFk = vItemFk
AND discount = 0 AND discount = 0
LIMIT 1; LIMIT 1;
IF vSale THEN IF vSaleFk THEN
UPDATE vn.sale UPDATE vn.sale
SET quantity = quantity + vAmount, SET quantity = quantity + vAmount,
originalQuantity = quantity originalQuantity = quantity
WHERE id = vSale; WHERE id = vSaleFk;
SELECT s.quantity INTO vNewQuantity
FROM vn.sale s
WHERE id = vSale;
ELSE ELSE
-- Obtiene el coste -- Obtiene el coste
SELECT SUM(rc.`price`) valueSum INTO vPriceFixed SELECT SUM(rc.`price`) valueSum INTO vPriceFixed
FROM orderRowComponent rc FROM orderRowComponent rc
JOIN vn.component c ON c.id = rc.componentFk JOIN vn.component c ON c.id = rc.componentFk
JOIN vn.componentType ct ON ct.id = c.typeFk AND ct.isBase JOIN vn.componentType ct ON ct.id = c.typeFk
WHERE rc.rowFk = vRowId; AND ct.isBase
WHERE rc.rowFk = vRowFk;
INSERT INTO vn.sale INSERT INTO vn.sale
SET itemFk = vItem, SET itemFk = vItemFk,
ticketFk = vTicket, ticketFk = vTicketFk,
concept = vConcept, concept = vConcept,
quantity = vAmount, quantity = vAmount,
price = vPrice, price = vPrice,
priceFixed = vPriceFixed, priceFixed = vPriceFixed,
isPriceFixed = TRUE; isPriceFixed = TRUE;
SET vSale = LAST_INSERT_ID(); SET vSaleFk = LAST_INSERT_ID();
INSERT INTO vn.saleComponent INSERT INTO vn.saleComponent (saleFk, componentFk, `value`)
(saleFk, componentFk, `value`) SELECT vSaleFk, rc.componentFk, rc.price
SELECT vSale, rc.componentFk, rc.price
FROM orderRowComponent rc FROM orderRowComponent rc
JOIN vn.component c ON c.id = rc.componentFk JOIN vn.component c ON c.id = rc.componentFk
WHERE rc.rowFk = vRowId WHERE rc.rowFk = vRowFk
GROUP BY vSale, rc.componentFk; GROUP BY vSaleFk, rc.componentFk;
END IF; END IF;
UPDATE order_row SET Id_Movimiento = vSale UPDATE orderRow
WHERE id = vRowId; SET saleFk = vSaleFk
WHERE id = vRowFk;
-- Inserta en putOrder si la compra es de Floramondo
IF vIsLogifloraItem THEN
CALL cache.availableNoRaids_refresh(vCalc,FALSE,vWarehouse,vShipment);
SET @available := 0;
SELECT GREATEST(0,available) INTO @available
FROM cache.availableNoRaids
WHERE calc_id = vCalc
AND item_id = vItem;
UPDATE cache.availableNoRaids
SET available = GREATEST(0,available - vAmount)
WHERE item_id = vItem
AND calc_id = vCalc;
INSERT INTO edi.putOrder (
deliveryInformationID,
supplyResponseId,
quantity ,
EndUserPartyId,
EndUserPartyGLN,
FHAdminNumber,
saleFk
)
SELECT di.ID,
i.supplyResponseFk,
CEIL((vAmount - @available)/ sr.NumberOfItemsPerCask),
o.address_id ,
vClientId,
IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber),
vSale
FROM edi.deliveryInformation di
JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID
JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk
LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientId
JOIN edi.floraHollandConfig fhc
JOIN hedera.`order` o ON o.id = vSelf
WHERE i.id = vItem
AND di.LatestOrderDateTime > util.VN_NOW()
AND vAmount > @available
LIMIT 1;
END IF;
END LOOP; END LOOP;
CLOSE vRows;
CLOSE cRows;
END LOOP; END LOOP;
CLOSE vDates;
CLOSE cDates; UPDATE `order`
SET confirmed = TRUE,
UPDATE `order` SET confirmed = TRUE, confirm_date = util.VN_NOW() confirm_date = util.VN_NOW()
WHERE id = vSelf; WHERE id = vSelf;
COMMIT; COMMIT;