Public/New-Earning.ps1

function New-Earning {
    <#
    .SYNOPSIS
        Creates a new earning entry.
 
    .DESCRIPTION
        Creates a new earning with specified name, start date, frequency, and amount.
        The earning is saved to the data store and associated with an account.
 
    .PARAMETER Name
        The name of the earning.
 
    .PARAMETER StartDate
        The date when the earning starts.
 
    .PARAMETER Frequency
        How often the earning occurs (Daily, Weekly, BiWeekly, Monthly, Quarterly, Yearly).
 
    .PARAMETER Amount
        The amount of the earning.
 
    .PARAMETER Account
        The account name to associate with this earning. If only one account exists,
        it will be auto-selected. If the account doesn't exist, it will be created.
 
    .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
        New-Earning -Name "Salary" -StartDate "2025-01-01" -Frequency BiWeekly -Amount 2500.00
 
    .EXAMPLE
        New-Earning -Name "Freelance" -StartDate "2025-01-01" -Frequency Monthly -Amount 500 -Account "Chase Checking"
 
    .OUTPUTS
        Earning object
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]$Name,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [datetime]$StartDate,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateSet('Daily', 'Weekly', 'BiWeekly', 'Monthly', 'Bimonthly', 'Quarterly', 'Yearly')]
        [string]$Frequency,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [decimal]$Amount,

        [Parameter(ValueFromPipelineByPropertyName)]
        [string]$Account,

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

    process {
        $resolvedPath = Resolve-DataPath -DataPath $DataPath -Budget $Budget
        if (-not $resolvedPath) { return }

        # Resolve account parameter
        $accountId = Resolve-AccountParameter -Account $Account -DataPath $resolvedPath -CallingCmdlet $PSCmdlet
        if (-not $accountId) { return }

        $earning = [Earning]::new($Name, $StartDate, $Frequency, $Amount, $accountId)

        $existingEarnings = Read-EntityData -EntityType 'Earning' -DataPath $resolvedPath
        $earningsList = [System.Collections.ArrayList]@($existingEarnings)
        $earningsList.Add($earning.ToHashtable()) | Out-Null

        if (Write-EntityData -EntityType 'Earning' -Data $earningsList -DataPath $resolvedPath) {
            Write-Verbose "Created earning: $Name (Account: $accountId)"
            return $earning
        }
    }
}