SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform_imr].[kreditorenbewegungen]
AS
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",
	c85 AS "Betrieb",
	c112 AS "Bookkeep Date",
	c111 AS "Text",
	'1' AS "Mandant",
	c97 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",
	SUM(c97) OVER (PARTITION BY c83) AS "Debitorsaldo_",
	c103 AS "Invoice Type_Invoices",
	c102 AS "Invoice Number_Invoices",
	c101 AS "Subsidiary_Invoices",
	c100 AS "Invoice Date_Invoices",
	'1' AS "Hauptbetrieb",
	c99 AS "Standort_ori",
	c98 AS "Sel Name",
	c97 AS "OP-Saldo",
	c96 AS "Kostenstelle",
	c95 AS "Tage",
	c94 AS "Staffel",
	c93 AS "Kunde",
	c92 AS "Beleg",
	c91 AS "Employee Number_Employees_journal_accountings",
	c90 AS "Name_Employees_journal_accountings",
	c89 AS "Anzahl_Stellen_Invoice_Number",
	c88 AS "Forderungsart_aus_erste_Stelle_Inv_Number",
	c87 AS "Forderungsart",
	c86 AS "Standort_aus_2_Stelle_Invoice_Number",
	c85 AS "Standort",
	c84 AS "Status Rechnung"
