Public/Set-Biller.ps1

function Set-Biller {
    <#
    .SYNOPSIS
        Updates an existing biller entry.
 
    .DESCRIPTION
        Modifies properties of an existing biller identified by ID.
 
    .PARAMETER Id
        The ID of the biller to update.
 
    .PARAMETER Name
        New name for the biller.
 
    .PARAMETER StartDate
        New start date for the biller.
 
    .PARAMETER Frequency
        New frequency for the biller.
 
    .PARAMETER Amount
        New amount for the biller.
 
    .PARAMETER Account
        The account name to associate with this biller. If the account doesn't exist,
        it will be created with a warning.
 
    .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
        Set-Biller -Id "abc123" -Amount 150.00
 
    .EXAMPLE
        Set-Biller -Id "abc123" -Account "Chase Checking"
 
    .EXAMPLE
        Set-Biller -Id "abc123" -Amount 150.00 -Budget "daily-expenses"
 
    .OUTPUTS
        Updated Biller object
    #>

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

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [datetime]$StartDate,

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

        [Parameter()]
        [decimal]$Amount,

        [Parameter()]
        [string]$Account,

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

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

        $billers = Read-EntityData -EntityType 'Biller' -DataPath $resolvedPath
        $biller = $billers | Where-Object { $_.Id -eq $Id }

        if (-not $biller) {
            Write-Error "Biller with ID '$Id' not found."
            return
        }

        if ($PSBoundParameters.ContainsKey('Name')) { $biller.Name = $Name }
        if ($PSBoundParameters.ContainsKey('StartDate')) { $biller.StartDate = $StartDate }
        if ($PSBoundParameters.ContainsKey('Frequency')) { $biller.Frequency = $Frequency }
        if ($PSBoundParameters.ContainsKey('Amount')) { $biller.Amount = $Amount }
        
        if ($PSBoundParameters.ContainsKey('Account')) {
            # Find the account by name
            $accounts = Read-EntityData -EntityType 'Account' -DataPath $resolvedPath
            $matchingAccount = $accounts | Where-Object { $_.Name -eq $Account }
            
            if ($matchingAccount) {
                $accountId = $matchingAccount.Id
            }
            else {
                # Create the account with a warning
                Write-Warning "Account '$Account' not found. Creating new account..."
                $newAccount = New-Account -Name $Account -Bank 'Unknown' -Last4Digits '0000' -DataPath $resolvedPath
                if ($newAccount) {
                    $accountId = $newAccount.Id
                }
            }
            
            # Handle legacy data that may not have AccountId property
            if ($biller.PSObject.Properties.Name -contains 'AccountId') {
                $biller.AccountId = $accountId
            }
            else {
                $biller | Add-Member -NotePropertyName 'AccountId' -NotePropertyValue $accountId -Force
            }
        }

        if (Write-EntityData -EntityType 'Biller' -Data $billers -DataPath $resolvedPath) {
            Write-Verbose "Updated biller: $($biller.Name)"
            return $biller
        }
    }
}