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.

41 lines
1.6 KiB

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