SELECT
distinct ast.REGLEREASSORT ,
ast.isfigemaxi ,
(SELECT sum (qte) FROM edocachpre eachpre, ldocachpre lachpre WHERE eachpre.iddocachpre = lachpre.iddocachpre AND lachpre.idarticle = a.idarticle AND eachpre.issup = 0 AND eachpre.isvalid = 0 AND lachpre.isvalid = 0 AND eachpre.idmagasin = '002' ) AS qteEnpreparation,
(SELECT max(eachpre.iddocachpre) FROM edocachpre eachpre, ldocachpre lachpre WHERE eachpre.iddocachpre = lachpre.iddocachpre AND lachpre.idarticle = a.idarticle AND eachpre.issup = 0 AND eachpre.isvalid = 0 AND lachpre.isvalid = 0 AND eachpre.idmagasin = '002' ) AS iddocPrepa ,
a.idarticle,
a.libelle ,
vf.libellen1 as famille ,
vf.libellen2 as sfamille,
a.libelle,
a.typearticle ,
sa.libelle as statut,
t.nom,
coalesce(r.unite,au.unite) as unite,
a.marque,
a.poids,
a.codetva as idcodetva,
a.co deeco as idcodeeco,
taxeeco.libelle as codeeco,
taxe.libelle as codetva ,
a.refconstructeur,
a.modeappro,
a.memo ,
fic.nomfichier as photo,
r.refere nce,
a.IDSTATUTART as idstatutart ,
l2.libelle as typemarque,
a.typologie,
a.section ,
g.LIBELLE as gamme ,
s.libelle as saison ,
a.isgerernumserie ,
a.lib court ,
a.libelle2 ,
a.idgamme,
a.idsaison,
a.idstatutart,
a.marque as idmarque,
a.typearticle as idtype,
a.typologie as idtypologie ,
a.typeMarque as id typeMarque,
a.section as idsection ,
t.idtiers,
ast.qteminipropose,
ast.qtemaxipropose,
ast.qtesecupropose,
getIdArtPxAch(a.idarticle,null,0,null,nul l,'00',t.idtiers,'B') as artpxachat ,
getIdArtPxAch(a.idarticle,null,null,null,null,'00'
,t.idtiers,'O') as artpxachatpromo ,
r.reference as reffou,
a.idmagasinprincipal,
r.colisage as colisageachat,
(select count(*) from edocachpre eachpre, ldocachpre lachpre where eachpre.iddocachpre = lachpre.iddocachpre and lachpre.idarticle = a.idarticle and eachpre.issup = 0 and eachpre.isvalid = 0 AND eachpre.idmagasin = '002' and lachpre.isvalid = 0 ) as ispreparation,
r.qtemincmd,
l.qteprepacmd,
lc.qtereservecmd,
ast.qtereserve ,
ast.qteattente,
h12m.moyvte12,
ast.emplacement
FROM article a
inner join artstock ast on a.idarticle=ast.IDARTICLE
and ast.idmagasin = '002'
INNER JOIN statutart sa on sa.idstatutart=a.idstatutart
INNER JOIN artunite au on au.idarticle=a.idarticle
AND au.isubase=1
inner join vfamille vf on vf.idarbo = a.idarbo1
left outer join ltabcod l2 on l2.valcode = a.typemarque
and l2.idetabcod='TYPEMARQUE'
and l2.langue='FR'
left outer join gamme g on a.idgamme=g.idgamme
LEFT OUTER JOIN taxe taxe ON a.codetva = taxe.idtaxe
LEFT OUTER JOIN taxe taxeeco ON a.codeeco = taxe.idtaxe
left outer join saison s on s.idsaison=a.idsaison
left outer join partypearticle type on type.idpartypearticle = a.typearticle
left outer join ltabcod l on l.valcode = a.marque
and l.idetabcod= 'MARQUE'
and l.langue='FR'
left outer join ltabcod l3 on l3.valcode = a.typologie
and l3.idetabcod='TYPOLOGIEART'
and l3.langue='FR'
left outer join ltabcod l4 on l4.valcode = a.section
and l4.idetabcod='SECTIONART'
and l4.langue='FR'
inner JOIN artfou af on af.idarticle=a.idarticle
and af.ispr incipal='1'
inner JOIN tiers t ON af.idtiers = t.idtiers
LEFT OUTER JOIN
(
SELECT
sum(l.qtetotexped - l.qtelivre) as qtepre pacmd, l.idarticle, l.idmagasin
FROM ldocvte l
INNER JOIN edocvte e ON e.iddocvte = l.iddocvte
AND e.typedoc = 'CC'
AND l.issolde = 0
AND e.isvalid = 1
AND e.issolde = 0
AND e.issup = 0
group by l.idarticle, l.idmagasin
)
l on ast.idarticle = l.idarticle
and ast.idmagasin = l.idmagasin
LEFT OUTER JOIN
(
SELECT
sum(m.qte mvt) as qtereservecmd, l.idarticle, l.idmagasin
FROM ldocvte l
INNER JOIN mvtstock m ON m.iddocvte = l.iddocvte
AND m.idl docvte = l.idldocvte
INNER JOIN edocvte e ON e.iddocvte = l.iddocvte
AND e.typedoc = 'CC'
AND l.issolde = 0
AND e.isvalid = 1
AND e.issolde = 0
AND e.issup = 0
WHERE EXISTS (select 1 from ldocvte where l.iddocvte = iddocvteparent )
group by l.idarticle, l.idmagasin
)
lc on ast.idarticle = lc.idarticle
and ast.idmagasin = lc.idmagasin
LEFT OUTER JOIN fichierjoint fic on a.idarticle = fic.idarticle
and fic.typedocument='P'
and fic.isdefaut=1
LEFT OUTER JOIN refexterne r ON a.id article = r.idarticle
and r.isdefaut=1
and r.idtiers = af.idtiers
and r.isactif='1'
LEFT OUTER JOIN HISTOARTICLE12M h12m ON a.idarticle = h12m.idart icle
and h12m.idmagasin = '002'
and h12m.unite = au.unite
where a.idarticle in
(
'004998',
'005998',
'0281086',
'0642190',
'100004',
'100005',
'10002859',
'1001840',
'100810',
'100841',
'1011915',
'1012249',
'1018213',
'1019736',
'1022431',
'1022566',
'1022627',
'1022650',
'1022868',
'1023792',
'1025508',
'1025717',
'1
026902',
'102713',
'102738',
'102883',
'1029188',
'1031151',
'1031217',
'103157',
'103158',
'103159',
'103160',
'103161',
'103162',
'103163',
'103164',
'103165',
'103
166',
'103167',
'103168',
'103169',
'103170',
'103171',
'103172',
'103173',
'103174',
'103175',
'103176',
'103179'
)
order by t.nom -