SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform].[Aftersales_Rechnungen_verk_AW_final] AS

select T2.[Hauptbetrieb] as Hauptbetrieb_imr,
        T2.[Standort] as Standort_imr,
       "GC_Department"."Hauptbetrieb_ID"                                                     AS "Hauptbetrieb_ID",
       "GC_Department"."Hauptbetrieb_Name"                                                   AS "Hauptbetrieb_Name",
       "GC_Department"."Standort_ID"                                                         AS "Standort_ID",
       "GC_Department"."Standort_Name"                                                       AS "Standort_Name",
       'Serviceberater'                                                                      AS "Employee_Function",
       "T2"."Serviceberater"                                                                 AS "Serviceberater",
       "T2"."Order Number" + ' - ' + CONVERT(VARCHAR(10), "T2"."Invoice Date", 121)          AS "Order_Number",
       ''                                                                                    AS "Invoice_Desc_100",
       T2.[Fabrikat] as Fabrikat_imr,
       case when "GC_Marken"."Fabrikat" is null then 'Fremd' else "GC_Marken"."Fabrikat" end AS "Fabrikat",
       "T2"."Model"                                                                          AS "Model",
       "T2"."Fahrzeug"                                                                       AS "Fahrzeug",
       "T2"."Kostenstelle"                                                                   AS "Kostenstelle",
       "T2"."Kostenstelle"                                                                   AS "Produktbuchungsgruppe_imr",
       "T2"."Marke"                                                                          AS "Marke",
       "T2"."Umsatzart"                                                                      AS "Umsatzart",
       "T2"."Auftragsart"                                                                    AS "Auftragsart",
       ''                                                                                    AS "Auftragsposition",
       ''                                                                                    AS "Kundenart",
       "T2"."Kunde"                                                                          AS "Kunde",
       "T2"."Order Number" + ' - ' + CONVERT(VARCHAR(10), "T2"."Invoice Date", 121)          AS "Order_Desc_30",
       ''                                                                                    AS "Invoice_Desc_30",
       "GC_Produktbuchungsgruppen"."Zuordnung"                                               AS "Zuordnung_Produktbuchungsgruppe",
       "T2"."Kostenstelle"                                                                   AS "Produktbuchungsgruppe",
       ''                                                                                    AS "FZG_Altersstaffel",
       ''                                                                                    AS "Repair_Group_Desc",
       ''                                                                                    AS "DB1____EK",
       ''                                                                                    AS "Rechnung_Gutschrift",
       ''                                                                                    AS "Parts_Focus_Group",
       ''                                                                                    AS "Parts_Make_Desc",
       ''                                                                                    AS "Parts_Group_Desc",
       left("T2"."Geschäftsart", 1)                                                          AS "PLZ_1_Stelle",
       left("T2"."Geschäftsart", 2)                                                          AS "PLZ_2_Stelle",
       left("T2"."Geschäftsart", 3)                                                          AS "PLZ_3_Stelle",
       left("T2"."Geschäftsart", 4)                                                          AS "PLZ_4_Stelle",
       "T2"."Geschäftsart"                                                                   AS "PLZ",
       'Serviceberater'                                                                      AS "Zuordnung_Funktion",
       case
           when datediff(dd, "T2"."Invoice Date", getdate()) <
                100 then "T2"."Order Number" + ' - ' + CONVERT(VARCHAR(10), "T2"."Invoice Date", 121)
           else N'älter 100 Tage' end                                                         AS "Order_Desc_100",
       0.0                                                                                   AS "Lohn",
       0.0                                                                                   AS "Teile",
       0.0                                                                                   AS "EW_Teile",
       0.0                                                                                   AS "Sonst_",
       0.0                                                                                   AS "TÜV",
       0.0                                                                                   AS "Fremdl_",
       0.0                                                                                   AS "Mietw_",
       0.0                                                                                   AS "Rädereinl_",
       "T2"."verk. Std."                                                                     AS "verk__Std_",
       0.0                                                                                   AS "ben__Std_",
       0.0                                                                                   AS "NL_Lohn",
       0.0                                                                                   AS "NL_Teile",
       0.0                                                                                   AS "NL_Sonst_",
       0.0                                                                                   AS "DG",
       0.0                                                                                   AS "NL_Sonst__",
       0.0                                                                                   AS "Anz__AT",
       0.0                                                                                   AS "Anwesenheit_Mech_Karo_Lack",
       0.0                                                                                   AS "Anwesenheit_Meister",
       "T2"."Invoice Date"                                                                   AS "Invoice_Date",
       "T2"."Order Number" + ' - ' + CONVERT(VARCHAR(10), "T2"."Invoice Date", 121)          AS "Rechnungsausgang",
       case
           when case when "GC_Marken"."Fabrikat" is null then 'Fremd' else "GC_Marken"."Fabrikat" end = 'Fremd' then 9
           else "GC_Marken"."Order_By" end                                                   AS "Fabrikat_Order_By",
       "T2"."Kundenart"                                                                      AS "Serviceberater_Rg_Steller"
from transform_imr."Aftersales_Rechnungen_verk_AW_final" "T2" 
    LEFT OUTER JOIN "data"."GC_Department" "GC_Department" on "T2"."Hauptbetrieb" = "GC_Department"."Hauptbetrieb" and "T2"."Standort" = "GC_Department"."Standort"
    LEFT OUTER JOIN "data"."GC_Marken" "GC_Marken" on "T2"."Fabrikat" = "GC_Marken"."Description"
    LEFT OUTER JOIN "data"."GC_Produktbuchungsgruppen" "GC_Produktbuchungsgruppen"  on "T2"."Kostenstelle" = "GC_Produktbuchungsgruppen"."Produktbuchungsgruppe"
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
