Public/Export-Transfer.ps1

function Export-Transfer {
    <#
    .SYNOPSIS
        Exports transfers to a file.
 
    .DESCRIPTION
        Exports one or all transfers 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 transfer name to export. If omitted, exports all transfers.
 
    .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-Transfer -OutputPath "transfers.csv"
 
        Exports all transfers to a CSV file.
 
    .EXAMPLE
        Export-Transfer -Name "Savings" -OutputPath "savings-transfer.json"
 
        Exports a specific transfer to JSON format.
 
    .EXAMPLE
        Export-Transfer -OutputPath "C:\Backups\transfers.json" -Format JSON -Budget "MyPersonalBudget"
 
        Exports all transfers 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 transfers
    $getParams = @{}
    if ($Name) { $getParams['Name'] = $Name }
    if ($Budget) { $getParams['Budget'] = $Budget }
    if ($DataPath) { $getParams['DataPath'] = $DataPath }

    $transfers = Get-Transfer @getParams

    if (-not $transfers -or $transfers.Count -eq 0) {
        Write-Warning "No transfers found to export."
        return
    }

    # Determine format from file extension if not specified
    if (-not $Format) {
        $extension = [System.IO.Path]::GetExtension($OutputPath).ToLower()
        $Format = switch ($extension) {
            '.json' { 'JSON' }
            '.csv' { 'CSV' }
            default { 'CSV' }
        }
    }

    # Ensure directory exists
    $directory = Split-Path -Path $OutputPath -Parent
    if ($directory -and -not (Test-Path $directory)) {
        New-Item -Path $directory -ItemType Directory -Force | Out-Null
    }

    # Calculate annual amount for each transfer
    $transfersWithAnnual = $transfers | ForEach-Object {
        $annualMultiplier = switch ($_.Frequency) {
            'Daily' { 365 }
            'Weekly' { 52 }
            'BiWeekly' { 26 }
            'Monthly' { 12 }
            'Bimonthly' { 6 }
            'Quarterly' { 4 }
            'Yearly' { 1 }
            default { 1 }
        }
        $_ | Add-Member -NotePropertyName 'AnnualAmount' -NotePropertyValue ($_.Amount * $annualMultiplier) -PassThru
    }

    # Export based on format
    try {
        switch ($Format) {
            'CSV' {
                $transfersWithAnnual | Select-Object Id, Name, StartDate, Frequency, Amount, AnnualAmount, FromAccountId, ToAccountId |
                    Export-Csv -Path $OutputPath -NoTypeInformation -ErrorAction Stop
                Write-Verbose "Exported $($transfers.Count) transfer(s) to CSV: $OutputPath"
            }
            'JSON' {
                $transfersWithAnnual | ConvertTo-Json -Depth 10 |
                    Set-Content -Path $OutputPath -ErrorAction Stop
                Write-Verbose "Exported $($transfers.Count) transfer(s) to JSON: $OutputPath"
            }
        }
        return $OutputPath
    }
    catch {
        Write-Error "Failed to export transfers: $_"
    }
}