mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-19 21:18:16 +02:00
fix: prevent CA multiplication in analytics by using DISTINCT ON per article
artfou1 has multiple rows per article (multi-supplier), causing each mvtart movement to be counted N times in the JOIN. Use a CTE with DISTINCT ON (art_no_id) ORDER BY no_id to pick one canonical supplier per article, matching the same pattern used in pgGetArticlesByFournisseur. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e8b50e143
commit
b420dc2a2b
1 file changed
+12
-6
+12
-6
@@ -645,21 +645,27 @@ export async function pgGetCaByFournisseur(
|
|||||||
moisN1: string
|
moisN1: string
|
||||||
): Promise<CaByFournisseurRow[]> {
|
): Promise<CaByFournisseurRow[]> {
|
||||||
const result = await pgNoParallel(sql`
|
const result = await pgNoParallel(sql`
|
||||||
|
WITH art_fou AS (
|
||||||
|
-- Un seul fournisseur par article (le plus ancien no_id = fournisseur principal)
|
||||||
|
SELECT DISTINCT ON (art_no_id) art_no_id, code AS codefou
|
||||||
|
FROM artfou1
|
||||||
|
ORDER BY art_no_id, no_id
|
||||||
|
)
|
||||||
SELECT
|
SELECT
|
||||||
af.code AS code,
|
af.codefou AS code,
|
||||||
MAX(f.raisonsociale)::text AS nom,
|
MAX(f.raisonsociale)::text AS nom,
|
||||||
m.site,
|
m.site,
|
||||||
TO_CHAR(m.datmvt, 'YYYY-MM') AS mois,
|
TO_CHAR(m.datmvt, 'YYYY-MM') AS mois,
|
||||||
SUM(-m.mntmvtttc)::float AS ca_ttc
|
SUM(-m.mntmvtttc)::float AS ca_ttc
|
||||||
FROM mvtart m
|
FROM mvtart m
|
||||||
JOIN articles a ON a.no_id = m.artnoid
|
JOIN articles a ON a.no_id = m.artnoid
|
||||||
JOIN artfou1 af ON af.art_no_id = a.no_id
|
JOIN art_fou af ON af.art_no_id = a.no_id
|
||||||
JOIN fouadr1 f ON f.code = af.code AND f.sit_code = '000'
|
JOIN fouadr1 f ON f.code = af.codefou AND f.sit_code = '000'
|
||||||
WHERE (TO_CHAR(m.datmvt, 'YYYY-MM') = ${mois} OR TO_CHAR(m.datmvt, 'YYYY-MM') = ${moisN1})
|
WHERE (TO_CHAR(m.datmvt, 'YYYY-MM') = ${mois} OR TO_CHAR(m.datmvt, 'YYYY-MM') = ${moisN1})
|
||||||
AND m.site IN ('292', '579')
|
AND m.site IN ('292', '579')
|
||||||
AND m.genremvt = 3
|
AND m.genremvt = 3
|
||||||
GROUP BY af.code, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
GROUP BY af.codefou, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
||||||
ORDER BY af.code, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
ORDER BY af.codefou, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
||||||
`);
|
`);
|
||||||
|
|
||||||
console.log(`[pg-ff] CaByFournisseur: ${result.rows.length} lignes pour ${mois}/${moisN1}`);
|
console.log(`[pg-ff] CaByFournisseur: ${result.rows.length} lignes pour ${mois}/${moisN1}`);
|
||||||
|
|||||||
Reference in new issue
Block a user