All MicroEvals
from decimal import Decimal from datetime import datetime, d...
Create MicroEval

from decimal import Decimal from datetime import datetime, d...

Prompt

from decimal import Decimal from datetime import datetime, date from typing import List, Dict, Any, Optional from dataclasses import dataclass from enum import Enum import structlog logger = structlog.get_logger() class AccountType(str, Enum): ASSET = "Asset" LIABILITY = "Liability" EQUITY = "Equity" INCOME = "Income" EXPENSE = "Expense" class TransactionStatus(str, Enum): PENDING = "pending" CLEARED = "cleared" RECONCILED = "reconciled" REJECTED = "rejected" @dataclass class LedgerAccount: """Chart of Accounts Entry""" account_id: str account_name: str account_type: AccountType currency: str = "EUR" parent_account: Optional[str] = None is_active: bool = True @dataclass class LedgerEntry: """Journal Entry Line Item""" entry_id: str transaction_id: str account_id: str debit_amount: Decimal credit_amount: Decimal currency: str description: str posting_date: date value_date: date created_at: datetime class LedgerEngine: """Double-Entry Bookkeeping Ledger Engine""" def __init__(self): self.chart_of_accounts = self._initialize_chart_of_accounts() def _initialize_chart_of_accounts(self) -> Dict[str, LedgerAccount]: """Initialize PSP Chart of Accounts""" accounts = { # Assets "1000": LedgerAccount("1000", "Cash and Cash Equivalents", AccountType.ASSET), "1100": LedgerAccount("1100", "Bank Accounts", AccountType.ASSET, parent_account="1000"), "1110": LedgerAccount("1110", "Settlement Account - Erste", AccountType.ASSET, parent_account="1100"), "1120": LedgerAccount("1120", "Settlement Account - Card Schemes", AccountType.ASSET, parent_account="1100"), "1200": LedgerAccount("1200", "Merchant Clearing Accounts", AccountType.ASSET), "1210": LedgerAccount("1210", "Merchant Clearing - Pending", AccountType.ASSET, parent_account="1200"), "1220": LedgerAccount("1220", "Merchant Clearing - Settled", AccountType.ASSET, parent_account="1200"), "1300": LedgerAccount("1300", "Reserve Funds", AccountType.ASSET), "1310": LedgerAccount("1310", "Merchant Reserves", AccountType.ASSET, parent_account="1300"), "1320": LedgerAccount("1320", "Chargeback Reserves", AccountType.ASSET, parent_account="1300"), # Liabilities "2000": LedgerAccount("2000", "Payables", AccountType.LIABILITY), "2100": LedgerAccount("2100", "Merchant Payables", AccountType.LIABILITY, parent_account="2000"), "2110": LedgerAccount("2110", "Merchant Payout Queue", AccountType.LIABILITY, parent_account="2100"), "2200": LedgerAccount("2200", "Card Scheme Payables", AccountType.LIABILITY, parent_account="2000"), "2210": LedgerAccount("2210", "Interchange Payable", AccountType.LIABILITY, parent_account="2200"), "2220": LedgerAccount("2220", "Scheme Fees Payable", AccountType.LIABILITY, parent_account="2200"), "2300": LedgerAccount("2300", "Customer Deposits", AccountType.LIABILITY), "2310": LedgerAccount("2310", "Cardholder Refunds Payable", AccountType.LIABILITY, parent_account="2300"), # Equity "3000": LedgerAccount("3000", "Capital", AccountType.EQUITY), "3100": LedgerAccount("3100", "Retained Earnings", AccountType.EQUITY, parent_account="3000"), # Income "4000": LedgerAccount("4000", "Revenue", AccountType.INCOME), "4100": LedgerAccount("4100", "Transaction Fees", AccountType.INCOME, parent_account="4000"), "4110": LedgerAccount("4110", "Card Transaction Fees", AccountType.INCOME, parent_account="4100"), "4120": LedgerAccount("4120", "SEPA Transaction Fees", AccountType.INCOME, parent_account="4100"), "4200": LedgerAccount("4200", "Merchant Service Fees", AccountType.INCOME, parent_account="4000"), "4210": LedgerAccount("4210", "Monthly Service Fees", AccountType.INCOME, parent_account="4200"), "4300": LedgerAccount("4300", "Interest Income", AccountType.INCOME, parent_account="4000"), # Expenses "5000": LedgerAccount("5000", "Operating Expenses", AccountType.EXPENSE), "5100": LedgerAccount("5100", "Interchange Expense", AccountType.EXPENSE, parent_account="5000"), "5200": LedgerAccount("5200", "Scheme Fees Expense", AccountType.EXPENSE, parent_account="5000"), "5300": LedgerAccount("5300", "Processing Costs", AccountType.EXPENSE, parent_account="5000"), "5400": LedgerAccount("5400", "Chargeback Losses", AccountType.EXPENSE, parent_account="5000"), "5500": LedgerAccount("5500", "Fraud Losses", AccountType.EXPENSE, parent_account="5000"), } return accounts def create_journal_entry( self, transaction_id: str, entries: List[Dict[str, Any]], description: str, posting_date: Optional[date] = None, value_date: Optional[date] = None ) -> List[LedgerEntry]: """Create double-entry journal entry""" try: if posting_date is None: posting_date = date.today() if value_date is None: value_date = posting_date total_debits = Decimal('0') total_credits = Decimal('0') ledger_entries = [] for entry in entries: account_id = entry["account_id"] debit = Decimal(str(entry.get("debit", 0))) credit = Decimal(str(entry.get("credit", 0))) if account_id not in self.chart_of_accounts: raise ValueError(f"Invalid account ID: {account_id}") total_debits += debit total_credits += credit ledger_entry = LedgerEntry( entry_id=f"{transaction_id}_{account_id}", transaction_id=transaction_id, account_id=account_id, debit_amount=debit, credit_amount=credit, currency="EUR", description=description, posting_date=posting_date, value_date=value_date, created_at=datetime.utcnow() ) ledger_entries.append(ledger_entry) # Verify balanced entry if total_debits != total_credits: raise ValueError(f"Unbalanced entry: debits={total_debits}, credits={total_credits}") logger.info( "journal_entry_created", transaction_id=transaction_id, entry_count=len(ledger_entries), total_amount=float(total_debits) ) return ledger_entries except Exception as e: logger.error("journal_entry_failed", error=str(e), transaction_id=transaction_id) raise def record_card_authorization( self, transaction_id: str, amount: Decimal, merchant_id: str, interchange_rate: Decimal = Decimal('0.015'), scheme_fee: Decimal = Decimal('0.005') ) -> List[LedgerEntry]: """Record card authorization in ledger""" interchange_amount = amount * interchange_rate scheme_fee_amount = amount * scheme_fee merchant_net = amount - interchange_amount - scheme_fee_amount entries = [ {"account_id": "1210", "debit": amount, "credit": Decimal('0')}, # Merchant Clearing Pending {"account_id": "2100", "debit": Decimal('0'), "credit": merchant_net}, # Merchant Payable {"account_id": "5100", "debit": interchange_amount, "credit": Decimal('0')}, # Interchange Expense {"account_id": "5200", "debit": scheme_fee_amount, "credit": Decimal('0')}, # Scheme Fee Expense ] return self.create_journal_entry( transaction_id=transaction_id, entries=entries, description=f"Card authorization for merchant {merchant_id}" ) def record_card_settlement( self, transaction_id: str, amount: Decimal, merchant_id: str ) -> List[LedgerEntry]: """Record card settlement (batch close)""" entries = [ {"account_id": "1220", "debit": amount, "credit": Decimal('0')}, # Merchant Clearing Settled {"account_id": "1210", "debit": Decimal('0'), "credit": amount}, # Merchant Clearing Pending (clear) ] return self.create_journal_entry( transaction_id=transaction_id, entries=entries, description=f"Card settlement for merchant {merchant_id}" ) def record_merchant_payout( self, transaction_id: str, amount: Decimal, merchant_id: str, payout_fee: Decimal = Decimal('0') ) -> List[LedgerEntry]: """Record merchant payout""" net_payout = amount - payout_fee entries = [ {"account_id": "2100", "debit": amount, "credit": Decimal('0')}, # Clear Merchant Payable {"account_id": "1110", "debit": Decimal('0'), "credit": net_payout}, # Bank Account (cash out) {"account_id": "4100", "debit": Decimal('0'), "credit": payout_fee}, # Payout Fee Revenue ] return self.create_journal_entry( transaction_id=transaction_id, entries=entries, description=f"Payout to merchant {merchant_id}" ) def record_sepa_payment( self, transaction_id: str, amount: Decimal, description: str ) -> List[LedgerEntry]: """Record SEPA payment""" entries = [ {"account_id": "1110", "debit": amount, "credit": Decimal('0')}, # Bank Account (incoming) {"account_id": "2100", "debit": Decimal('0'), "credit": amount}, # Merchant Payable ] return self.create_journal_entry( transaction_id=transaction_id, entries=entries, description=description ) ledger_engine = LedgerEngine() List all the errors in this code. Don't write anything else.

Drag to resize