mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-21 05:58:10 +02:00
fix: use artfou1.preference=true and ABS(SUM(mntmvtttc)) for correct CA TTC in analytics
Replace last_reception CTE with direct LEFT JOIN artfou1 WHERE preference=true (fournisseur principal de l'article). Use ABS(SUM(mntmvtttc)) instead of SUM(-mntmvtttc). Both fournisseur and nomenclature queries now use LEFT JOIN to include all sold articles. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
bc76237f48
commit
79aadf62eb
1 file changed
+18
-30
+18
-30
@@ -638,40 +638,28 @@ export interface CaByNomenclatureRow {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne le CA TTC par fournisseur et site pour deux mois donnés (mois et mois N-1).
|
* Retourne le CA TTC par fournisseur et site pour deux mois donnés (mois et mois N-1).
|
||||||
|
* Attribution via artfou1.preference = true (fournisseur principal de l'article).
|
||||||
*/
|
*/
|
||||||
export async function pgGetCaByFournisseur(
|
export async function pgGetCaByFournisseur(
|
||||||
mois: string,
|
mois: string,
|
||||||
moisN1: string
|
moisN1: string
|
||||||
): Promise<CaByFournisseurRow[]> {
|
): Promise<CaByFournisseurRow[]> {
|
||||||
const result = await pgNoParallel(sql`
|
const result = await pgNoParallel(sql`
|
||||||
WITH last_reception AS (
|
|
||||||
-- Attribution par dernière entrée en stock (genremvt 1 ou 2).
|
|
||||||
-- mvtart ne stocke pas le codefou directement → on joint artfou1.
|
|
||||||
-- DISTINCT ON (artnoid) ORDER BY datmvt DESC, af.no_id DESC :
|
|
||||||
-- 1. prend la réception la plus récente
|
|
||||||
-- 2. en cas d'égalité de date, prend le dernier artfou1 (no_id DESC)
|
|
||||||
SELECT DISTINCT ON (m.artnoid)
|
|
||||||
m.artnoid,
|
|
||||||
af.code AS codefou
|
|
||||||
FROM mvtart m
|
|
||||||
JOIN artfou1 af ON af.art_no_id = m.artnoid
|
|
||||||
WHERE m.genremvt IN (1, 2)
|
|
||||||
ORDER BY m.artnoid, m.datmvt DESC, af.no_id DESC
|
|
||||||
)
|
|
||||||
SELECT
|
SELECT
|
||||||
lr.codefou AS code,
|
COALESCE(af.code, 'SANS_FOURNISSEUR')::text AS code,
|
||||||
MAX(fi.nom)::text AS nom,
|
COALESCE(fi.nom, af.code, 'Sans fournisseur')::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
|
ABS(SUM(m.mntmvtttc))::float AS ca_ttc
|
||||||
FROM mvtart m
|
FROM mvtart m
|
||||||
JOIN last_reception lr ON lr.artnoid = m.artnoid
|
JOIN articles a ON a.no_id = m.artnoid
|
||||||
JOIN fouident fi ON fi.code = lr.codefou
|
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = true
|
||||||
|
LEFT JOIN fouident fi ON fi.code = af.code
|
||||||
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 lr.codefou, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
GROUP BY af.code, fi.nom, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
||||||
ORDER BY lr.codefou, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
ORDER BY af.code, 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}`);
|
||||||
@@ -679,7 +667,7 @@ export async function pgGetCaByFournisseur(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retourne le CA TTC par nomenclature (code3) et site pour deux mois donnés.
|
* Retourne le CA TTC par nomenclature et site pour deux mois donnés.
|
||||||
*/
|
*/
|
||||||
export async function pgGetCaByNomenclature(
|
export async function pgGetCaByNomenclature(
|
||||||
mois: string,
|
mois: string,
|
||||||
@@ -687,18 +675,18 @@ export async function pgGetCaByNomenclature(
|
|||||||
): Promise<CaByNomenclatureRow[]> {
|
): Promise<CaByNomenclatureRow[]> {
|
||||||
const result = await pgNoParallel(sql`
|
const result = await pgNoParallel(sql`
|
||||||
SELECT
|
SELECT
|
||||||
n.code AS code,
|
COALESCE(n.code, 'SANS_NOM')::text AS code,
|
||||||
n.libelle::text AS libelle,
|
COALESCE(n.libelle, 'Sans nomenclature')::text AS libelle,
|
||||||
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
|
ABS(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 nomenclature n ON n.no_id = a.nom_no_id
|
LEFT JOIN nomenclature n ON n.no_id = a.nom_no_id
|
||||||
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 n.code, n.libelle, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
GROUP BY n.no_id, n.code, n.libelle, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
||||||
ORDER BY n.code, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
ORDER BY n.code, m.site, TO_CHAR(m.datmvt, 'YYYY-MM')
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user