Last active
August 29, 2015 14:16
-
-
Save XPlantefeve/4c3ca72e7c8b817051f4 to your computer and use it in GitHub Desktop.
Conversion of a Grisbi personal finance file to csv. #Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Convert-Currency ($transaction) | |
{ | |
If ($transaction.Exb -eq '1') { | |
return [math]::Round([decimal]$transaction.Am / | |
[decimal]$transaction.Exr,2) + $transaction.exf | |
} else { | |
return [math]::Round([decimal]$transaction.Am * | |
[decimal]$transaction.Exr,2) + $transaction.exf | |
} | |
} | |
[xml]$comptes = Get-Content -Path "${PSScriptRoot}\file.gsb" -Encoding UTF8 | |
$transactions = $comptes.SelectNodes("//Transaction") | |
$startingdate = [DateTime]($transactions | Select -Property Dt | | |
Sort-Object -Property [DateTime]Dt | Select-Object -First 1).Dt | |
$startingdate = $startingdate.AddDays(1-$startingdate.Day) | |
# corrections | |
foreach ($tnb in | |
@(11206,11260,11268,11288,11305,11318,11340,11341,11342,11343,11344,11345,11346,11357,11358,11414,11415,11416,9317)) | |
{ $comptes.SelectSingleNode("//Transaction[@Nb=${tnb}]").Exb='1' } | |
$categories = $comptes.SelectNodes("//Category") | |
$currencies = $comptes.SelectNodes("//Curency") | |
foreach ($account in ($comptes.SelectNodes("//Account[@Currency!=1]"))) | |
{ | |
$xpath = "//Transaction[@Ac='$($account.Number)']" | |
$transactions = $comptes.SelectNodes($xpath) | |
foreach ($transaction in $transactions) | |
{ | |
If ($transaction.Exr -ne '0.00') | |
{ | |
$exchr = $transaction.Exr | |
$exchb = $transaction.Exb | |
} else { | |
$transaction.Exr = $exchr | |
$transaction.Exb = $exchb | |
} | |
} | |
} | |
$xpath = "//Transaction[@Exr!='0.00']" | |
$transactions = $comptes.SelectNodes($xpath) | |
foreach ($transaction in $transactions) { [string]$transaction.Am = | |
Convert-Currency($t) } | |
$header = @('date') | |
foreach ($category in $categories) | |
{ | |
$header += $category.Na | |
} | |
$header -join ';' | |
Out-File -FilePath "${PSScriptRoot}\cmpt.csv" -InputObject $($header -join ';') | |
while ( $startingdate -lt (Get-Date) ) | |
{ | |
$line = @($startingdate) | |
$mymonth = "{0:MM}" -f $startingdate | |
$myyear = "{0:yyyy}" -f $startingdate | |
foreach ($category in $categories) | |
{ | |
$mymonth = "{0:MM}" -f $startingdate | |
$myyear = "{0:yyyy}" -f $startingdate | |
$xpath = "//Transaction[starts-with(@Dt,'$mymonth') and contains(@Dt,'$myyear')]" | |
$transactions = $comptes.SelectNodes($xpath) | |
$total = ($transactions | where { [DateTime]$_.Dt -ge $startingdate -and [DateTime]$_.Dt -lt $startingdate.AddMonths(1) -and $_.Ca -eq $category.Nb } | Measure-Object -Property Am -Sum).sum | |
if (!$total) { $total = '0' } | |
$line += $total | |
} | |
$line -join ';' | |
Out-File -FilePath "${PSScriptRoot}\cmpt.csv" -Append -InputObject $($line -join ';') | |
$startingdate = $startingdate.AddMonths(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment