Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piecash ledger create incorrect output for commodities #238

Open
meman987 opened this issue Sep 19, 2024 · 1 comment
Open

piecash ledger create incorrect output for commodities #238

meman987 opened this issue Sep 19, 2024 · 1 comment

Comments

@meman987
Copy link

Commodities have thousand separators in the ledger output. This gives incorrect balances. See below:

2022-03-10 Buy XYZ
    Assets:Cash                         SEK -120,000.00
    Assets:Avanza                       XYZ 1,200 @@ SEK 120,000.00

2023-01-02 Sell XYZ
    Assets:_:ALBBV:13 Avanza 1          XYZ -120 @@ SEK 14,000.00
    Assets:Cash                         SEK 14,400.00
    PL:Capital Gains

hledger bal -f tmp.txt                                                                                              [18:11:09]
           XYZ 1,200  Assets:Avanza
     SEK -105,600.00  Assets:Cash
        XYZ -120,000  Assets:_:ALBBV:13 Avanza 1
         SEK -400.00  PL:Capital Gains
--------------------
     SEK -106,000.00
        XYZ -118,800

It should be

2022-03-10 Buy XYZ
    Assets:Cash                         SEK -120,000.00
    Assets:Avanza                       XYZ 1200 @@ SEK 120,000.00

2023-01-02 Sell XYZ
    Assets:_:ALBBV:13 Avanza 1          XYZ -120 @@ SEK 14,000.00
    Assets:Cash                         SEK 14,400.00
    PL:Capital Gains

 hledger bal -f tmp2.txt                                                                                              [18:10:52]
            XYZ 1200  Assets:Avanza
     SEK -105,600.00  Assets:Cash
            XYZ -120  Assets:_:ALBBV:13 Avanza 1
         SEK -400.00  PL:Capital Gains
--------------------
     SEK -106,000.00
            XYZ 1080
@meman987
Copy link
Author

meman987 commented Sep 19, 2024

This is a rather serious bug. So here is a temporary fix while waiting for a proper one (for others having the same problem):

import re

f = open('invest.lgr', 'r')
d = f.readlines()
f.close()

res = []
for l in d:
  l2 = l
  match = re.search('.* [+-]?(?:\d+|\d{1,3}(?:,\d{3})*)(?:\.\d*)? @@.*', l)
  if not match is None:
    i = j = l.index('@@') - 2
    while j > 0:
      if not l[j] in '0123456789,.':
        break
      j -= 1
    old_val = l[j:(i+1)]
    new_val = str(float(old_val.replace(',','')))
    l2 = l.replace(old_val, new_val)
  res.append(l2)

with open(f'invest.lgr', 'w') as outfile:
  outfile.write(''.join(res))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant