Browse Source

Queries - aktueller Stand

Reisacher C7 11 months ago
parent
commit
f10fc9a3a8

+ 4 - 0
sql/import/fm_aha_import.sql

@@ -49,9 +49,11 @@ CASE
   WHEN CLE."Branch Code" = 'GÖG'  THEN ('60') 
   WHEN CLE."Branch Code" = 'LEH' THEN ('70') 
   WHEN CLE."Branch Code" = 'WTB' THEN ('80') 
+  WHEN CLE."Branch Code" = 'AAM' THEN ('60') 
   ELSE 'n.v.' END as filiale_nummer,
 
 case
+  WHEN CLE."Document No_" IN ('VRGF206001957') THEN 'NA'
   when (not CLE."Main Area" IN (1,2,3)
 		and not left(CLE."Document No_",2) IN ('AL','BF','E4','HV','KA','LB','LO','LP','LQ','LR','RB','SP','UM')
 		and not CLE."Document No_" LIKE '166%'
@@ -108,6 +110,8 @@ left join "DMS1"."dbo"."AH Augsburg$Employee" E on ASH."Service Advisor No_" = E
 where CLE."Remaining Amount" <> 0 
   and CLE."Journal Batch Name" <> 'ERSTIMPORT' 
   and not C."No_" LIKE '600%' 
+  and CLE."Document No_" NOT IN ('VRGF206000366','VRGF206000656', 'WRG20609530', 'VRGF236004511')
+
   
   and (
   

+ 2 - 1
sql/import/fm_aha_report.sql

@@ -10,7 +10,7 @@ document_number as O500_BELEGNR1,
 entry_number as Reserve_1, 
 transact_sum as REBEB, 
 transact_outstanding as O500_HW2BETR_OFFEN, 
-transact_outstanding as O500_HW2BUCHGBETR, 
+transact_sum as O500_HW2BUCHGBETR, 
 customer_number as KNDNR,  
 concat(customer_number,' - ', replace(replace(customer_name, "  ", " "), "'", "´")) as KNAME, 
 customer_phone as KTELF, 
@@ -22,6 +22,7 @@ salesman_number as KDBERA,
 salesman_name as KDBNAME, 
 verkaeufer as Stufe5, 
 reminder_level as Reserve_2,
+case when transact_sum > 0 then 'R' else 'G' end as Reserve_3,
 mahnstufe as O500_MAHNSTUFE, 
 mahnstufe2 as Next_MAHNSTUFE, 
 branch_code as Stufe3, 

+ 3 - 2
sql/import/fm_ahr_import.sql

@@ -11,7 +11,7 @@ CLE."Amount" as transact_sum,
 CLE."Remaining Amount" as transact_outstanding,
 
 C."No_" as customer_number,
-C."Name" as customer_name,
+case when C."No_" = '036201' then 'Alphabet' else C."Name" end as customer_name,
 C."Phone No_" as customer_phone,
 C."Address" as customer_address,
 C."Post Code" as customer_zipcode,
@@ -92,6 +92,7 @@ CASE
   WHEN CLE."Branch Code" = 'KRU' THEN ('30') 
   WHEN CLE."Branch Code" = 'ULM' THEN ('40') 
   WHEN CLE."Branch Code" = 'LL'  THEN ('50')
+  WHEN CLE."Branch Code" = 'GZ'  THEN ('55')
   WHEN substring(CLE."Document No_" ,8,1) = '1' THEN ('10') 
   WHEN substring(CLE."Document No_" ,8,1) = '2' THEN ('20') 
   WHEN substring(CLE."Document No_" ,8,1) = '3' THEN ('30') 
@@ -156,7 +157,7 @@ left join "DMS1"."dbo"."AH Reisacher$Employee" E on ASH."Service Advisor No_" =
 where CLE."Remaining Amount" <> 0 
   and CLE."Journal Batch Name" <> 'ERSTIMPORT' 
   and not C."No_" LIKE '600%' 
-  
+  and CLE."Document No_" not in ('VRGF201000476', 'VGGF234000024')
   and (
   
   ((CLE."Document No_" LIKE 'VRGGFZ%' 

+ 2 - 1
sql/import/fm_ahr_report.sql

@@ -10,7 +10,7 @@ document_number as O500_BELEGNR1,
 entry_number as Reserve_1, 
 transact_sum as REBEB, 
 transact_outstanding as O500_HW2BETR_OFFEN, 
-transact_outstanding as O500_HW2BUCHGBETR, 
+transact_sum as O500_HW2BUCHGBETR, 
 customer_number as KNDNR,  
 concat(customer_number,' - ', replace(replace(customer_name, "  ", " "), "'", "´")) as KNAME, 
 customer_phone as KTELF, 
@@ -22,6 +22,7 @@ salesman_number as KDBERA,
 salesman_name as KDBNAME, 
 verkaeufer as Stufe5, 
 reminder_level as Reserve_2,
+case when transact_sum > 0 then 'R' else 'G' end as Reserve_3,
 mahnstufe as O500_MAHNSTUFE, 
 mahnstufe2 as Next_MAHNSTUFE, 
 branch_code as Stufe3, 

+ 282 - 0
sql/import/fm_rem_import.sql

@@ -0,0 +1,282 @@
+SELECT DISTINCT CLE."VIN" AS chassis_number,
+	CLE."Make Code" AS make_code,
+	CLE."Posting Date" AS posting_date,
+	CLE."Due Date" AS due_date,
+	C."No_" AS account_number,
+	CLE."Document No_" AS document_number,
+	CLE."Entry No_" AS entry_number,
+	CLE."Amount" AS transact_sum,
+	CLE."Remaining Amount" AS transact_outstanding,
+	C."No_" AS customer_number,
+	CASE 
+		WHEN C."No_" = '036201'
+			THEN 'Alphabet'
+		ELSE C."Name"
+		END AS customer_name,
+	C."Phone No_" AS customer_phone,
+	C."Address" AS customer_address,
+	C."Post Code" AS customer_zipcode,
+	C."City" AS customer_city,
+	CLE."Salesperson Code" AS salesman_number,
+	SP."Name" AS salesman_name,
+	CASE 
+		WHEN CLE."Salesperson Code" = '104'
+			THEN 'cab'
+		WHEN CLE."Salesperson Code" = '106'
+			THEN 'bv'
+		WHEN CLE."Salesperson Code" = '109'
+			THEN 'fd'
+		WHEN CLE."Salesperson Code" = '111'
+			THEN 'aho'
+		WHEN CLE."Salesperson Code" = '133'
+			THEN 'mk'
+		WHEN CLE."Salesperson Code" = '141'
+			THEN 'ka'
+		WHEN CLE."Salesperson Code" = '142'
+			THEN 'fl'
+		WHEN CLE."Salesperson Code" = '143'
+			THEN 'tb'
+		WHEN CLE."Salesperson Code" = '144'
+			THEN 'gr'
+		WHEN CLE."Salesperson Code" = '145'
+			THEN 'kgu'
+		WHEN CLE."Salesperson Code" = '148'
+			THEN 'hbo'
+		WHEN CLE."Salesperson Code" = '149'
+			THEN 'sbe'
+		WHEN CLE."Salesperson Code" = '152'
+			THEN 'hp'
+		WHEN CLE."Salesperson Code" = '153'
+			THEN 'rhe'
+		WHEN CLE."Salesperson Code" = '154'
+			THEN 'fja'
+		WHEN CLE."Salesperson Code" = '302'
+			THEN 'jja'
+		WHEN CLE."Salesperson Code" = '303'
+			THEN 'twa'
+		WHEN CLE."Salesperson Code" = '304'
+			THEN 'sdo'
+		WHEN CLE."Salesperson Code" = '308'
+			THEN 'tbe'
+		WHEN CLE."Salesperson Code" = '309'
+			THEN 'ba'
+		WHEN CLE."Salesperson Code" = '332'
+			THEN 'kkl'
+		WHEN CLE."Salesperson Code" = '341'
+			THEN 'af'
+		WHEN CLE."Salesperson Code" = '342'
+			THEN 'vpo'
+		WHEN CLE."Salesperson Code" = '343'
+			THEN 'mku'
+		WHEN CLE."Salesperson Code" = '344'
+			THEN 'rwo'
+		WHEN CLE."Salesperson Code" = '345'
+			THEN 'osa'
+		WHEN CLE."Salesperson Code" = '351'
+			THEN 'ipl'
+		WHEN CLE."Salesperson Code" = '353'
+			THEN 'sse'
+		WHEN CLE."Salesperson Code" = '354'
+			THEN 'rze'
+		WHEN CLE."Salesperson Code" = '550'
+			THEN 'aan'
+		WHEN CLE."Salesperson Code" = '811'
+			THEN 'tal'
+		WHEN CLE."Salesperson Code" = '820'
+			THEN 'wr'
+		WHEN CLE."Salesperson Code" = '821'
+			THEN 'mma'
+		WHEN CLE."Salesperson Code" = '822'
+			THEN 'mme'
+		ELSE ''
+		END AS salesman_user,
+	CASE 
+		WHEN E."Name" <> ''
+			THEN ASH."Service Advisor No_" + ' - ' + E."Name"
+		ELSE CLE."Salesperson Code" + ' - ' + SP."Name"
+		END AS verkaeufer,
+	RCE."Reminder Level" AS reminder_level,
+	CASE 
+		WHEN RCE."Reminder Level" = '0'
+			THEN 'Mahnst. 0: Kontoauszug vor Mahnung'
+		WHEN RCE."Reminder Level" = '1'
+			THEN 'Mahnst. 1: Zahlungserinnerung'
+		WHEN RCE."Reminder Level" = '2'
+			THEN 'Mahnst. 2: RA-Androhung'
+		WHEN RCE."Reminder Level" = '3'
+			THEN 'Mahnst. M: Rechtsanwalt'
+		ELSE NULL
+		END AS mahnstufe,
+	CASE 
+		WHEN RCE."Reminder Level" = '0'
+			THEN 'Mahnst. 1: Zahlungserinnerung'
+		WHEN RCE."Reminder Level" = '1'
+			THEN 'Mahnst. 2: RA-Androhung'
+		WHEN RCE."Reminder Level" = '2'
+			THEN 'Mahnst. M: Rechtsanwalt'
+		WHEN RCE."Reminder Level" = '3'
+			THEN '-'
+		ELSE NULL
+		END AS mahnstufe2,
+	CASE 
+		WHEN CLE."Branch Code" <> ''
+			THEN CLE."Branch Code"
+		WHEN substring(CLE."Document No_", 8, 1) = '1'
+			THEN ('MM')
+		WHEN substring(CLE."Document No_", 8, 1) = '2'
+			THEN ('VÖH')
+		WHEN substring(CLE."Document No_", 8, 1) = '3'
+			THEN ('KRU')
+		WHEN substring(CLE."Document No_", 8, 1) = '4'
+			THEN ('ULM')
+		WHEN substring(CLE."Document No_", 8, 1) = '5'
+			THEN ('LL')
+		ELSE NULL
+		END AS branch_code,
+	CASE 
+		WHEN CLE."Branch Code" = 'MM'
+			THEN ('10')
+		WHEN CLE."Branch Code" = 'VÖH'
+			THEN ('20')
+		WHEN CLE."Branch Code" = 'KRU'
+			THEN ('30')
+		WHEN CLE."Branch Code" = 'ULM'
+			THEN ('40')
+		WHEN CLE."Branch Code" = 'LL'
+			THEN ('50')
+		WHEN CLE."Branch Code" = 'GZ'
+			THEN ('55')
+		WHEN substring(CLE."Document No_", 8, 1) = '1'
+			THEN ('10')
+		WHEN substring(CLE."Document No_", 8, 1) = '2'
+			THEN ('20')
+		WHEN substring(CLE."Document No_", 8, 1) = '3'
+			THEN ('30')
+		WHEN substring(CLE."Document No_", 8, 1) = '4'
+			THEN ('40')
+		WHEN substring(CLE."Document No_", 8, 1) = '5'
+			THEN ('50')
+		ELSE 'n.v.'
+		END AS filiale_nummer,
+	CASE 
+		WHEN (
+				NOT CLE."Main Area" IN (1, 2, 3)
+				AND NOT left(CLE."Document No_", 2) IN ('AL', 'BF', 'E4', 'HV', 'KA', 'LB', 'LO', 'LP', 'LQ', 'LR', 'RB', 'SP', 'UM')
+				AND NOT CLE."Document No_" LIKE '166%'
+				AND NOT CLE."Document No_" LIKE '169%'
+				AND NOT CLE."Document No_" LIKE 'UB0%'
+				)
+			THEN (
+					CASE 
+						WHEN C."No_" IN ('690101', '690102', '690103', '690104', '690105')
+							THEN ('RA')
+						WHEN left(CLE."Document No_", 3) IN ('D12', 'D13', 'D14', 'D15', 'D16', 'D23', 'D24', 'D25', 'D32', 'D34', 'D35', 'D43', 'D44', 'D45', 'D51', 'D52', 'D54', 'D55')
+							THEN ('SC')
+						WHEN left(CLE."Document No_", 3) IN ('D17', 'D18', 'D26', 'D27', 'D36', 'D46', 'D47', 'D56', 'D57')
+							THEN ('TZ')
+						WHEN right(CLE."Document No_", 2) = 'NA'
+							THEN ('NA')
+						WHEN right(CLE."Document No_", 2) = 'GA'
+							THEN ('GA')
+						ELSE ('n.v.')
+						END
+					)
+		WHEN (
+				(
+					CLE."Document No_" LIKE 'VRGGFZ%'
+					OR CLE."Document No_" LIKE 'VGUGFZ%'
+					OR CLE."Document No_" LIKE 'VRGF%'
+					OR CLE."Document No_" LIKE 'VRF%'
+					OR CLE."Document No_" LIKE 'VGGF%'
+					)
+				AND VLE."Entry Type" = 1
+				)
+			THEN (
+					CASE 
+						WHEN substring(VLE."Department Code", 3, 1) = '1'
+							THEN ('NA')
+						WHEN substring(VLE."Department Code", 3, 1) = '2'
+							THEN ('GA')
+						WHEN substring(CLE."Department Code", 3, 1) = '4'
+							THEN ('SC')
+						WHEN substring(CLE."Department Code", 3, 1) = '5'
+							THEN ('TZ')
+						ELSE ('NA/GA')
+						END
+					)
+		ELSE CASE 
+				WHEN (CLE."Main Area" = 3)
+					THEN ('SC')
+				WHEN (CLE."Main Area" = 1)
+					THEN ('TZ')
+				WHEN (CLE."Main Area" = 2)
+					THEN ('NA/GA')
+				ELSE 'SC/TZ'
+				END
+		END AS kostenstelle_text,
+	CASE 
+		WHEN V."Date of Sales Shipment" > '2000-01-01'
+			THEN V."Date of Sales Shipment"
+		ELSE NULL
+		END AS shipment_date,
+	CASE 
+		WHEN C."No_" <> C2."No_"
+			THEN C2."No_"
+		ELSE ''
+		END AS customer_number_leasing,
+	CASE 
+		WHEN C."No_" <> C2."No_"
+			THEN C2."Name"
+		ELSE ''
+		END AS customer_name_leasing,
+	PSDT1."Text" AS text1,
+	PSDT2."Text" AS text2
+FROM "DMS1"."dbo"."Reisacher Electric Mobility$Customer Ledger Entry" AS CLE
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Customer" AS C ON C."No_" = CLE."Customer No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Department" AS D ON CLE."Department Code" = D."Code"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Salesperson_Purchaser" AS SP ON CLE."Salesperson Code" = SP."Code"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Reminder_Fin_ Charge Entry" AS RCE ON CLE."Entry No_" = RCE."Customer Entry No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Vehicle Ledger Entry" AS VLE ON CLE."Document No_" = VLE."Document No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Vehicle" AS V ON CLE."VIN" = V."VIN"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Customer" AS C2 ON V."Customer No_" = C2."No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Posted Sales Document Text" AS PSDT1 ON CLE."Document No_" = PSDT1."Document No_"
+	AND PSDT1."Line No_" = 10000
+	AND PSDT1."Header_Footer" = 1
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Posted Sales Document Text" AS PSDT2 ON CLE."Document No_" = PSDT2."Document No_"
+	AND PSDT2."Line No_" = 20000
+	AND PSDT2."Header_Footer" = 1
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Service Ledger Entry" SLE ON CLE."Document No_" = SLE."Document No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Archived Service Header" ASH ON SLE."Order No_" = ASH."No_"
+LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Employee" E ON ASH."Service Advisor No_" = E."No_"
+WHERE CLE."Remaining Amount" <> 0
+	AND CLE."Journal Batch Name" <> 'ERSTIMPORT'
+	AND NOT C."No_" LIKE '600%'
+	AND CLE."Document No_" NOT IN ('VRGF201000476', 'VGGF234000024')
+	AND (
+		(
+			(
+				CLE."Document No_" LIKE 'VRGGFZ%'
+				OR CLE."Document No_" LIKE 'VGUGFZ%'
+				OR CLE."Document No_" LIKE 'VRGF%'
+				OR CLE."Document No_" LIKE 'VRF%'
+				OR CLE."Document No_" LIKE 'VGGF%'
+				)
+			AND VLE."Entry Type" = 1
+			)
+		OR (
+			NOT CLE."Main Area" IN (1, 2, 3)
+			AND NOT left(CLE."Document No_", 2) IN ('AL', 'BF', 'E4', 'HV', 'KA', 'LB', 'LO', 'LP', 'LQ', 'LR', 'RB', 'SP', 'UM')
+			AND NOT CLE."Document No_" LIKE '166%'
+			AND NOT CLE."Document No_" LIKE '169%'
+			AND NOT CLE."Document No_" LIKE 'UB0%'
+			)
+		OR (
+			CLE."Document No_" LIKE 'WRG%'
+			OR CLE."Document No_" LIKE 'WGG%'
+			OR CLE."Document No_" LIKE 'WGGS%'
+			OR CLE."Document No_" LIKE 'VRGGET%'
+			OR CLE."Document No_" LIKE 'VRT%'
+			OR CLE."Document No_" LIKE 'VRGT%'
+			OR CLE."Document No_" LIKE 'VGUGET%'
+			)
+		)

+ 47 - 0
sql/import/fm_rem_report.sql

@@ -0,0 +1,47 @@
+select
+
+chassis_number as FAGNR, 
+make_code as FTYPE, 
+posting_date as REDAT,
+posting_date as OPDAT, 
+due_date as FLDAT, 
+account_number as O500_KTONR, 
+document_number as O500_BELEGNR1, 
+entry_number as Reserve_1, 
+transact_sum as REBEB, 
+transact_outstanding as O500_HW2BETR_OFFEN, 
+transact_sum as O500_HW2BUCHGBETR, 
+customer_number as KNDNR,  
+concat(customer_number,' - ', replace(replace(customer_name, "  ", " "), "'", "´")) as KNAME, 
+customer_phone as KTELF, 
+customer_address as KSTRA, 
+customer_zipcode as KDPLZ,  
+customer_city as KDORT,
+salesman_user as KDBNR, 
+salesman_number as KDBERA, 
+salesman_name as KDBNAME, 
+verkaeufer as Stufe5, 
+reminder_level as Reserve_2,
+case when transact_sum > 0 then 'R' else 'G' end as Reserve_3,
+mahnstufe as O500_MAHNSTUFE, 
+mahnstufe2 as Next_MAHNSTUFE, 
+branch_code as Stufe3, 
+filiale_nummer as FFIRM, 
+'Forderungen DMS' as Stufe1,
+kostenstelle_text as Stufe2,
+'n.v.' as Stufe4,
+
+branch_code as Betrieb,
+shipment_date,
+customer_number_leasing,
+customer_name_leasing,
+text1,
+text2
+  
+  
+from fm_rem_import as t1
+
+INNER JOIN ( SELECT document_number, chassis_number, due_date, entry_number, MAX(reminder_level) as reminder_level
+             FROM fm_rem_import 
+			 GROUP BY 1, 2, 3, 4 ) AS t2 
+  USING(document_number, chassis_number, due_date, entry_number, reminder_level)

+ 93 - 0
sql/view/fm_rem_naga.sql

@@ -0,0 +1,93 @@
+select
+Betrieb,
+Stufe2, 
+FAGNR, 
+FTYPE,
+KNDNR,
+KNAME, 
+Stufe5, 
+REDAT, 
+O500_HW2BUCHGBETR, 
+FLDAT as FLDAT_von, 
+FLDAT as FLDAT_bis, 
+FLDAT, 
+O500_MAHNSTUFE, 
+Next_MAHNSTUFE,  
+ZA_1,
+ZA_2,
+ZA_3,
+O500_HW2BETR_OFFEN, 
+OPDAT,
+O500_BELEGNR1,
+Ktelf, 
+Kstra, 
+Kdplz, 
+Kdort,  
+Bemerkung, 
+Wiedervorlage, 
+Bemerkung_2, 
+Bearbeitet, 
+Mahnen, 
+`User`, 
+Aenderungsdatum,  
+User_2, 
+Reklamation, 
+Aenderungsdatum_u2,
+geplAuslief_NA, 
+Auslief_NA, 
+geplAuslief_GA, 
+Auslief_GA, 
+LKNAME, 
+LKTELF, 
+LKSTRA, 
+LKDPLZ, 
+LKDORT,
+Reserve_1,
+Reserve_3,
+shipment_date,
+customer_number_leasing,
+customer_name_leasing,
+text1,
+text2,
+round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) as "Faell_Tage",
+case 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 14 ) then '0-2 Wochen'  
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 28 )then '2-4 Wochen' 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 42 )then '4-6 Wochen' 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) >= 42 ) then '> 6 Wochen'   
+  else 'n.v.' 
+end as "Wochen"
+
+from fm_rem_report
+
+where stufe2 NOT IN ('SC','TZ','RA')
+
+and 
+
+case
+  when [userid] IN ('aba','bwo','mba','sop') then stufe2 IN ('NA')
+  when [userid] IN ('hko','sc','sv','fpo','fda') then stufe2 IN ('GA')
+  else 1 end
+  
+and 
+
+case
+  when [userid] IN ('mh') then betrieb IN ('MM','VÖH','KRU','ULM')
+  when [userid] IN ('mc') then betrieb IN ('MM','VÖH','KRU','LL')
+  when [userid] IN ('mj') then betrieb IN ('MM','KRU','ULM','LL')
+  when [userid] IN ('hg') then betrieb IN ('VÖH','KRU','ULM','LL')
+  when [userid] IN ('??') then betrieb IN ('MM','LL')
+  when [userid] IN ('be') then betrieb IN ('VÖH','KRU')
+  when [userid] IN ('mba') then betrieb IN ('MM','KRU')
+  when [userid] IN ('??') then betrieb IN ('GZ','KRU')
+  when [userid] IN ('mvo') then betrieb IN ('GZ')
+  when [userid] IN ('aho','azh','bv','fpo','hko','jlt','jsh','jti','mga','sc','sko','sop') then betrieb IN ('MM')
+  when [userid] IN ('mba','kkl') then betrieb IN ('KRU')
+  when [userid] IN ('asl','bta','bwo','fda','lho','mn','mne','mvo','nba','rl','rp','son','ssu','tbo','um','vb','vs') then betrieb IN ('ULM')
+  when [userid] IN ('ak','ala','ash','bb','dp','jdr','muy','mzo','nst','rhe','skr','sv','ype') then betrieb IN ('LL')
+  when [userid] IN ('cab','fd','aho','mk','ka','fl','tb','gr','kgu','hbo','sbe','hp',
+					'fja','jja','twa','sdo','tbe','ba','af','vpo','mku','rwo','osa','ipl',
+					'sse','rze','aan','tal','wr','mma') then kdbnr = [userid]
+  else 1 end
+
+order by FLDAT asc

