You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ERPNext彩票号码生成功能异常:无报错但字段未更新

问题分析与解决步骤

核心问题梳理

当前代码存在几个关键问题,导致字段不更新且未满足「客户累计消费满10万发放彩票号码」的需求:

  • 前后端字段名不统一:客户端操作lottery_number字段,后端更新custom_nomor_undian,两者未对齐;
  • 逻辑未实现「累计消费」:现有代码仅判断单张发票金额,而非客户的累计消费总额;
  • 后端on_submit事件的更新方式存在隐患;
  • 前后端事件触发冲突:after_submit(客户端)和on_submit(后端)同时执行,可能导致逻辑覆盖或异常。

分步修正方案

1. 统一字段名称

先确认Sales Invoice自定义字段的实际名称(比如后端用的custom_nomor_undian),确保前后端代码中使用的字段名完全一致。例如客户端代码需把lottery_number替换为custom_nomor_undian。

2. 实现「累计消费」核心逻辑

要满足需求,需先计算客户所有已提交Sales Invoice的总额,同时记录已兑换额度避免重复发放:

  • 给Customer doctype新增两个自定义字段:custom_accumulated_consumption(记录累计消费额)、custom_redeemed_lottery_amount(记录已兑换过彩票的累计金额);
  • 给Sales Invoice doctype确认custom_nomor_undian字段存在且权限为可读写。

3. 后端代码优化(推荐用后端实现,避免前端篡改)

替换原sales_invoice.py代码,实现完整累计逻辑:

import random
from frappe.model.document import Document
import frappe

class SalesInvoice(Document):
    def on_submit(self):
        # 仅处理指定客户组
        if self.customer_group not in ["Langganan Gorontalo", "Grosir Gorontalo"]:
            return
        
        # 获取客户文档并更新累计消费额
        customer = frappe.get_doc("Customer", self.customer)
        customer.custom_accumulated_consumption = (customer.custom_accumulated_consumption or 0) + self.base_grand_total
        customer.save()
        
        # 计算可兑换的彩票组数:每10万兑换3个号码
        total_redeemable = customer.custom_accumulated_consumption - (customer.custom_redeemed_lottery_amount or 0)
        if total_redeemable >= 100000:
            redeem_times = total_redeemable // 100000
            # 生成对应数量的彩票号码
            lottery_numbers = [random.randint(100000, 999999) for _ in range(redeem_times * 3)]
            lottery_numbers_str = ', '.join(str(n) for n in lottery_numbers)
            
            # 更新当前发票的彩票号码字段
            self.ignore_validate_update_after_submit = True
            self.db_set("custom_nomor_undian", lottery_numbers_str)
            
            # 更新客户已兑换额度
            customer.custom_redeemed_lottery_amount = (customer.custom_redeemed_lottery_amount or 0) + redeem_times * 100000
            customer.save()
            
            frappe.msgprint(f"已为客户生成{len(lottery_numbers)}个彩票号码:{lottery_numbers_str}")

4. 移除客户端冲突逻辑

由于后端已实现完整逻辑,建议删除原客户端脚本,避免前后端逻辑冲突。

5. 关键排查点

  • 确认所有新增自定义字段已正确添加到对应doctype,且字段权限设置为可读写;
  • 查看ERPNext系统日志(后台「系统日志」或frappe.log文件),排查是否存在隐性错误;
  • 测试时使用符合条件的客户组和发票金额,提交后检查客户累计字段与发票彩票号码字段是否更新。

内容的提问来源于stack exchange,提问作者Merck Kian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:01:03