SELECT CASE 
		WHEN (
				(
					(NOT T1."MAKE" IN ('OP', 'PE'))
					OR (T1."MAKE" IS NULL)
					)
				OR (T1."MAKE" = ' ')
				)
			THEN (
					(
						CASE 
							WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
								THEN ('OP')
							WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
								THEN ('PE')
							ELSE ('00')
							END
						)
					)
		ELSE (T1."MAKE")
		END + '-' + (
		CASE 
			WHEN (
					(((rtrim(T2."ACCT_NR"))) LIKE '2%')
					AND (T1."SITE" IS NULL)
					)
				THEN ('01')
			ELSE ((right('00' + (left(T1."SITE", 2)), 2)))
			END
		) + '-' + ((rtrim(T2."ACCT_NR"))) + '-' + (
		CASE 
			WHEN (
					(T1."DEPARTMENT" IS NULL)
					OR ((rtrim(T1."DEPARTMENT")) = '')
					)
				THEN ('00')
			ELSE ((rtrim(T1."DEPARTMENT")))
			END
		) + '-' + (
		CASE 
			WHEN (
					(T1."DESTINATION" IS NULL)
					OR ((rtrim(T1."DESTINATION")) = '')
					)
				THEN ('00')
			ELSE ((right('00' + (rtrim(T1."DESTINATION")), 2)))
			END
		) + '-' + (
		(
			rtrim(CASE 
					WHEN (
							(T3."MODEL_LINE" IS NOT NULL)
							AND (T3."MODEL_LINE" <> '')
							)
						THEN (T3."MODEL_LINE")
					WHEN (
							(T1."PRODUCT_GROUP" IS NOT NULL)
							AND (T1."PRODUCT_GROUP" <> '')
							)
						THEN (T1."PRODUCT_GROUP")
					WHEN (
							(T1."REPAIR_GROUP" IS NOT NULL)
							AND (T1."REPAIR_GROUP" <> '')
							)
						THEN (T1."REPAIR_GROUP")
					ELSE ('00')
					END)
			)
		) AS "Acct Nr",
	T2."LEDGER_ACCTS_NAME" AS "Ledger Accts Name",
	T2."LEDGER_ACCTS_NAME2" AS "Ledger Accts Name2",
	T2."HANDLER" AS "Handler_2",
	T2."DEPT_SPLIT" AS "Dept Split",
	T2."TYPE_ACCTT" AS "Type Acctt",
	T1."ACCT_NO" AS "Acct No",
	T1."BOOKKEEP_DATE" AS "Bookkeep Date",
	T1."BOOKKEEP_PERIOD" AS "Bookkeep Period",
	T1."DOCUMENT_NO" AS "Document No",
	T1."ORIGIN" AS "Origin",
	T1."STATUS" AS "Status",
	T1."DEBIT_AMOUNT" AS "Debit Amount",
	T1."CREDIT_AMOUNT" AS "Credit Amount",
	T1."DEBIT_QUANTITY" AS "Debit Quantity",
	T1."CREDIT_QUANTITY" AS "Credit Quantity",
	T1."AA_TRTYPE" AS "Aa Trtype",
	T1."DEPARTMENT" AS "Department",
	T1."STOCK" AS "Stock",
	T1."MAKE_FAMILY" AS "Make Family",
	T1."MAKE" AS "Make",
	T1."VEHICLE_TYPE" AS "Vehicle Type",
	T1."MODEL_LINE" AS "Model Line",
	T1."FACTORY_MODEL" AS "Factory Model",
	T1."WORKSHOP_MODEL" AS "Workshop Model",
	T1."PRODUCT_GROUP" AS "Product Group",
	T1."REPAIR_GROUP" AS "Repair Group",
	T1."KIT_GROUP" AS "Kit Group",
	T1."TIME_CODE" AS "Time Code",
	T1."INT_VOUCHER_NO" AS "Int Voucher No",
	T1."BALANCING_MARK" AS "Balancing Mark",
	T1."USED_VEH_DEST_CODE" AS "Used Veh Dest Code",
	T1."USE_OF_VEHICLE" AS "Use Of Vehicle",
	T1."ACCT_NO_NEXT_CHART" AS "Acct No Next Chart",
	T4."REFERENCE_IDENT" AS "Reference Ident",
	T4."TRANSACT_DATE" AS "Transact Date",
	T4."HANDLER" AS "Handler",
	T4."PROGRAM" AS "Program",
	T4."FUNCTION_CODE" AS "Function Code",
	T4."MODUL" AS "Modul",
	T4."DOCUMENT_KEY" AS "Document Key",
	T4."COMMENT" AS "Comment",
	T5."DEPARTMENT_TYPE_ID" AS "Department Type Id",
	T5."DESCRIPTION" AS "Description_2",
	T5."DEPARTMENT_GROUP" AS "Department Group",
	T6."AA_TRTYPE_ID" AS "Aa Trtype Id",
	T6."DESCRIPTION" AS "Description",
	T6."OWN_DESCRIPTION" AS "Own Description",
	'1' AS "Rechtseinheit",
	'01' AS "Betrieb",
	CASE 
		WHEN (
				(
					(NOT T1."MAKE" IN ('OP', 'PE'))
					OR (T1."MAKE" IS NULL)
					)
				OR (T1."MAKE" = ' ')
				)
			THEN (
					(
						CASE 
							WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
								THEN ('OP')
							WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
								THEN ('PE')
							ELSE ('00')
							END
						)
					)
		ELSE (T1."MAKE")
		END AS "Marke",
	T1."DEBIT_AMOUNT" + T1."CREDIT_AMOUNT" AS "Betrag",
	'1' AS "Mandant",
	(T1."DEBIT_QUANTITY" + T1."CREDIT_QUANTITY") AS "Menge",
	CASE 
		WHEN ((- 1 * datediff(day, (getdate()), T1."BOOKKEEP_DATE")) <= 120)
			THEN (T4."COMMENT")
		ELSE NULL
		END AS "Text",
	(left(T1."ACCT_NO", 1)) AS "Susa_2",
	T7."ORDER_NUMBER" AS "Order Number",
	T7."LINE_NUMBER" AS "Line Number",
	T7."INV_TIME" AS "Inv Time",
	T7."INV_TIME_INT" AS "Inv Time Int",
	T7."MAKE_TIME_UNIT" AS "Make Time Unit",
	T1."SITE" AS "Site",
	CASE 
		WHEN (T1."DEPARTMENT" = '    ')
			THEN ('00')
		ELSE (T1."DEPARTMENT")
		END AS "KST",
	T3."MODEL_LINE" AS "Model Line_vpp5q",
	T3."MOD_LIN_SPECIFY" AS "Mod Lin Specify_vpp5q",
	CASE 
		WHEN (T3."MODEL_LINE" IS NOT NULL)
			THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
		ELSE ('00 - ohne')
		END AS "Kostentr�ger_mit_Bez_",
	(
		substring((
				CASE 
					WHEN (T3."MODEL_LINE" IS NOT NULL)
						THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
					ELSE ('00 - ohne')
					END
				), 1, 2)
		) AS "Kostentr�ger",
	T8."CUSTOMER_NUMBER" AS "Customer Number",
	T9."CUSTOMER_GROUP" AS "Customer Group",
	CASE 
		WHEN (
				(T1."DESTINATION" = '00')
				AND (T1."PRICE_CODE" <> '')
				)
			THEN (T1."PRICE_CODE")
		WHEN (
				(
					(T1."DESTINATION" = '00')
					AND (T1."PRICE_CODE" = '')
					)
				AND (T9."CUSTOMER_GROUP" <> '')
				)
			THEN (T9."CUSTOMER_GROUP")
		WHEN ((rtrim(T1."DESTINATION")) = '')
			THEN ('00')
		ELSE (T1."DESTINATION")
		END AS "Absatzkanal",
	T2."TYPE_ACCTT" AS "GuV_Bilanz",
	(left(T1."ACCT_NO", 1)) AS "Susa",
	CASE 
		WHEN (
				(
					(
						substring((
								CASE 
									WHEN (T1."DEPARTMENT" = '    ')
										THEN ('00')
									ELSE (T1."DEPARTMENT")
									END
								), 1, 1)
						) = '1'
					)
				AND (
					NOT (
						(
							substring((
									CASE 
										WHEN (T3."MODEL_LINE" IS NOT NULL)
											THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
										ELSE ('00 - ohne')
										END
									), 1, 2)
							)
						) IN ('00')
					)
				)
			THEN ('Neuwagen')
		WHEN (
				(
					(
						substring((
								CASE 
									WHEN (T1."DEPARTMENT" = '    ')
										THEN ('00')
									ELSE (T1."DEPARTMENT")
									END
								), 1, 1)
						) = '2'
					)
				AND (
					NOT (
						(
							substring((
									CASE 
										WHEN (T3."MODEL_LINE" IS NOT NULL)
											THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
										ELSE ('00 - ohne')
										END
									), 1, 2)
							)
						) IN ('00')
					)
				)
			THEN ('Gebrauchtwagen')
		ELSE ('Ohne Kostentr�ger')
		END AS "Ebene31",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) IN ('OP')
				)
			THEN ('OP')
		ELSE NULL
		END AS "Marke f�r Kostentr�ger",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) IN ('OP')
				)
			THEN (
					(
						CASE 
							WHEN (
									(
										CASE 
											WHEN (
													(
														(NOT T1."MAKE" IN ('OP', 'PE'))
														OR (T1."MAKE" IS NULL)
														)
													OR (T1."MAKE" = ' ')
													)
												THEN (
														(
															CASE 
																WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
																	THEN ('OP')
																WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
																	THEN ('PE')
																ELSE ('00')
																END
															)
														)
											ELSE (T1."MAKE")
											END
										) IN ('OP')
									)
								THEN ('OP')
							ELSE NULL
							END
						) + (
						CASE 
							WHEN (T3."MODEL_LINE" IS NOT NULL)
								THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
							ELSE ('00 - ohne')
							END
						)
					)
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) IN ('VW')
				)
			THEN (
					(
						CASE 
							WHEN (
									(
										CASE 
											WHEN (
													(
														(NOT T1."MAKE" IN ('OP', 'PE'))
														OR (T1."MAKE" IS NULL)
														)
													OR (T1."MAKE" = ' ')
													)
												THEN (
														(
															CASE 
																WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
																	THEN ('OP')
																WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
																	THEN ('PE')
																ELSE ('00')
																END
															)
														)
											ELSE (T1."MAKE")
											END
										) IN ('OP')
									)
								THEN ('OP')
							ELSE NULL
							END
						) + (
						CASE 
							WHEN (T3."MODEL_LINE" IS NOT NULL)
								THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
							ELSE ('00 - ohne')
							END
						)
					)
		ELSE (
				(
					CASE 
						WHEN (T3."MODEL_LINE" IS NOT NULL)
							THEN (T3."MODEL_LINE" + ' - ' + T3."MOD_LIN_SPECIFY")
						ELSE ('00 - ohne')
						END
					)
				)
		END AS "Kostentr�ger_mit_Bez",
	'' AS "ACCT_Detail",
	'mit Marke' AS "Buchungen ohne Marke",
	'mit KST' AS "Buchungen ohne KST",
	'mit Absatzkanal' AS "Buchungen ohne Absatzkanal",
	T1."DESTINATION" AS "Destination",
	T10."Hauptbetrieb_ID" AS "Hauptbetrieb Id",
	T10."Hauptbetrieb_Name" AS "Hauptbetrieb Name",
	T10."Standort_ID" AS "Standort Id",
	T10."Standort_Name" AS "Standort Name",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) = 'OP'
				)
			THEN ('OP - Opel')
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) = 'PE'
				)
			THEN ('PE - Peugeot')
		WHEN (
				(
					CASE 
						WHEN (
								(
									(NOT T1."MAKE" IN ('OP', 'PE'))
									OR (T1."MAKE" IS NULL)
									)
								OR (T1."MAKE" = ' ')
								)
							THEN (
									(
										CASE 
											WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
												THEN ('OP')
											WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
												THEN ('PE')
											ELSE ('00')
											END
										)
									)
						ELSE (T1."MAKE")
						END
					) = 'AU'
				)
			THEN ('00 - ohne Marke')
		ELSE ('Marke fehlt')
		END AS "Marke_mit_Bez",
	CASE 
		WHEN (T1."STRATEGIC_AREA" IN ('AUTO      ', 'Opel      ', 'OPEL      '))
			THEN ('OP')
		WHEN (T1."STRATEGIC_AREA" IN ('PEUGEOT   '))
			THEN ('PE')
		ELSE ('00')
		END AS "Marke_Strategic_Area"
