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

127 read currencie from input file #128

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions icsv2ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import subprocess
import readline
import configparser
import locale
from argparse import HelpFormatter
from datetime import datetime
from operator import attrgetter
from locale import atof
from decimal import *


class FileType(object):
Expand Down Expand Up @@ -515,20 +517,27 @@ def __init__(self, fields, raw_csv, options):
desc.append(fields[int(index) - 1].strip())
self.desc = ' '.join(desc).strip()

self.credit = get_field_at_index(fields, options.credit, options.csv_decimal_comma, options.ledger_decimal_comma)
self.debit = get_field_at_index(fields, options.debit, options.csv_decimal_comma, options.ledger_decimal_comma)
if self.credit and self.debit and atof(self.credit) == 0:
self.credit = ''
elif self.credit and self.debit and atof(self.debit) == 0:
self.debit = ''

self.credit, self.credit_currency = get_field_at_index(fields, options.credit, options.csv_decimal_comma, options.ledger_decimal_comma)
self.debit, self.debit_currency = get_field_at_index(fields, options.debit, options.csv_decimal_comma, options.ledger_decimal_comma)

if (not atof(self.credit)) or (not atof(self.debit)):
if atof(self.credit) == 0:
self.credit = ""
else:
self.debit = ""

self.credit_account = options.account
if options.src_account:
self.credit_account = options.src_account

self.currency = options.currency

self.credit_currency = getattr(
options, 'credit_currency', self.currency)
options, 'credit_currency', self.credit_currency or self.currency)
self.debit_currency = getattr(
options, 'debit_currency', self.debit_currency or self.currency)

self.cleared_character = options.cleared_character

if options.template_file:
Expand Down Expand Up @@ -565,7 +574,7 @@ def journal_entry(self, transaction_index, payee, account, tags):
if uuid:
uuid = uuid[0]
tags.remove(uuid)

# format tags to proper ganged string for ledger
if self.options.multiline_tags:
tags_separator = '\n ; '
Expand All @@ -586,7 +595,7 @@ def journal_entry(self, transaction_index, payee, account, tags):
'uuid': uuid,

'debit_account': account,
'debit_currency': self.currency if self.debit else "",
'debit_currency': self.debit_currency if self.debit else "",
'debit': self.debit,

'credit_account': self.credit_account,
Expand All @@ -613,35 +622,38 @@ def get_field_at_index(fields, index, csv_decimal_comma, ledger_decimal_comma):
if index == 0 or index > len(fields):
return ""

locale._override_localeconv["thousands_sep"] = ","
locale._override_localeconv["decimal_point"] = "."
if csv_decimal_comma:
decimal_separator = ','
else:
decimal_separator = '.'

re_non_number = '[^-0-9' + decimal_separator + ']'
locale._override_localeconv["thousands_sep"] = "."
locale._override_localeconv["decimal_point"] = ","

raw_value = fields[abs(index) - 1]
# Add negative symbol to raw_value if between parentheses
# E.g. ($13.37) becomes -$13.37
if raw_value.startswith("(") and raw_value.endswith(")"):
raw_value = "-" + raw_value[1:-1]

value = re.sub(re_non_number, '', raw_value)
match = re.match(r"\s*((?P<minus>-)?\s*(?P<currency>[^-\d\s.,]+)?\s*){2}\s*(?P<number>[\d.,]+)\s*", raw_value);

value = (match.group('minus') or "" ) + match.group('number')
value = Decimal(locale.delocalize(value));

# Invert sign of value if index is negative.
if index < 0:
if value.startswith("-"):
value = value[1:]
elif value == "":
value = ""
else:
value = "-" + value
value = value * -1

if csv_decimal_comma and not ledger_decimal_comma:
value = value.replace(',', '.')
if not csv_decimal_comma and ledger_decimal_comma:
value = value.replace('.', ',')
value = str(value);

return value
locale._override_localeconv["thousands_sep"] = ","
locale._override_localeconv["decimal_point"] = "."
if ledger_decimal_comma:
locale._override_localeconv["thousands_sep"] = "."
locale._override_localeconv["decimal_point"] = ","
value = locale.str(value)

locale._override_localeconv ={}
return value, match.group("currency")


def csv_md5sum_from_ledger(ledger_file):
Expand Down