Public/Export-Earning.ps1

function Export-Earning {
    <#
    .SYNOPSIS
        Exports earnings to a file.
 
    .DESCRIPTION
        Exports one or all earnings from a budget to CSV or JSON format.
 
    .PARAMETER OutputPath
        The file path for the export. Extension determines format (.csv or .json).
 
    .PARAMETER Name
        Optional earning name to export. If omitted, exports all earnings.
 
    .PARAMETER Format
        Output format: CSV or JSON. If not specified, determined by file extension.
 
    .PARAMETER Budget
        Optional budget name to target. Uses active budget if not specified.
 
    .PARAMETER DataPath
        Optional custom path for data storage. Overrides budget-based paths.
 
    .EXAMPLE
        Export-Earning -OutputPath "earnings.csv"
 
        Exports all earnings to a CSV file.
 
    .EXAMPLE
        Export-Earning -Name "Salary" -OutputPath "salary.json"
 
        Exports a specific earning to JSON format.
 
    .EXAMPLE
        Export-Earning -OutputPath "C:\Backups\earnings.json" -Format JSON -Budget "MyFamilyBudget"
 
        Exports all earnings from a specific budget to JSON.
 
    .OUTPUTS
        File path of the exported data
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$OutputPath,

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [ValidateSet('CSV', 'JSON')]
        [string]$Format,

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

    # Get earnings
    $getParams = @{}
    if ($Name) { $getParams['Name'] = $Name }
    if ($Budget) { $getParams['Budget'] = $Budget }
    if ($DataPath) { $getParams['DataPath'] = $DataPath }

    $earnings = Get-Earning @getParams

    # Build export parameters
    $exportParams = @{
        Data = $earnings
        OutputPath = $OutputPath
        Properties = @('Id', 'Name', 'StartDate', 'Frequency', 'Amount', 'AccountId')
        EntityType = 'earning'
    }
    if ($Format) { $exportParams['Format'] = $Format }

    # Use helper for export
    return Export-EntityData @exportParams
}