FROM (
	SELECT customers_suppliers."customer_number" AS c83,
		CASE 
			WHEN (journal_accountings."clearing_number" = 0)
				THEN ('offen')
			ELSE ('bezahlt')
			END AS c84,
		('0' + (convert(VARCHAR(50), journal_accountings."branch_number"))) AS c85,
		CASE 
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 2, 1)) BETWEEN '1' AND '4')
					)
				THEN ('0' + (substring(journal_accountings."invoice_number", 2, 1)))
			ELSE NULL
			END AS c86,
		CASE 
			WHEN (
					(
						CASE 
							WHEN (invoices."invoice_type" IN (2, 4, 6, 3))
								THEN ('3')
							WHEN (invoices."invoice_type" IN (5))
								THEN ('6')
							WHEN (invoices."invoice_type" IN (7))
								THEN ('1')
							WHEN (invoices."invoice_type" IN (8))
								THEN ('2')
							WHEN (document_types."document_type_in_journal" = 'R')
								THEN ('7')
							ELSE NULL
							END
						) = '1'
					)
				THEN ('Neuwagen')
			WHEN (
					(
						CASE 
							WHEN (invoices."invoice_type" IN (2, 4, 6, 3))
								THEN ('3')
							WHEN (invoices."invoice_type" IN (5))
								THEN ('6')
							WHEN (invoices."invoice_type" IN (7))
								THEN ('1')
							WHEN (invoices."invoice_type" IN (8))
								THEN ('2')
							WHEN (document_types."document_type_in_journal" = 'R')
								THEN ('7')
							ELSE NULL
							END
						) = '2'
					)
				THEN ('Gebrauchtwagen')
			WHEN (
					(
						CASE 
							WHEN (invoices."invoice_type" IN (2, 4, 6, 3))
								THEN ('3')
							WHEN (invoices."invoice_type" IN (5))
								THEN ('6')
							WHEN (invoices."invoice_type" IN (7))
								THEN ('1')
							WHEN (invoices."invoice_type" IN (8))
								THEN ('2')
							WHEN (document_types."document_type_in_journal" = 'R')
								THEN ('7')
							ELSE NULL
							END
						) = '3'
					)
				THEN ('Service')
			WHEN (
					(
						CASE 
							WHEN (invoices."invoice_type" IN (2, 4, 6, 3))
								THEN ('3')
							WHEN (invoices."invoice_type" IN (5))
								THEN ('6')
							WHEN (invoices."invoice_type" IN (7))
								THEN ('1')
							WHEN (invoices."invoice_type" IN (8))
								THEN ('2')
							WHEN (document_types."document_type_in_journal" = 'R')
								THEN ('7')
							ELSE NULL
							END
						) = '6'
					)
				THEN ('Teile')
			ELSE ('nicht zuzuordnen')
			END AS c87,
		CASE 
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 1, 1)) IN ('2', '3', '4'))
					)
				THEN ('Service')
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 1, 1)) IN ('5'))
					)
				THEN ('Teile')
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 1, 1)) IN ('6'))
					)
				THEN ('Garantie')
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 1, 1)) IN ('7'))
					)
				THEN ('Neuwagen')
			WHEN (
					(((len(journal_accountings."invoice_number"))) = 7)
					AND ((substring(journal_accountings."invoice_number", 1, 1)) IN ('8'))
					)
				THEN ('Gebrauchtwagen')
			WHEN (journal_accountings."document_type" = 'R')
				THEN ('Rent')
			ELSE ('nicht zuzuordnen')
			END AS c88,
		(len(journal_accountings."invoice_number")) AS c89,
		employees."name" AS c90,
		employees."employee_number" AS c91,
		CASE 
			WHEN (journal_accountings."posting_text" IS NOT NULL)
				THEN ((substring((convert(VARCHAR(50), journal_accountings."document_number")), 1, 7)) + ' - ' + journal_accountings."posting_text" + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))
			ELSE ((convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))
			END AS c92,
		((substring((convert(VARCHAR(50), customers_suppliers."customer_number")), 1, 7)) + ' - ' + customers_suppliers."first_name" + ' ' + customers_suppliers."family_name") AS c93,
		CASE 
			WHEN (((day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date"))))) BETWEEN 0 AND 14)
				THEN ('< 2 Wochen')
			WHEN (((day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date"))))) BETWEEN 15 AND 28)
				THEN ('2 - 4 Wochen')
			WHEN (((day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date"))))) BETWEEN 29 AND 42)
				THEN ('4 - 6 Wochen')
			WHEN (((day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date"))))) BETWEEN 43 AND 84)
				THEN ('6 - 12 Wochen')
			WHEN (((day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date"))))) > 84)
				THEN ('> 12 Wochen')
			ELSE NULL
			END AS c94,
		(day((getdate()) - (convert(DATETIME, journal_accountings."accounting_date")))) AS c95,
		CASE 
			WHEN (invoices."invoice_type" IN (2, 4, 6, 3))
				THEN ('3')
			WHEN (invoices."invoice_type" IN (5))
				THEN ('6')
			WHEN (invoices."invoice_type" IN (7))
				THEN ('1')
			WHEN (invoices."invoice_type" IN (8))
				THEN ('2')
			WHEN (document_types."document_type_in_journal" = 'R')
				THEN ('7')
			ELSE NULL
			END AS c96,
		(
			CASE 
				WHEN (journal_accountings."debit_or_credit" = 'H')
					THEN (journal_accountings."posted_value" / 100)
				ELSE (journal_accountings."posted_value" / 100 * - 1)
				END
			) AS c97,
		(substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name" AS c98,
		CASE 
			WHEN (invoices."subsidiary" IS NOT NULL)
				THEN ('0' + (substring((convert(VARCHAR(50), invoices."subsidiary")), 1, 1)))
			ELSE ('nicht zuzuordnen')
			END AS c99,
		invoices."invoice_date" AS c100,
		invoices."subsidiary" AS c101,
		invoices."invoice_number" AS c102,
		invoices."invoice_type" AS c103,
		document_types."document_type_description" AS c104,
		document_types."document_type_in_journal" AS c105,
		customers_suppliers."family_name" AS c106,
		customers_suppliers."first_name" AS c107,
		customers_suppliers."is_supplier" AS c108,
		journal_accountings."nominal_account_number" AS c109,
		CASE 
			WHEN (journal_accountings."debit_or_credit" = 'H')
				THEN (journal_accountings."posted_count" / 100 * - 1)
			ELSE (journal_accountings."posted_count" / 100)
			END AS c110,
		((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date")))) AS c111,
		journal_accountings."accounting_date" AS c112,
		CASE 
			WHEN (
					(
						(
							(
								((((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))) LIKE '%RG-BONUS%')
								OR ((((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))) LIKE '%GEBURTSTAGS%')
								)
							OR ((((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))) LIKE '%STARTGUTHABEN%')
							)
						OR ((((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))) LIKE '%BONUS F%')
						)
					OR ((((rtrim((convert(VARCHAR(50), journal_accountings."document_number")))) + ' - ' + journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + ((substring((convert(VARCHAR(50), employees."employee_number")), 1, 4)) + ' - ' + employees."name") + ' - ' + (convert(VARCHAR(50), year(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), month(journal_accountings."document_date")) + '-' + convert(VARCHAR(50), day(journal_accountings."document_date"))))) LIKE '%RG-ABZUG%')
					)
				THEN ('Bonus/Startguthaben')
			ELSE ('ohne Bonus/Startguthaben')
			END AS c113,
		journal_accountings."free_form_accounting_text" AS c114,
		journal_accountings."previously_used_account_no" AS c115,
		journal_accountings."skr51_cost_unit" AS c116,
		journal_accountings."skr51_sales_channel" AS c117,
		journal_accountings."skr51_cost_center" AS c118,
		journal_accountings."skr51_make" AS c119,
		journal_accountings."skr51_branch" AS c120,
		journal_accountings."day_of_actual_accounting" AS c121,
		journal_accountings."days_for_cash_discount" AS c122,
		journal_accountings."vat_key" AS c123,
		journal_accountings."account_statement_page" AS c124,
		journal_accountings."account_statement_number" AS c125,
		journal_accountings."vat_id_number" AS c126,
		journal_accountings."vehicle_reference" AS c127,
		journal_accountings."posting_text" AS c128,
		journal_accountings."term_of_payment" AS c129,
		journal_accountings."cash_discount" AS c130,
		journal_accountings."journal_line" AS c131,
		journal_accountings."journal_page" AS c132,
		journal_accountings."last_dunning_date" AS c133,
		journal_accountings."dunning_level" AS c134,
		journal_accountings."invoice_number" AS c135,
		journal_accountings."invoice_date" AS c136,
		journal_accountings."employee_number" AS c137,
		journal_accountings."serial_number_each_month" AS c138,
		journal_accountings."account_form_page_line" AS c139,
		journal_accountings."account_form_page_number" AS c140,
		journal_accountings."contra_account_text" AS c141,
		journal_accountings."nominal_contra_account" AS c142,
		journal_accountings."customer_contra_account" AS c143,
		journal_accountings."branch_number" AS c144,
		journal_accountings."posted_count" AS c145,
		journal_accountings."debit_or_credit" AS c146,
		journal_accountings."posted_value" AS c147,
		journal_accountings."document_date" AS c148,
		journal_accountings."clearing_number" AS c149,
		journal_accountings."is_balanced" AS c150,
		journal_accountings."customer_number" AS c151,
		journal_accountings."position_in_document" AS c152,
		journal_accountings."document_number" AS c153,
		journal_accountings."document_type" AS c154
	FROM (
		(
			(
				(
					LOCOSOFT.dbo."journal_accountings" journal_accountings LEFT OUTER JOIN LOCOSOFT."dbo"."employees" employees ON journal_accountings."employee_number" = employees."employee_number"
					) LEFT OUTER JOIN LOCOSOFT."dbo"."customers_suppliers" customers_suppliers ON customers_suppliers."customer_number" = journal_accountings."customer_number"
				) LEFT OUTER JOIN LOCOSOFT."dbo"."document_types" document_types ON document_types."document_type_in_journal" = journal_accountings."document_type"
			) LEFT OUTER JOIN LOCOSOFT."dbo"."invoices" invoices ON invoices."invtype_invnr" = journal_accountings."invoice_number"
		)
	WHERE (
			(
				(
					(journal_accountings."nominal_account_number" IN (1600, 1610))
					AND (journal_accountings."customer_number" IS NOT NULL)
					)
				AND (customers_suppliers."customer_number" IS NOT NULL)
				)
			AND (journal_accountings."document_type" IN ('W', 'E', 'L'))
			)
	) D1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