+ 91 - 0
sql/view/fm_rem_sc.sql

@@ -0,0 +1,91 @@
+select 
+Betrieb,  
+Stufe2, 
+FAGNR, 
+FTYPE,
+KNDNR,
+KNAME, 
+Stufe5, 
+REDAT, 
+O500_HW2BUCHGBETR,  
+FLDAT as FLDAT_von, 
+FLDAT as FLDAT_bis, 
+FLDAT, 
+O500_MAHNSTUFE, 
+Next_MAHNSTUFE, 
+O500_HW2BETR_OFFEN, 
+OPDAT,
+O500_BELEGNR1,
+Ktelf, 
+Kstra, 
+Kdplz, 
+Kdort,  
+Bemerkung, 
+Wiedervorlage, 
+Bemerkung_2, 
+Bearbeitet, 
+Mahnen, 
+`User`, 
+Aenderungsdatum,  
+User_2, 
+Aenderungsdatum_u2,
+KZALA, 
+Versicherung,
+Rechtsanwalt,
+Reklamation, 
+Selbstbeteiligung, 
+SelbstbeteiligungBetrag, 
+Mwst, 
+MwstBetrag,
+Reserve_1,
+Reserve_3,
+round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) as "Faell_Tage",
+case 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 14 ) then '0-2 Wochen'  
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 28 )then '2-4 Wochen' 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 42 )then '4-6 Wochen' 
+  when (round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) >= 42 ) then '> 6 Wochen'   
+  else 'n.v.' 
+end as "Wochen"
+
+from fm_rem_report
+
+where stufe2 IN ('SC','TZ')
+
+and
+
+case 
+  when [userid] IN ('aet','afr','afo','asd','asf','asi','azn','bc','bsc','bz','ch','cbo','cgr','cho','cke','cki','cpe','cwd','de','dek','dfa','dfi','dki','dsu','eeb','ef','egi','eh','ekd','el','em','es','fau','fdo','fe','fgh','fhs','fhu','fsc','gcu',
+  'ge','gj','gk','hh','hce','hfr','hk','ho','ite','jal','jb','jhe','jhl','jkn','jmr','jg','jre',
+  'kh','kgr','kni','kta','kwe','kwi','lgi','lkr','lm','lre','lt','lwr','mb','mdi','mdk','me','mei','mf','mfr','mge','mgi','mmi','mop','msi','msu','mtr','mwe','nba','nf','nfi','ngu','nke','nna','nsa','nsc','nsm','nwa','obo','oke','ost','pkr','pma','psc','pwi',
+  'rhi','rm','ro','rr','sbr','sft','ssr','tre','skr','smr','spa','spe','ssh','sso','thg','tgn','tke','tkh','tno','ts','tsp','tze','var','vfe','vko','wb','we','wh','wk','wm','ws','wsc','yko','yun') then stufe2 IN ('SC')
+  when [userid] IN ('ach','asa','atz','bs','bsr','cfo','cla','cle','cw','dbi','dwe','ead','esr','fs','gmo','js','kk','ksc','mae','lmo','man','mmz','msd','mrr','mvk','mvs','ng',
+  'or','pjr','psh','rcl','rti','sb','sd','se','sf','shu','sl','spr','ssg','st','sz','ta','thc','tl','tpa','tsa','tzg','tzw','usc','ut') then stufe2 IN ('TZ')
+  else 1 end
+
+and 
+
+case 
+  when [userid] IN ('sz') then betrieb IN ('VÖH','KRU','LL')
+  when [userid] IN ('me') then betrieb IN ('VÖH','KRU','ULM')
+  when [userid] IN ('ch','lre','ws') then betrieb IN ('MM','VÖH')
+  when [userid] IN ('el','wsc') then betrieb IN ('MM','KRU')
+  when [userid] IN ('nsa') then betrieb IN ('MM','KRU','GZ','LL','ULM')
+  when [userid] IN ('ef') then betrieb IN ('MM','LL')
+  when [userid] IN ('sd') then betrieb IN ('VÖH','KRU')
+    when [userid] IN ('??') then betrieb IN ('GZ','KRU')
+   when [userid] IN ('mvo', 'lmo') then betrieb IN ('GZ')
+  when [userid] IN ('jb') then betrieb IN ('VÖH','ULM')
+  when [userid] IN ('kh','wh') then betrieb IN ('KRU','ULM')
+  when [userid] IN ('fe') then betrieb IN ('KRU','LL')
+  when [userid] IN ('lgl') then betrieb IN ('ULM','KRU')
+  when [userid] IN ('aet','afr','ag','asf','atz','bv','bsc','bsr','bz','cbo','cki','cpe','dek','dfa','egi','eh','es','fau','fhu','gcu','gk','hh','hk','jal','jg','js','jre','khe','kk','kni','kwi','ma','mmz','mrr','mrs','msu','mwe','nfi','ng','oke','pkr','pjr','pwi','rhi','rm','ro','sf','sl','smr','tke','tma','ut','we','yun') then betrieb IN ('MM')
+  when [userid] IN ('gj','cw','st') then betrieb IN ('VÖH')
+  when [userid] IN ('asd','dbi','shu','ssr','rr','rcl','jj','kkl','spe') then betrieb IN ('KRU')
+  when [userid] IN ('afo','mmi','csi','dsu','mdi','ngu','ite','jhl','spr','vko') then betrieb IN ('GZ')
+  when [userid] IN ('ach','asi','asl','awr','azn','bs','cgr','cla','cle','de','dfi','dki','dsm','dwe','ead','eeb','ekd','esr','fce','fgh','fs','fsc','gml','gmo','hce','hja','ho','jkn','jmr','kta','ksc','lgi','lbr','lkr','lt','lwr','mb','mdk','mei','mge','mgi','mtr','nba','nke','nf','nsm','nwa',
+  'obo','or','psc','psh','pz','rl','rti','sb','se','tre','sbr','ssg','sis','ta','tgn','thg','tkh','tno','tsa','tsp','tze','tzg','tzw','um','usc','vdu','vfe','vko','wb','wm') then betrieb IN ('ULM')
+  when [userid] IN ('agr','ak','ala','asa','ash','bc','cfo','cho','cwd','dp','fdo','feb','fkn','hfr','jdr','jhe','kgr','kwe','lra','mae','man','mfr','mop','msd','msi','mvk','mvs','mzo','nna','nst','pma','sft','spa','ssh','thc','tl','tpa','ts','var','yko') then betrieb IN ('LL')
+  else 1 end
+
+order by FLDAT asc