diff --git a/models/__init__.py b/models/__init__.py index d3f915a5eb89f0b1bdf5151da8a6f12ba4f6a1d0..6e64884a9e8d45458bd4ef2f92bed38c6510dace 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -2,3 +2,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) from . import sale_project +from . import sale_order diff --git a/models/sale_order.py b/models/sale_order.py new file mode 100644 index 0000000000000000000000000000000000000000..2a39c4063a0b102ea6d136a2e48f4025a5c6e20a --- /dev/null +++ b/models/sale_order.py @@ -0,0 +1,29 @@ +# Copyright 2021 Le Filament (https://le-filament.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from odoo import models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + # ------------------------------------------------------ + # Business method + # ------------------------------------------------------ + def clean_sale_order(self): + super(SaleOrder, self).clean_sale_order() + sol_line_section = self.order_line.filtered( + lambda s: s.display_type == 'line_section' and s.sale_project_id is not None + ) + for sol_section in sol_line_section: + next_sol = False + for sol in self.order_line.sorted(key=lambda r: (r.sequence, r.id)): + if sol.sequence > sol_section.sequence \ + or (sol.sequence == sol_section.sequence + and sol.id > sol_section.id): + next_sol = sol + break + if not next_sol or\ + next_sol.sale_project_id != sol_section.sale_project_id \ + or not next_sol.product_id: + sol_section.unlink()