高度封装的前后端框架-odoo回顾(五): 翻译官方教程<<第14章:和其他模块交互>>

Chapter 14: Interact With Other Modules

第14章: 和其他模块交互

In the previous chapter, we used inheritance to modify the behavior of a module.
在上一章,我们使用继承去修改一个模型的行为
In our real estate scenario, we would like to go a step further and be able to generate invoices for our customers.
在我们的不动产场景下,我们想要更进一步并且能够为我们的客户生成发票
Odoo provides an Invoicing module, so it would be neat to create an invoice directly from our real estate module, i.e. once a property is set to ‘Sold’, an invoice is created in the Invoicing application.
Odoo提供一个开票模块,所以从我们的不动产模块直接创建一个发票就会变的很容易了

Concrete Example: Account Move

具体示例: 记账凭证

Note

记住:

Goal: at the end of this section:

目标: 在也一节结尾

  • A new module estate_account should be created
  • 一个新的模块estate_account 应该被创建
  • When a property is sold, an invoice should be issued for the buyer
  • 当一个不动产被售卖,应该为买方开具一个发票在这里插入图片描述

Invoice creation

发票创建

Any time we interact with another module, we need to keep in mind the modularity.
任何时候,我们想要和另外一个模块交互,我们应该记住模块化思想
If we intend to sell our application to real estate agencies, some may want the invoicing feature but others may not want it.
如果我们想要销售我们的应用给不动产经纪人,一些人可能想要发票特性,但是其他人不想要

Link Module

关联模块

The common approach for such use cases is to create a ‘link’ module.
对于这样案例的常用方案是创建一个关联模块
In our case, the module would depend on estate and account and would include the invoice creation logic of the estate property.
在我们的案例中,这个模块需要依赖estate和account,而且包含不动产的发票创建逻辑
This way the real estate and the accounting modules can be installed independently.
这种方法下,不动产模块和记账模块可以被独立安装
When both are installed, the link module provides the new feature.
当两个都安装的时候,关联模块可以提供新特性

Exercise

练习

Create a link module.
创建一个关联模块

Create the estate_account module, which depends on the estate and account modules. For now, it will be an empty shell.
创建estate_account 模块,它需要依赖estate和account模块.现在,它应该是个空壳子
Tip: you already did this at the beginning of the tutorial. The process is very similar.
贴士: 你应该在教程开头做过这个事情了,这个过程是类似的

When the estate_account module appears in the list, go ahead and install it!
当estate_account 模块出现在列表中,继续,安装它.
You’ll notice that the Invoicing application is installed as well. This is expected since your module depends on it. If you uninstall the Invoicing application, your module will be uninstalled as well.
你将会注意到,Invoicing应用也被安装了.这是符合期待的,因为你的模块依赖于它.如果你想要卸载Invoicing应用,你的omky也会被卸载

Invoice Creation

发票创建

It’s now time to generate the invoice. We want to add functionality to the estate.property model, i.e. we want to add some extra logic for when a property is sold. Does that sound familiar? If not, it’s a good idea to go back to the previous chapter since you might have missed something 😉
现在是时间去生成发票了.我们想要增加功能到estate.property模型中.例如,我们想要增加一些额外的逻辑到不动产销售的时候.听起来很类似吧?如果没有,回头到之前的章节是个好主意,因为你可能错过了一些东西.

As a first step, we need to extend the action called when pressing the ‘Sold’ button on a property. To do so, we need to create a model inheritance in the estate_account module for the estate.property model. For now, the overridden action will simply return the super call. Maybe an example will make things clearer:
在第一步,我们需要扩展当Sold按钮被按下去的时候的action调用

from odoo import models

class InheritedModel(models.Model):
    _inherit = "inherited.model"

    def inherited_action(self):
        return super().inherited_action()

A practical example can be found here.
一个练习用的例子可以在这里被发现

Exercise

练习

Add the first step of invoice creation.
开始发票创建的第一步

  • Create a estate_property.py file in the correct folder of the estate_account module.
  • 创建一个estate_property.py文件到estate_account的正确的文件夹下面
    _inherit the estate.property model.
    继承estate.property模型
  • Override the action_sold method (you might have named it differently) to return the super call.
  • 重写action_sold方法(你可能给他一个不同的方法名)

Tip: to make sure it works, add a print or a debugger breakpoint in the overridden method.
贴士: 为了确认它工作了,增加一个打印或者一个debugger断电到重写的方法里

Is it working? If not, maybe check that all Python files are correctly imported.
它工作了吗?如果没有,可能要检查所有python文件是否正常导入

