### Create accounts for journal entry example Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/examples.html Creates four accounts (Bank, Client, Supplier, Asset) with specific account types and adds them to the session. This is the first step for the standard journal entry example. ```python from python_accounting.models import Account with get_session(engine) as session: bank_account = Account( name="Bank Account", account_type=Account.AccountType.BANK, currency_id=currency.id, entity_id=entity.id, ) client_account = Account( name="Client Account", account_type=Account.AccountType.RECEIVABLE, currency_id=currency.id, entity_id=entity.id, ) supplier_account = Account( name="Supplier Account", account_type=Account.AccountType.PAYABLE, currency_id=currency.id, entity_id=entity.id, ) asset_account = Account( name="Asset Account", account_type=Account.AccountType.NON_CURRENT_ASSET, currency_id=currency.id, entity_id=entity.id, ) session.add_all([ bank_account, client_account, supplier_account, asset_account ]) session.commit() ``` -------------------------------- ### Install python-accounting with pip Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Install the python-accounting library using pip. Requires Python 3.9+. ```bash pip install python-accounting ``` -------------------------------- ### IncomeStatement.__init__ Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/reports/income_statement/index.html Initializes an IncomeStatement instance with a session and optional start and end dates. ```APIDOC ## IncomeStatement.__init__ ### Description Initializes an IncomeStatement instance with a session and optional start and end dates. ### Method Constructor ### Signature `IncomeStatement(session, start_date: datetime.datetime = None, end_date: datetime.datetime = None)` ### Parameters - **session** (Session) - Required - The accounting session to which the report belongs. - **start_date** (datetime) - Optional - The earliest transaction date for Transaction amounts to be included in the report. - **end_date** (datetime) - Optional - The latest transaction date for Transaction amounts to be included in the report. ### Returns An IncomeStatement instance. ### Notes This class represents the Income Statement/Profit and Loss of a given Entity. ``` -------------------------------- ### BalanceSheet Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/balance_sheet.html Documentation for the BalanceSheet class constructor, including parameters, return attributes, and an example usage. ```APIDOC ## BalanceSheet ### Description Generates a Balance Sheet (Statement of Financial Position) for a given accounting session and end date, comparing asset balances to liability and equity balances. ### Parameters - **session** (Session) - Required - The accounting session. - **end_date** (datetime) - Required - The latest transaction date for Transaction amounts to be included in the statement. ### Returns Returns a `BalanceSheet` object with the following attributes: - **sections** (StrEnum) - The sections of the Balance Sheet. - **results** (StrEnum) - The results of the Balance Sheet. - **accounts** (dict) - The Accounts in the sections of the report, by Account category. - **balances** (dict) - The total balances of the Accounts in the sections of the report, by Account category. - **totals** (dict) - The Total balances of Accounts in the sections of the report. - **result_amounts** (dict) - The amounts results of the report. ### Example ```python balance_sheet = BalanceSheet(session) print(balance_sheet) ``` Output: ``` Example Company Balance Sheet For the period: 01, Jan 2024 to 23, Feb 2024 Assets Non Current Asset 120.0000 Receivables 70.0000 Bank 50.0000 _______________ Total Assets 240.0000 Liabilities Control 20.0000 Payable 120.0000 _______________ Total Liabilities 140.0000 _______________ Net Assets 100.0000 =============== Equity Income Statement 100.0000 _______________ Total Equity 100.0000 =============== ``` ``` -------------------------------- ### Generate and print a Cashflow Statement Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/cashflow_statement.md.txt Creates a Cashflow Statement using a session and prints the formatted report. The output shows sections for Operating, Investment, and Financing Cash Flow, along with Net Cash Flow and Cashbook Balance. Note the example uses 'BalanceSheet' instead of 'CashFlowStatement', which may be a typo in the source. ```python cashflow_statememt = BalanceSheet(session) print(cashflow_statememt) Test Entity Cashflow Statement For the period: 01, Jan 2024 to 23, Feb 2024 Operating Cash Flow Net Profit 100.0000 Receivables -70.0000 Payables 120.0000 Taxation 20.0000 _______________ Total Operating Cash Flow 170.0000 Investment Cash Flow Non Current Assets -120.0000 _______________ Total Investment Cash Flow -120.0000 Financing Cash Flow Equity 0.0000 _______________ Total Financing Cash Flow 0.0000 Net Cash Flow Beginning Cash Balance 0.0000 Net Cash Flow 50.0000 _______________ Ending Cash Balance 50.0000 =============== _______________ Cashbook Balance 50.0000 =============== ``` -------------------------------- ### statement(session, start_date=None, end_date=None, schedule=False) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/autoapi/models/account/index.rst.txt Gets a chronological listing of Transactions posted to an Account between given dates. ```APIDOC ## statement(session, start_date=None, end_date=None, schedule=False) ### Description Gets a chronological listing of the Transactions posted to the Account between the dates given. ### Method statement ### Parameters - **session** (Session) - Required - The accounting session to which the Account belongs. - **start_date** (datetime) - Optional - The earliest transaction date for Transaction amounts to be included in the statement. - **end_date** (datetime) - Optional - The latest transaction date for Transaction amounts to be included in the statement. - **schedule** (bool) - Optional - Whether to exclude assignable Transactions and only list clearable Transactions with outstanding amounts. ### Raises - **InvalidAccountTypeError** - If the Account type is not Receivable or Payable. ### Returns - **dict** - With a summary of the opening and closing balance in the case of a statement, the total, cleared and uncleared amounts if its a schedule together with a list of Transactions. - Statements: - opening_balance (Decimal): The balance of the Account at the beginning of the statement period. - transactions (list): Transactions posted to the Account during the period. - closing_balance (Decimal): The balance of the Account at the end of the statement period. - Schedule: - transactions (list): Outstanding clearable Transactions posted to the Account as at the end date. - total_amount (Decimal): The total amount of the Transactions in the Schedule. - cleared_amount (Decimal): The amount of the Transactions in the Schedule that has been cleared. - uncleared_amount (Decimal): The amount of the Transactions in the Schedule that is still outstanding. ``` -------------------------------- ### IncomeStatement.net_profit(session, start_date, end_date) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/autoapi/reports/index.rst.txt Static method to get the net profit for a given period. It takes a session and optional start and end dates, and returns the net profit or loss as a Decimal. ```APIDOC ## IncomeStatement.net_profit(session, start_date, end_date) ### Description Get the value of net profit for the given period. ### Method Static method ### Signature `net_profit(session, start_date: datetime.datetime = None, end_date: datetime.datetime = None) -> decimal.Decimal` ### Parameters - **session** (Session) - Required - The accounting session to which the report belongs. - **start_date** (datetime) - Optional - The earliest transaction date for Transaction amounts to be included in the report. - **end_date** (datetime) - Optional - The latest transaction date for Transaction amounts to be included in the report. ### Returns - **Decimal** - The net profit or loss for the Entity for the period. ``` -------------------------------- ### database_init() Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/database/database_init/index.html Initializes the database by setting up all tables that do not currently exist. Returns None. ```APIDOC ## database_init() ### Description Initializes the database by setting up all tables that do not currently exist. ### Method Function ### Signature `database_init() -> None` ### Parameters None ### Returns None ``` -------------------------------- ### Configure database engine and create tables Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Configure the library by creating a SQLAlchemy engine from the database URL in the config and running migrations to create tables. ```python from python_accounting.config import config from python_accounting.models import Base from sqlalchemy import create_engine database = config.database engine = create_engine(database["url"]) Base.metadata.create_all(engine) # run migrations to create tables ``` -------------------------------- ### Create chart of accounts Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Set up the chart of accounts by creating Account instances of various types (CONTROL, BANK, OPERATING_REVENUE, RECEIVABLE, PAYABLE, OPERATING_EXPENSE, DIRECT_EXPENSE, NON_CURRENT_ASSET) and adding them all to the session. ```python from python_accounting.models import Account tax_account = Account( name="Tax Account", account_type=Account.AccountType.CONTROL, currency_id=currency.id, entity_id=entity.id, ) bank_account = Account( name="Bank Account", account_type=Account.AccountType.BANK, currency_id=currency.id, entity_id=entity.id, ) revenue_account = Account( name="Revenue Account", account_type=Account.AccountType.OPERATING_REVENUE, currency_id=currency.id, entity_id=entity.id, ) client_account = Account( name="Client Account", account_type=Account.AccountType.RECEIVABLE, currency_id=currency.id, entity_id=entity.id, ) supplier_account = Account( name="Supplier Account", account_type=Account.AccountType.PAYABLE, currency_id=currency.id, entity_id=entity.id, ) opex_account = Account( name="Opex Account", account_type=Account.AccountType.OPERATING_EXPENSE, currency_id=currency.id, entity_id=entity.id, ) expense_account = Account( name="Expense Account", account_type=Account.AccountType.DIRECT_EXPENSE, currency_id=currency.id, entity_id=entity.id, ) asset_account = Account( name="Asset Account", account_type=Account.AccountType.NON_CURRENT_ASSET, currency_id=currency.id, entity_id=entity.id, ) session.add_all([ tax_account, bank_account, revenue_account, client_account, supplier_account, opex_account, expense_account, asset_account ]) session.commit() ``` -------------------------------- ### Generate and print Trial Balance Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/trial_balance.md.txt Creates a TrialBalance instance with the accounting session and prints it, showing the report with Income Statement and Balance Sheet sections, totals, and debit/credit totals. ```python trial_balance = TrialBalance(session) print(trial_balance) Test Entity Trial Balance For the period: 01, Jan 2024 to 24, Feb 2024 Income Statement Operating Revenue -420.0000 Non Operating Revenue -50.0000 Operating Expense 100.0000 Direct Expense 65.0000 Overhead Expense 40.0000 _______________ Total Income Statement -265.0000 Balance Sheet Non Current Asset 100.0000 Inventory 100.0000 Bank 374.5000 Receivable 22.0000 Control -4.5000 Current Liability -100.0000 Payable -220.0000 Equity -77.0000 Reconciliation 70.0000 _______________ Total Balance Sheet 265.0000 _______________ Total Debits 871.5000 =============== _______________ Total Credits 871.5000 =============== ``` -------------------------------- ### closing_balance(session, end_date=None) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/autoapi/models/account/index.rst.txt Gets the closing balance of an Account as at a given date. ```APIDOC ## closing_balance(session, end_date=None) ### Description Gets the closing balance of the Account as at the given date. ### Method closing_balance ### Parameters - **session** (Session) - Required - The accounting session to which the Account belongs. - **end_date** (datetime) - Optional - The latest transaction date for Transaction amounts to be included in the balance. ### Returns - **Decimal** - The total opening balance of the Account for the year. ``` -------------------------------- ### Cashflow Statement Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/reports.html Documentation for the Cashflow Statement report, including parameters, return structure, and an example. ```APIDOC ## Cashflow Statement ### Description Generates a cashflow statement that links the income statement to the balance sheet by reconciling the entity's performance with changes in asset, liability, equity, and cashbook balances. ### Parameters - **session** (Session) - Required - The accounting session. - **start_date** (datetime) - Required - The earliest transaction date for transaction amounts to be included in the statement. - **end_date** (datetime) - Required - The latest transaction date for transaction amounts to be included in the statement. ### Returns Returns a `CashFlowStatement` with: - **sections** (StrEnum) - The sections of the Cashflow Statement. - **results** (StrEnum) - The results of the Cashflow Statement. - **accounts** (dict) - The Accounts in the sections of the report, by Account category. - **balances** (dict) - The total balances of the Accounts in the sections of the report, by Account category. - **totals** (dict) - The Total balances of Accounts in the sections of the report. - **result_amounts** (dict) - The amounts results of the report. ### Example ```python cashflow_statememt = BalanceSheet(session) print(cashflow_statememt) ``` Output: ``` Test Entity Cashflow Statement For the period: 01, Jan 2024 to 23, Feb 2024 Operating Cash Flow Net Profit 100.0000 Receivables -70.0000 Payables 120.0000 Taxation 20.0000 _______________ Total Operating Cash Flow 170.0000 Investment Cash Flow Non Current Assets -120.0000 _______________ Total Investment Cash Flow -120.0000 Financing Cash Flow Equity 0.0000 _______________ Total Financing Cash Flow 0.0000 Net Cash Flow Beginning Cash Balance 0.0000 Net Cash Flow 50.0000 _______________ Ending Cash Balance 50.0000 =============== _______________ Cashbook Balance 50.0000 =============== ``` ``` -------------------------------- ### Create and print a BalanceSheet Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/balance_sheet.md.txt Creates a BalanceSheet instance with the accounting session and prints it. The output shows the balance sheet with sections for Assets, Liabilities, and Equity, including totals and net assets. ```python balance_sheet = BalanceSheet(session) print(balance_sheet) ``` -------------------------------- ### ClearingMixin.clearances(session) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/mixins/clearing/index.html Gets the assignments made to clear the Transaction. Returns a list of assignments made to clear the Transaction. ```APIDOC ## ClearingMixin.clearances(session) ### Description Gets the assignments made to clear the Transaction. ### Method clearances ### Parameters - **session** (Session) - Required - The accounting session to which the Transaction belongs. ### Returns - **list** - A List of assignments made to clear the Transaction. ``` -------------------------------- ### Create and post a CashSale with a LineItem Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Creates a LineItem for a cash sale, attaches it to a CashSale transaction, and posts the transaction to the ledger. The comment notes that posting sends the transaction to the ledger. ```python from python_accounting.models import LineItem cash_sale_line_item = LineItem( narration="Cash Sale line item", account_id=revenue_account.id, amount=100, tax_id=output_tax.id, entity_id=entity.id, ) session.add(cash_sale_line_item) session.flush() cash_sale.line_items.add(cash_sale_line_item) session.add(cash_sale) cash_sale.post(session) # This posts the Transaction to the Ledger ``` -------------------------------- ### Create Tax records Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Create output and input tax records (Output Vat at 20% and Input Vat at 10%) linked to the previously created tax account, then add them to the session. ```python from python_accounting.models import Tax output_tax = Tax( name="Output Vat", code="OTPT", account_id=tax_account.id, # This account was created earlier rate=20, entity_id=entity.id, ) input_tax = Tax( name="Input Vat", code="INPT", account_id=tax_account.id, rate=10, entity_id=entity.id, ) session.add_all([output_tax, input_tax]) session.commit() ``` -------------------------------- ### InvalidBalanceDateError Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/autoapi/exceptions/index.rst.txt Raised when the balance date is not earlier than the reporting period's start, unless the entity allows mid-year balances. ```APIDOC ## InvalidBalanceDateError ### Description Unless the Entity allows for mid year balances, the balance date must be earlier than its reporting period's start. ``` -------------------------------- ### Create and post a compound journal entry with multiple line items Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/compound_transaction.md.txt Creates a JournalEntry with compound=True, specifying a main account amount of 25. Adds three LineItems with different amounts and credit flags, then posts the entry. The printed closing balances show the effect: bank account 25, client account -30, supplier account 15, asset account -10. ```python from datetime import datetime from python_accounting.models import Transaction, LineItem from python_accounting.transactions import JournalEntry with get_session(engine) as session: journal_entry = JournalEntry( narration="Journal Entry Transaction", transaction_date=datetime.now(), account_id=bank_account.id, entity_id=entity.id, compound=True, # <- Turn on compound flag credited=False, main_account_amount=25, # <- Specify how much to post to the main (bank) account ) session.add(journal_entry) session.flush() client_account_line_item = LineItem( narration="Client Account line item", account_id=client_account.id, credited=True, # <- Specify to credit this line item's amount to is account amount=30, entity_id=entity.id, ) supplier_account_line_item = LineItem( narration="Supplier Account line item", account_id=client_account.id, amount=15, entity_id=entity.id, ) asset_account_line_item = LineItem( narration="Asset Account line item", account_id=asset_account.id, credited=True, # <- Specify to credit this line item's amount to is account amount=10, entity_id=entity.id, ) session.add_all([ client_account_line_item, supplier_account_line_item, asset_account_line_item ]) session.flush() journal_entry.line_items.add(client_account_line_item) journal_entry.line_items.add(supplier_account_line_item) journal_entry.line_items.add(asset_account_line_item) session.add(journal_entry) journal_entry.post(session) print(bank_account.closing_balance(session)) # 25 print(client_account.closing_balance(session)) # -30 print(supplier_account.closing_balance(session)) # 15 print(asset_account.closing_balance(session)) # -10 ``` -------------------------------- ### mixins.ClearingMixin Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/mixins/index.html Provides clearable Transactions functionality for assigning them to assignable Transactions. Includes methods to get cleared amount, clearances, and unclear. ```APIDOC ## mixins.ClearingMixin ### Description Provides clearable Transactions functionality for assigning them to assignable Transactions. ### Methods #### cleared(session) -> decimal.Decimal Gets how much of the Transaction amount has been cleared by assignable Transactions. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - The total amount of assignments made against Transaction. **Return type:** - Decimal #### clearances(session) -> list Gets the assignments made to clear the Transaction. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - A List of assignments made to clear the Transaction. #### unclear(session) -> None Removes all assignments made to clear this Transaction. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - None ``` -------------------------------- ### mixins.AssigningMixin Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/mixins/index.html Provides assignable Transactions functionality for clearing clearable Transactions. Includes methods to get balance, assignments, unassign, and bulk assign. ```APIDOC ## mixins.AssigningMixin ### Description Provides assignable Transactions functionality for clearing clearable Transactions. ### Methods #### balance(session) -> decimal.Decimal Gets how much of the Transaction amount is remaining available for assigning to clearable Transactions. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - The difference between the Transaction amount and the total amount of assignments made to it. **Return type:** - Decimal #### assignments(session) -> list Gets the assignments made on the Transaction. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - A List of assignments made for the Transaction. #### unassign(session) -> None Removes all assignments made to this Transaction. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - None #### bulk_assign(session) -> None Assigns the Transaction's amount to all outstanding clearable Transactions for the main account on a FIFO basis. **Parameters:** - **session** (_Session_) - The accounting session to which the Transaction belongs. **Returns:** - None ``` -------------------------------- ### Generate Account Schedule with statement(schedule=True) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/account_schedule.md.txt Creates an account schedule by calling the statement method with the schedule parameter set to True. The output shows the returned dictionary structure with transactions, total_amount, cleared_amount, and uncleared_amount keys. ```python account = session.get(Account, account.id) statement = account.statement(session, None, None, True) print(statement) { 'transactions': [...], 'total_amount': Decimal('0'), 'cleared_amount': Decimal('0'), 'uncleared_amount': Decimal('0') } ``` -------------------------------- ### ClearingMixin.cleared(session) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/autoapi/mixins/clearing/index.html Gets how much of the Transaction amount has been cleared by assignable Transactions. Returns the total amount of assignments made against the Transaction as a Decimal. ```APIDOC ## ClearingMixin.cleared(session) ### Description Gets how much of the Transaction amount has been cleared by assignable Transactions. ### Method cleared ### Parameters - **session** (Session) - Required - The accounting session to which the Transaction belongs. ### Returns - **Decimal** - The total amount of assignments made against Transaction. ``` -------------------------------- ### Create AgingSchedule and print balances Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/aging_schedule.md.txt Creates an AgingSchedule for receivable accounts and prints the accounts list and balances dictionary. The balances output shows the default age brackets with zero amounts. ```python schedule = AgingSchedule(session, Account.AccountType.RECEIVABLE) print(schedule.accounts) [...] print(schedule.balances) { 'current': Decimal('0'), '31 - 90 days': Decimal('0'), '91 - 180 days': Decimal('0'), '181 - 270 days': Decimal('0'), '271 - 365 days': Decimal('0'), '365+ Bad Debts': Decimal('0') } ``` -------------------------------- ### Generate Balance Sheet Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/index.html Initializes a BalanceSheet object with a session and prints the report. ```python from python_accounting.reports import BalanceSheet balance_sheet = BalanceSheet(session) print(balance_sheet) ``` -------------------------------- ### Post a standard journal entry with credited=False Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/examples.html Creates a JournalEntry with credited=False, adds three line items (Client, Supplier, Asset), posts the transaction, and prints closing balances. The main account is debited with the total amount, and line items are credited. ```python from datetime import datetime from python_accounting.models import Transaction, LineItem from python_accounting.transactions import JournalEntry with get_session(engine) as session: journal_entry = JournalEntry( narration="Journal Entry Transaction", transaction_date=datetime.now(), account_id=bank_account.id, entity_id=entity.id, credited=False # <- Debit the main (bank) account ) session.add(journal_entry) session.flush() client_account_line_item = LineItem( narration="Client Account line item", account_id=client_account.id, amount=30, entity_id=entity.id, ) supplier_account_line_item = LineItem( narration="Supplier Account line item", account_id=supplier_account.id, amount=15, entity_id=entity.id, ) asset_account_line_item = LineItem( narration="Asset Account line item", account_id=asset_account.id, amount=10, entity_id=entity.id, ) session.add_all([ client_account_line_item, supplier_account_line_item, asset_account_line_item ]) session.flush() journal_entry.line_items.add(client_account_line_item) journal_entry.line_items.add(supplier_account_line_item) journal_entry.line_items.add(asset_account_line_item) session.add(journal_entry) journal_entry.post(session) print(bank_account.closing_balance(session)) # 55 print(client_account.closing_balance(session)) # -30 print(supplier_account.closing_balance(session)) # -15 print(asset_account.closing_balance(session)) # -10 ``` -------------------------------- ### Create Entity and Currency Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Create a reporting Entity and its reporting Currency within a session. Committing the Entity automatically sets up a Reporting Period. ```python from python_accounting.database.session import get_session from python_accounting.models import Entity, Currency with get_session(engine) as session: entity = Entity(name="Example Company") session.add(entity) session.commit() # This automatically sets up a Reporting Period for the Entity currency = Currency(name="US Dollars", code="USD", entity_id=entity.id) session.add(currency) session.commit() ``` -------------------------------- ### opening_balance(session, year) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/autoapi/models/account/index.rst.txt Retrieves the opening balance of an Account for a given year. ```APIDOC ## opening_balance(session, year) ### Description Retrieves the total opening balance of the Account for the given calendar year. ### Method opening_balance ### Parameters - **session** (Session) - Required - The accounting session to which the Account belongs. - **year** (int) - Required - The calendar year for which to retrieve the opening balance. ### Returns - **Decimal** - The total opening balance of the Account for the year. ``` -------------------------------- ### TrialBalance Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/reports.html Documentation for the TrialBalance class constructor, which generates a trial balance report. ```APIDOC ## TrialBalance ### Description Generates a trial balance report comparing the closing balances of all accounts in the chart, debits against credits, categorized as Income Statement or Balance Sheet accounts. ### Parameters - **session** (Session) - Required - The accounting session. - **end_date** (datetime) - Required - The latest transaction date for Transaction amounts to be included in the statement. ### Returns An `TrialBalance` with: - **sections** (StrEnum) - The sections of the Balance Sheet. - **results** (StrEnum) - The results of the Balance Sheet. - **accounts** (dict) - The Accounts in the sections of the report, by Account category. - **balances** (dict) - The total balances of the Accounts in the sections of the report, by Account category. - **totals** (dict) - The Total balances of Accounts in the sections of the report. - **result_amounts** (dict) - The amounts results of the report. ### Example ```python trial_balance = TrialBalance(session) print(trial_balance) Test Entity Trial Balance For the period: 01, Jan 2024 to 24, Feb 2024 Income Statement Operating Revenue -420.0000 Non Operating Revenue -50.0000 Operating Expense 100.0000 Direct Expense 65.0000 Overhead Expense 40.0000 _______________ Total Income Statement -265.0000 Balance Sheet Non Current Asset 100.0000 Inventory 100.0000 Bank 374.5000 Receivable 22.0000 Control -4.5000 Current Liability -100.0000 Payable -220.0000 Equity -77.0000 Reconciliation 70.0000 _______________ Total Balance Sheet 265.0000 _______________ Total Debits 871.5000 =============== _______________ Total Credits 871.5000 =============== ``` ``` -------------------------------- ### Standard Journal Entry with credited=False Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/compound_transaction.md.txt Create four accounts (bank, client, supplier, asset) and then post a standard Journal Entry with credited=False, adding three line items and printing the resulting closing balances. ```python from python_accounting.models import Account with get_session(engine) as session: bank_account = Account( name="Bank Account", account_type=Account.AccountType.BANK, currency_id=currency.id, entity_id=entity.id, ) client_account = Account( name="Client Account", account_type=Account.AccountType.RECEIVABLE, currency_id=currency.id, entity_id=entity.id, ) supplier_account = Account( name="Supplier Account", account_type=Account.AccountType.PAYABLE, currency_id=currency.id, entity_id=entity.id, ) asset_account = Account( name="Asset Account", account_type=Account.AccountType.NON_CURRENT_ASSET, currency_id=currency.id, entity_id=entity.id, ) session.add_all([ bank_account, client_account, supplier_account, asset_account ]) session.commit() ``` ```python from datetime import datetime from python_accounting.models import Transaction, LineItem from python_accounting.transactions import JournalEntry with get_session(engine) as session: journal_entry = JournalEntry( narration="Journal Entry Transaction", transaction_date=datetime.now(), account_id=bank_account.id, entity_id=entity.id, credited=False # <- Debit the main (bank) account ) session.add(journal_entry) session.flush() client_account_line_item = LineItem( narration="Client Account line item", account_id=client_account.id, amount=30, entity_id=entity.id, ) supplier_account_line_item = LineItem( narration="Supplier Account line item", account_id=supplier_account.id, amount=15, entity_id=entity.id, ) asset_account_line_item = LineItem( narration="Asset Account line item", account_id=asset_account.id, amount=10, entity_id=entity.id, ) session.add_all([ client_account_line_item, supplier_account_line_item, asset_account_line_item ]) session.flush() journal_entry.line_items.add(client_account_line_item) journal_entry.line_items.add(supplier_account_line_item) journal_entry.line_items.add(asset_account_line_item) session.add(journal_entry) journal_entry.post(session) print(bank_account.closing_balance(session)) # 55 print(client_account.closing_balance(session)) # -30 print(supplier_account.closing_balance(session)) # -15 print(asset_account.closing_balance(session)) # -10 ``` -------------------------------- ### TrialBalance(session) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/trial_balance.html Generates a trial balance report comparing debits and credits for all accounts in the chart of accounts. ```APIDOC ## TrialBalance(session) ### Description Generates a trial balance report that compares the closing balances of all accounts in the chart, categorised as Income Statement or Balance Sheet accounts. ### Parameters - **session** (Session) - Required - The accounting session. - **end_date** (datetime) - Optional - The latest transaction date for Transaction amounts to be included in the statement. ### Returns A `TrialBalance` object with the following attributes: - **sections** (StrEnum) - The sections of the Balance Sheet. - **results** (StrEnum) - The results of the Balance Sheet. - **accounts** (dict) - The Accounts in the sections of the report, by Account category. - **balances** (dict) - The total balances of the Accounts in the sections of the report, by Account category. - **totals** (dict) - The Total balances of Accounts in the sections of the report. - **result_amounts** (dict) - The amounts results of the report. ### Example ```python trial_balance = TrialBalance(session) print(trial_balance) ``` ``` -------------------------------- ### Generate and print an IncomeStatement Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/income_statement.html Creates an IncomeStatement using a session and prints it. The output shows the formatted report with sections for Operating Revenues, Operating Expenses, Gross Profit, Non Operating Revenues, Total Revenue, Non Operating Expenses, Total Expenses, and Net Profit. ```python statement = IncomeStatement(session) print(statement) Example Company Income Statement For the period: 01, Jan 2024 to 23, Feb 2024 Operating Revenues Operating Revenue 200.0000 Operating Expenses Operating Expense 100.0000 _______________ Gross Profit 100.0000 Non Operating Revenues Non Operating Revenue 0.0000 _______________ Total Revenue 100.0000 Non Operating Expenses _______________ Total Expenses 0.0000 _______________ Net Profit 100.0000 =============== ``` -------------------------------- ### AgingSchedule Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/aging_schedule.html Documentation for the AgingSchedule class constructor and its usage. ```APIDOC ## AgingSchedule ### Description Creates an aging schedule that categorizes outstanding receivable or payable amounts by age brackets. The brackets can be configured to any arbitrary number of days. ### Constructor `AgingSchedule(session, account_type, end_date=None)` ### Parameters - **session** (Session) - Required - The accounting session. - **account_type** (Account.AccountType) - Required - Must be either `Account.AccountType.RECEIVABLE` or `Account.AccountType.PAYABLE`. - **end_date** (datetime) - Optional - The latest transaction date for amounts to be included. Defaults to current date if not provided. ### Returns An `AgingSchedule` object with: - **accounts** (list) - Accounts which have outstanding clearable transactions as at the end date. - **balances** (dict) - The total amount outstanding for each age bracket. ### Account Balances Property Each returned Account includes a `balances` property indicating how much of the account's outstanding balance falls into each age bracket. ### Example ```python schedule = AgingSchedule(session, Account.AccountType.RECEIVABLE) print(schedule.accounts) [...] print(schedule.balances) { 'current': Decimal('0'), '31 - 90 days': Decimal('0'), '91 - 180 days': Decimal('0'), '181 - 270 days': Decimal('0'), '271 - 365 days': Decimal('0'), '365+ Bad Debts': Decimal('0') } ``` ``` -------------------------------- ### Generate an account statement Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/account_statement.md.txt Retrieves an Account by its id, calls its statement method with the session, and prints the resulting dictionary. The output shows the structure with opening_balance, transactions, and closing_balance, where transactions include debit, credit, and balance properties. ```python account = session.get(Account, account.id) statement = account.statement(session) print(statement) { 'opening_balance': Decimal('0'), 'transactions': [...], 'closing_balance': Decimal('0') } ``` -------------------------------- ### Create output tax Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/tax_inclusive.md.txt Creates a Tax object with a 10% rate and associates it with the tax account. ```python from python_accounting.models import Tax with get_session(engine) as session: output_tax = Tax( name="Input Vat", code="INPT", account_id=tax_account.id, rate=10, entity_id=entity.id, ) session.add_all(output_tax, input_tax) session.commit() ``` -------------------------------- ### Generate Balance Sheet Report Source: https://github.com/ekmungai/python-accounting/blob/main/docs/index.rst Creates a BalanceSheet report for a session and prints it, showing assets, liabilities, net assets, and equity with detailed breakdowns. ```python from python_accounting.reports import BalanceSheet balance_sheet = BalanceSheet(session) print(balance_sheet) Example Company Balance Sheet For the period: 01, Jan 2024 to 23, Feb 2024 Assets Non Current Asset 100.0000 (asset purchase) Bank 30.0000 (120 cash sale - 110 cash purchase + 50 client receipt - 30 journal entry) Receivable 130.0000 (150 credit sale + 30 Tax - 50 client receipt) _______________ Total Assets 260.0000 Liabilities Control 30.0000 (Taxes: 20 cash sale + 30 credit sale - 10 cash purchase - 10 credit purchase) Payable 110.0000 (100 credit purchase + 10 Tax) _______________ Total Liabilities 140.0000 _______________ Net Assets 120.0000 =============== Equity Income Statement 120.0000 _______________ Total Equity 120.0000 =============== ``` -------------------------------- ### Create and post multiple transaction types with LineItems Source: https://github.com/ekmungai/python-accounting/blob/main/README.md Creates and posts five transaction types (ClientInvoice, CashPurchase, SupplierBill, JournalEntry, ClientReceipt) with their associated LineItems. Each transaction is added to the session, flushed, and then posted to the ledger. ```python from python_accounting.transactions import ( CashSale, ClientInvoice, CashPurchase, SupplierBill, ClientReceipt, ) client_invoice = ClientInvoice( narration="Client Invoice Transaction", transaction_date=datetime.now(), account_id=client_account.id, entity_id=entity.id, ) session.add(client_invoice) session.flush() client_invoice_line_item = LineItem( narration="Client Invoice line item", account_id=revenue_account.id, amount=50, quantity=3, tax_id=output_tax.id, entity_id=entity.id, ) session.add(client_invoice_line_item) session.flush() client_invoice.line_items.add(client_invoice_line_item) session.add(client_invoice) client_invoice.post(session) cash_purchase = CashPurchase( narration="Cash Purchase Transaction", transaction_date=datetime.now(), account_id=bank_account.id, entity_id=entity.id, ) session.add(cash_purchase) session.flush() cash_purchase_line_item = LineItem( narration="Cash Purchase line item", account_id=opex_account.id, amount=25, quantity=4, tax_id=output_tax.id, entity_id=entity.id, ) session.add(cash_purchase_line_item) session.flush() cash_purchase.line_items.add(cash_purchase_line_item) session.add(cash_purchase) cash_purchase.post(session) supplier_bill = SupplierBill( narration="Credit Purchase Transaction", transaction_date=datetime.now(), account_id=supplier_account.id, entity_id=entity.id, ) session.add(supplier_bill) session.flush() supplier_bill_line_item = LineItem( narration="Credit Purchase line item", account_id=asset_account.id, amount=25, quantity=4, tax_id=output_tax.id, entity_id=entity.id, ) session.add(supplier_bill_line_item) session.flush() supplier_bill.line_items.add(supplier_bill_line_item) session.add(supplier_bill) supplier_bill.post(session) journal_entry = JournalEntry( narration="Journal Entry Transaction", transaction_date=datetime.now(), account_id=bank_account.id, entity_id=entity.id, ) session.add(journal_entry) session.flush() journal_entry_line_item = LineItem( narration="Journal Entry line item", account_id=expense_account.id, amount=30, entity_id=entity.id, ) session.add(journal_entry_line_item) session.flush() journal_entry.line_items.add(journal_entry_line_item) session.add(journal_entry) journal_entry.post(session) client_receipt = ClientReceipt( narration="Client Receipt Transaction", transaction_date=datetime.now(), account_id=client_account.id, entity_id=entity.id, ) session.add(client_receipt) session.flush() client_receipt_line_item = LineItem( narration="Client Receipt line item", account_id=bank_account.id, amount=50, quantity=1, entity_id=entity.id, ) session.add(client_receipt_line_item) session.flush() client_receipt.line_items.add(client_receipt_line_item) session.add(client_receipt) client_receipt.post(session) ``` -------------------------------- ### Account.statement(session, start_date, end_date) Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/account_statement.html Documentation for the Account.statement method, which generates an account statement for a given account within a specified date range. ```APIDOC ## Account.statement(session, start_date, end_date) ### Description Generates an account statement for a given account, listing transactions posted within a specified date range and their effect on the account balance. ### Method Method call on an Account object. ### Endpoint `account.statement(session, start_date, end_date)` ### Parameters - **session** (Session) - Required - The accounting session to which the Account belongs. - **start_date** (datetime) - Required - The earliest transaction date for Transaction amounts to be included in the statement. - **end_date** (datetime) - Required - The latest transaction date for Transaction amounts to be included in the statement. ### Returns A dictionary with: - **opening_balance** (Decimal) - The balance of the Account at the beginning of the statement period. - **transactions** (list) - Transactions posted to the Account during the period. Each transaction includes standard attributes plus `debit`, `credit`, and `balance` properties. - **closing_balance** (Decimal) - The balance of the Account at the end of the statement period. ### Example ```python account = session.get(Account, account.id) statement = account.statement(session) print(statement) # { # 'opening_balance': Decimal('0'), # 'transactions': [...], # 'closing_balance': Decimal('0') # } ``` ``` -------------------------------- ### Create and print an IncomeStatement Source: https://github.com/ekmungai/python-accounting/blob/main/docs/_build/html/_sources/income_statement.md.txt Creates an IncomeStatement for the given accounting session and prints it. The output shows the statement with sections for Operating Revenues, Operating Expenses, Gross Profit, Non Operating Revenues, Total Revenue, Non Operating Expenses, Total Expenses, and Net Profit for the period from 01 Jan 2024 to 23 Feb 2024. ```python statement = IncomeStatement(session) print(statement) ```