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 78 79 80 81 82 83
| -- BalanceCPT => genere un tableau qui sont la balance comptable de mes regs. ces données sont mises à jour quotidiennement.
With BalanceCPT AS
(
SELECT rcptdet.idg_reg
,rcptdet.numcpt
,rcptdet.totdebex + rcptdet.totdebper AS ReportDebitCPT
,rcptdet.totcredex + rcptdet.totcredper AS ReportCreditCPT
,rcptdet.totdebj AS CumulDebitJourCPT
,rcptdet.totcredj AS CumulCreditJourCPT
,rcptdet.totdebex + rcptdet.totdebper + rcptdet.totdebj AS CumulDebitCPT
,rcptdet.totcredex + rcptdet.totcredper + rcptdet.totcredj AS CumulCreditCPT
,CASE WHEN (rcptdet.totdebex + rcptdet.totdebper + rcptdet.totdebj)-(rcptdet.totcredex + rcptdet.totcredper + rcptdet.totcredj) > 0 THEN
(rcptdet.totdebex + rcptdet.totdebper + rcptdet.totdebj)-(rcptdet.totcredex + rcptdet.totcredper + rcptdet.totcredj)
ELSE 0.00
END AS SoldeDebitCPT
,CASE WHEN (rcptdet.totcredex + rcptdet.totcredper + rcptdet.totcredj)-(rcptdet.totdebex + rcptdet.totdebper + rcptdet.totdebj) > 0 THEN
(rcptdet.totcredex + rcptdet.totcredper + rcptdet.totcredj)-(rcptdet.totdebex + rcptdet.totdebper + rcptdet.totdebj)
ELSE 0.00
END AS SoldeCreditCPT
FROM base.rcptdet
ORDER BY idg_reg, numcpt
),
-- avec ces données, je calcule la sommes pour chaque reg => Balance.
Balance AS
(
SELECT BalanceCPT.idg_reg AS Numreg
,SUM(ReportDebitCPT) AS ReportDebit
,SUM(ReportCreditCPT) AS ReportCredit
,SUM(CumulDebitJourCPT) AS CumulDebitJour
,SUM(CumulCreditJourCPT) AS CumulCreditJour
,SUM(CumulDebitCPT) AS CumulDebit
,SUM(CumulCreditCPT) AS CumulCredit
,SUM(SoldeDebitCPT) AS SoldeDebit
,SUM(SoldeCreditCPT) AS SoldeCredit
FROM BalanceCPT
GROUP BY BalanceCPT.idg_reg
)
-- la, j'affiche les regs qui sont vides ou qui n'ont jamais dépensé ou encaisser de recette.
Select --RR.idreg
PCT.intitule AS NomPlanComtable
,CATR.libelle AS Categorie
,RPM.typereg AS Typereg
,RR.intitule AS Nomreg
,RR.num_reg AS ID_reg_chor
,RPM.dteuser AS Datereg
,COUNT(CASE WHEN RH.numbenef > 0 THEN 1 ELSE NULL END) AS Nb_de_tiers
,RU.nom AS Nom
,RU.prenom AS Prenom
,RU.nomuser AS identifiant
,RG.nomgroupe AS GroupeUtilisateur
,Balance.SoldeDebit AS SoldeDebit
-- ,Balance.SoldeCredit AS SoldeCredit
FROM base.rreg AS RR
LEFT JOIN base.asso_cat_reg AS ACATR -- catégorie
ON RR.idreg = ACATR.idreg
LEFT JOIN base.cat_reg AS CATR
ON ACATR.id_cat_reg = CATR.id
LEFT JOIN base.rpasse AS RPASS -- utilisateur et groupe
ON RR.idreg = RPASS.idg_reg
LEFT JOIN base.rutils AS RU
ON RPASS.numuser = RU.numuser
LEFT JOIN base.rgroupes AS RG
ON RPASS.numgroupe = RG.numgroupe
LEFT JOIN base.plancpt_reg AS PCPTR --nom plan comptable
ON RR.idreg = PCPTR.id_reg
LEFT JOIN base.plancpt_lib AS PCT
ON PCPTR.id_plancpt = PCT.id
LEFT JOIN Balance --balance
ON RR.idreg = Balance.Numreg
LEFT JOIN base.rparam AS RPM --date reg
ON RR.idreg = RPM.idg_reg
LEFT JOIN base.rhabitue AS RH --nombre de reg
ON RR.idreg = RH.idg_reg
WHERE (RG.parent = 1 OR RG.parent = 2 OR RG.parent = 3271)
-- condition permettant d'avoir que les regs vide = 0 et les regs avec de l'avance 50 000.
AND (
(Balance.ReportDebit = 0 AND Balance.ReportCredit = 0 AND Balance.CumulDebitJour = 0 AND Balance.CumulCreditJour = 0 AND Balance.CumulDebit = 0 AND Balance.CumulCredit = 0 AND Balance.SoldeDebit = 0 AND Balance.SoldeCredit = 0)
OR (Balance.ReportDebit = 0 AND Balance.ReportCredit = 0 AND Balance.CumulDebitJour = 50000 AND Balance.CumulCreditJour = 50000 AND Balance.CumulDebit = 50000 AND Balance.CumulCredit = 50000 AND Balance.SoldeDebit = 50000 AND Balance.SoldeCredit = 50000)
OR (Balance.ReportDebit = 50000 AND Balance.ReportCredit = 50000 AND Balance.CumulDebitJour = 0 AND Balance.CumulCreditJour = 0 AND Balance.CumulDebit = 50000 AND Balance.CumulCredit = 50000 AND Balance.SoldeDebit = 50000 AND Balance.SoldeCredit = 50000)
)
--AND RR.type_reg = 'RR'
GROUP BY PCT.intitule, CATR.libelle, RPM.typereg, RR.intitule, RR.num_reg, RPM.dteuser, RU.nom, RU.prenom, RU.nomuser, RG.nomgroupe, SoldeDebit--, SoldeCredit
ORDER BY NomPlanComtable, Categorie, Nomreg asc |
Partager