人财事物信息化 - account_closing_balance.py

这段代码是一个基于Frappe框架的Python脚本,主要用于处理会计期末结账时的账户余额计算和记录。以下是对代码整体功能的详细解释:

模块导入

import frappe
from frappe.model.document import Document
from frappe.utils import cint, cstr

from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
    get_accounting_dimensions,
)
  • frappe:这是Frappe框架的核心模块,用于与数据库交互、创建文档等操作。
  • Document:用于定义和操作Frappe框架中的文档对象。
  • cintcstrfrappe.utils 模块中的函数,分别用于将值转换为整数和字符串。
  • get_accounting_dimensions:从 erpnext 应用中导入的函数,用于获取会计维度信息。

类定义

class AccountClosingBalance(Document):
    # begin: auto-generated types
    # This code is auto-generated. Do not modify anything in this block.

    from typing import TYPE_CHECKING

    if TYPE_CHECKING:
        from frappe.types import DF

        account: DF.Link | None
        account_currency: DF.Link | None
        closing_date: DF.Date | None
        company: DF.Link | None
        cost_center: DF.Link | None
        credit: DF.Currency
        credit_in_account_currency: DF.Currency
        debit: DF.Currency
        debit_in_account_currency: DF.Currency
        finance_book: DF.Link | None
        is_period_closing_voucher_entry: DF.Check
        period_closing_voucher: DF.Link | None
        project: DF.Link | None
    # end: auto-generated types

    pass
  • AccountClosingBalance 类继承自 Document 类,用于定义一个名为 Account Closing Balance 的文档类型。
  • 类中的类型注解部分是自动生成的,用于定义文档的字段及其类型。

函数定义

make_closing_entries 函数

def make_closing_entries(closing_entries, voucher_name, company, closing_date):
    accounting_dimensions = get_accounting_dimensions()

    previous_closing_entries = get_previous_closing_entries(company, closing_date, accounting_dimensions)
    combined_entries = closing_entries + previous_closing_entries

    merged_entries = aggregate_with_last_account_closing_balance(combined_entries, accounting_dimensions)

    for _key, value in merged_entries.items():
        cle = frappe.new_doc("Account Closing Balance")
        cle.update(value)
        cle.update(value["dimensions"])
        cle.update(
            {
                "period_closing_voucher": voucher_name,
                "closing_date": closing_date,
            }
        )
        cle.flags.ignore_permissions = True
        cle.flags.ignore_links = True
        cle.submit()
  • 功能:创建期末结账分录。
  • 步骤:
    1. 获取会计维度信息。
    2. 调用 get_previous_closing_entries 函数获取上一个期间的结账分录。
    3. 将当前结账分录和上一个期间的结账分录合并。
    4. 调用 aggregate_with_last_account_closing_balance 函数对合并后的分录进行汇总。
    5. 遍历汇总后的分录,创建 Account Closing Balance 文档并更新其字段值,最后提交文档。

aggregate_with_last_account_closing_balance 函数

def aggregate_with_last_account_closing_balance(entries, accounting_dimensions):
    merged_entries = {}
    for entry in entries:
        key, key_values = generate_key(entry, accounting_dimensions)
        merged_entries.setdefault(
            key,
            {
                "debit": 0,
                "credit": 0,
                "debit_in_account_currency": 0,
                "credit_in_account_currency": 0,
            },
        )

        merged_entries[key]["dimensions"] = key_values
        merged_entries[key]["debit"] += entry.get("debit")
        merged_entries[key]["credit"] += entry.get("credit")
        merged_entries[key]["debit_in_account_currency"] += entry.get("debit_in_account_currency")
        merged_entries[key]["credit_in_account_currency"] += entry.get("credit_in_account_currency")

    return merged_entries
  • 功能:对结账分录进行汇总,将相同维度的分录合并。
  • 步骤:
    1. 初始化一个空字典 merged_entries 用于存储汇总后的分录。
    2. 遍历所有分录,调用 generate_key 函数生成每个分录的唯一键。
    3. 如果键不存在,则在 merged_entries 中初始化该键对应的分录信息。
    4. 更新该键对应的分录的借方、贷方金额以及借方、贷方金额的账户本位币金额。
    5. 返回汇总后的分录字典。

generate_key 函数

def generate_key(entry, accounting_dimensions):
    key = [
        cstr(entry.get("account")),
        cstr(entry.get("account_currency")),
        cstr(entry.get("cost_center")),
        cstr(entry.get("project")),
        cstr(entry.get("finance_book")),
        cint(entry.get("is_period_closing_voucher_entry")),
    ]

    key_values = {
        "company": cstr(entry.get("company")),
        "account": cstr(entry.get("account")),
        "account_currency": cstr(entry.get("account_currency")),
        "cost_center": cstr(entry.get("cost_center")),
        "project": cstr(entry.get("project")),
        "finance_book": cstr(entry.get("finance_book")),
        "is_period_closing_voucher_entry": cint(entry.get("is_period_closing_voucher_entry")),
    }
    for dimension in accounting_dimensions:
        key.append(cstr(entry.get(dimension)))
        key_values[dimension] = cstr(entry.get(dimension))

    return tuple(key), key_values
  • 功能:为每个结账分录生成一个唯一的键,用于在汇总时区分不同的分录。
  • 步骤:
    1. 初始化一个列表 key 和一个字典 key_values,用于存储键和键对应的值。
    2. 将分录的基本信息添加到 keykey_values 中。
    3. 遍历会计维度信息,将每个维度的值添加到 keykey_values 中。
    4. key 转换为元组并返回。

get_previous_closing_entries 函数

def get_previous_closing_entries(company, closing_date, accounting_dimensions):
    entries = []
    last_period_closing_voucher = frappe.db.get_all(
        "Period Closing Voucher",
        filters={"docstatus": 1, "company": company, "period_end_date": ("<", closing_date)},
        fields=["name"],
        order_by="period_end_date desc",
        limit=1,
    )

    if last_period_closing_voucher:
        account_closing_balance = frappe.qb.DocType("Account Closing Balance")
        query = frappe.qb.from_(account_closing_balance).select(
            account_closing_balance.company,
            account_closing_balance.account,
            account_closing_balance.account_currency,
            account_closing_balance.debit,
            account_closing_balance.credit,
            account_closing_balance.debit_in_account_currency,
            account_closing_balance.credit_in_account_currency,
            account_closing_balance.cost_center,
            account_closing_balance.project,
            account_closing_balance.finance_book,
            account_closing_balance.is_period_closing_voucher_entry,
        )

        for dimension in accounting_dimensions:
            query = query.select(account_closing_balance[dimension])

        query = query.where(
            account_closing_balance.period_closing_voucher == last_period_closing_voucher[0].name
        )
        entries = query.run(as_dict=1)

    return entries
  • 功能:获取上一个期间的结账分录。
  • 步骤:
    1. 从数据库中查询上一个期间的结账凭证(Period Closing Voucher)。
    2. 如果存在上一个期间的结账凭证,则构建一个查询,从 Account Closing Balance 文档中查询与该凭证关联的所有分录。
    3. 执行查询并将结果以字典形式返回。

总结

这段代码的主要功能是处理会计期末结账时的账户余额计算和记录。它通过获取当前期间和上一个期间的结账分录,对其进行汇总,并创建新的 Account Closing Balance 文档来记录汇总后的分录信息。

Discard
Save
Review Changes ← Back to Content
Message Status Space Raised By Last update on