SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform_imr].[offene_auftraege_ums_ben_aw]
AS
SELECT c55 AS "Order Number_ori",
	c101 AS "Order Position",
	c100 AS "Labour Type",
	c99 AS "Subsidiary",
	c98 AS "Is Invoiced",
	c97 AS "Invoice Type",
	c96 AS "Invoice Number",
	c95 AS "Order Date",
	c94 AS "Estimated Inbound Time",
	c93 AS "Estimated Outbound Time",
	c92 AS "Order Print Date",
	c91 AS "Order Taking Employee No",
	c90 AS "Order Delivery Employee No",
	c89 AS "Vehicle Number",
	c88 AS "Order Mileage",
	c87 AS "Order Customer",
	c86 AS "Paying Customer",
	c85 AS "Holder Number",
	c84 AS "Parts Rebate Group Sell",
	c83 AS "Clearing Delay Type",
	c82 AS "Code_Labour_Type",
	c81 AS "Description_Labour_Type",
	c80 AS "Hauptbetrieb",
	c79 AS "Standort",
	c78 AS "Name_Serviceberater",
	c77 AS "Serviceberater",
	c76 AS "Umsatzart",
	c75 AS "Customer Number_Cust",
	c74 AS "First Name_Cust",
	c73 AS "Family Name_Cust",
	c72 AS "Kunde",
	c71 AS "Order Number",
	c70 AS "Internal Number_Vehicle",
	c69 AS "Vin_Vehicle",
	c68 AS "License Plate_Vehicle",
	c67 AS "Make Number_Vehicle",
	'Service' AS "Auftragsart",
	c66 AS "Make Number_Makes",
	c65 AS "Description_Makes",
	c65 AS "Fabrikat",
	c64 AS "Description_Models",
	c63 AS "Model",
	1 AS "DG_1",
	COUNT(c99) OVER (PARTITION BY c55) AS "DG_2",
	c62 AS "Order Number_Labours",
	c61 AS "Order Position_Labours",
	c56 AS "Net Price In Order",
	c60 AS "Text Line",
	c59 AS "Order Number_ben_AW",
	c58 AS "Monteur_und_Stunden",
	--1.0 * c57 / (COUNT(c99) OVER (PARTITION BY c55)) AS "ben. Std",
	--CAST(1.0 * c57 / COUNT(c99) OVER (PARTITION BY c55) AS DECIMAL(18,6)) AS "ben. Std",
	CAST(1.0 * c57/60 / COUNT(c99) OVER (PARTITION BY c55) AS DECIMAL(18,6)) AS "ben. Std",
	c56 AS "Umsatz Lohn",
	[labour_operation_id] AS [labour_operation_id],
	[charge_type] AS [charge_type]
