Public/Set-Earning.ps1
|
function Set-Earning { <# .SYNOPSIS Updates an existing earning entry. .DESCRIPTION Modifies properties of an existing earning identified by ID. .PARAMETER Id The ID of the earning to update. .PARAMETER Name New name for the earning. .PARAMETER StartDate New start date for the earning. .PARAMETER Frequency New frequency for the earning. .PARAMETER Amount New amount for the earning. .PARAMETER Account The account name to associate with this earning. 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-Earning -Id "abc123" -Amount 2750.00 .EXAMPLE Set-Earning -Id "abc123" -Account "Chase Checking" .OUTPUTS Updated Earning 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 } $earnings = Read-EntityData -EntityType 'Earning' -DataPath $resolvedPath $earning = $earnings | Where-Object { $_.Id -eq $Id } if (-not $earning) { Write-Error "Earning with ID '$Id' not found." return } if ($PSBoundParameters.ContainsKey('Name')) { $earning.Name = $Name } if ($PSBoundParameters.ContainsKey('StartDate')) { $earning.StartDate = $StartDate } if ($PSBoundParameters.ContainsKey('Frequency')) { $earning.Frequency = $Frequency } if ($PSBoundParameters.ContainsKey('Amount')) { $earning.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 ($earning.PSObject.Properties.Name -contains 'AccountId') { $earning.AccountId = $accountId } else { $earning | Add-Member -NotePropertyName 'AccountId' -NotePropertyValue $accountId -Force } } if (Write-EntityData -EntityType 'Earning' -Data $earnings -DataPath $resolvedPath) { Write-Verbose "Updated earning: $($earning.Name)" return $earning } } } |