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.

38 lines
1.5 KiB

  1. # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
  2. # Copyright 2014-2019 Tecnativa - Pedro M. Baeza
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from odoo import api, fields, models
  5. class ProductSupplierInfo(models.Model):
  6. _inherit = "product.supplierinfo"
  7. discount = fields.Float(string="Discount (%)", digits="Discount")
  8. @api.onchange("name")
  9. def onchange_name(self):
  10. """Apply the default supplier discount of the selected supplier"""
  11. for supplierinfo in self.filtered("name"):
  12. supplierinfo.discount = supplierinfo.name.default_supplierinfo_discount
  13. @api.model
  14. def _get_po_to_supplierinfo_synced_fields(self):
  15. """Overwrite this method for adding other fields to be synchronized
  16. with product.supplierinfo.
  17. """
  18. return ["discount"]
  19. @api.model_create_multi
  20. def create(self, vals_list):
  21. """Insert discount (or others) from context from purchase.order's
  22. _add_supplier_to_product method"""
  23. for vals in vals_list:
  24. product_tmpl_id = vals.get("product_tmpl_id")
  25. po_line_map = self.env.context.get("po_line_map", {})
  26. if product_tmpl_id in po_line_map:
  27. po_line = po_line_map[product_tmpl_id]
  28. for field in self._get_po_to_supplierinfo_synced_fields():
  29. if not vals.get(field):
  30. vals[field] = po_line[field]
  31. return super().create(vals_list)