模块交互
Invoicing应用程序中创建发票
一个具体示例: 记账凭证(Account Move
创建一个
estate_account
模块预期效果动画地址:https://www.odoo.com/documentation/14.0/zh_CN/_images/create_inv.gif
链接模块(Link Module
此类使用案例的常见方法是创建“链接”模块。在我们的案例中,该模块依赖estate
和account
,包括房产的发票创建逻辑。采用这种方式,estate
和account
模块可以独立安装。当两者都安装后,链接模块将提供新功能。
练习--创建链接模块
estate 和account
的 estate_account
空壳模块,创建以后安装该模块。你可能会注意到,Invoicing
应用也被安装了。这是意料之中的,因为你的模块依赖它。 如果你卸载Invoicing
模块,你的模块也会被卸载。
__init__.py为空
创建发票
estate.property模型添加功能,即我们希望在出售房产时添加一些额外的逻辑。
estate_account模块中为创建一个模型,继承estate.property
模型。现在,重写操作,仅返回super
调用,拿个例子来说可能更清楚:
from odoo import models
class InheritedModel(models.Model:
_inherit = "inherited.model"
def inherited_action(self:
return super(.inherited_action(
可以在这找个具体的示例
class AccountMove(models.Model:
_name = "account.move"
_inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin', 'sequence.mixin']
_description = "Journal Entry"
#... 略
def action_invoice_paid(self:
''' Hook to be overrided called when the invoice moves to the paid state. '''
pass
class AccountMove(models.Model:
_inherit = 'account.move'
def action_invoice_paid(self:
""" When an invoice linked to a sales order selling registrations is
paid confirm attendees. Attendees should indeed not be confirmed before
full payment. """
res = super(AccountMove, self.action_invoice_paid(
self.mapped('line_ids.sale_line_ids'._update_registrations(confirm=True, mark_as_paid=True
return res
练习--添加创建发票的第一步
- 在
-
_inherit
estate.property
模块 - 重写
action_sold
方法(你可能已经将该方法命名为不同的名称了 以返回super
调用
estate_account
模块中的正确目录创建 estate_property.py
文件
提示: 为了确保它正常工作,添加一个print
或者调试断点到重写的方法中。
odoo14\custom\estate_account\models\__init__.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from . import estate_property
odoo14\custom\estate_account\models\estate_property.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from odoo import models class InheritedEstateProperty(models.Model: _inherit = "estate.property" def set_property_sold(self: return super(.set_property_sold(
修改
odoo14\custom\estate_account\__init__.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from . import models
它有效吗?如果没有,请检查是否正确导入了所有Python文件。
学习的一个好方法是看看其他模块是如何完成你想做的事情的。例如,销售的一个基本流程是从销售订单创建发票。这看起来是一个很好的起点,因为它正是我们想要做的。花一些时间思考和理解创建发票方法。
- 一个
- 一个
move_type
: 它有几个可能的值 -
journal_id
: the accounting journal
partner_id
: 顾客
这足够创建一个张空发票。
练习--添加发票创建第二步
action_sold,并创建一个空的 account.move
:
- 从当前的
-
move_type
应该和Customer Invoice
对应
estate.property
获取 partner_id
- 使用
-
create
方法不接受结果集作为字段值。
self.env[model_name].create(values
创建一个对象, 其中values
为一个字典。
修改odoo14\custom\estate_account\models\estate_property.py
def set_property_sold(self:
self.env['account.move'].create({}
return super(.set_property_sold(
当房产设置为“已售出”时,你现在应该在Invoiceing/customer/Invoices
中创建一个新的客户发票。
quantity
price_unit
name
:发票行的描述
此外,发票行需要链接到发票。将发票行链接到发票的最简单、最有效的方法是在创建发票时包含所有行。为此在account.move
创建中包含invoice_line_ids
字段,这是一个One2many
字段。One2many
和Many2many
使用通用ORM方法中描述的特殊“commands”。这种格式是一个按顺序执行的三元组列表,其中每个三元组都是要对结果集执行的命令。下面是一个在创建test.model
时包含一个One2many
字段line_ids
的简单示例:
def inherited_action(self:
self.env["test.model"].create(
{
"name": "Test",
"line_ids": [
(
0,
0,
{
"field_1": "value_1",
"field_2": "value_2",
},
],
}
return super(.inherited_action(
练习--添加创建发票的第三步
创建account.move
时添加两个发票行。每个售出的房产都将按照以下条件开具发票:
- 售价的6%
- 额外100.00行政费
invoice_line_ids。对于每个发票行,我们需要一个 name
, quantity
和price_unit
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from odoo import models
from odoo.exceptions import UserError
class InheritedEstateProperty(models.Model:
_inherit = "estate.property"
def set_property_sold(self:
print('override set_property_sold'
journal = self.env['account.move'].with_context(default_move_type='out_invoice'._get_default_journal(
if not journal:
raise UserError('Please define an accounting sales journal for the company'
self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.buyer_id,
'journal_id': journal.id, # company comes from the journal
'invoice_line_ids': [{
'name': 'Avaliable house 01',
'quantity': 1,
'price_unit': 0.6 * self.best_price
},{
'name': ' Administrative fees',
'quantity': 1,
'price_unit': 100
}]
}
return super(.set_property_sold(
重启服务,验证效果