SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform_imr].[op_aus_loc_belege_ohne_8520]
AS
SELECT "Accounting Date" AS "Accounting Date",
	"Document Type" AS "Document Type",
	"Document Number" AS "Document Number",
	"Position In Document" AS "Position In Document",
	"Nominal Account Number" AS "Nominal Account Number",
	"Customer Number" AS "Customer Number",
	"Is Balanced" AS "Is Balanced",
	"Clearing Number" AS "Clearing Number",
	"Document Date" AS "Document Date",
	"Posted Value" AS "Posted Value",
	"Debit Or Credit" AS "Debit Or Credit",
	"Posted Count" AS "Posted Count",
	"Branch Number" AS "Branch Number",
	"Customer Contra Account" AS "Customer Contra Account",
	"Nominal Contra Account" AS "Nominal Contra Account",
	"Contra Account Text" AS "Contra Account Text",
	"Account Form Page Number" AS "Account Form Page Number",
	"Account Form Page Line" AS "Account Form Page Line",
	"Serial Number Each Month" AS "Serial Number Each Month",
	"Employee Number" AS "Employee Number",
	"Invoice Date" AS "Invoice Date",
	"Invoice Number" AS "Invoice Number",
	"Dunning Level" AS "Dunning Level",
	"Last Dunning Date" AS "Last Dunning Date",
	"Journal Page" AS "Journal Page",
	"Journal Line" AS "Journal Line",
	"Cash Discount" AS "Cash Discount",
	"Term Of Payment" AS "Term Of Payment",
	"Posting Text" AS "Posting Text",
	"Vehicle Reference" AS "Vehicle Reference",
	"Vat Id Number" AS "Vat Id Number",
	"Account Statement Number" AS "Account Statement Number",
	"Account Statement Page" AS "Account Statement Page",
	"Vat Key" AS "Vat Key",
	"Days For Cash Discount" AS "Days For Cash Discount",
	"Day Of Actual Accounting" AS "Day Of Actual Accounting",
	"Skr51 Branch" AS "Skr51 Branch",
	"Skr51 Make" AS "Skr51 Make",
	"Skr51 Cost Center" AS "Skr51 Cost Center",
	"Skr51 Sales Channel" AS "Skr51 Sales Channel",
	"Skr51 Cost Unit" AS "Skr51 Cost Unit",
	"Previously Used Account No" AS "Previously Used Account No",
	"Free Form Accounting Text" AS "Free Form Accounting Text",
	"Free Form Document Text" AS "Free Form Document Text",
	"Rechtseinheit" AS "Rechtseinheit",
	"Betrieb" AS "Betrieb",
	"Bookkeep Date" AS "Bookkeep Date",
	"Text" AS "Text",
	"Mandant" AS "Mandant",
	"Betrag" AS "Betrag",
	"Menge" AS "Menge",
	"Acct Nr" AS "Acct Nr",
	"Customer Number_suctomer_supplier" AS "Customer Number_suctomer_supplier",
	"Is Supplier" AS "Is Supplier",
	"First Name" AS "First Name",
	"Family Name" AS "Family Name",
	"Document Type In Journal" AS "Document Type In Journal",
	"Document Type Description" AS "Document Type Description",
	SUM("Betrag") OVER (PARTITION BY "Customer Number_suctomer_supplier") AS "Debitorsaldo_",
	"Invoice Type_Invoices" AS "Invoice Type_Invoices",
	"Invoice Number_Invoices" AS "Invoice Number_Invoices",
	"Subsidiary_Invoices" AS "Subsidiary_Invoices",
	"Invoice Date_Invoices" AS "Invoice Date_Invoices",
	"Hauptbetrieb" AS "Hauptbetrieb",
	"Standort_ori" AS "Standort_ori",
	"Sel Name" AS "Sel Name",
	"OP-Saldo" AS "OP-Saldo",
	"Kostenstelle" AS "Kostenstelle",
	"Tage" AS "Tage",
	"Staffel" AS "Staffel",
	"Kunde" AS "Kunde",
	"Beleg" AS "Beleg",
	"Employee Number_Employees_journal_accountings" AS "Employee Number_Employees_journal_accountings",
	"Name_Employees_journal_accountings" AS "Name_Employees_journal_accountings",
	"Anzahl_Stellen_Invoice_Number" AS "Anzahl_Stellen_Invoice_Number",
	"Forderungsart_aus_erste_Stelle_Inv_Number" AS "Forderungsart_aus_erste_Stelle_Inv_Number",
	"Forderungsart" AS "Forderungsart",
	"Standort_aus_2_Stelle_Invoice_Number" AS "Standort_aus_2_Stelle_Invoice_Number",
	"Standort" AS "Standort"
