﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
	<Reports>
		<Report id="f40f6725-7976-4dad-9d3c-4852f27fdc35" codekey="department_managers" categoryCodekey="UserStats" name="GCL Mail-Adressen Führungskräfte" description="Retrieving the department managers, also as a multiple assignment, including their business e-Mail adress for automatic operations">
			<MetaData created="2020-05-11T16:59:11" createdBy="Betz, Holger (holger.betz1)" createdBy_user_id="37276" modified="2020-09-02T15:10:39" modifiedBy="ELECT, Administrator (Administrator)" modifiedBy_user_id="2" />
			<ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
			<Mandators mandatorMode="IncludeMandators" mandator_id="d5a90a49-e697-4aeb-8a5b-298782815490" mandatorName="ELECT" isStandard="False" isUsedByMenu="False" />
			<Roles>
				<Role id="90" />
			</Roles>
			<command>
				CREATE TABLE #departmentManagers
				(
				user_id INT,
				managers NVARCHAR(MAX) NULL,
				company NVARCHAR(200) NULL,
				company2 NVARCHAR(200) NULL
				)

				DECLARE @codeKey NVARCHAR(200)
				SET @codeKey = 'Manager'

				INSERT INTO #departmentManagers
				SELECT
				v_Users.intUserCn, -- user which is a manager
				Nachname + ', ' + Vorname + ' (' +  uc.email + ');' -- mail to notify the user itself
				,NULL, NULL
				FROM tblOrganisationRolesOfUsers AS orou
				INNER JOIN tblOrganisationRoles ON tblOrganisationRoles.id = orou.organisationRole_id AND tblOrganisationRoles.codekey = @codeKey
				INNER JOIN v_Users ON v_Users.intUserCn = orou.user_id
				LEFT JOIN tblUsersContacts AS uc ON uc.user_id = v_Users.intUserCn AND uc.contactType_id = 1
				WHERE EXISTS(
				SELECT TOP 1 * FROM tblUsersOrganisationRolesUsers WHERE userIs_id = v_Users.intUserCn
				)
				AND ISNULL(uc.email,'') &lt;&gt; ''
				AND v_Users.userStatus NOT IN (2,3)

				UPDATE #departmentManagers
				SET company = v_Users.companyAD,
				company2 = uca.customAttribute3
				FROM #departmentManagers
				INNER JOIN v_Users ON v_Users.intUserCn = #departmentManagers.user_id
				LEFT JOIN tblUserCustomAttributes AS uca ON uca.user_id = v_Users.intUserCn

				SELECT * FROM #departmentManagers
				WHERE managers IS NOT NULL
				ORDER BY user_id


				DROP TABLE #departmentManagers
			</command>
		</Report>
	</Reports>
</ReportsExport>