hotfix: refs #6777 manaCustomerUpdate #2423

Merged
robert merged 1 commits from 6777-hotfixManaCustomerUpdate into master 2024-05-08 06:35:02 +00:00
1 changed files with 69 additions and 76 deletions

View File

@ -5,97 +5,90 @@ BEGIN
DECLARE vFromDated DATE; DECLARE vFromDated DATE;
DECLARE vForDeleteDated DATE; DECLARE vForDeleteDated DATE;
DECLARE vManaId INT; DECLARE vManaId INT;
DECLARE vManaAutoId INT; DECLARE vManaAutoId INT;
DECLARE vClaimManaId INT; DECLARE vClaimManaId INT;
DECLARE vManaBankId INT; DECLARE vManaBankId INT;
DECLARE vManaGreugeTypeId INT; DECLARE vManaGreugeTypeId INT;
DECLARE vManaFromDays INT; DECLARE vManaFromDays INT;
DECLARE vManaToDays INT; DECLARE vManaToDays INT;
SELECT id INTO vManaId SELECT id INTO vManaId
FROM vn.component WHERE code = 'mana'; FROM vn.component WHERE code = 'mana';
SELECT id INTO vManaAutoId SELECT id INTO vManaAutoId
FROM vn.component WHERE code = 'autoMana'; FROM vn.component WHERE code = 'autoMana';
SELECT id INTO vClaimManaId SELECT id INTO vClaimManaId
FROM vn.component WHERE code = 'manaClaim'; FROM vn.component WHERE code = 'manaClaim';
SELECT id INTO vManaBankId SELECT id INTO vManaBankId
FROM vn.accounting WHERE code = 'mana'; FROM vn.accounting WHERE code = 'mana';
SELECT id INTO vManaGreugeTypeId SELECT id INTO vManaGreugeTypeId
FROM vn.greugeType WHERE code = 'mana'; FROM vn.greugeType WHERE code = 'mana';
SELECT manaFromDays, manaToDays SELECT manaFromDays, manaToDays
INTO vManaFromDays, vManaToDays INTO vManaFromDays, vManaToDays
FROM vn.salespersonConfig; FROM vn.salespersonConfig;
SELECT MAX(dated) SELECT MAX(dated) INTO vFromDated
INTO vFromDated FROM vn.clientManaCache;
FROM vn.clientManaCache;
DELETE DELETE FROM vn.clientManaCache
FROM vn.clientManaCache WHERE dated = vFromDated;
WHERE dated = vFromDated;
SELECT MAX(dated) INTO vFromDated
SELECT MAX(dated) FROM vn.clientManaCache;
INTO vFromDated
FROM vn.clientManaCache; IF vFromDated IS NULL THEN
SELECT manaDateFrom
IF ISNULL(vFromDated) THEN INTO vFromDated
SELECT manaDateFrom FROM vn.salespersonConfig;
INTO vFromDated END IF;
FROM vn.salespersonConfig;
END IF;
WHILE vFromDated + INTERVAL vManaToDays DAY < util.VN_CURDATE() DO WHILE vFromDated + INTERVAL vManaToDays DAY < util.VN_CURDATE() DO
SELECT SELECT vFromDated + INTERVAL vManaToDays DAY,
vFromDated + INTERVAL vManaToDays DAY, vFromDated - INTERVAL vManaFromDays DAY
vFromDated - INTERVAL vManaFromDays DAY INTO vToDated,
INTO vForDeleteDated;
vToDated,
vForDeleteDated; DELETE FROM vn.clientManaCache
WHERE dated <= vForDeleteDated;
DELETE FROM vn.clientManaCache
WHERE dated <= vForDeleteDated;
INSERT INTO vn.clientManaCache(clientFk, mana, dated) INSERT INTO vn.clientManaCache(clientFk, mana, dated)
SELECT SELECT Id_Cliente,
Id_Cliente, SUM(mana),
SUM(mana), vToDated
vToDated FROM (
FROM SELECT a.clientFk Id_Cliente, s.quantity * sc.value mana
( FROM vn.ticket t
SELECT cs.Id_Cliente, Cantidad * Valor as mana JOIN vn.address a ON a.id = t.addressFk
FROM vn.ticket t JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn2008.Consignatarios cs using(Id_Consigna) JOIN vn.saleComponent sc ON sc.saleFk = s.id
JOIN vn2008.Movimientos m on m.Id_Ticket = t.id WHERE sc.componentFk IN (vManaAutoId, vManaId, vClaimManaId)
JOIN vn2008.Movimientos_componentes mc on mc.Id_Movimiento = m.Id_Movimiento AND t.shipped > vFromDated
WHERE Id_Componente IN (vManaAutoId, vManaId, vClaimManaId) AND DATE(t.shipped) <= vToDated
AND t.shipped > vFromDated UNION ALL
AND date(t.shipped) <= vToDated SELECT clientFk, - amountPaid
UNION ALL FROM vn.receipt
SELECT r.Id_Cliente, - Entregado WHERE bankFk = vManaBankId
FROM vn2008.Recibos r AND payed > vFromDated
WHERE Id_Banco = vManaBankId AND payed <= vToDated
AND Fechacobro > vFromDated UNION ALL
AND Fechacobro <= vToDated SELECT clientFk, amount
UNION ALL FROM vn.greuge
SELECT clientFk, amount WHERE greugeTypeFk = vManaGreugeTypeId
FROM vn.greuge AND shipped > vFromDated
WHERE greugeTypeFk = vManaGreugeTypeId AND shipped <= vToDated
AND shipped > vFromDated UNION ALL
AND shipped <= vToDated SELECT clientFk, mana
UNION ALL FROM vn.clientManaCache
SELECT clientFk, mana WHERE dated = vFromDated
FROM vn.clientManaCache ) sub
WHERE dated = vFromDated GROUP BY Id_Cliente
) sub HAVING Id_Cliente;
GROUP BY Id_Cliente
HAVING Id_Cliente;
SET vFromDated = vToDated; SET vFromDated = vToDated;
END WHILE; END WHILE;
END$$ END$$
DELIMITER ; DELIMITER ;