Bringing field from class sale.order to account.invoice class

88 Views Asked by At

I need help with bringing the job_title_2 field data from class sale.order to account.invoice based on the record from job_title.

class SaleOrderLine(models.Model):
    _inherit = 'sale.order'

    job_title = fields.Char('Job Title')
    job_title_2 = fields.Char('Job Title 2')
class InvoiceLine(models.Model):
    _inherit = 'account.invoice'
    
    job_title = fields.Char('Job Title')
    job_title_2 = fields.Char('Job Title 2')

I tried using but not working

@api.depends('sale_order_id')
def get_order(self):
    if self.sale_order_id:
        self.job_title_2= self.sale_order_id.job_title_2
2

There are 2 best solutions below

0
Kenly On

The get_order method will not be triggered and the value of job_title_2 will not change. To use a computed method, you need to set the compute parameter on the job_title_2 field.

Example:

job_title_2 = fields.Char(compute="get_order")

The account.invoice model must have a sale_order_id field

If you want to bring fields from the sale order to the account invoice, you can override the sale order _prepare_invoice method and add your field values

Example:

@api.multi
def _prepare_invoice(self):
    res = super(SaleOrderLine, self)._prepare_invoice()
    res['job_title'] = self.job_title
    res['job_title_2'] = self.job_title_2
    return res
0
Adam Strauss On

First you have to create your desired field in account.invoice which I think you already have.

Second there is a method in sale.order which is responsible for creating an invoice and sending data from sales order to the invoice. Just override this method.

class SaleOrderLine(models.Model):
    _inherit = 'sale.order'

    job_title = fields.Char('Job Title')
    job_title_2 = fields.Char('Job Title 2')

    @api.multi
    def _prepare_invoice(self):
        res = super(SaleOrderLine, self)._prepare_invoice()
        res.update({
            'job_title_2': self.job_title_2
            'job_title': self.job_title
        })
        return res