42 lines
1.4 KiB
MySQL
42 lines
1.4 KiB
MySQL
|
DELIMITER $$
|
||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`XDiario_reconciliation`()
|
||
|
BEGIN
|
||
|
/**
|
||
|
* Realiza la conciliación diaria de los asientos contables,
|
||
|
* identificando y notificando los asientos descuadrados
|
||
|
* y ajustando los saldos en caso necesario.
|
||
|
*/
|
||
|
INSERT INTO mail (receiver, subject, body)
|
||
|
SELECT 'cau@verdnatura.es',
|
||
|
'Asientos descuadrados',
|
||
|
GROUP_CONCAT(CONCAT(' Asiento: ', ASIEN, ' - Importe:', recon) SEPARATOR ' | \n')
|
||
|
FROM (
|
||
|
SELECT ASIEN,
|
||
|
SUM(IFNULL(ROUND(Eurodebe, 2), 0)) - SUM(IFNULL(ROUND(EUROHABER, 2), 0)) recon
|
||
|
FROM XDiario
|
||
|
WHERE NOT enlazado
|
||
|
GROUP BY ASIEN
|
||
|
HAVING ABS(SUM(IFNULL(ROUND(Eurodebe, 2), 0)) - SUM(IFNULL(ROUND(EUROHABER, 2), 0))) > 0.01
|
||
|
) sub
|
||
|
HAVING COUNT(*);
|
||
|
|
||
|
UPDATE XDiario xd
|
||
|
JOIN (
|
||
|
SELECT xd.id, SUBCTA, recon
|
||
|
FROM XDiario xd
|
||
|
JOIN (
|
||
|
SELECT ASIEN,
|
||
|
SUM(IFNULL(ROUND(Eurodebe, 2), 0)) - SUM(IFNULL(ROUND(EUROHABER, 2), 0)) recon
|
||
|
FROM XDiario
|
||
|
WHERE NOT enlazado
|
||
|
GROUP BY ASIEN
|
||
|
HAVING recon
|
||
|
) sub ON sub.ASIEN = xd.ASIEN
|
||
|
WHERE xd.SUBCTA > '5999999999'
|
||
|
GROUP BY xd.ASIEN
|
||
|
) sub ON sub.id = xd.id
|
||
|
SET xd.Eurohaber = IF(IFNULL(xd.Eurohaber, 0) = 0, xd.Eurohaber, xd.Eurohaber + sub.recon),
|
||
|
xd.Eurodebe = IF(IFNULL(xd.Eurodebe, 0) = 0, xd.Eurodebe, xd.Eurodebe - sub.recon);
|
||
|
END$$
|
||
|
DELIMITER ;
|