diff --git a/kapital_bank/bank_api.py b/kapital_bank/bank_api.py index 7135713..7682a28 100644 --- a/kapital_bank/bank_api.py +++ b/kapital_bank/bank_api.py @@ -1283,8 +1283,21 @@ def get_statement_transactions(from_date, to_date, account_iban, login_name=None if not parsed_date: continue - # Amount and currency (use account currency, not the counterparty/operation currency from API) - amount = abs(flt(txn.get("fcyAmount") or txn.get("lcyAmount") or 0)) + # acCcy = transaction currency per the bank; fcyAmount is in acCcy; lcyAmount is always AZN. + # We need the amount in account_currency so BT deposit/withdrawal is correct. + ac_ccy = (txn.get("acCcy") or "").strip().upper() + fcy_amount = abs(flt(txn.get("fcyAmount") or 0)) + lcy_amount = abs(flt(txn.get("lcyAmount") or 0)) + + if not ac_ccy or ac_ccy == account_currency.upper(): + # Same currency — fcyAmount is in account_currency + amount = fcy_amount or lcy_amount + elif account_currency.upper() == "AZN": + # Cross-currency on AZN account: lcyAmount gives the correct AZN value + amount = lcy_amount or fcy_amount + else: + # Non-AZN account with different acCcy: lcyAmount (AZN) as fallback + amount = lcy_amount or fcy_amount currency = account_currency # Direction diff --git a/kapital_bank/mapping.py b/kapital_bank/mapping.py index 553621d..9e34594 100644 --- a/kapital_bank/mapping.py +++ b/kapital_bank/mapping.py @@ -459,11 +459,13 @@ def _create_journal_entry_for_brt(txn, paid_from, paid_to, bank_txn, multi_curre is_multi = from_currency != company_currency or to_currency != company_currency currency_precision = cint(frappe.db.get_single_value("System Settings", "currency_precision") or 2) - # Use currency from: mapping row → bank transaction kb_currency → company currency + # Use currency from: mapping row → txn dict → bank transaction currency → company currency + # NOTE: use bank_txn.currency (= GL account currency, matches the BT amount), + # NOT kb_currency (= the foreign currency from the bank API, may differ from BT amount). txn_currency = ( mapping_currency or txn.get("currency") - or (getattr(bank_txn, "kb_currency", None) or "") + or (getattr(bank_txn, "currency", None) or "") or company_currency ).strip()