-
Notifications
You must be signed in to change notification settings - Fork 0
/
persons.ps1
423 lines (376 loc) · 23.1 KB
/
persons.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Init auth
$clientId = "<YOUR CLIENTID"
$clientSecret = "YOUR CLIENTSECRET"
# Init endpoints
$uriAuth = "https://api.ctbps.nl/v3/OAuth/Token"
$uriPerson = "https://api.ctbps.nl/v3/odata/Person"
$uriCurrentEmployment = "https://api.ctbps.nl/v3/odata/CurrentEmployment"
$uriEmploymentHistory = "https://api.ctbps.nl/v3/odata/EmploymentHistory"
$uriFunction = "https://api.ctbps.nl/v3/odata/Function"
$uriDepartment = "https://api.ctbps.nl/v3/odata/Department"
$uriOrganization = "https://api.ctbps.nl/v3/odata/Organization"
$uriAddresses = "https://api.ctbps.nl/v3/odata/Address"
$uriTelephone = "https://api.ctbps.nl/v3/odata/Phone"
$uriGroups = "https://api.ctbps.nl/v3/odata/Group"
$uriGroupParticipant = "https://api.ctbps.nl/v3/odata/GroupParticipant"
$uriEmail = "https://api.ctbps.nl/v3/odata/Email"
$uriCostCenters = "https://api.ctbps.nl/v3/odata/CompanyCostCenters(companyid=guid'0f699459-0391-e511-80cf-44a8421bf766')"
$uriCostCenterAllocations = "https://api.ctbps.nl/v3/odata/CostCenterAllocation"
$uriSalaryEmployments = "https://api.ctbps.nl/v3/odata/SalaryEmployment"
$uriCostCenters = 'https://api.ctbps.nl/v3/odata/CompanyCostCenter?$filter=CompanyId%20eq%20(guid%2753edff19-0391-e511-80cf-44a8421bf766%27)'
# Enable TLS 1.2
if ([Net.ServicePointManager]::SecurityProtocol -notmatch "Tls12") {
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
}
Write-Verbose "Starting..." -Verbose
$pair = "${clientId}:${clientSecret}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$headers = @{ "Authorization" = $basicAuthValue; }
$body = @{ "scope" = "customer"; "grant_type" = "client_credentials"; }
Write-Verbose "Authenticating..." -Verbose
$result = Invoke-WebRequest -Method POST -Uri $uriAuth -Headers $headers -Body $body -ContentType "application/x-www-form-urlencoded"
$content = $result.Content | ConvertFrom-Json
$accessToken = $content.access_token
$headers = @{ "Authorization" = "Bearer $accessToken" }
Write-Verbose "Retrieving data from endpoints..." -Verbose
try {
$data = Invoke-RestMethod -Method GET -Uri $uriPerson -Headers $headers
$persons = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriAddresses -Headers $headers
$addresses = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriCurrentEmployment -Headers $headers
$currentEmployments = $data.value
$currentAssignments = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriEmploymentHistory -Headers $headers
$employmentHistory = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriFunction -Headers $headers
$functions = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriDepartment -Headers $headers
$departments = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriOrganization -Headers $headers
$organizations = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriTelephone -Headers $headers
$telphoneNumbers = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriGroups -Headers $headers
$Groups = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriGroupParticipant -Headers $headers
$GroupParticipants = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriEmail -Headers $headers
$Emailaddresses = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriCostCenterAllocations -Headers $headers
$CCAllocations = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriSalaryEmployments -Headers $headers
$SalaryEmployments = $data.value
$data = Invoke-RestMethod -Method GET -Uri $uriCostCenters -Headers $headers
$CostCenters = $data.value
}
catch {
Write-Verbose $_
Write-Verbose "Failure in retrieving data from endpoints, aborting..." -Verbose
exit
}
# Group data for processing
Write-Verbose "Grouping data..." -Verbose
$currentEmployments = $currentEmployments | Group-Object PersonId -AsHashTable
$currentAssignments = $currentAssignments | Group-Object PersonId -AsHashTable
$employmentHistory = $employmentHistory | Group-Object PersonId -AsHashTable
$functions = $functions | Group-Object Code -AsHashTable
$organizations = $organizations | Group-Object ID -AsHashTable
$addresses = $addresses | Group-Object PersonId -AsHashTable
$telphoneNumbers = $telphoneNumbers | Group-Object PersonId -AsHashTable
$Groups = $Groups | Group-Object ID -AsHashTable
$GroupParticipants = $GroupParticipants | Group-Object ParticipantId -AsHashTable
$Emailaddresses = $Emailaddresses | Group-Object PersonId -AsHashTable
$SalaryEmployments = $SalaryEmployments | Group-Object PersonId -AsHashTable
$CCAllocations = $CCAllocations | Group-Object SalaryEmploymentId -AsHashTable
$CostCenters = $CostCenters | Group-Object ID -AsHashTable
# Extend the persons with employments and required fields
Write-Verbose "Augmenting persons..." -Verbose
$persons | Add-Member -MemberType NoteProperty -Name "Contracts" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "Addresses" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "PrivateEmailAddress" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "ExternalId" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "MobileWork" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "Groups" -Value $null -Force
#Setting Global Vars
$today = Get-Date
$persons | ForEach-Object {
# Map required fields
$_.ExternalId = $_.ID
$_.DisplayName = "$($_.NameComplete) ($($_.PersonNumber))"
$activeEmployee = $false
# Add the contracts with full function name and clear salary values
$contracts = @();
$Contract = @();
$assignment = @();
$counter = 0
#If($_.PersonNumber -eq "14700"){ #Enable this for testing one person
$personContracts = $employmentHistory[$_.ID]
if ($null -ne $personContracts) {
foreach($contractitem in $personContracts){
$Contract = @();
$counter = $counter + 1
$ContractStartDate = $contractitem.EpisodeStartDate
if(-not([string]::IsNullOrEmpty($ContractStartDate))){
$ContractStart = [datetime]$ContractStartDate
}
$ContractEndDate = $contractitem.EpisodeEndDate
if(-not([string]::IsNullOrEmpty($ContractEndDate))){
$ContractEnd = [datetime]$ContractEndDate
} else {
$ContractEndDate = "2999-12-31"
$ContractEnd = [datetime]$ContractEndDate
}
$StartDatePeriod = $ContractStart.AddDays(-45)
$EndDatePeriod = $ContractEnd.AddDays(45)
If(($StartDatePeriod -lt $today) -And ($EndDatePeriod -gt $today)){
$fullfunction = $functions[$contractitem.FunctionCode]
If($null -ne $fullfunction){
$functionLongName = $fullfunction.LongName
}
if($null -eq $fullfunction){
$functionLongName = $null
}
$activeEmployee = $true
$Contract = [PSCustomObject]@{
ID = $contractitem.ContractId + "_" + $counter + "C"
PersonId = $contractitem.PersonId
OrganizationId = $contractitem.OrganizationId
EpisodeStartDate = $contractitem.EpisodeStartDate
EpisodeEndDate = $contractitem.EpisodeEndDate
EmploymentId = $contractitem.EmploymentId
EmploymentStatus = $contractitem.EmploymentStatus
EmploymentStartDate = $contractitem.EmploymentStartDate
AnniversaryEmploymentDate = $contractitem.AnniversaryEmploymentDate
EmploymentStartReason = $contractitem.EmploymentStartReason
EmploymentEndDate = $contractitem.EmploymentEndDate
EmploymentEndReason = $contractitem.EmploymentEndReason
ProbationDuration = $contractitem.ProbationDuration
ProbationEndDate = $contractitem.ProbationEndDate
ResignationPeriod = $contractitem.ResignationPeriod
ResignationRequestDate = $contractitem.ResignationRequestDate
ContractId = $contractitem.ContractId
ContractStartDate = $contractitem.ContractStartDate
ContractEndDate = $contractitem.ContractEndDate
ContractType = $contractitem.ContractType
ContractDuration = $contractitem.ContractDuration
ContractIndex = $contractitem.ContractIndex
ParttimePercentage = $contractitem.ParttimePercentage
DaysPerWeek = $contractitem.DaysPerWeek
IsMinMaxWorker = $contractitem.IsMinMaxWorker
MinHoursPerWeek = $contractitem.MinHoursPerWeek
MaxHoursPerWeek = $contractitem.MaxHoursPerWeek
IsOnCallWorker = $contractitem.IsOnCallWorker
PersonFunctionId = $contractitem.PersonFunctionId
FunctionId = $contractitem.FunctionId
FunctionStartDate = $contractitem.FunctionStartDate
FunctionEndDate = $contractitem.FunctionEndDate
FunctionStartReason = $contractitem.FunctionStartReason
FunctionCode = $contractitem.FunctionCode
FunctionName = $contractitem.FunctionName
FunctionLongName = $functionLongName
RoomNumber = $contractitem.RoomNumber
DepartmentId = $contractitem.DepartmentId
DepartmentCode = $contractitem.DepartmentCode
DepartmentName = $contractitem.DepartmentName
DepartmentNameShort = $contractitem.DepartmentNameShort
CostCenterId = $contractitem.CostCenterId
CostCenterCode = $contractitem.CostCenterCode
CostCenterName = $contractitem.CostCenterName
PersonSalaryId = $contractitem.PersonSalaryId
SalaryId = $contractitem.SalaryId
SalaryStartDate = $contractitem.SalaryStartDate
SalaryEndDate = $contractitem.SalaryEndDate
SalaryTable = $null
SalaryReason = $contractitem.SalaryReason
Scale = $contractitem.Scale
Step = $contractitem.Step
Period = $contractitem.Period
VariantSalary = $null
VariantHourlyWage = $contractitem.VariantHourlyWage
NettoHourlyWage1 = $contractitem.NettoHourlyWage1
NettoHourlyWage2 = $contractitem.NettoHourlyWage2
ManagerName = $contractitem.ManagerName
ManagerId = $contractitem.ManagerId
SubstituteManagerName = $contractitem.SubstituteManagerName
SubstituteManagerId = $contractitem.SubstituteManagerId
TimeScheduleId = $contractitem.TimeScheduleId
TimeScheduleStartDate = $contractitem.TimeScheduleStartDate
TimeScheduleEndDate = $contractitem.TimeScheduleEndDate
PersonStandardHoursPerWeek = $contractitem.PersonStandardHoursPerWeek
ReasonContractChange = $contractitem.ReasonContractChange
Payment = $contractitem.Payment
EmployeeType = $contractitem.EmployeeType
LocationName = $contractitem.LocationName
LocationId = $contractitem.LocationId
PrimaryContract = 1
}
} $contracts += $Contract
}
}
#} #Enable this for testing one person
If($activeEmployee -eq $true){
$PersonSalaryEmployments = $SalaryEmployments[$_.ID]
if ($null -ne $PersonSalaryEmployments){
foreach($employment in $PersonSalaryEmployments){
$salaryID = $employment.ID
$assignedCC = $CCAllocations[$salaryID]
if ($null -ne $assignedCC){
foreach($cc in $assignedCC){
$tmp_assignedCC = $null
$tmp_costcenter = $null
$tmp_department = $null
$additionalDepID = $null
$additionalDepCode = $null
$additionalDepName = $null
$additionalDepNameShort = $null
if($null -ne $cc.CompanyCostCenterId){
$tmp_assignedCC = $cc.CompanyCostCenterId
$tmp_costcenter = $CostCenters[$tmp_assignedCC]
If($null -ne $tmp_costcenter){
$costingCode = $tmp_costcenter.CostingCode
$tmp_department = $departments | Where-Object DepartmentCode -eq $costingCode
$additionalDepID = $tmp_department.ID
$additionalDepCode = $tmp_department.DepartmentCode
$additionalDepName = $tmp_Department.Name
$additionalDepNameShort = $tmp_Department.ShortName
$personAssignments = $currentEmployments[$_.ID]
if ($null -ne $personAssignments) {
foreach($assignment in $personAssignments){
If($additionalDepCode -ne $assignment.DepartmentCode){
$fullfunction = $functions[$assignment.FunctionCode]
If($null -ne $fullfunction){
$functionLongName = $fullfunction.LongName
}
if($null -eq $fullfunction){
$functionLongName = $null
}
$assignment = [PSCustomObject]@{
ID = $assignment.ContractId + $additionalDepCode
PersonId = $assignment.PersonId
OrganizationId = $assignment.OrganizationId
EpisodeStartDate = $assignment.EpisodeStartDate
EpisodeEndDate = $assignment.EpisodeEndDate
EmploymentId = $assignment.EmploymentId
EmploymentStatus = $assignment.EmploymentStatus
EmploymentStartDate = $assignment.EmploymentStartDate
AnniversaryEmploymentDate = $assignment.AnniversaryEmploymentDate
EmploymentStartReason = $assignment.EmploymentStartReason
EmploymentEndDate = $assignment.EmploymentEndDate
EmploymentEndReason = $assignment.EmploymentEndReason
ProbationDuration = $assignment.ProbationDuration
ProbationEndDate = $assignment.ProbationEndDate
ResignationPeriod = $assignment.ResignationPeriod
ResignationRequestDate = $assignment.ResignationRequestDate
ContractId = $assignment.ContractId
ContractStartDate = $assignment.ContractStartDate
ContractEndDate = $assignment.ContractEndDate
ContractType = $assignment.ContractType
ContractDuration = $assignment.ContractDuration
ContractIndex = $assignment.ContractIndex
ParttimePercentage = $assignment.ParttimePercentage
DaysPerWeek = $assignment.DaysPerWeek
IsMinMaxWorker = $assignment.IsMinMaxWorker
MinHoursPerWeek = $assignment.MinHoursPerWeek
MaxHoursPerWeek = $assignment.MaxHoursPerWeek
IsOnCallWorker = $assignment.IsOnCallWorker
PersonFunctionId = $assignment.PersonFunctionId
FunctionId = $assignment.FunctionId
FunctionStartDate = $assignment.FunctionStartDate
FunctionEndDate = $assignment.FunctionEndDate
FunctionStartReason = $assignment.FunctionStartReason
FunctionCode = $assignment.FunctionCode
FunctionName = $assignment.FunctionName
FunctionLongName = $functionLongName
RoomNumber = $assignment.RoomNumber
DepartmentId = $additionalDepID
DepartmentCode = $additionalDepCode
DepartmentName = $additionalDepName
DepartmentNameShort = $additionalDepNameShort
CostCenterId = $assignment.CostCenterId
CostCenterCode = $assignment.CostCenterCode
CostCenterName = $assignment.CostCenterName
PersonSalaryId = $assignment.PersonSalaryId
SalaryId = $assignment.SalaryId
SalaryStartDate = $assignment.SalaryStartDate
SalaryEndDate = $assignment.SalaryEndDate
SalaryTable = $null
SalaryReason = $assignment.SalaryReason
Scale = $assignment.Scale
Step = $assignment.Step
Period = $assignment.Period
VariantSalary = $null
VariantHourlyWage = $assignment.VariantHourlyWage
NettoHourlyWage1 = $assignment.NettoHourlyWage1
NettoHourlyWage2 = $assignment.NettoHourlyWage2
ManagerName = $assignment.ManagerName
ManagerId = $assignment.ManagerId
SubstituteManagerName = $assignment.SubstituteManagerName
SubstituteManagerId = $assignment.SubstituteManagerId
TimeScheduleId = $assignment.TimeScheduleId
TimeScheduleStartDate = $assignment.TimeScheduleStartDate
TimeScheduleEndDate = $assignment.TimeScheduleEndDate
PersonStandardHoursPerWeek = $assignment.PersonStandardHoursPerWeek
ReasonContractChange = $assignment.ReasonContractChange
Payment = $assignment.Payment
EmployeeType = $assignment.EmployeeType
LocationName = $assignment.LocationName
LocationId = $assignment.LocationId
PrimaryContract = 0
}
}
}
}
$contracts += $assignment
}
}
}
}
}
}
}
If($contracts.length -gt 0){
$_.Contracts = $contracts
}
# Add the addresses
$personAddresses = $addresses[$_.ID]
if ($null -ne $personAddresses) {
$personHomeAddress = $personAddresses | Where-Object isPostAddress -eq $true
$_.Addresses = $personHomeAddress
}
# Add the Mobile number Work
$persontelphoneNumbers = $telphoneNumbers[$_.ID]
if ($null -ne $persontelphoneNumbers) {
$personMobileWork = $persontelphoneNumbers | Where-Object PhoneType -eq "Mobiel Werk"
$MobileWorkSet = $personMobileWork | Select-Object PhoneNumber -First 1
$_.MobileWork = $MobileWorkSet.PhoneNumber
}
# Add the Group Memberships
$personGroups = @();
$personGroupMemberships = $GroupParticipants[$_.ID]
if ($null -ne $personGroupMemberships) {
ForEach($groupItem in $personGroupMemberships){
$groupItem | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $null -Force
$groupSelect = $groups[$groupItem.GroupId]
$groupName = $groupSelect.Name
$groupItem.GroupName = $groupName
$personGroups += $groupItem
}
}
$_.Groups = $personGroups
}
# Make sure persons are unique
$persons = $persons | Sort-Object ExternalId -Unique
# Make sure the persons have contracts
$persons = $persons | Where-Object { $null -ne $_.Contracts }
# Make sure to output per person to allow for streaming
Write-Verbose "Uploading persons..." -Verbose
$persons | ForEach-Object {
$jsonPerson = $_ | ConvertTo-Json -Depth 3 -Compress
Write-Output $jsonPerson
Start-Sleep -Milliseconds 50
}
Write-Verbose "Done." -Verbose