Skip to content

Commit

Permalink
Fixed transaction import comparison references across account only
Browse files Browse the repository at this point in the history
  • Loading branch information
dbtdsilva committed Jul 18, 2024
1 parent 2ea9580 commit d2a9b57
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
19 changes: 17 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_help_format(prog):
help="Specific the start-date to link accounts (format: YYYY-MM-DD)",
type=lambda s: datetime.strptime(s, '%Y-%m-%d'))
parser_tx_link.add_argument('--end-date',
help="Specific the start-date to link accounts (format: YYYY-MM-DD)",
help="Specific the end-date to link accounts (format: YYYY-MM-DD)",
type=lambda s: datetime.strptime(s, '%Y-%m-%d'))
parser_tx_link.add_argument('--amount-diff',
type=float,
Expand All @@ -85,6 +85,16 @@ def get_help_format(prog):

# Categorize transactions
parser_tx_category = jobs.add_parser('tx-category', help="Categorize existing transactions (interactive)")
parser_tx_category.add_argument('--start-date',
help="Specific the start-date to categorize accounts (format: YYYY-MM-DD)",
type=lambda s: datetime.strptime(s, '%Y-%m-%d'))
parser_tx_category.add_argument('--end-date',
help="Specific the end-date to categorize accounts (format: YYYY-MM-DD)",
type=lambda s: datetime.strptime(s, '%Y-%m-%d'))
parser_tx_category.add_argument('--account-ids',
type=str,
help="Account IDs separated by comma (e.g: 3,4,7)",
default="")

args = parser.parse_args()

Expand All @@ -99,4 +109,9 @@ def get_help_format(prog):
amount_diff_percentage=args.amount_diff,
date_diff_days=args.date_diff)
elif args.job == 'tx-category':
firefly_sync_cli.categorize()
firefly_sync_cli.categorize(
start_date=args.start_date, end_date=args.end_date,
account_ids=[account_id
for account_ids in args.account_ids
for account_id in account_ids.split(',')
if account_id])
13 changes: 9 additions & 4 deletions src/firefly_api/api_routes/account_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ def get_accounts(self, type: AccountType):
data = self._internal_get('/', params={'type': type.value})
return [Account(id=item["id"], **item['attributes']) for item in data]

def get_account_transactions(self, account_id: str, start_date: datetime, end_date: datetime) -> List[Transaction]:
data = self._internal_get(
f'{account_id}/transactions', params={'start': start_date.strftime(BaseApi.DATE_FORMAT),
'end': end_date.strftime(BaseApi.DATE_FORMAT)})
def get_account_transactions(self, account_id: str, start_date: datetime = None,
end_date: datetime = None) -> List[Transaction]:
params = {}
if start_date is not None:
params['start'] = start_date.strftime(BaseApi.DATE_FORMAT)
if end_date is not None:
params['end'] = end_date.strftime(BaseApi.DATE_FORMAT)

data = self._internal_get(f'{account_id}/transactions', params=params)
return [Transaction(id=item['id'], **item['attributes']['transactions'][0])
for item in data]
7 changes: 4 additions & 3 deletions src/firefly_sync_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any
from typing import Any, List
from datetime import datetime

from .firefly_api.api import FireflyApi
Expand All @@ -25,8 +25,9 @@ def create_cron_job(self, cli_token) -> Any:
data = self.api.cron.create_cron_job(cli_token)
logging.info(f'Cron job run sucessfully: {data}')

def categorize(self):
CategorizationService(self.api, self.dry_run).interactive_categorize()
def categorize(self, start_date: datetime = None, end_date: datetime = None, account_ids: List[str] = None):
CategorizationService(self.api, self.dry_run).interactive_categorize(
start_date=start_date, end_date=end_date, account_ids=account_ids)

def import_file(self, file: str) -> bool:
return TransactionImportService(self.api, self.dry_run).import_file(file=file)
Expand Down
38 changes: 25 additions & 13 deletions src/services/categorization_service.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
from datetime import datetime, timedelta
from datetime import datetime
from typing import List

from .base_service import BaseService
from ..firefly_api.models.transaction_type import TransactionType
from ..firefly_api.models.account_type import AccountType
from ..firefly_api.api import FireflyApi
import logging


class CategorizationService(BaseService):

def __init__(self, api: FireflyApi, dry_run: bool) -> None:
super().__init__(api, dry_run)

def interactive_categorize(self):
transactions = self.api.transactions.get_transactions(transaction_type=TransactionType.ALL,
start_date=datetime.now() - timedelta(days=30))
def interactive_categorize(self, start_date: datetime = None, end_date: datetime = None,
account_ids: List[str] = []):
accounts = self.api.accounts.get_accounts(type=AccountType.ASSET)
categories = self.api.categories.get_categories()

for transaction in transactions:
for index, category in enumerate(categories):
print(f"{index + 1}. {category.name}", end=', ' if index != len(categories) - 1 else None)
print(f"Transaction: {transaction.description} | {transaction.amount} {transaction.currency_code} "
f"| {transaction.type} | {transaction.date}")
for account in accounts:
if len(account_ids) != 0 and account.id not in account_ids:
continue

selected_category_index = BaseService._get_allowed_input(len(categories) - 1)
selected_category = categories[selected_category_index]
print(selected_category)
logging.info(f'Getting transactions for account \'{account.name}\'')
transactions = self.api.accounts.get_account_transactions(
account_id=account.id,
start_date=start_date,
end_date=end_date)

for transaction in transactions:
for index, category in enumerate(categories):
print(f"{index + 1}. {category.name}", end=', ' if index != len(categories) - 1 else None)
print(f"Transaction: {transaction.description} | {transaction.amount} {transaction.currency_code} "
f"| {transaction.type} | {transaction.date}")

selected_category_index = BaseService._get_allowed_input(len(categories) - 1)
selected_category = categories[selected_category_index]
print(selected_category)
3 changes: 1 addition & 2 deletions src/services/transaction_import_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def import_file(self, file: str) -> bool:

start_date = min(t.date for t in parsed_transactions)
end_date = max(t.date for t in parsed_transactions)
stored_transactions = self.api.accounts.get_account_transactions(
account_id=account.id, start_date=start_date, end_date=end_date)
stored_transactions = self.api.transactions.get_transactions(start_date=start_date, end_date=end_date)

stored_transactions_by_reference = {
hash_part.strip(): t
Expand Down

0 comments on commit d2a9b57

Please sign in to comment.