1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
CREATE PROCEDURE generer_mouvements_BQ()
BEGIN
DECLARE NoMouvement INT;
DECLARE done INT DEFAULT 0;
DECLARE id_facture, id_compte_comptable INT default 0;
DECLARE libelle, direction, type_facture VARCHAR(256);
DECLARE montant DECIMAL(10,2);
DECLARE compteClient CURSOR FOR
SELECT f.id_f,
m.id_cc,
ref_internee,
montant,
direction,
IF(direction = 'credit', 'avoir', 'droit')
FROM f
INNER JOIN m
ON m.id_m = f.id_m
INNER JOIN mc
ON mc.id_cc = m.id_cc
WHERE id_etat_facture > 2
AND id_etat_facture < 9
GROUP BY id_f;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
select MAX(mouvement)+1 into NoMouvement FROM mouvements_comptable;
OPEN compteClient;
REPEAT
FETCH compteClient INTO id_f, id_cc, libelle, montant, direction, type_facture;
IF done != 1 THEN
INSERT INTO mouvements_comptable
(
mouvement,
journal,
id_facture,
id_compte_comptable,
libelle_mouvement,
montant,
direction,
analitique,
type_facture
)
VALUES(
NoMouvement,
'BQ',
id_facture,
id_compte_comptable,
libelle,
montant,
direction,
'LRJ INFO',
type_facture
), (
NoMouvement,
'BQ',
id_facture,
418,
libelle,
montant,
IF(direction = 'credit', 'debit', 'credit'),
'LRJ INFO',
type_facture
);
SET NoMouvement = NoMouvement + 1;
END IF;
UNTIL done = 1 END REPEAT;
CLOSE compteClient;
END// |
Partager