1
votes

I am working with odoo 10 and have created a few fields, and have the following problem:

I created my own field (x_order_bestelldatum) in the module sale.order. When I confirm an offer, I enter the effective order date in this field. Now, when I create an invoice, I want to read the x_order_bestelldatum field of the module sale.order in module account.invoice so that I can print the order date on the invoice.

How can I read out the field x_order_bestelldatum from model sale.order in model account.invoice?

Here is my code in file models.py, but it does not work:

from odoo import models, fields, api

class felder_saleorder(models.Model):

    _inherit = 'sale.order'
    x_order_bestelldatum = fields.Date(string="Bestelldatum")


class felder_invoice(models.Model):

    _inherit = 'account.invoice'
    sale_order = fields.Many2one('sale.order','sale_order')
    x_bestelldatum = fields.Date(string="Bestelldatum",related='sale_order.x_order_bestelldatum')
    x_lieferdatum = fields.Date(string="Lieferdatum")

That does not work (the field x_bestelldatum remains empty):

sale_order = fields.Many2one('sale.order','sale_order')
x_bestelldatum = fields.Date(string="Bestelldatum",related='sale_order.x_order_bestelldatum')
2
Welcome to Stack Overflow! What doesn't work exactly? I suggest you read "How to ask?" article.Antoine Boisier-Michaud

2 Answers

1
votes

Well lets not do it your way plus I can't get it how you are doing it. So, I am suggesting some other possibilities which might help you.

First lets talk about the sale order field whom you named as 'sale_order'. There are two possible options with which you are entering data in the sale_order field. 1). Automatically through python 2). Manually by selecting from the list.

In case of entering data Manually you should use onchange function:

@api.onchange('sale_order')
def get_order_date(self):
    if self.sale_order:
        self.x_bestelldatum = self.sale_order.x_order_bestelldatum

In Case if you are automatically entering data through python then you can use monetary or create function to fill the field.

Create function:

@api.model
def create(self, vals):
    new_record = super(felder_invoice, self).create(vals)
    if new_record.sale_order:
        new_record.x_bestelldatum = new_record.sale_order.x_order_bestelldatum

    return new_record

Compute Funtion:

First you need to amend your field deceleration in py file like that

x_bestelldatum = fields.Date(string="Bestelldatum",compute="get_order_date")

Then you need to create a compute function for that:

@api.one
@api.depends('sale_order')
def get_order_date(self):
if self.sale_order:
    self.x_bestelldatum = self.sale_order.x_order_bestelldatum

You can get the order date by using above mentioned functions and willbe able to get its value in reports/prints. Hope it helps. Cheers!

0
votes

Thank you for your answer.

I have tried a few variants, but it still does not work. The field 'x_bestelldatum' in the module 'account.invoice' will always be empty.

This is what my code looks like:

class felder_invoice(models.Model):

    _inherit = 'account.invoice'
    x_lieferdatum = fields.Date(string="Lieferdatum")


    sale_order = fields.Many2one('sale.order','sale_order')
    x_bestelldatum = fields.Date(string="Bestelldatum",compute="get_order_date")

    @api.one
    @api.depends('sale_order')
    def get_order_date(self):
    if self.sale_order:
        self.x_bestelldatum = self.sale_order.x_order_bestelldatum

Is that right? Are all fields from 'sale.order' transferred to the new locale field 'sale_order'?

sale_order = fields.Many2one('sale.order','sale_order')