Skip to Content
Menu
This question has been flagged
4 Replies
6870 Views

I created two custom fields in 'sale.order.line' which work just fine:

class rlmSalesOrder(models.Model):
_inherit = 'sale.order.line'
fromeachof = fields.Integer(string='From Each Of',store=True,default="1")
total_ordered = fields.Integer(string='Total Ordered',compute='_compute_total_ordered')


I also created two custom fields in 'sale.order' which also work just fine:

class rlmSaleOrder(models.Model):
    _inherit = 'sale.order'
    
    project_name = fields.Char(string='Project Name',store=True)
    due_date = fields.Date(string='Due Date',store=True)


Now when the order is converted to an invoice, and I want to show the value of "fromeachof", I created the field under "account.invoice.line" and related it to "sale_line_ids.fromeachof" which also works fine:

class rlmInvoice(models.Model):
_inherit = 'account.invoice.line'
fromeachof = fields.Integer(string='From Each Of',store=True, related="sale_line_ids.fromeachof") 

However, I can't create "project_name" or "due_date" under "account.invoice" to show on the invoice:

class rlmAccountInvoice(models.Model):
    _inherit = "account.invoice"
    
    project_name = fields.Char(string='Project Name',store=True,related="sale_order_id.project_name")
    due_date = fields.Date(string='Due Date',store=True,related="sale_order_id.project_name")


No matter what I put in the related field, I get a Key Error (I've tried every combination I could think of):

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py", line 209, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py", line 197, in execute
    application_iter = app(environ, start_response)
  File "/opt/odoo/odoo/service/server.py", line 250, in app
    return self.app(e, s)
  File "/opt/odoo/odoo/service/wsgi_server.py", line 166, in application
    return application_unproxied(environ, start_response)
  File "/opt/odoo/odoo/service/wsgi_server.py", line 154, in application_unproxied
    result = handler(environ, start_response)
  File "/opt/odoo/odoo/http.py", line 1318, in __call__
    return self.dispatch(environ, start_response)
  File "/opt/odoo/odoo/http.py", line 1292, in __call__
    return self.app(environ, start_wrapped)
  File "/usr/local/lib/python3.5/dist-packages/werkzeug/wsgi.py", line 600, in __call__
    return self.app(environ, start_response)
  File "/opt/odoo/odoo/http.py", line 1473, in dispatch
    odoo.registry(db).check_signaling()
  File "/opt/odoo/odoo/__init__.py", line 76, in registry
    return modules.registry.Registry(database_name)
  File "/opt/odoo/odoo/modules/registry.py", line 61, in __new__
    return cls.new(db_name)
  File "/opt/odoo/odoo/modules/registry.py", line 85, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/opt/odoo/odoo/modules/loading.py", line 383, in load_modules
    registry.setup_models(cr)
  File "/opt/odoo/odoo/modules/registry.py", line 277, in setup_models
    model._setup_fields()
  File "/opt/odoo/odoo/models.py", line 2425, in _setup_fields
    field.setup_full(self)
  File "/opt/odoo/odoo/fields.py", line 471, in setup_full
    self._setup_related_full(model)
  File "/opt/odoo/odoo/fields.py", line 508, in _setup_related_full
    field = target._fields[name]
KeyError: 'sale_order_id'


I can't figure out how to link the fields.  What's the right way to do this?


Avatar
Discard
Best Answer

Hello @Russ,

in account.invoice direct sale_order_id field not available. Here is only sale order name('origin') available. So, You can solve this problem by following field. after that you can make any field related with custom so_reference field.

@api.model
def get_sale_order_reference(self):
for rec in self:
res = rec.env['sale.order'].search([('name', '=', rec.origin)])
rec.so_reference = res

so_reference = fields.Many2one('sale.order', compute='get_sale_order_reference')

I hope it will work for you.

Avatar
Discard
Best Answer

Hello Mitul,

What's the purpose of the first override method _create_invoice()?  It seems like the sale order custom field already flows through the invoice header with just the overriding of _prepare_invoice().

Thanks,

Joseph

Avatar
Discard
Best Answer

Hello Russ Schneider,

By default account.invoice does't have sale_order_id field.

Invoice line have sale_order_line relation

from sale_order_line you can get sale_order_id (sale_order_line.order_id)

Avatar
Discard
Best Answer

Hello,

 you can override the method of sale order and sale_advance_payment_inv object. when you create new invoice at that time you need to pass the value of your custom field.

You need to override this method.


class SaleAdvancePaymentInv(models.TransientModel):
   _inherit = "sale.advance.payment.inv"
@api.multi def _create_invoice(self, order, so_line, amount):
invoice = super(SaleAdvancePaymentInv, self)._create_invoice(order, so_line, amount):
      invoice.write({'project_name': order.project_name})
return invoice

and second method of sale order class

class sale_order(models.Model)
   _inherit = 'sale.order'
   
@api.multi
    def _prepare_invoice(self):
      res = super(sale_order, self)._prepare_invoice()
      res.update({'project_name': self.project_name})
      return res
Avatar
Discard
Related Posts Replies Views Activity
7
Oct 19
9237
1
Jun 19
3463
2
Jul 24
942
1
Jun 24
3564
0
Feb 24
218