Get-BalanceTermOrg.ps1

<#
.Synopsis
   The function returns balance on terminal for a persons in specified oragnization centers.
.DESCRIPTION
   The function returns balance on terminal for persons in specified oragnization centers. This balance is from database table and isn't calculate.
.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 OrgCenterID
    Database ID code of organization center.
.PARAMETER IncludeChildern
    If parameter is used function returns balance for persons in specified organization center and all child organization centers.
.PARAMETER Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-BalanceTermOrg -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -OrgCenterID "3"
   Command retrives balance for a persons from specified organization center with authentication via AccessKey
.EXAMPLE
   Get-BalanceTermOrg -URL https://intranet.company.com/webtime12/api -Token $MyToken -OrgCenterID "3" -Simplified
   Command balance for a persons from specified organization center with authentication via Token, and then simplifies the acquired object.
#>

function Get-BalanceTermOrg {
    [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]$OrgCenter,
        [Parameter(Mandatory = $false)]
        [switch]$IncludeChildern,
        [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/BalanceTermOrg"

       $Body = @{
            orgcenter = $OrgCenter;
            includechildern = $IncludeChildern
       }

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

       If ($($Simplified.IsPresent)) {
            $sbalancetermorg = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($balto in $($balancetermorg)) {
                $balanto = New-Object PSObject
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name Firstname -Value $($balto.Person.Firstname)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name Surname -Value $($balto.Person.Surname)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name DegreeBefore -Value $($balto.Person.DegreeBefore)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name DegreeAfter -Value $($balto.Person.DegreeAfter)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name PersonId -Value $($balto.Person.Id)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name PersonCode -Value $($balto.Person.Code)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name Ordinal -Value $($balto.Person.Ordinal)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name Balance -Value $($balto.Balance)
                Add-Member -InputObject $balanto -MemberType NoteProperty -Name Error -Value $($balto.Error)

                $blto.Add($balanto) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Balance on Terminal."
            Write-Output $sbalancetermorg
            }
        else {
            Write-Verbose -Message "Return object Balance on Terminal."
            Write-Output $balancetermorg
        }
    }
}