Chapter 14: Interact With Other Modules
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.
Concrete Example: Account Move
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.
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 ;-)
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:
A practical example can be found here.
Is it working? If not, maybe check that all Python files are correctly imported.
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.
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.
To create an invoice, we need the following information:
partner_id: the customer
move_type: it has several possible values
journal_id: the accounting journal
This is enough to create an empty invoice.
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
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’ which have been made human readable with the
Command namespace. This namespace represents a triplet command to execute on a set of records. The triplet was originally the only option to do these commands, but it is now standard to use the namespace instead. The format is to place them in a list which is executed sequentially. Here is a simple example to include a One2many field
line_ids at creation of a
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.