add date range validation for transaction creation
This commit is contained in:
@@ -7,6 +7,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import ynab
|
import ynab
|
||||||
|
from dateutil.relativedelta import relativedelta
|
||||||
|
|
||||||
from category_suggester import CategoryHistory
|
from category_suggester import CategoryHistory
|
||||||
from wf_credit_extractor import WellsFargoPayloadExtractor as CreditExtractor
|
from wf_credit_extractor import WellsFargoPayloadExtractor as CreditExtractor
|
||||||
@@ -601,27 +602,48 @@ def _execute_import(
|
|||||||
# --- Create new + duplicates ---
|
# --- Create new + duplicates ---
|
||||||
to_create = classification.new + classification.duplicates
|
to_create = classification.new + classification.duplicates
|
||||||
if to_create:
|
if to_create:
|
||||||
new_txns = [
|
today = date.today()
|
||||||
ynab.NewTransaction(
|
earliest_ynab_date = today - relativedelta(years=5)
|
||||||
account_id=t["account_id"],
|
eligible_to_create = []
|
||||||
date=date.fromisoformat(t["date"]),
|
skipped_for_date = []
|
||||||
amount=t["amount"],
|
for transaction in to_create:
|
||||||
payee_name=t["payee_name"],
|
transaction_date = date.fromisoformat(transaction["date"])
|
||||||
category_id=t.get("category_id"),
|
if earliest_ynab_date <= transaction_date <= today:
|
||||||
cleared=ynab.TransactionClearedStatus(t["cleared"]),
|
eligible_to_create.append(transaction)
|
||||||
flag_color=ynab.TransactionFlagColor(t["flag_color"]),
|
else:
|
||||||
import_id=t["import_id"],
|
skipped_for_date.append(transaction)
|
||||||
approved=t.get("approved", False),
|
|
||||||
|
if skipped_for_date:
|
||||||
|
print(
|
||||||
|
f" [ynab] Skipped {len(skipped_for_date)} transaction(s) outside "
|
||||||
|
f"the supported date range ({earliest_ynab_date} through {today}):"
|
||||||
|
)
|
||||||
|
for transaction in skipped_for_date:
|
||||||
|
payee = transaction.get("payee_name") or "(no payee)"
|
||||||
|
print(f" {transaction['date']} | {payee}")
|
||||||
|
|
||||||
|
if eligible_to_create:
|
||||||
|
new_txns = [
|
||||||
|
ynab.NewTransaction(
|
||||||
|
account_id=t["account_id"],
|
||||||
|
date=date.fromisoformat(t["date"]),
|
||||||
|
amount=t["amount"],
|
||||||
|
payee_name=t["payee_name"],
|
||||||
|
category_id=t.get("category_id"),
|
||||||
|
cleared=ynab.TransactionClearedStatus(t["cleared"]),
|
||||||
|
flag_color=ynab.TransactionFlagColor(t["flag_color"]),
|
||||||
|
import_id=t["import_id"],
|
||||||
|
approved=t.get("approved", False),
|
||||||
|
)
|
||||||
|
for t in eligible_to_create
|
||||||
|
]
|
||||||
|
resp = ynab_client.create_transactions(new_txns)
|
||||||
|
created_ids = resp.transaction_ids or []
|
||||||
|
dup_ids = resp.duplicate_import_ids or []
|
||||||
|
print(
|
||||||
|
f" [ynab] Created {len(created_ids)} transaction(s)"
|
||||||
|
+ (f", {len(dup_ids)} server-side duplicate(s) skipped" if dup_ids else "")
|
||||||
)
|
)
|
||||||
for t in to_create
|
|
||||||
]
|
|
||||||
resp = ynab_client.create_transactions(new_txns)
|
|
||||||
created_ids = resp.transaction_ids or []
|
|
||||||
dup_ids = resp.duplicate_import_ids or []
|
|
||||||
print(
|
|
||||||
f" [ynab] Created {len(created_ids)} transaction(s)"
|
|
||||||
+ (f", {len(dup_ids)} server-side duplicate(s) skipped" if dup_ids else "")
|
|
||||||
)
|
|
||||||
|
|
||||||
# --- Update pending→posted ---
|
# --- Update pending→posted ---
|
||||||
if classification.pending_to_posted:
|
if classification.pending_to_posted:
|
||||||
|
|||||||
Reference in New Issue
Block a user