[SQL Server] Envoyer par mail un tableaux avec des colonnes fusionnées et un sous total général

-- =============================================

-- Author:      Zouhaier KHARROUBI

-- Create date: 13/04/2017

-- Description: Envoyer par mail un tableaux avec des colonnes fusionnées et un sous total général

-- =============================================

Le résultat de la requête :

REALISATION   CA DU 01/09/2016 AU 13/04/2017

N°   Ligne

Magasin

ECRAN

CLAVIER

SOURIS

CLE   USB

DISQUE   DUR

WEB   CAM

CARTE   RESEAU

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

CA   Réalisé

Objectif   CA

Taux   Réalisation (%)

1

Magasin 1

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

2

Magasin 2

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

3

Magasin 3

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

TOTAL

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

BEGIN

      -- SET NOCOUNT ON added to prevent extra result sets from

      -- interfering with SELECT statements.

      SET NOCOUNT ON;

      IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#ChiffresAffaires]'))

            BEGIN

                  DROP TABLE #ChiffresAffaires

            END

     

      IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#ChiffresAffairesMail]'))

            BEGIN

                  DROP TABLE #ChiffresAffairesMail

            END

     

      CREATE TABLE #ChiffresAffaires

      (

      Num_Lig                                       INT IDENTITY(1,1)

      ,DateDebutExerciceComptable              DATE              NULL

      ,DateFinExerciceComptable                DATE              NULL

      ,Magasin                                       VARCHAR(17)       NULL

      ,FamilleProduit                                VARCHAR(17)       NULL

      ,MontantHT                                     NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire                  NUMERIC(24,2)     NULL DEFAULT 1

      ,TauxRealisation                         NUMERIC(24,2)     NULL

      )    

     

      CREATE TABLE #ChiffresAffairesMail

      (

      Num_Lig                                                   INT IDENTITY(1,1)

      ,DateDebutExerciceComptable                          DATE              NULL

      ,DateFinExerciceComptable                            DATE              NULL

      ,Magasin                                                   VARCHAR(17)       NULL

      ,MontantHT_ECRANS                                    NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_ECRANS                       NUMERIC(24,2)     NULL

      ,TauxRealisation_ECRANS                              NUMERIC(24,2)     NULL

      ,MontantHT_CLAVIER                                         NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_CLAVIER                      NUMERIC(24,2)     NULL

      ,TauxRealisation_CLAVIER                             NUMERIC(24,2)     NULL 

      ,MontantHT_SOURIS                                    NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_SOURIS                       NUMERIC(24,2)     NULL

      ,TauxRealisation_SOURIS                              NUMERIC(24,2)     NULL 

      ,MontantHT_CLEUSB                                    NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_CLEUSB                       NUMERIC(24,2)     NULL

      ,TauxRealisation_CLEUSB                              NUMERIC(24,2)     NULL 

      ,MontantHT_DISQUEDUR                                 NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_DISQUEDUR              NUMERIC(24,2)     NULL

      ,TauxRealisation_DISQUEDUR                           NUMERIC(24,2)     NULL 

      ,MontantHT_WEBCAM                                    NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_WEBCAM                       NUMERIC(24,2)     NULL

      ,TauxRealisation_WEBCAM                              NUMERIC(24,2)     NULL 

      ,MontantHT_CARTERESEAU                               NUMERIC(24,2)     NULL

      ,ObjectifChiffreAffaire_CARTERESEAU            NUMERIC(24,2)     NULL

      ,TauxRealisation_CARTERESEAU                   NUMERIC(24,2)     NULL

      )

           

   -- Insert statements for procedure here

      DECLARE @Annee                                      INT

                  ,@Mois                                         INT

                  ,@Annee_Debut_ExComptable          CHAR(4)

                  ,@Annee_Fin_ExComptable            CHAR(4)

                  ,@DateDebutExerciceComptableDATE

                  ,@DateFinExerciceComptable         DATE

                  ,@emailSubject                           VARCHAR(100)

                  ,@tableHTML                              NVARCHAR(MAX)

                  ,@body                                         VARCHAR(MAX)

                  ,@bodyTemp                               VARCHAR(MAX)

                  ,@recipients                             VARCHAR(MAX)

                  ,@Nbre_Lig                               INT

                  ,@Num_Lig                                INT

     

      --Calculer les date debut et fin pour l'exercice encours

      SELECT @Annee = DATEPART(YEAR,GETDATE())

      SELECT @Mois = DATEPART(MONTH,GETDATE())

      IF @Mois>=1 AND @Mois<=8

            BEGIN

                  SELECT @Annee_Debut_ExComptable = @Annee - 1  

                  SELECT @Annee_Fin_ExComptable = @Annee

            END

      ELSE

            BEGIN

                  SELECT @Annee_Debut_ExComptable = @Annee

                  SELECT @Annee_Fin_ExComptable = @Annee + 1    

            END

      SELECT @DateDebutExerciceComptable = @Annee_Debut_ExComptable + '0901'

      SELECT @DateFinExerciceComptable   = GETDATE()

      SELECT @emailSubject ='CA MagasinS - REALISATION DU ' + CONVERT(VARCHAR(10),@DateDebutExerciceComptable,103) + ' AU ' + CONVERT(VARCHAR(10),@DateFinExerciceComptable,103)

      SELECT @recipients      ='Cette adresse e-mail est protégée contre les robots spammeurs. Vous devez activer le JavaScript pour la visualiser.'

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT)

      SELECT @DateDebutExerciceComptable,@DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT FROM Ventes

      WHERE Date_Vente>=@DateDebutExerciceComptable   AND Date_Vente<=@DateFinExerciceComptable

      AND FamilleProduit IN('ECRANS','CLAVIER','SOURIS','CLEUSB','DISQUEDUR','WEBCAM','CARTERESEAU')

      GROUP BY T5.Magasin,T3.FamilleProduit

     

      --Calculer les sous totaux

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='ECRANS') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='CLAVIER') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='SOURIS') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='CLEUSB') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='DISQUEDUR') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='WEBCAM') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

      INSERT INTO #ChiffresAffaires(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,MontantHT,ObjectifChiffreAffaire      )

      SELECT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit,SUM(MontantHT) MontantHT,SUM(ObjectifChiffreAffaire) ObjectifChiffreAffaire     FROM (

      SELECT @DateDebutExerciceComptable DateDebutExerciceComptable,@DateFinExerciceComptable DateFinExerciceComptable,'TotalGeneral' Magasin, FamilleProduit, MontantHT,ObjectifChiffreAffaire FROM #ChiffresAffaires

      WHERE FamilleProduit='CARTERESEAU') T GROUP BY DateDebutExerciceComptable,DateFinExerciceComptable,Magasin,FamilleProduit

     

     

      UPDATE #ChiffresAffaires

      SET

            TauxRealisation=(MontantHT /ObjectifChiffreAffaire) * 100

      WHERE ISNULL(ObjectifChiffreAffaire,0) <> 0

     

      INSERT INTO #ChiffresAffairesMail(DateDebutExerciceComptable,DateFinExerciceComptable,Magasin)

      SELECT DISTINCT DateDebutExerciceComptable,DateFinExerciceComptable,Magasin FROM #ChiffresAffaires

      WHERE Magasin<>'TotalGeneral'

      ORDER BY Magasin

     

      UPDATE T1

      SET

            MontantHT_ECRANS = ISNULL(T2.MontantHT,0)

            ,ObjectifChiffreAffaire_ECRANS = ISNULL(T2.ObjectifChiffreAffaire,0)

            ,TauxRealisation_ECRANS = ISNULL(T2.TauxRealisation,0)          

            ,MontantHT_CLAVIER = ISNULL(T3.MontantHT,0)

            ,ObjectifChiffreAffaire_CLAVIER = ISNULL(T3.ObjectifChiffreAffaire,0)

            ,TauxRealisation_CLAVIER = ISNULL(T3.TauxRealisation,0)

            ,MontantHT_SOURIS = ISNULL(T4.MontantHT,0)

            ,ObjectifChiffreAffaire_SOURIS = ISNULL(T4.ObjectifChiffreAffaire,0)

            ,TauxRealisation_SOURIS = ISNULL(T4.TauxRealisation,0)

            ,MontantHT_CLEUSB = ISNULL(T5.MontantHT,0)

            ,ObjectifChiffreAffaire_CLEUSB = ISNULL(T5.ObjectifChiffreAffaire,0)

            ,TauxRealisation_CLEUSB = ISNULL(T5.TauxRealisation,0)

            ,MontantHT_DISQUEDUR = ISNULL(T6.MontantHT,0)

            ,ObjectifChiffreAffaire_DISQUEDUR = ISNULL(T6.ObjectifChiffreAffaire,0)

            ,TauxRealisation_DISQUEDUR = ISNULL(T6.TauxRealisation,0)       

            ,MontantHT_WEBCAM = ISNULL(T7.MontantHT,0)

            ,ObjectifChiffreAffaire_WEBCAM = ISNULL(T7.ObjectifChiffreAffaire,0)

            ,TauxRealisation_WEBCAM = ISNULL(T7.TauxRealisation,0)          

            ,MontantHT_CARTERESEAU = ISNULL(T8.MontantHT,0)

            ,ObjectifChiffreAffaire_CARTERESEAU = ISNULL(T8.ObjectifChiffreAffaire,0)

            ,TauxRealisation_CARTERESEAU = ISNULL(T8.TauxRealisation,0)                      

      FROM #ChiffresAffairesMail T1

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='ECRANS') T2 ON T2.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='CLAVIER') T3 ON T3.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='SOURIS') T4 ON T4.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='CLEUSB') T5 ON T5.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='DISQUEDUR') T6 ON T6.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='WEBCAM') T7 ON T7.Magasin=T1.Magasin

      LEFT JOIN(SELECT Magasin,MontantHT,ObjectifChiffreAffaire,TauxRealisation FROM #ChiffresAffaires WHERE FamilleProduit='CARTERESEAU') T8 ON T8.Magasin=T1.Magasin

     

      IF EXISTS(SELECT Magasin FROM #ChiffresAffaires)

            BEGIN

                                              

                  SET @tableHTML = N'<H><font size="4">Bonjour</font><BR></BR><BR></BR></H>' +

                     N'<H><font size="4">Veuillez trouver ci-dessous,la synthèse du CA pour les différentes Magasins réalisé sur la période du ' + CONVERT(VARCHAR(10),@DateDebutExerciceComptable,103) + ' au ' + CONVERT(VARCHAR(10),@DateFinExerciceComptable,103) + ' </font><BR></BR></H>' +

                     N'<html><head><style>' +

                     N'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;} ' +

                     N'</style></head><body>' +

                     N'<div style="margin-top:20px; margin-left:5px; margin-bottom:15px; font-weight:bold; font-size:1.3em; font-family:calibri;">' +

                     N'<div style="margin-left:50px; font-family:Calibri;">' +

                     N'<style>.MonTableau tbody tr:nth-child(odd) {background-color: #999690;color:#ffffff;}

                     .MonTableau tbody tr:nth-child(even) {background-color: #999690;color:#ffffff;}</style>' +

                     N'<table cellpadding=0 cellspacing=0 border=0 class="MonTableau">' +

                     N'<tr bgcolor=PaleTurquoise>' +

                     N'<td align=center colspan="23"><font face="calibri" color=blak size=5><b>REALISATION CA DU ' + CONVERT(VARCHAR(10),@DateDebutExerciceComptable,103) + ' AU ' + CONVERT(VARCHAR(10),@DateFinExerciceComptable,103) + '</b></font></td></tr>' +

                     N'<tr bgcolor=#4b6c9e>' +

                     N'<td align=center rowspan="2"><font face="calibri" color=White><b>N° Ligne</b></font></td>' +  

                     N'<td align=center rowspan="2"><font face="calibri" color=White><b>Magasin</b></font></td>' +  

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>ECRANS</b></font></td>' +

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>CLAVIER</b></font></td>' +

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>SOURIS</b></font></td>' +

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>CLE USB</b></font></td>' +

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>DISQUE DUR</b></font></td>' +                 

                  N'<td align=center colspan="3"><font face="calibri" color=White><b>WEB CAM</b></font></td>' +

                     N'<td align=center colspan="3"><font face="calibri" color=White><b>CARTE RESEAU</b></font></td></tr>' +                

                     N'<tr bgcolor=#4b6c9e><td style = "width:500px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:500px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:500px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +    

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td>' +             

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>CA Réalisé</b></font></td>'   +

                     N'<td style = "width:150px" align=center><font face="calibri" color=White><b>Objectif CA</b></font></td>'   +

                     N'<td style = "width:315px" align=center><font face="calibri" color=White><b>Taux Réalisation (%)</b></font></td></tr>'

     

                 

                  SELECT @Nbre_Lig=COUNT(Num_Lig) FROM #ChiffresAffairesMail

                  SELECT @Num_Lig =1

                 

                  WHILE @Num_Lig <= @Nbre_Lig

                        BEGIN

                 

                             SELECT @bodyTemp =     

                             (  

                             SELECT td_center= @Num_Lig ,

                             td = Magasin,

                             td_right = dbo.ufs_MontantFormat(MontantHT_ECRANS)                     ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_ECRANS)                        ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_ECRANS)               ,

                             td_right = dbo.ufs_MontantFormat(MontantHT_CLAVIER)              ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_CLAVIER)                  ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_CLAVIER)        ,    

                             td_right = dbo.ufs_MontantFormat(MontantHT_SOURIS)                           ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_SOURIS)                        ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_SOURIS)                     ,    

                             td_right = dbo.ufs_MontantFormat(MontantHT_CLEUSB)               ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_CLEUSB)             ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_CLEUSB)         ,    

                             td_right = dbo.ufs_MontantFormat(MontantHT_DISQUEDUR)                        ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_DISQUEDUR)                           ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_DISQUEDUR)                  ,    

                             td_right = dbo.ufs_MontantFormat(MontantHT_WEBCAM)               ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_WEBCAM)             ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_WEBCAM)         ,    

                             td_right = dbo.ufs_MontantFormat(MontantHT_CARTERESEAU)                      ,

                             td_right = dbo.ufs_MontantFormat(ObjectifChiffreAffaire_CARTERESEAU)                         ,

                             td_right = dbo.ufs_MontantFormat(TauxRealisation_CARTERESEAU)                    

                             FROM #ChiffresAffairesMail WHERE Num_Lig=@Num_Lig

                             FOR XML RAW('tr'), ELEMENTS

                             )

                            

                                  

                             IF @Num_Lig % 2 = 0

                                   BEGIN

                                         SET @bodyTemp = REPLACE(@bodyTemp, '<tr>', '<tr style="background-color:LemonChiffon;color:blue;">')

                                   END

                             ELSE

                                   BEGIN

                                         SET @bodyTemp = REPLACE(@bodyTemp, '<tr>', '<tr style="background-color:white;color:blue;">')

                                   END

                            

                             SELECT @body = ISNULL(@body,'') + @bodyTemp

                            

                             SELECT @Num_Lig = @Num_Lig + 1

                        END

                 

     

                  --Insertion des lignes des sous totaux  

                  SELECT @bodyTemp =     

                  (  

                  SELECT td_center= '' ,

                  td = 'TOTAL',

                  td_right = dbo.ufs_MontantFormat(T1.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T1.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T1.TauxRealisation)       ,

                  td_right = dbo.ufs_MontantFormat(T2.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T2.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T2.TauxRealisation)       ,    

                  td_right = dbo.ufs_MontantFormat(T3.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T3.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T3.TauxRealisation)       ,    

                  td_right = dbo.ufs_MontantFormat(T4.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T4.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T4.TauxRealisation)       ,    

                  td_right = dbo.ufs_MontantFormat(T5.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T5.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T5.TauxRealisation)       ,    

                  td_right = dbo.ufs_MontantFormat(T6.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T6.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T6.TauxRealisation)       ,    

                  td_right = dbo.ufs_MontantFormat(T7.MontantHT)             ,

                  td_right = dbo.ufs_MontantFormat(T7.ObjectifChiffreAffaire)                  ,

                  td_right = dbo.ufs_MontantFormat(T7.TauxRealisation)                  

                  FROM (SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='ECRANS') T1

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='CLAVIER') T2 ON T2.Magasin=T1.Magasin

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='SOURIS') T3 ON T3.Magasin=T1.Magasin

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='CLEUSB') T4 ON T4.Magasin=T1.Magasin

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='DISQUEDUR') T5 ON T5.Magasin=T1.Magasin

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='WEBCAM') T6 ON T6.Magasin=T1.Magasin

                  FULL JOIN(SELECT Magasin,MontantHT ,ObjectifChiffreAffaire ,TauxRealisation FROM #ChiffresAffaires WHERE Magasin='TotalGeneral' AND FamilleProduit='CARTERESEAU') T7 ON T7.Magasin=T1.Magasin

                  FOR XML RAW('tr'), ELEMENTS

                             )

                  SET @bodyTemp = REPLACE(@bodyTemp, '<tr>', '<b><tr style="background-color:red;color:blue;">')

                  SET @bodyTemp = REPLACE(@bodyTemp, '</tr>', '</b></tr>')

                  SET @bodyTemp = REPLACE(@bodyTemp, '<td_center></td_center>', '')

                  SET @bodyTemp = REPLACE(@bodyTemp, '<td>TOTAL</td>','<td align=right colspan="2">TOTAL</td>')    

                  SELECT @body = ISNULL(@body,'') + @bodyTemp

                  --Fin Insertion des lignes des sous totaux    

                 

                  SET @body = REPLACE(@body, '<td_right>', '<td align=right><font face="calibri">')

                  SET @body = REPLACE(@body, '</td_right>', '</td>')

     

                  SET @body = REPLACE(@body, '<td>', '<td align=left><font face="calibri">')

                  SET @body = REPLACE(@body, '</td>', '</font></td>')

                 

                  SET @body = REPLACE(@body, '<td_center>', '<td align=center><font face="calibri">')

                  SET @body = REPLACE(@body, '</td_center>', '</td>')

                 

                  SET @tableHTML = @tableHTML + @body + '</table></div></body></html>'

                  SET @tableHTML = @tableHTML + N'<H><BR></BR></H>'

                  SET @tableHTML = @tableHTML + N'<H><font size="4">Cordialement,</font><BR></BR><BR></BR></H>'

                  SET @tableHTML = @tableHTML + N'<H><font size="4">Mektaba.info</font></H>'

                  EXEC msdb.dbo.sp_send_dbmail

                     @profile_name = 'MonProfilMail',

                     @recipients = @recipients,

                     @body = @tableHTML,

                     @subject = @emailSubject,

                     @body_format = 'HTML'

                 

            END

DROP TABLE #ChiffresAffaires

END

-- =============================================

-- Author:       Zouhaier KHARROUBI

-- Create date:   13/04/2017

-- Description:   Formater les montants en rajoutant des épaces entre chaque trois chiffres

-- Exemple SELECT [dbo].[ufs_MontantFormat]('457892567.25') => Resultat : 457 892 567.25

-- =============================================

CREATE FUNCTION [dbo].[ufs_MontantFormat]

(

   @Montant NVARCHAR(25)

)

RETURNS NVARCHAR(26)

AS

BEGIN

DECLARE @Retour AS NVARCHAR(25)

SET @Retour= REPLACE(CONVERT(VARCHAR(26),CONVERT(MONEY,@Montant),1),',',SPACE(1))

RETURN @Retour

          

END