FROM (
	SELECT c112 AS "Accounting Date",
		c154 AS "Document Type",
		c153 AS "Document Number",
		c152 AS "Position In Document",
		c109 AS "Nominal Account Number",
		c151 AS "Customer Number",
		c150 AS "Is Balanced",
		c149 AS "Clearing Number",
		c148 AS "Document Date",
		c147 AS "Posted Value",
		c146 AS "Debit Or Credit",
		c145 AS "Posted Count",
		c144 AS "Branch Number",
		c143 AS "Customer Contra Account",
		c142 AS "Nominal Contra Account",
		c141 AS "Contra Account Text",
		c140 AS "Account Form Page Number",
		c139 AS "Account Form Page Line",
		c138 AS "Serial Number Each Month",
		c137 AS "Employee Number",
		c136 AS "Invoice Date",
		c135 AS "Invoice Number",
		c134 AS "Dunning Level",
		c133 AS "Last Dunning Date",
		c132 AS "Journal Page",
		c131 AS "Journal Line",
		c130 AS "Cash Discount",
		c129 AS "Term Of Payment",
		c128 AS "Posting Text",
		c127 AS "Vehicle Reference",
		c126 AS "Vat Id Number",
		c125 AS "Account Statement Number",
		c124 AS "Account Statement Page",
		c123 AS "Vat Key",
		c122 AS "Days For Cash Discount",
		c121 AS "Day Of Actual Accounting",
		c120 AS "Skr51 Branch",
		c119 AS "Skr51 Make",
		c118 AS "Skr51 Cost Center",
		c117 AS "Skr51 Sales Channel",
		c116 AS "Skr51 Cost Unit",
		c115 AS "Previously Used Account No",
		c114 AS "Free Form Accounting Text",
		c113 AS "Free Form Document Text",
		'1' AS "Rechtseinheit",
		c84 AS "Betrieb",
		c112 AS "Bookkeep Date",
		c111 AS "Text",
		'1' AS "Mandant",
		c96 AS "Betrag",
		c110 AS "Menge",
		c109 AS "Acct Nr",
		c83 AS "Customer Number_suctomer_supplier",
		c108 AS "Is Supplier",
		c107 AS "First Name",
		c106 AS "Family Name",
		c105 AS "Document Type In Journal",
		c104 AS "Document Type Description",
		c103 AS "Invoice Type_Invoices",
		c102 AS "Invoice Number_Invoices",
		c101 AS "Subsidiary_Invoices",
		c100 AS "Invoice Date_Invoices",
		c99 AS "Hauptbetrieb",
		c98 AS "Standort_ori",
		c97 AS "Sel Name",
		c96 AS "OP-Saldo",
		c95 AS "Kostenstelle",
		c94 AS "Tage",
		c93 AS "Staffel",
		c92 AS "Kunde",
		c91 AS "Beleg",
		c90 AS "Employee Number_Employees_journal_accountings",
		c89 AS "Name_Employees_journal_accountings",
		c88 AS "Anzahl_Stellen_Invoice_Number",
		c87 AS "Forderungsart_aus_erste_Stelle_Inv_Number",
		c86 AS "Forderungsart",
		c85 AS "Standort_aus_2_Stelle_Invoice_Number",
		c84 AS "Standort",
		SUM(c96) OVER (PARTITION BY c83) AS c80
	FROM (
		SELECT T3."customer_number" AS c83,
			('0' + (convert(VARCHAR(50), T1."branch_number"))) AS c84,
			CASE 
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 2, 1)
								) BETWEEN '1' AND '4'
							)
						)
					THEN (
							'0' + (
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 2, 1)
								)
							)
				ELSE NULL
				END AS c85,
			CASE 
				WHEN (
						(
							CASE 
								WHEN (T5."invoice_type" IN (2, 4, 6, 3))
									THEN ('3')
								WHEN (T5."invoice_type" IN (5))
									THEN ('6')
								WHEN (T5."invoice_type" IN (7))
									THEN ('1')
								WHEN (T5."invoice_type" IN (8))
									THEN ('2')
								WHEN (T4."document_type_in_journal" = 'R')
									THEN ('7')
								ELSE NULL
								END
							) = '1'
						)
					THEN ('Neuwagen')
				WHEN (
						(
							CASE 
								WHEN (T5."invoice_type" IN (2, 4, 6, 3))
									THEN ('3')
								WHEN (T5."invoice_type" IN (5))
									THEN ('6')
								WHEN (T5."invoice_type" IN (7))
									THEN ('1')
								WHEN (T5."invoice_type" IN (8))
									THEN ('2')
								WHEN (T4."document_type_in_journal" = 'R')
									THEN ('7')
								ELSE NULL
								END
							) = '2'
						)
					THEN ('Gebrauchtwagen')
				WHEN (
						(
							CASE 
								WHEN (T5."invoice_type" IN (2, 4, 6, 3))
									THEN ('3')
								WHEN (T5."invoice_type" IN (5))
									THEN ('6')
								WHEN (T5."invoice_type" IN (7))
									THEN ('1')
								WHEN (T5."invoice_type" IN (8))
									THEN ('2')
								WHEN (T4."document_type_in_journal" = 'R')
									THEN ('7')
								ELSE NULL
								END
							) = '3'
						)
					THEN ('Service')
				WHEN (
						(
							CASE 
								WHEN (T5."invoice_type" IN (2, 4, 6, 3))
									THEN ('3')
								WHEN (T5."invoice_type" IN (5))
									THEN ('6')
								WHEN (T5."invoice_type" IN (7))
									THEN ('1')
								WHEN (T5."invoice_type" IN (8))
									THEN ('2')
								WHEN (T4."document_type_in_journal" = 'R')
									THEN ('7')
								ELSE NULL
								END
							) = '6'
						)
					THEN ('Teile')
				ELSE ('nicht zuzuordnen')
				END AS c86,
			CASE 
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 1, 1)
								) IN ('2', '3', '4')
							)
						)
					THEN ('Service')
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 1, 1)
								) IN ('5')
							)
						)
					THEN ('Teile')
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 1, 1)
								) IN ('6')
							)
						)
					THEN ('Garantie')
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 1, 1)
								) IN ('7')
							)
						)
					THEN ('Neuwagen')
				WHEN (
						(
							(
								(
									len((
											CASE 
												WHEN (
														(T1."invoice_number" IS NULL)
														OR (T1."invoice_number" = ' ')
														)
													THEN (' - ')
												ELSE (T1."invoice_number")
												END
											))
									)
								) = 7
							)
						AND (
							(
								substring((
										CASE 
											WHEN (
													(T1."invoice_number" IS NULL)
													OR (T1."invoice_number" = ' ')
													)
												THEN (' - ')
											ELSE (T1."invoice_number")
											END
										), 1, 1)
								) IN ('8')
							)
						)
					THEN ('Gebrauchtwagen')
				WHEN (T1."document_type" = 'R')
					THEN ('Rent')
				ELSE ('nicht zuzuordnen')
				END AS c87,
			(
				len((
						CASE 
							WHEN (
									(T1."invoice_number" IS NULL)
									OR (T1."invoice_number" = ' ')
									)
								THEN (' - ')
							ELSE (T1."invoice_number")
							END
						))
				) AS c88,
			T2."name" AS c89,
			T2."employee_number" AS c90,
			(
				(
					CASE 
						WHEN (
								(T1."posting_text" IS NULL)
								OR (T1."posting_text" = ' ')
								)
							THEN (' - ')
						ELSE (T1."posting_text")
						END
					) + '/' + (
					CASE 
						WHEN (
								(T1."vehicle_reference" IS NULL)
								OR (T1."vehicle_reference" = ' ')
								)
							THEN (' - ')
						ELSE (T1."vehicle_reference")
						END
					)
				) AS c91,
			CASE 
				WHEN (T3."first_name" IS NOT NULL)
					THEN ((substring((convert(VARCHAR(50), T3."customer_number")), 1, 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
				ELSE ((substring((convert(VARCHAR(50), T3."customer_number")), 1, 7)) + ' ' + T3."family_name")
				END AS c92,
			CASE 
				WHEN (((day((getdate()) - (convert(DATETIME, T1."accounting_date"))))) BETWEEN 0 AND 14)
					THEN ('< 2 Wochen')
				WHEN (((day((getdate()) - (convert(DATETIME, T1."accounting_date"))))) BETWEEN 15 AND 28)
					THEN ('2 - 4 Wochen')
				WHEN (((day((getdate()) - (convert(DATETIME, T1."accounting_date"))))) BETWEEN 29 AND 42)
					THEN ('4 - 6 Wochen')
				WHEN (((day((getdate()) - (convert(DATETIME, T1."accounting_date"))))) BETWEEN 43 AND 84)
					THEN ('6 - 12 Wochen')
				WHEN (((day((getdate()) - (convert(DATETIME, T1."accounting_date"))))) > 84)
					THEN ('> 12 Wochen')
				ELSE NULL
				END AS c93,
			(day((getdate()) - (convert(DATETIME, T1."accounting_date")))) AS c94,
			CASE 
				WHEN (T5."invoice_type" IN (2, 4, 6, 3))
					THEN ('3')
				WHEN (T5."invoice_type" IN (5))
					THEN ('6')
				WHEN (T5."invoice_type" IN (7))
					THEN ('1')
				WHEN (T5."invoice_type" IN (8))
					THEN ('2')
				WHEN (T4."document_type_in_journal" = 'R')
					THEN ('7')
				ELSE NULL
				END AS c95,
			(
				CASE 
					WHEN (T1."debit_or_credit" = 'H')
						THEN (T1."posted_value" / 100 * - 1)
					ELSE (T1."posted_value" / 100)
					END
				) AS c96,
			(substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name" AS c97,
			CASE 
				WHEN (T5."subsidiary" IS NOT NULL)
					THEN ('0' + (substring((convert(VARCHAR(50), T5."subsidiary")), 1, 1)))
				ELSE ('nicht zuzuordnen')
				END AS c98,
			CASE 
				WHEN ((('0' + (convert(VARCHAR(50), T1."branch_number")))) IN ('015                                                                                                                                                                                                                                                            '))
					THEN ('15')
				ELSE ('1')
				END AS c99,
			T5."invoice_date" AS c100,
			T5."subsidiary" AS c101,
			T5."invoice_number" AS c102,
			T5."invoice_type" AS c103,
			T4."document_type_description" AS c104,
			T4."document_type_in_journal" AS c105,
			T3."family_name" AS c106,
			T3."first_name" AS c107,
			T3."is_supplier" AS c108,
			T1."nominal_account_number" AS c109,
			CASE 
				WHEN (T1."debit_or_credit" = 'H')
					THEN (T1."posted_count" / 100 * - 1)
				ELSE (T1."posted_count" / 100)
				END AS c110,
			CASE 
				WHEN (
						(
							CASE 
								WHEN (
										(T1."posting_text" IS NULL)
										OR (T1."posting_text" = ' ')
										)
									THEN (' - ')
								ELSE (T1."posting_text")
								END
							) IS NOT NULL
						)
					THEN (
							(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
								CASE 
									WHEN (
											(T1."invoice_number" IS NULL)
											OR (T1."invoice_number" = ' ')
											)
										THEN (' - ')
									ELSE (T1."invoice_number")
									END
								) + ' - ' + (
								CASE 
									WHEN (
											(T1."posting_text" IS NULL)
											OR (T1."posting_text" = ' ')
											)
										THEN (' - ')
									ELSE (T1."posting_text")
									END
								) + '/' + (
								CASE 
									WHEN (
											(T1."vehicle_reference" IS NULL)
											OR (T1."vehicle_reference" = ' ')
											)
										THEN (' - ')
									ELSE (T1."vehicle_reference")
									END
								) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
							)
				ELSE (
						(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
							CASE 
								WHEN (
										(T1."invoice_number" IS NULL)
										OR (T1."invoice_number" = ' ')
										)
									THEN (' - ')
								ELSE (T1."invoice_number")
								END
							) + ' - ' + (
							CASE 
								WHEN (
										(T1."vehicle_reference" IS NULL)
										OR (T1."vehicle_reference" = ' ')
										)
									THEN (' - ')
								ELSE (T1."vehicle_reference")
								END
							) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
						)
				END AS c111,
			T1."accounting_date" AS c112,
			CASE 
				WHEN (
						(
							(
								(
									(
										(
											CASE 
												WHEN (
														(
															CASE 
																WHEN (
																		(T1."posting_text" IS NULL)
																		OR (T1."posting_text" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."posting_text")
																END
															) IS NOT NULL
														)
													THEN (
															(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
																CASE 
																	WHEN (
																			(T1."invoice_number" IS NULL)
																			OR (T1."invoice_number" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."invoice_number")
																	END
																) + ' - ' + (
																CASE 
																	WHEN (
																			(T1."posting_text" IS NULL)
																			OR (T1."posting_text" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."posting_text")
																	END
																) + '/' + (
																CASE 
																	WHEN (
																			(T1."vehicle_reference" IS NULL)
																			OR (T1."vehicle_reference" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."vehicle_reference")
																	END
																) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
															)
												ELSE (
														(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
															CASE 
																WHEN (
																		(T1."invoice_number" IS NULL)
																		OR (T1."invoice_number" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."invoice_number")
																END
															) + ' - ' + (
															CASE 
																WHEN (
																		(T1."vehicle_reference" IS NULL)
																		OR (T1."vehicle_reference" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."vehicle_reference")
																END
															) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
														)
												END
											) LIKE '%RG-BONUS%'
										)
									OR (
										(
											CASE 
												WHEN (
														(
															CASE 
																WHEN (
																		(T1."posting_text" IS NULL)
																		OR (T1."posting_text" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."posting_text")
																END
															) IS NOT NULL
														)
													THEN (
															(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
																CASE 
																	WHEN (
																			(T1."invoice_number" IS NULL)
																			OR (T1."invoice_number" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."invoice_number")
																	END
																) + ' - ' + (
																CASE 
																	WHEN (
																			(T1."posting_text" IS NULL)
																			OR (T1."posting_text" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."posting_text")
																	END
																) + '/' + (
																CASE 
																	WHEN (
																			(T1."vehicle_reference" IS NULL)
																			OR (T1."vehicle_reference" = ' ')
																			)
																		THEN (' - ')
																	ELSE (T1."vehicle_reference")
																	END
																) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
															)
												ELSE (
														(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
															CASE 
																WHEN (
																		(T1."invoice_number" IS NULL)
																		OR (T1."invoice_number" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."invoice_number")
																END
															) + ' - ' + (
															CASE 
																WHEN (
																		(T1."vehicle_reference" IS NULL)
																		OR (T1."vehicle_reference" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."vehicle_reference")
																END
															) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
														)
												END
											) LIKE '%GEBURTSTAGS%'
										)
									)
								OR (
									(
										CASE 
											WHEN (
													(
														CASE 
															WHEN (
																	(T1."posting_text" IS NULL)
																	OR (T1."posting_text" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."posting_text")
															END
														) IS NOT NULL
													)
												THEN (
														(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
															CASE 
																WHEN (
																		(T1."invoice_number" IS NULL)
																		OR (T1."invoice_number" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."invoice_number")
																END
															) + ' - ' + (
															CASE 
																WHEN (
																		(T1."posting_text" IS NULL)
																		OR (T1."posting_text" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."posting_text")
																END
															) + '/' + (
															CASE 
																WHEN (
																		(T1."vehicle_reference" IS NULL)
																		OR (T1."vehicle_reference" = ' ')
																		)
																	THEN (' - ')
																ELSE (T1."vehicle_reference")
																END
															) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
														)
											ELSE (
													(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
														CASE 
															WHEN (
																	(T1."invoice_number" IS NULL)
																	OR (T1."invoice_number" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."invoice_number")
															END
														) + ' - ' + (
														CASE 
															WHEN (
																	(T1."vehicle_reference" IS NULL)
																	OR (T1."vehicle_reference" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."vehicle_reference")
															END
														) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
													)
											END
										) LIKE '%STARTGUTHABEN%'
									)
								)
							OR (
								(
									CASE 
										WHEN (
												(
													CASE 
														WHEN (
																(T1."posting_text" IS NULL)
																OR (T1."posting_text" = ' ')
																)
															THEN (' - ')
														ELSE (T1."posting_text")
														END
													) IS NOT NULL
												)
											THEN (
													(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
														CASE 
															WHEN (
																	(T1."invoice_number" IS NULL)
																	OR (T1."invoice_number" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."invoice_number")
															END
														) + ' - ' + (
														CASE 
															WHEN (
																	(T1."posting_text" IS NULL)
																	OR (T1."posting_text" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."posting_text")
															END
														) + '/' + (
														CASE 
															WHEN (
																	(T1."vehicle_reference" IS NULL)
																	OR (T1."vehicle_reference" = ' ')
																	)
																THEN (' - ')
															ELSE (T1."vehicle_reference")
															END
														) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
													)
										ELSE (
												(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
													CASE 
														WHEN (
																(T1."invoice_number" IS NULL)
																OR (T1."invoice_number" = ' ')
																)
															THEN (' - ')
														ELSE (T1."invoice_number")
														END
													) + ' - ' + (
													CASE 
														WHEN (
																(T1."vehicle_reference" IS NULL)
																OR (T1."vehicle_reference" = ' ')
																)
															THEN (' - ')
														ELSE (T1."vehicle_reference")
														END
													) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
												)
										END
									) LIKE '%BONUS F%'
								)
							)
						OR (
							(
								CASE 
									WHEN (
											(
												CASE 
													WHEN (
															(T1."posting_text" IS NULL)
															OR (T1."posting_text" = ' ')
															)
														THEN (' - ')
													ELSE (T1."posting_text")
													END
												) IS NOT NULL
											)
										THEN (
												(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
													CASE 
														WHEN (
																(T1."invoice_number" IS NULL)
																OR (T1."invoice_number" = ' ')
																)
															THEN (' - ')
														ELSE (T1."invoice_number")
														END
													) + ' - ' + (
													CASE 
														WHEN (
																(T1."posting_text" IS NULL)
																OR (T1."posting_text" = ' ')
																)
															THEN (' - ')
														ELSE (T1."posting_text")
														END
													) + '/' + (
													CASE 
														WHEN (
																(T1."vehicle_reference" IS NULL)
																OR (T1."vehicle_reference" = ' ')
																)
															THEN (' - ')
														ELSE (T1."vehicle_reference")
														END
													) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
												)
									ELSE (
											(rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + (
												CASE 
													WHEN (
															(T1."invoice_number" IS NULL)
															OR (T1."invoice_number" = ' ')
															)
														THEN (' - ')
													ELSE (T1."invoice_number")
													END
												) + ' - ' + (
												CASE 
													WHEN (
															(T1."vehicle_reference" IS NULL)
															OR (T1."vehicle_reference" = ' ')
															)
														THEN (' - ')
													ELSE (T1."vehicle_reference")
													END
												) + ' - ' + ((substring((convert(VARCHAR(50), T2."employee_number")), 1, 4)) + ' - ' + T2."name") + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date")))
											)
									END
								) LIKE '%RG-ABZUG%'
							)
						)
					THEN ('Bonus/Startguthaben')
				ELSE ('ohne Bonus/Startguthaben')
				END AS c113,
			T1."free_form_accounting_text" AS c114,
			T1."previously_used_account_no" AS c115,
			T1."skr51_cost_unit" AS c116,
			T1."skr51_sales_channel" AS c117,
			T1."skr51_cost_center" AS c118,
			T1."skr51_make" AS c119,
			T1."skr51_branch" AS c120,
			T1."day_of_actual_accounting" AS c121,
			(convert(VARCHAR(50), year(T1."accounting_date")) + '-' + convert(VARCHAR(50), month(T1."accounting_date")) + '-' + convert(VARCHAR(50), day(T1."accounting_date"))) AS c122,
			T1."vat_key" AS c123,
			T1."account_statement_page" AS c124,
			T1."account_statement_number" AS c125,
			T1."vat_id_number" AS c126,
			CASE 
				WHEN (
						(T1."vehicle_reference" IS NULL)
						OR (T1."vehicle_reference" = ' ')
						)
					THEN (' - ')
				ELSE (T1."vehicle_reference")
				END AS c127,
			CASE 
				WHEN (
						(T1."posting_text" IS NULL)
						OR (T1."posting_text" = ' ')
						)
					THEN (' - ')
				ELSE (T1."posting_text")
				END AS c128,
			T1."term_of_payment" AS c129,
			T1."cash_discount" AS c130,
			T1."journal_line" AS c131,
			T1."journal_page" AS c132,
			T1."last_dunning_date" AS c133,
			T1."dunning_level" AS c134,
			CASE 
				WHEN (
						(T1."invoice_number" IS NULL)
						OR (T1."invoice_number" = ' ')
						)
					THEN (' - ')
				ELSE (T1."invoice_number")
				END AS c135,
			T1."invoice_date" AS c136,
			T1."employee_number" AS c137,
			T1."serial_number_each_month" AS c138,
			T1."account_form_page_line" AS c139,
			T1."account_form_page_number" AS c140,
			T1."contra_account_text" AS c141,
			T1."nominal_contra_account" AS c142,
			T1."customer_contra_account" AS c143,
			T1."branch_number" AS c144,
			T1."posted_count" AS c145,
			T1."debit_or_credit" AS c146,
			T1."posted_value" AS c147,
			T1."document_date" AS c148,
			T1."clearing_number" AS c149,
			T1."is_balanced" AS c150,
			T1."customer_number" AS c151,
			T1."position_in_document" AS c152,
			(T1."document_number") AS c153,
			T1."document_type" AS c154
		FROM (
			(
				(
					(
						LOCOSOFT."dbo"."journal_accountings" T1 LEFT OUTER JOIN LOCOSOFT."dbo"."employees" T2 ON T1."employee_number" = T2."employee_number"
						) LEFT OUTER JOIN LOCOSOFT."dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."customer_number"
					) LEFT OUTER JOIN LOCOSOFT."dbo"."document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
				) LEFT OUTER JOIN LOCOSOFT."dbo"."invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
			)
		WHERE (
				(
					(
						(
							(T1."nominal_account_number" IN (1400, 1409, 1451, 1408, 1525))
							AND (T1."clearing_number" = 0)
							)
						AND (T1."customer_number" IS NOT NULL)
						)
					AND (NOT T4."document_type_in_journal" IN ('A', 'E', 'U', 'V'))
					)
				AND (T3."customer_number" IS NOT NULL)
				)
		) D2
	) D1
WHERE (c80 <> 0)
	-- order by "Customer Number" asc,"Invoice Number" asc,"Document Type In Journal" asc,"Nominal Account Number" asc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
