refactor: refs #6453 order_confirmWithUser
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Guillermo Bonet 2024-07-10 08:10:34 +02:00
parent b068327352
commit b75bdc07c2
1 changed files with 71 additions and 71 deletions

View File

@ -1,36 +1,40 @@
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 on
* date, store and user. * 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 vIsOk 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 vTicketFk INT;
DECLARE vNotes VARCHAR(255); DECLARE vNotes VARCHAR(255);
DECLARE vItem INT; DECLARE vItem INT;
DECLARE vConcept VARCHAR(30); DECLARE vConcept VARCHAR(30);
DECLARE vAmount INT; DECLARE vAmount INT;
DECLARE vPrice DECIMAL(10,2); DECLARE vPrice DECIMAL(10,2);
DECLARE vSale INT; DECLARE vSaleFk INT;
DECLARE vRate INT; DECLARE vRate INT;
DECLARE vRowId INT; DECLARE vRowFk INT;
DECLARE vPriceFixed DECIMAL(10,2); DECLARE vPriceFixed DECIMAL(10,2);
DECLARE vDelivery DATE; DECLARE vDelivery DATE;
DECLARE vAddress INT; DECLARE vAddressFk INT;
DECLARE vIsConfirmed BOOL; DECLARE vIsConfirmed BOOL;
DECLARE vClientId INT; DECLARE vClientFk INT;
DECLARE vCompanyId INT; DECLARE vCompanyFk INT;
DECLARE vAgencyModeId INT; DECLARE vAgencyModeFk INT;
DECLARE vCalc INT; DECLARE vCalcFk INT;
DECLARE vIsLogifloraItem BOOL; DECLARE vIsLogifloraItem BOOL;
DECLARE vIsTaxDataChecked BOOL; DECLARE vIsTaxDataChecked BOOL;
DECLARE vAvailable INT;
DECLARE cDates CURSOR FOR DECLARE cDates CURSOR FOR
SELECT zgs.shipped, r.warehouse_id SELECT zgs.shipped, r.warehouse_id
@ -46,12 +50,11 @@ BEGIN
FROM order_row r FROM order_row r
JOIN vn.item i ON i.id = r.item_id JOIN vn.item i ON i.id = r.item_id
WHERE r.amount WHERE r.amount
AND r.warehouse_id = vWarehouse AND r.warehouse_id = vWarehouseFk
AND r.order_id = vSelf AND r.order_id = 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
@ -68,13 +71,13 @@ BEGIN
o.agency_id, o.agency_id,
c.isTaxDataChecked c.isTaxDataChecked
INTO vDelivery, INTO vDelivery,
vAddress, vAddressFk,
vNotes, vNotes,
vClientId, vClientFk,
vCompanyId, vCompanyFk,
vAgencyModeId, vAgencyModeFk,
vIsTaxDataChecked vIsTaxDataChecked
FROM hedera.`order` o 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;
@ -85,11 +88,11 @@ BEGIN
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(vDelivery, 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;
@ -97,12 +100,12 @@ BEGIN
CALL order_checkEditable(vSelf); CALL order_checkEditable(vSelf);
-- Check order is not empty -- Check order is not empty
SELECT COUNT(*) > 0 INTO vOk SELECT COUNT(*) > 0 INTO vIsOk
FROM order_row FROM order_row
WHERE order_id = vSelf WHERE order_id = vSelf
AND amount > 0; AND amount > 0;
IF NOT vOk THEN IF NOT vIsOk THEN
CALL util.throw('ORDER_EMPTY'); CALL util.throw('ORDER_EMPTY');
END IF; END IF;
@ -110,9 +113,9 @@ BEGIN
OPEN cDates; OPEN cDates;
lDates: lDates:
LOOP LOOP
SET vTicket = NULL; SET vTicketFk = NULL;
SET vDone = FALSE; SET vDone = FALSE;
FETCH cDates INTO vShipment, vWarehouse; FETCH cDates INTO vShipment, vWarehouseFk;
IF vDone THEN IF vDone THEN
LEAVE lDates; LEAVE lDates;
@ -126,13 +129,13 @@ BEGIN
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 util.dayend(vShipment)
) )
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 ON o.address_id = t.addressFk JOIN hedera.`order` o ON o.address_id = t.addressFk
AND vWarehouse = t.warehouseFk AND vWarehouseFk = t.warehouseFk
AND o.date_send = t.landed AND o.date_send = t.landed
AND DATE(t.shipped) = vShipment AND DATE(t.shipped) = vShipment
WHERE o.id = vSelf WHERE o.id = vSelf
@ -142,36 +145,36 @@ BEGIN
LIMIT 1; LIMIT 1;
-- 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 THEN IF vTicketFk IS NULL 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, vDelivery,
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 = (SELECT id FROM vn.state WHERE code = '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;
-- Añade las notas -- Añade las notas
IF vNotes IS NOT NULL AND vNotes <> '' THEN IF vNotes IS NOT NULL AND vNotes <> '' THEN
INSERT INTO vn.ticketObservation SET INSERT INTO vn.ticketObservation SET
ticketFk = vTicket, ticketFk = vTicketFk,
observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
`description` = vNotes `description` = vNotes
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
@ -182,74 +185,71 @@ BEGIN
OPEN cRows; OPEN cRows;
lRows: LOOP lRows: LOOP
SET vDone = FALSE; SET vDone = FALSE;
FETCH cRows INTO vRowId, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; FETCH cRows INTO vRowFk, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem;
IF vDone THEN IF vDone THEN
LEAVE lRows; LEAVE lRows;
END IF; END IF;
SET vSale = NULL; SET vSaleFk = NULL;
SELECT s.id INTO vSale SELECT s.id INTO vSaleFk
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 = vItem
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;
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 AND ct.isBase
WHERE rc.rowFk = vRowId; WHERE rc.rowFk = vRowFk;
INSERT INTO vn.sale INSERT INTO vn.sale
SET itemFk = vItem, SET itemFk = vItem,
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 UPDATE order_row
SET Id_Movimiento = vSale SET Id_Movimiento = vSaleFk
WHERE id = vRowId; WHERE id = vRowFk;
-- Inserta en putOrder si la compra es de Floramondo -- Inserta en putOrder si la compra es de Floramondo
IF vIsLogifloraItem THEN IF vIsLogifloraItem THEN
CALL cache.availableNoRaids_refresh(vCalc,FALSE,vWarehouse,vShipment); CALL cache.availableNoRaids_refresh(vCalcFk, FALSE,vWarehouseFk, vShipment);
SET @available := 0; SELECT GREATEST(0, available) INTO vAvailable
SELECT GREATEST(0, available) INTO @available
FROM cache.availableNoRaids FROM cache.availableNoRaids
WHERE calc_id = vCalc WHERE calc_id = vCalcFk
AND item_id = vItem; AND item_id = vItem;
UPDATE cache.availableNoRaids UPDATE cache.availableNoRaids
SET available = GREATEST(0, available - vAmount) SET available = GREATEST(0, available - vAmount)
WHERE item_id = vItem WHERE item_id = vItem
AND calc_id = vCalc; AND calc_id = vCalcFk;
INSERT INTO edi.putOrder ( INSERT INTO edi.putOrder (
deliveryInformationID, deliveryInformationID,
@ -262,20 +262,20 @@ BEGIN
) )
SELECT di.ID, SELECT di.ID,
i.supplyResponseFk, i.supplyResponseFk,
CEIL((vAmount - @available)/ sr.NumberOfItemsPerCask), CEIL((vAmount - vAvailable)/ sr.NumberOfItemsPerCask),
o.address_id , o.address_id ,
vClientId, vClientFk,
IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber), IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber),
vSale vSaleFk
FROM edi.deliveryInformation di FROM edi.deliveryInformation di
JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID
JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk
LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientId LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientFk
JOIN edi.floraHollandConfig fhc JOIN edi.floraHollandConfig fhc
JOIN hedera.`order` o ON o.id = vSelf JOIN `order` o ON o.id = vSelf
WHERE i.id = vItem WHERE i.id = vItem
AND di.LatestOrderDateTime > util.VN_NOW() AND di.LatestOrderDateTime > util.VN_NOW()
AND vAmount > @available AND vAmount > vAvailable
LIMIT 1; LIMIT 1;
END IF; END IF;
END LOOP; END LOOP;