FROM "OPTIMA"."import"."ACCOUNT_INFO" T2,
	(
		(
			(
				(
					"OPTIMA"."import"."ACCT_DOC_KEY" T1 LEFT JOIN "OPTIMA"."import"."vPP5Q" T3 ON (T1."MODEL_LINE" = T3."MODEL_LINE")
						AND (T1."MAKE" = T3."MAKE_CD")
					) LEFT JOIN "OPTIMA"."import"."DEPARTMENT_TYPE" T5 ON T1."DEPARTMENT" = T5."DEPARTMENT_TYPE_ID"
				) LEFT JOIN "OPTIMA"."import"."AA_TRTYPE" T6 ON T1."AA_TRTYPE" = T6."AA_TRTYPE_ID"
			) LEFT JOIN "OPTIMA"."data"."GC_Department" T10 ON (T1."CLIENT_DB" = T10."Hauptbetrieb")
			AND (T1."SITE" = T10."Site")
		),
	(
		(
			(
				"OPTIMA"."import"."ACCT_DOC_DATA" T4 LEFT JOIN "OPTIMA"."import"."ACCT_DOC_SALESCLAS" T8 ON T4."REFERENCE_IDENT" = T8."REFERENCE_IDENT"
				) LEFT JOIN "OPTIMA"."import"."ORDER_LINE" T7 ON (T7."ORDER_NUMBER" = T8."ORDER_NUMBER")
				AND (T7."LINE_NUMBER" = T8."ORDER_LINE_NUMBER")
			) LEFT JOIN "OPTIMA"."import"."CUSTOMER" T9 ON T9."CUSTOMER_NUMBER" = T8."CUSTOMER_NUMBER"
		)
WHERE (T2."ACCT_NR" = T1."ACCT_NO")
	AND (T1."UNIQUE_IDENT" = T4."REFERENCE_IDENT")
	AND (
		(T2."TYPE_ACCTT" = '1')
		AND (T1."BOOKKEEP_DATE" >= convert(DATETIME, '2019-01-01 00:00:00.000'))
		)
