You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

  1. # Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models
  4. from lxml import etree
  5. class SaleOrder(models.Model):
  6. _inherit = 'sale.order'
  7. default_sale_discount = fields.Float(
  8. related="partner_id.commercial_partner_id.default_sale_discount",
  9. string="Default sales discount (%)",
  10. )
  11. @api.model
  12. def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
  13. submenu=False):
  14. """Inject the default in the context of the line this way for
  15. making it inheritable.
  16. """
  17. res = super(SaleOrder, self).fields_view_get(
  18. view_id=view_id, view_type=view_type, toolbar=toolbar,
  19. submenu=submenu
  20. )
  21. if view_type != 'form': # pragma: no cover
  22. return res
  23. eview = etree.fromstring(res['arch'])
  24. xml_order_line = eview.xpath("//field[@name='order_line']")
  25. xml_discount = eview.xpath("//field[@name='default_sale_discount']")
  26. if xml_order_line and xml_discount:
  27. # This should be handled in "string" mode, as the context can
  28. # contain a expression that can only be evaled on execution time
  29. # on the JS web client
  30. node_val = xml_order_line[0].get('context', '{}').strip()[1:-1]
  31. elems = node_val.split(',') if node_val else []
  32. to_add = ["'default_discount': default_sale_discount"]
  33. xml_order_line[0].set('context', '{' + ', '.join(elems + to_add) + '}')
  34. res['arch'] = etree.tostring(eview)
  35. return res