Odoo开发:针对REBU销售税修改发票日记账分录
解决Odoo中REBU (sales)特殊税的日记账分录拆分问题
需求明确
部分产品需缴纳REBU (sales)特殊税,税额计算规则如下(以采购成本100欧元、售价300欧元为例):
- 免税税基 = 采购成本 = 100欧元
- 应税税基 = (销售价格 - 采购成本)/1.21 = (300-100)/1.21 ≈ 165.29欧元
- 税额 = (销售价格 - 采购成本) - 应税税基 = 200 - 165.29 ≈ 34.71欧元
要求发票行保持不变,仅调整生成的日记账分录,拆分出免税、应税和税额对应的行。
实现思路
用0%税率的REBU (sales)税维持发票行显示正确,通过代码重写日记账生成逻辑,拆分税基为两部分:
- 采购成本部分关联0%免税税
- 售价与采购成本的差额部分拆分出应税基和税额,关联21%的REBU税
具体代码实现
重写Odoo生成日记账行的核心方法,识别REBU税行后拆分金额:
from odoo import models, api class AccountMove(models.Model): _inherit = 'account.move' def _get_move_lines(self, cancel=False): move_lines = super()._get_move_lines(cancel=cancel) new_move_lines = [] for line in move_lines: # 跳过非发票行或无税的行 if line.get('tax_ids'): tax_ids = self.env['account.tax'].browse(line['tax_ids'][0][2]) reb_tax = tax_ids.filtered(lambda t: 'REBU (sales)' in t.name and t.amount == 0) if reb_tax: # 获取对应发票行的采购成本(从批次字段取) invoice_line = self.invoice_line_ids.filtered(lambda l: l.id == line.get('invoice_line_id')) if invoice_line.lot_id and invoice_line.lot_id.x_serial_purchase_precio: purchase_cost = invoice_line.lot_id.x_serial_purchase_precio sale_subtotal = invoice_line.price_subtotal # 计算各部分金额 tax_exempt_base = purchase_cost taxable_base = (sale_subtotal - purchase_cost) / 1.21 tax_amount = (sale_subtotal - purchase_cost) - taxable_base # 添加免税部分的日记账行 new_move_lines.append({ **line, 'name': f"{line['name']} (REBU 免税)", 'price_unit': tax_exempt_base, 'quantity': 1, 'tax_ids': [(6, 0, reb_tax.ids)], }) # 获取21%的REBU税(需提前在系统配置) reb_tax_21 = self.env['account.tax'].search([ ('name', '=', 'REBU (sales) 21%'), ('type_tax_use', '=', 'sale') ], limit=1) if reb_tax_21: # 添加应税基部分的行 new_move_lines.append({ **line, 'name': f"{line['name']} (REBU 应税)", 'price_unit': taxable_base, 'quantity': 1, 'tax_ids': [(6, 0, reb_tax_21.ids)], }) # 添加税额行(关联税金科目) new_move_lines.append({ 'name': 'REBU 税额', 'account_id': reb_tax_21.account_id.id, 'price_unit': tax_amount, 'quantity': 1, 'tax_ids': [], 'move_id': line['move_id'], }) continue new_move_lines.append(line) return new_move_lines
关键注意事项
- 税配置前提:系统中需提前创建两个REBU税:
- 0%税率的
REBU (sales)(用于发票行显示) - 21%税率的
REBU (sales) 21%(用于计算应税部分税额),并正确配置对应的税金科目
- 0%税率的
- 字段校验:确保批次的
x_serial_purchase_precio字段在入库时已正确赋值,避免计算时取值为空 - 版本适配:如果使用Odoo 14及以下版本,可能需要替换重写的方法为
_move_line_get_item,不同版本的日记账生成方法有差异,需对应调整 - 测试验证:创建测试发票,关联带采购成本的批次,检查生成的日记账分录是否符合计算规则
内容的提问来源于stack exchange,提问作者Roberto López
相关产品推荐
相关产品推荐