FROM (
	SELECT T1."order_number" AS c55,
		T2."net_price_in_order" AS c56,
		T9.ben_Std_Auftrag AS c57,
		T9.Monteur_und_Stunden AS c58,
		T9."order_number" AS c59,
		T2."text_line" AS c60,
		T2."order_position" AS c61,
		T2."order_number" AS c62,
		(substring((upper(T8."description")), 1, 3)) AS c63,
		T8."description" AS c64,
		T7."description" AS c65,
		T7."make_number" AS c66,
		T6."make_number" AS c67,
		T6."license_plate" AS c68,
		T6."vin" AS c69,
		T6."internal_number" AS c70,
		CASE 
			WHEN (T6."license_plate" IS NOT NULL)
				THEN (
						(substring((convert(VARCHAR(50), T1."order_number")), 1, 6)) + ' - ' + (
							CASE 
								WHEN (T1."order_taking_employee_no" = 0)
									THEN ('SB fehlt')
								ELSE ((substring((convert(VARCHAR(50), T1."order_taking_employee_no")), 1, 4)) + ' - ' + T4."name")
								END
							) + ' - ' + T5."family_name" + ' - ' + (convert(VARCHAR(50), year(T1."order_date")) + '-' + convert(VARCHAR(50), month(T1."order_date")) + '-' + convert(VARCHAR(50), day(T1."order_date"))) + ' - ' + T6."license_plate"
						)
			ELSE (
					(substring((convert(VARCHAR(50), T1."order_number")), 1, 6)) + ' - ' + (
						CASE 
							WHEN (T1."order_taking_employee_no" = 0)
								THEN ('SB fehlt')
							ELSE ((substring((convert(VARCHAR(50), T1."order_taking_employee_no")), 1, 4)) + ' - ' + T4."name")
							END
						) + ' - ' + T5."family_name" + ' - ' + (convert(VARCHAR(50), year(T1."order_date")) + '-' + convert(VARCHAR(50), month(T1."order_date")) + '-' + convert(VARCHAR(50), day(T1."order_date")))
					)
			END AS c71,
		(substring((convert(VARCHAR(50), T5."customer_number")), 1, 7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" AS c72,
		T5."family_name" AS c73,
		T5."first_name" AS c74,
		T5."customer_number" AS c75,
		T3."code" + ' - ' + T3."description" AS c76,
		CASE 
			WHEN (T1."order_taking_employee_no" = 0)
				THEN ('SB fehlt')
			ELSE ((substring((convert(VARCHAR(50), T1."order_taking_employee_no")), 1, 4)) + ' - ' + T4."name")
			END AS c77,
		T4."name" AS c78,
		'' + (convert(VARCHAR(50), T1."subsidiary")) AS c79,
		CASE 
			WHEN (('0' + (convert(VARCHAR(50), T1."subsidiary"))) IN ('015                                                                                                                                                                                                                                                            '))
				THEN ('15')
			ELSE ('1')
			END AS c80,
		T3."description" AS c81,
		T3."code" AS c82,
		T1."clearing_delay_type" AS c83,
		T1."parts_rebate_group_sell" AS c84,
		T1."holder_number" AS c85,
		T1."paying_customer" AS c86,
		T1."order_customer" AS c87,
		T1."order_mileage" AS c88,
		T1."vehicle_number" AS c89,
		T1."order_delivery_employee_no" AS c90,
		T1."order_taking_employee_no" AS c91,
		T1."order_print_date" AS c92,
		T1."estimated_outbound_time" AS c93,
		T1."estimated_inbound_time" AS c94,
		T1."order_date" AS c95,
		T2."invoice_number" AS c96,
		T2."invoice_type" AS c97,
		T2."is_invoiced" AS c98,
		T1."subsidiary" AS c99,
		T2."labour_type" AS c100,
		T1."order_position" AS c101,
		T2."labour_operation_id" AS [labour_operation_id],
		T2."charge_type" AS [charge_type]
	FROM (
		(
			(
				(
					(
						(
							(
								(
									LOCOSOFT."dbo"."order_positions" T1 LEFT OUTER JOIN LOCOSOFT."dbo"."labours" T2 ON T1."order_number" = T2."order_number"
									) LEFT OUTER JOIN LOCOSOFT."dbo"."labour_types" T3 ON T3."code" = T2."labour_type"
								) LEFT OUTER JOIN LOCOSOFT."dbo"."employees" T4 ON T1."order_taking_employee_no" = T4."employee_number"
							) LEFT OUTER JOIN LOCOSOFT."dbo"."customers_suppliers" T5 ON T1."order_customer" = T5."customer_number"
						) LEFT OUTER JOIN LOCOSOFT."dbo"."vehicles" T6 ON T1."vehicle_number" = T6."internal_number"
					) LEFT OUTER JOIN LOCOSOFT."dbo"."makes" T7 ON T6."make_number" = T7."make_number"
				) LEFT OUTER JOIN LOCOSOFT."dbo"."models" T8 ON (T6."make_number" = T8."make_number")
				AND (T6."model_code" = T8."model_code")
			) LEFT OUTER JOIN [transform_ims]."ben_AW_Order_Number" T9 ON T2."order_number" = T9."order_number"
		)
	WHERE (
			(
				(
					(T2."invoice_number" IS NULL)
					AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
					)
				AND (T2."net_price_in_order" IS NOT NULL)
				)
			AND (T2."labour_type" <> 'F')
			AND (T2."charge_type" <> '0')
			)
		-- order by c55 asc
	) D1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
