人财事物信息化 - sales_invoice.py

这份代码文件 sales_invoice.py 主要定义了与销售发票(Sales Invoice)相关的功能和类,用于处理销售发票的各种业务逻辑。下面是对该文件的详细介绍:

1. 导入模块

import frappe
import pandas as pd
from frappe import _, msgprint, throw
from frappe.contacts.doctype.address.address import get_address_display
from frappe.model.mapper import get_mapped_doc
from frappe.model.utils import get_fetch_values
from frappe.utils import add_days, cint, cstr, flt, formatdate, get_link_to_form, getdate, nowdate

import erpnext
from erpnext.accounts.deferred_revenue import validate_service_stop_date
from erpnext.accounts.doctype.loyalty_program.loyalty_program import (
    get_loyalty_program_details_with_points,
    validate_loyalty_points,
)
from erpnext.accounts.doctype.repost_accounting_ledger.repost_accounting_ledger import (
    validate_docs_for_deferred_accounting,
    validate_docs_for_voucher_types,
)
from erpnext.accounts.doctype.tax_withholding_category.tax_withholding_category import (
    get_party_tax_withholding_details,
)
from erpnext.accounts.general_ledger import get_round_off_account_and_cost_center
from erpnext.accounts.party import get_due_date, get_party_account, get_party_details
from erpnext.accounts.utils import (
    cancel_exchange_gain_loss_journal,
    get_account_currency,
    update_voucher_outstanding,
)
from erpnext.assets.doctype.asset.depreciation import (
    depreciate_asset,
    get_disposal_account_and_cost_center,
    get_gl_entries_on_asset_disposal,
    get_gl_entries_on_asset_regain,
    reset_depreciation_schedule,
    reverse_depreciation_entry_made_after_disposal,
)
from erpnext.assets.doctype.asset_activity.asset_activity import add_asset_activity
from erpnext.controllers.accounts_controller import validate_account_head
from erpnext.controllers.selling_controller import SellingController
from erpnext.projects.doctype.timesheet.timesheet import get_projectwise_timesheet_data
from erpnext.setup.doctype.company.company import update_company_current_month_sales
from erpnext.stock.doctype.delivery_note.delivery_note import update_billed_amount_based_on_so
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

导入了多个模块和函数,包括 frappe 框架的核心模块、pandas 库,以及 erpnext 应用中的多个功能模块,用于处理销售发票涉及的各种业务,如会计、资产、项目、库存等。

2. 类定义:SalesInvoice

class SalesInvoice(SellingController):
    # ...

定义了 SalesInvoice 类,继承自 SellingController。该类包含了销售发票的各种操作方法,如初始化、验证、保存、提交、取消等。

3. 方法列表

SalesInvoice 类中包含了大量的方法,用于处理销售发票的不同业务逻辑,例如:

  • 验证方法validatevalidate_accountsvalidate_for_repost 等,用于在不同阶段对销售发票的数据进行验证。
  • 生命周期方法before_savebefore_submiton_submitbefore_cancelon_cancel 等,用于在销售发票的保存、提交、取消等操作前后执行特定的逻辑。
  • 数据处理方法set_missing_valuesupdate_time_sheetset_paid_amount 等,用于设置销售发票的缺失值、更新时间表、设置支付金额等。
  • 会计分录方法make_gl_entriesmake_customer_gl_entrymake_tax_gl_entries 等,用于生成销售发票的会计分录。

4. 全局函数

文件中还定义了多个全局函数,用于处理与销售发票相关的其他业务逻辑,例如:

  • get_total_in_party_account_currency:获取销售发票在客户账户货币下的总金额。
  • is_overdue:判断销售发票是否逾期。
  • get_discounting_status:获取销售发票的贴现状态。
  • create_sales_invoice_from_file:从文件中创建销售发票。

总结

这份代码文件实现了销售发票的核心业务逻辑,包括验证、保存、提交、取消、会计分录生成等操作。通过 SalesInvoice 类和多个全局函数,对销售发票的各个环节进行了全面的处理,确保销售发票的业务流程顺利进行。

create_sales_invoice_from_file函数

以下是 create_sales_invoice_from_file 函数,实现了根据上传 CSV 批量创建销售发票的功能:

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

import frappe
import pandas as pd

@frappe.whitelist()
def create_sales_invoice_from_file(docname, file_url):
    try:
        # 获取上传的文件
        file = frappe.get_doc("File", {"file_url": file_url})
        file_path = frappe.get_site_path('public', file.file_name.lstrip('/'))

        # 读取 CSV 文件
        df = pd.read_csv(file_path)

        created_invoices = []
        for index, row in df.iterrows():
            # 创建销售发票文档
            invoice = frappe.new_doc("Sales Invoice")

            # 假设 CSV 文件包含以下列:
            # customer, item_code, qty, rate
            invoice.customer = row.get('customer')
            invoice.posting_date = row.get('posting_date', nowdate())

            # 添加发票项目
            item = {
                "item_code": row.get('item_code'),
                "qty": row.get('qty'),
                "rate": row.get('rate')
            }
            invoice.append("items", item)

            # 保存发票
            invoice.insert()
            created_invoices.append(invoice.name)

        return created_invoices
    except Exception as e:
        frappe.log_error(f"创建销售发票时出错: {str(e)}")
        frappe.throw(_("创建销售发票时出错,请检查文件格式和数据。"))

代码说明:

  1. create_sales_invoice_from_file 函数

    • 首先,通过 frappe.get_doc 获取上传的文件对象,然后得到文件的本地路径。
    • 使用 pandasread_csv 方法读取 CSV 文件。
    • 遍历 CSV 文件的每一行,为每行创建一个新的 Sales Invoice 文档。
    • 假设 CSV 文件包含 customeritem_codeqtyrate 列,将这些数据填充到销售发票和发票项目中。
    • 保存销售发票文档,并将创建的发票名称添加到 created_invoices 列表中。
    • 最后返回创建的发票名称列表。
  2. 错误处理

    • 如果在创建销售发票过程中出现错误,将错误信息记录到日志中,并抛出异常提示用户检查文件格式和数据。

请根据实际的 CSV 文件列名和销售发票文档字段进行调整。

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