Get-MonthlyAccount.ps1

<#
.Synopsis
   The function returns calculated monthly accounts for a person.
.DESCRIPTION
   The function calculated monthly accounts for a person and specified period.
.PARAMETER URL
    Server API url.
.PARAMETER Token
    Authentication token for accessing API data. If you use the token for authentication, don't enter access key.
.PARAMETER AccessKey
    The AccessKey to get an authentication token for accessing API data. If you use the access key to get authentication token, don't enter token.
.PARAMETER PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.PARAMETER DateFrom
    Date from which data is returned. If not specified, the first day of the month will be used.
.PARAMETER DateTo
    Date to which data is returned. If not specified, the current date will be used.
.PARAMETER Exportable
    With parameter function returns exportable accounts only.
.PARAMETER Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-MonthlyAccount -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045"
   Command retrives daily accounts for a person identified by PersonCode with authentication via AccessKey
.EXAMPLE
   Get-MonthlyAccount -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -Simplified
   Command retrives daily accounts for a person identified by PersonCode with authentication via Token, and then simplifies the acquired object.
#>

function Get-MonthlyAccount {
    [CmdletBinding(DefaultParameterSetName='AccessKey')]
    param(
        [Parameter(Mandatory = $true)]
        [string]$URL,
        [Parameter(Mandatory = $true,ParameterSetName='Token')]
        [string]$Token,
        [Parameter(Mandatory = $true,ParameterSetName='AccessKey')]
        [string]$AccessKey,
        [Parameter(Mandatory = $false)]
        [string]$PersonCode,
        [Parameter(Mandatory = $false)]
        [string]$PersonID,
        [Parameter(Mandatory = $false)]
        [string]$DateFrom = $(Get-Date -Format yyyy-MM-01T00:00:00.00Z),
        [Parameter(Mandatory = $false)]
        [string]$DateTo = $(Get-Date -Format yyyy-MM-ddT00:00:00.00Z),
        [Parameter(Mandatory = $false)]
        [switch]$Exportable,
        [Parameter(Mandatory = $false)]
        [switch]$Simplified
    )

    Process {

       If ($PSCmdlet.ParameterSetName -eq 'AccessKey') {
            $SchemeToken = Get-Token -URL $URL -AccessKey $AccessKey
            $Token = $SchemeToken.Scheme + " " + $SchemeToken.Token
            }

       $url = $url + "/WorkSheet/MonthlyAccounts"

       $Body = @{
            exportableonly = $Exportable.IsPresent ;
            datefrom = $DateFrom;
            dateto = $DateTo;
            personcode = $PersonCode;
            personid = $PersonId
       }

       $json = $body | ConvertTo-Json
       Write-Verbose -Message "Send request to API endpoint $URL with access key $Token."
       $MonthlyAccount = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'

       If ($($Simplified.IsPresent)) {
            $sMonthlyAccount = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($moac in $($MonthlyAccount.MonthlyAccounts)) {
                $maccount = New-Object PSObject
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Firstname -Value $($MonthlyAccount.Person.Firstname)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Surname -Value $($MonthlyAccount.Person.Surname)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name DegreeBefore -Value $($MonthlyAccount.Person.DegreeBefore)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name DegreeAfter -Value $($MonthlyAccount.Person.DegreeAfter)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name PersonId -Value $($MonthlyAccount.Person.Id)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name PersonCode -Value $($MonthlyAccount.Person.Code)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Ordinal -Value $($MonthlyAccount.Person.Ordinal)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Date -Value $($moac.Date)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountId -Value $($moac.Account.Id)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountName -Value $($moac.Account.Name)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountShortage -Value $($moac.Account.Shortage)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountPriority -Value $($moac.Account.Priority)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountType -Value $($moac.Account.Type)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountExportHourCode -Value $($moac.Account.ExportHourCode)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountExportShiftCode -Value $($moac.Account.ExportShiftCode)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name AccountExportable -Value $($moac.Account.Exportable)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Minutes -Value $($moac.Minutes)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name Days -Value $($moac.Days)
                Add-Member -InputObject $maccount -MemberType NoteProperty -Name CostCenterId -Value $($moac.CostCenterId)

                $ma.Add($maccount) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Monthly Account."
            Write-Output $sMonthlyAccount
            }
        else {
            Write-Verbose -Message "Return object Monthly Account."
            Write-Output $MonthlyAccount
        }
    }
}