/*<VERSION_MINIMUM>10.31aa</VERSION_MINIMUM>*/
delete from <PREFIXE_DW>SIGNATURES_ATTENDUES  

insert into <PREFIXE_DW>SIGNATURES_ATTENDUES (SA_TYPE_TRANSACTION,SA_CODE_SIGNATAIRE,SA_LIBELLE_SIGNATAIRE,SA_NUMERO_DOCUMENT,SA_ORDRE,SA_SIGNABLE,SA_ROLE,SA_DATE_MISE_EN_SIGNATURE,SA_BON_A_SIGNER,SA_INFORME,SA_NUMERO_ROLE,SA_CODE_FOURNISSEUR,SA_RAISON_SOCIALE_FOURNISSEUR,SA_MONTANT ,SA_CODE_ACHETEUR,SA_NOM_ACHETEUR,SA_TYPE_DOCUMENT)
select  'FAC',SIGNAT,isnull(NOM,' '),NO_FAC,ORDRE, case when (SIGNABLE=0x54) then 1 else 0 end ,FAROLES,CASE WHEN (DTE_MISE = ' ' OR DTE_MISE = '00000000' ) THEN CONVERT (datetime,'19000101' ,103) WHEN (DTE_MISE < '17530101') THEN CONVERT (datetime,'19000101' ,103) ELSE CONVERT (datetime,DTE_MISE ,103) END,case when (BAP_BAS=0x54) then 1 else 0 end,case when (INFORME=0x54) then 1 else 0 end,0,isnull(DOC_CODE_FOURNISSEUR,''),isnull(fou.FRS_RAISON_SOCIALE,' ') ,isnull(fac.MONTANT,0),isnull(fac.DOC_CODE_ACHETEUR,' '),isnull(fac.ACH_NOM_ACHETEUR,' '),isnull(fac.DOC_TYPE_FAC,' ')
from <PREFIXE_HA>SIG_BAP
left outer join <PREFIXE_HA>HASTUTIL on SIGNAT=CODE
left outer join <PREFIXE_DW>FAC_ENTETES fac on NO_FAC=DOC_NUMERO_FAC
left outer join <PREFIXE_DW>FOURNISSEURS fou on fac.DOC_CODE_FOURNISSEUR=fou.FRS_CODE_FOURNISSEUR
union 
select 'CA',SIGNAT,isnull(NOM,' '),NO_CDE,ORDRE,case when (SIGNABLE=0x54) then 1 else 0 end,CAROLES,CASE WHEN (DTE_MISE = ' ' OR DTE_MISE = '00000000' ) THEN CONVERT (datetime,'19000101' ,103) WHEN (DTE_MISE < '17530101') THEN CONVERT (datetime,'19000101' ,103) ELSE CONVERT (datetime,DTE_MISE ,103) END ,case when (BON_SIGNE=0x54) then 1 else 0 end,case when (INFORME=0x54) then 1 else 0 end,ID_ROLE,isnull(DOC_CODE_FOURNISSEUR,''),isnull(fou.FRS_RAISON_SOCIALE,' ') ,isnull(cde.MONTANT,0),isnull(cde.DOC_CODE_ACHETEUR,' '),isnull(cde.ACH_NOM_ACHETEUR,' '),isnull(cde.DOC_TYPE_DOCUMENT_CDE,' ')
from <PREFIXE_HA>SIG_CDE
left outer join <PREFIXE_HA>HASTUTIL on SIGNAT=CODE
left outer join <PREFIXE_DW>CA_ENTETES cde on NO_CDE=DOC_NUMERO_CDE
left outer join <PREFIXE_DW>FOURNISSEURS fou on cde.DOC_CODE_FOURNISSEUR=fou.FRS_CODE_FOURNISSEUR
where NO_CDE<>0
union
select 'DA',SIGNAT,isnull(NOM,' '),NO_DA,ORDRE,case when (SIGNABLE=0x54) then 1 else 0 end,DAROLES ,CASE WHEN (DTE_MISE = ' ' OR DTE_MISE = '00000000' ) THEN CONVERT (datetime,'19000101' ,103) WHEN (DTE_MISE < '17530101') THEN CONVERT (datetime,'19000101' ,103) ELSE CONVERT (datetime,DTE_MISE ,103) END ,0,case when (INFORME=0x54) then 1 else 0 end,ID_ROLE,isnull(DOC_CODE_FOURNISSEUR,''),isnull(fou.FRS_RAISON_SOCIALE,' '),isnull(da.MONTANT,0),isnull(da.DOC_CODE_ACHETEUR,' '),isnull(da.ACH_NOM_ACHETEUR,' '),isnull(da.DOC_TYPE_DOCUMENT_DA,' ')
from <PREFIXE_HA>SIG_DA
left outer join <PREFIXE_HA>HASTUTIL on SIGNAT=CODE
left outer join <PREFIXE_DW>DA_ENTETES da on NO_DA=DOC_NUMERO_DA
left outer join <PREFIXE_DW>FOURNISSEURS fou on da.DOC_CODE_FOURNISSEUR=fou.FRS_CODE_FOURNISSEUR