[CmdletBinding()] param( [ValidateSet('Set', 'Report')] [string]$Action = 'Report', [string]$Category, [decimal]$Limit = 0, [string]$Month = (Get-Date -Format 'yyyy-MM'), [string]$Currency = 'USD', [string]$ExpensesCsv = (Join-Path $env:USERPROFILE 'Productivity\expenses.csv'), [string]$Data = (Join-Path $env:APPDATA 'Productivity\budget.json') ) $ErrorActionPreference = 'Stop' function Read-Budget { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { return [pscustomobject]@{} } $raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 if ([string]::IsNullOrWhiteSpace($raw)) { return [pscustomobject]@{} } $value = $raw | ConvertFrom-Json if ($null -eq $value) { return [pscustomobject]@{} } return $value } switch ($Action) { 'Set' { if ([string]::IsNullOrWhiteSpace($Category)) { throw 'Укажите категорию через -Category.' } if ($Limit -lt 0) { throw 'Лимит не может быть отрицательным.' } $budget = Read-Budget -Path $Data $directory = Split-Path -LiteralPath $Data if ($directory -and -not (Test-Path -LiteralPath $directory)) { New-Item -ItemType Directory -Path $directory -Force | Out-Null } if (Test-Path -LiteralPath $Data) { Copy-Item -LiteralPath $Data -Destination ($Data + '.bak') -Force } $categoryKey = $Category.Trim().ToLower() $budget | Add-Member -NotePropertyName $categoryKey -NotePropertyValue ([math]::Round($Limit, 2)) -Force $json = ConvertTo-Json -InputObject $budget -Depth 5 Set-Content -LiteralPath $Data -Value $json -Encoding UTF8 Write-Host ("Лимит {0}: {1:N2} {2}" -f $categoryKey, $Limit, $Currency) } 'Report' { if (-not (Test-Path -LiteralPath $ExpensesCsv -PathType Leaf)) { throw "Файл расходов не найден: $ExpensesCsv" } $budget = Read-Budget -Path $Data $limits = @{} foreach ($property in $budget.PSObject.Properties) { $limits[$property.Name] = [decimal]$property.Value } $rows = @(Import-Csv -LiteralPath $ExpensesCsv | Where-Object { $_.date -like "$Month*" }) if ($rows.Count -eq 0) { Write-Host "Расходов за $Month нет." break } $actual = @{} foreach ($row in $rows) { $key = $row.category.Trim().ToLower() if (-not $actual.ContainsKey($key)) { $actual[$key] = [decimal]0 } $actual[$key] += [decimal]$row.amount } $categories = @((@($limits.Keys) + @($actual.Keys)) | Sort-Object -Unique) $report = foreach ($key in $categories) { $spent = if ($actual.ContainsKey($key)) { $actual[$key] } else { [decimal]0 } $cap = if ($limits.ContainsKey($key)) { $limits[$key] } else { $null } $left = if ($null -ne $cap) { $cap - $spent } else { $null } $percent = if ($null -ne $cap -and $cap -gt 0) { [math]::Round(($spent / $cap) * 100, 1) } else { $null } $limitValue = if ($null -ne $cap) { [math]::Round($cap, 2) } else { 'нет' } $leftValue = if ($null -ne $left) { [math]::Round($left, 2) } else { 'нет' } $percentValue = if ($null -ne $percent) { "$percent%" } else { 'нет' } [pscustomobject]@{ Category = $key Limit = $limitValue Spent = [math]::Round($spent, 2) Left = $leftValue Percent = $percentValue } } Write-Host "Бюджет за $Month, валюта $Currency" $report | Format-Table -AutoSize } }