Pax8API/Public/Get-Pax8PriceBook.ps1

function Get-Pax8PriceBook {
    [CmdletBinding()]
    param (
        [string]$Search,

        [string]$VendorName,

        [guid]$CompanyId,

        [ValidateRange(1, 200)]
        [int]$Size = 10,

        [switch]$All,

        [switch]$UsePortalRatePlanFallback,

        [ValidateRange(1, [int]::MaxValue)]
        [int]$PortalCurrencyId = 9,

        [int]$ThrottleMilliseconds = 0
    )

    $productParameters = @{}
    if ($Search) { $productParameters.Search = $Search }
    if ($VendorName) { $productParameters.VendorName = $VendorName }

    if ($Search) {
        if ($All) {
            $products = @(Find-Pax8Product @productParameters -All)
        } else {
            $products = @(Find-Pax8Product @productParameters -Size $Size)
        }
    } elseif ($All) {
        $products = @(Get-Pax8Product @productParameters -All)
    } else {
        $products = @((Get-Pax8Product @productParameters -Size $Size -Raw).content)
    }

    foreach ($product in $products) {
        if (-not $product.id) {
            continue
        }

        $pricingParameters = @{ ProductId = $product.id }
        if ($CompanyId) {
            $pricingParameters.CompanyId = $CompanyId
        }

        $pricing = $null
        $pricingError = $null
        try {
            $pricing = Get-Pax8ProductPricing @pricingParameters -Raw
        } catch {
            $pricingError = $_
            if (-not $UsePortalRatePlanFallback) {
                Write-Warning "Pricing lookup failed for product '$($product.id)' '$($product.name)': $($_.Exception.Message)"
                continue
            }
        }

        $publicRowsReturned = $false
        if ($pricing) {
            foreach ($price in @($pricing.content)) {
                foreach ($rate in @($price.rates)) {
                    $publicRowsReturned = $true
                    [pscustomobject]@{
                        ProductId = $price.productId
                        ProductName = $price.productName
                        VendorName = $product.vendorName
                        BillingTerm = $price.billingTerm
                        CommitmentTerm = $price.commitmentTerm
                        CommitmentTermInMonths = $price.commitmentTermInMonths
                        Type = $price.type
                        UnitOfMeasurement = $price.unitOfMeasurement
                        ChargeType = $rate.chargeType
                        PartnerBuyRate = $rate.partnerBuyRate
                        SuggestedRetailPrice = $rate.suggestedRetailPrice
                        StartQuantityRange = $rate.startQuantityRange
                        EndQuantityRange = $rate.endQuantityRange
                        PricingSource = 'PartnerOpenApi'
                    }
                }
            }
        }

        if ($UsePortalRatePlanFallback -and -not $publicRowsReturned) {
            if ($pricingError) {
                Write-Verbose "Falling back to Pax8 portal rate plans for product '$($product.id)' '$($product.name)' after pricing error: $($pricingError.Exception.Message)"
            } else {
                Write-Verbose "Falling back to Pax8 portal rate plans for product '$($product.id)' '$($product.name)' because OpenAPI pricing returned no rows."
            }

            try {
                $portalRows = @(Get-Pax8PortalProductRatePlan -ProductId ([guid]$product.id) -CurrencyId $PortalCurrencyId -UseChildProductsForArrears -Unique)
                foreach ($portalRow in $portalRows) {
                    [pscustomobject]@{
                        ProductId = $portalRow.SourceProductId
                        ProductName = $portalRow.ProductName
                        VendorName = $portalRow.VendorName
                        BillingTerm = $portalRow.BillingTerm
                        CommitmentTerm = $null
                        CommitmentTermInMonths = $null
                        Type = $portalRow.Type
                        UnitOfMeasurement = $null
                        ChargeType = if ([string]$portalRow.ProductName -match 'Arrears Charge') { 'Arrears Charge' } else { $null }
                        PartnerBuyRate = $portalRow.PartnerBuyRate
                        SuggestedRetailPrice = $portalRow.SuggestedRetailPrice
                        StartQuantityRange = $portalRow.RangeStart
                        EndQuantityRange = $portalRow.RangeEnd
                        PricingSource = $portalRow.PricingSource
                        SourceProductName = $portalRow.SourceName
                        SourceProductSku = $portalRow.SourceSku
                        SourceLegacyId = $portalRow.SourceLegacyId
                        PortalProductLegacyId = $portalRow.ProductLegacyId
                        PortalRatePlanId = $portalRow.RatePlanId
                        PortalRatePlanUuid = $portalRow.RatePlanUuid
                        PortalCurrencyId = $portalRow.CurrencyId
                        SkuBand = $portalRow.SkuBand
                        VendorSkuBand = $portalRow.VendorSkuBand
                    }
                }
            } catch {
                Write-Warning "Portal rate-plan fallback failed for product '$($product.id)' '$($product.name)': $($_.Exception.Message)"
            }
        }

        if ($ThrottleMilliseconds -gt 0) {
            Start-Sleep -Milliseconds $ThrottleMilliseconds
        }
    }
}