If the override is working, we can move forward and create the invoice. Unfortunately, there is no easy way to know how to create any given object in Odoo. Most of the time, it is necessary to have a look at its model to find the required fields and provide appropriate values.
如果重写生效,我们可以更进一步创建发票.不幸的是,没有一个简单的方法去了解如何创建任何odoo中给定的对象.绝大多数时候,很有必要去到模型中看一下,寻必填字段,提供合适的值.

A good way to learn is to look at how other modules already do what you want to do. For example, one of the basic flows of Sales is the creation of an invoice from a sales order. This looks like a good starting point since it does exactly what we want to do. Take some time to read and understand the _create_invoices method. When you are done crying because this simple task looks awfully complex, we can move forward in the tutorial.
一个学习的好方法是看一下其他现已经做了你想做的事情的模块怎么做的.例如,销售的基础流程之一是从每一笔销售单创建一个发票.这看起来像一个突破点,因为它做了我们想要做的事情.画一些时间去阅读和理解_create_invoices方法.你被干哭了因为简单的任务都看起来异常复杂,我们可以移动到教程里面了.

To create an invoice, we need the following information:
创建一个发票,我们需要下面的信息

  • a partner_id: the customer
  • 一个patner_id: 客户
  • a move_type: it has several possible values
  • 凭证类型:它又几个可能取值
  • a journal_id: the accounting journal
  • journal_id: 记账账簿
    This is enough to create an empty invoice.
    这就足够创建一个空发票了

Exercise

练习

Add the second step of invoice creation.
增加创建发票的第二步
Create an empty account.move in the override of the action_sold method:
在重写的action_sold 方法中,创建一个空的account.move(记账凭证)

  • the partner_id is taken from the current estate.property
  • partner_id从当前estate.property中取
  • the move_type should correspond to a ‘Customer Invoice’
  • 凭证类型应该对应Customer Invoice(客户发票)
  • the journal_id must be a sale journal (when in doubt, have a look here)
  • jornal_id(账簿id)应该是个销售账簿(当疑惑时候,看一下这里)

Tips:

  • to create an object, use self.env[model_name].create(values), where values is a dict.
  • 创建一个对象, 使用self.env[model_name].create(values),values是一个字典
  • the create method doesn’t accept recordsets as field values.
  • 创建方法不接受记录及作为字段值
    When a property is set to ‘Sold’, you should now have a new customer invoice created in Invoicing / Customers / Invoices.

Obviously we don’t have any invoice lines so far. To create an invoice line, we need the following information:
显然,到目前为止我们还没有任何发票行.为了创建发票行,我方需要下面信息

  • name: a description of the line
  • name: 发票行的描述
  • quantity
  • 数量
  • price_unit
  • 价格单位

Moreover, an invoice line needs to be linked to an invoice. The easiest and most efficient way to link a line to an invoice is to include all lines at invoice creation. To do this, the invoice_line_ids field is included in the account.move creation, which is a One2many. One2many and Many2many use special ‘commands’ described in Common ORM methods. This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. Here is a simple example to include a One2many field line_ids at creation of a test.model:
而且,一个发票行需要被关联到发票.关联行到发票的最简单而且最有效的方法是在发票创建的时候涵盖所有行.为了做到这点,invoice_lineids字段就被包括在account.move创建中了, 它是一个One2Many的字段.One2many和Many2many使用特别的"命令"描述与通用ORM方法.格式是一组顺序执行的三元列表(指的是一组列表,每个元素都是一个长度为三的列表),每个三元元素都是一个执行在一组记录上的命令.这是一个简单的例子,在创建时候囊括了一个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()

Exercise

练习:
Add the third step of invoice creation.
增加发票创建的第三步
Add two invoice lines during the creation of the account.move. Each property sold will be invoiced following these conditions:
在创建account.move的时候,增加两条发票行.每一次不动产销售将会按照下面条件开票

  • 6% of the selling price
  • 6%的售价
  • an additional 100.00 from administrative fees
    
  • 额外的100.00的管理费
    Tip: Add the invoice_line_ids at creation following the example above. For each line, we need a name, quantity and price_unit.
    贴士: 遵循上面的例子增加invoice_line_ids,每一行里,我们需要一个名字,一个数量,一个价格单位

This chapter might be one of the most difficult that has been covered so far, but it is the closest to what Odoo development will be in practice. In the next chapter, we will introduce the templating mechanism used in Odoo.
这一张可能是到目前位置教程涵盖的章节中最难的之一,但是这也是最接近odoo开发的练习.在下一章,我们会介绍odoo使用的模板引擎

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值