Private/Get-CalculatedAmount.ps1
|
function Get-CalculatedAmount { <# .SYNOPSIS Calculates amount for a target period based on frequency. .DESCRIPTION Converts an amount from one frequency to a target period (annual, monthly, quarterly, etc.). This helper function eliminates duplicated calculation logic across export functions. .PARAMETER Amount The base amount to calculate from. .PARAMETER Frequency The frequency of the base amount (Weekly, BiWeekly, Monthly, Bimonthly, Quarterly, Yearly). .PARAMETER TargetPeriod The target period to calculate for. Defaults to 'Annual'. Supported values: Annual, Monthly, Quarterly, Weekly, Daily .EXAMPLE Get-CalculatedAmount -Amount 100 -Frequency 'Weekly' -TargetPeriod 'Annual' # Returns 5200 (100 * 52) .EXAMPLE Get-CalculatedAmount -Amount 1200 -Frequency 'Yearly' -TargetPeriod 'Monthly' # Returns 100 (1200 / 12) .OUTPUTS [decimal] The calculated amount for the target period #> [CmdletBinding()] [OutputType([decimal])] param( [Parameter(Mandatory)] [decimal]$Amount, [Parameter(Mandatory)] [ValidateSet('Weekly', 'BiWeekly', 'Monthly', 'Bimonthly', 'Quarterly', 'Yearly')] [string]$Frequency, [Parameter()] [ValidateSet('Annual', 'Monthly', 'Quarterly', 'Weekly', 'Daily')] [string]$TargetPeriod = 'Annual' ) # First, convert to annual amount $annualAmount = switch ($Frequency) { 'Weekly' { $Amount * 52 } 'BiWeekly' { $Amount * 26 } 'Monthly' { $Amount * 12 } 'Bimonthly' { $Amount * 6 } 'Quarterly' { $Amount * 4 } 'Yearly' { $Amount } default { $Amount * 12 } } # Then convert from annual to target period $result = switch ($TargetPeriod) { 'Annual' { $annualAmount } 'Monthly' { $annualAmount / 12 } 'Quarterly' { $annualAmount / 4 } 'Weekly' { $annualAmount / 52 } 'Daily' { $annualAmount / 365 } default { $annualAmount } } return [Math]::Round($result, 2) } |