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.
 
    .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 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
 
    .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()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

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

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

        $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"
            return $earning
        }
    }
}