2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`manaCustomerUpdate`()
|
|
|
|
BEGIN
|
|
|
|
DECLARE vToDated DATE;
|
|
|
|
DECLARE vFromDated DATE;
|
|
|
|
DECLARE vForDeleteDated DATE;
|
|
|
|
DECLARE vManaId INT;
|
|
|
|
DECLARE vManaAutoId INT;
|
|
|
|
DECLARE vClaimManaId INT;
|
|
|
|
DECLARE vManaBankId INT;
|
|
|
|
DECLARE vManaGreugeTypeId INT;
|
|
|
|
DECLARE vManaFromDays INT;
|
|
|
|
DECLARE vManaToDays INT;
|
|
|
|
|
|
|
|
SELECT id INTO vManaId
|
|
|
|
FROM vn.component WHERE code = 'mana';
|
|
|
|
|
|
|
|
SELECT id INTO vManaAutoId
|
|
|
|
FROM vn.component WHERE code = 'autoMana';
|
|
|
|
|
|
|
|
SELECT id INTO vClaimManaId
|
|
|
|
FROM vn.component WHERE code = 'manaClaim';
|
|
|
|
|
|
|
|
SELECT id INTO vManaBankId
|
2024-03-05 09:25:23 +00:00
|
|
|
FROM vn.accounting WHERE code = 'mana';
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SELECT id INTO vManaGreugeTypeId
|
|
|
|
FROM vn.greugeType WHERE code = 'mana';
|
|
|
|
|
|
|
|
SELECT manaFromDays, manaToDays
|
|
|
|
INTO vManaFromDays, vManaToDays
|
|
|
|
FROM vn.salespersonConfig;
|
|
|
|
|
|
|
|
SELECT MAX(dated)
|
|
|
|
INTO vFromDated
|
|
|
|
FROM vn.clientManaCache;
|
|
|
|
|
|
|
|
DELETE
|
|
|
|
FROM vn.clientManaCache
|
|
|
|
WHERE dated = vFromDated;
|
|
|
|
|
|
|
|
SELECT MAX(dated)
|
|
|
|
INTO vFromDated
|
|
|
|
FROM vn.clientManaCache;
|
|
|
|
|
|
|
|
IF ISNULL(vFromDated) THEN
|
|
|
|
SELECT manaDateFrom
|
|
|
|
INTO vFromDated
|
|
|
|
FROM vn.salespersonConfig;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
WHILE vFromDated + INTERVAL vManaToDays DAY < util.VN_CURDATE() DO
|
|
|
|
SELECT
|
|
|
|
vFromDated + INTERVAL vManaToDays DAY,
|
|
|
|
vFromDated - INTERVAL vManaFromDays DAY
|
|
|
|
INTO
|
|
|
|
vToDated,
|
|
|
|
vForDeleteDated;
|
|
|
|
|
|
|
|
DELETE FROM vn.clientManaCache
|
|
|
|
WHERE dated <= vForDeleteDated;
|
|
|
|
|
|
|
|
INSERT INTO vn.clientManaCache(clientFk, mana, dated)
|
|
|
|
SELECT
|
|
|
|
Id_Cliente,
|
|
|
|
SUM(mana),
|
|
|
|
vToDated
|
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT cs.Id_Cliente, Cantidad * Valor as mana
|
2024-02-05 12:52:28 +00:00
|
|
|
FROM vn.ticket t
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn2008.Consignatarios cs using(Id_Consigna)
|
2024-02-05 12:52:28 +00:00
|
|
|
JOIN vn2008.Movimientos m on m.Id_Ticket = t.id
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn2008.Movimientos_componentes mc on mc.Id_Movimiento = m.Id_Movimiento
|
|
|
|
WHERE Id_Componente IN (vManaAutoId, vManaId, vClaimManaId)
|
2024-02-05 12:52:28 +00:00
|
|
|
AND t.shipped > vFromDated
|
|
|
|
AND date(t.shipped) <= vToDated
|
2024-01-15 11:31:03 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT r.Id_Cliente, - Entregado
|
|
|
|
FROM vn2008.Recibos r
|
|
|
|
WHERE Id_Banco = vManaBankId
|
|
|
|
AND Fechacobro > vFromDated
|
|
|
|
AND Fechacobro <= vToDated
|
|
|
|
UNION ALL
|
2024-02-05 08:02:31 +00:00
|
|
|
SELECT clientFk, amount
|
|
|
|
FROM vn.greuge
|
|
|
|
WHERE greugeTypeFk = vManaGreugeTypeId
|
|
|
|
AND shipped > vFromDated
|
|
|
|
AND shipped <= vToDated
|
2024-01-15 11:31:03 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT clientFk, mana
|
|
|
|
FROM vn.clientManaCache
|
|
|
|
WHERE dated = vFromDated
|
|
|
|
) sub
|
|
|
|
GROUP BY Id_Cliente
|
|
|
|
HAVING Id_Cliente;
|
|
|
|
|
|
|
|
SET vFromDated = vToDated;
|
|
|
|
END WHILE;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|