Get-DailyAccount.ps1

<#
.Synopsis
   The function returns calculated daily accounts for a person.
.DESCRIPTION
   The function calculated daily 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-DailyAccount -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-DailyAccount -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-DailyAccount {
    [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/DailyAccounts"

       $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."
       $DailyAccount = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'

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

                $da.Add($daccount) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Daily Account."
            Write-Output $sDailyAccount
            }
        else {
            Write-Verbose -Message "Return object Daily Account."
            Write-Output $DailyAccount
        }
    }
}