From af16f0c5a7473d6f90692e17a39edb38ba771072 Mon Sep 17 00:00:00 2001 From: manu Date: Wed, 11 Oct 2023 11:55:13 +0200 Subject: [PATCH] [IMP]sale_quotation_number: Boolean to determine whether quotation sequence has been used --- .../migrations/17.0.1.1.1/post-migration.py | 20 +++++++++++++++ sale_quotation_number/models/sale_order.py | 25 +++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 sale_quotation_number/migrations/17.0.1.1.1/post-migration.py diff --git a/sale_quotation_number/migrations/17.0.1.1.1/post-migration.py b/sale_quotation_number/migrations/17.0.1.1.1/post-migration.py new file mode 100644 index 00000000000..e76647fc862 --- /dev/null +++ b/sale_quotation_number/migrations/17.0.1.1.1/post-migration.py @@ -0,0 +1,20 @@ +# Copyright 2023 Manuel Regidor (Sygel) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +import odoo + + +def migrate(cr, version): + env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) + sequence = env["ir.sequence"].search([("code", "=", "sale.quotation")], limit=1) + orders = ( + env["sale.order"] + .search( + [ + ("state", "in", ["draft", "sent"]), + ("company_id.keep_name_so", "=", False), + ] + ) + .filtered(lambda a: a.name[: len(sequence.prefix)] == sequence.prefix) + ) + orders.write({"quotation_seq_used": True}) diff --git a/sale_quotation_number/models/sale_order.py b/sale_quotation_number/models/sale_order.py index 1cc6fc24f4d..b49dc671259 100644 --- a/sale_quotation_number/models/sale_order.py +++ b/sale_quotation_number/models/sale_order.py @@ -4,20 +4,25 @@ # © 2020 Manuel Regidor # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) -from odoo import api, models +from odoo import api, fields, models class SaleOrder(models.Model): _inherit = "sale.order" + quotation_seq_used = fields.Boolean( + string="Quotation Sequence Used", default=False, copy=False, readonly=True + ) + @api.model_create_multi def create(self, vals_list): for vals in vals_list: - if not vals.get("name"): - if self.is_using_quotation_number(vals): + if self.is_using_quotation_number(vals): + if not vals.get("name"): company_id = vals.get("company_id", self.env.company.id) sequence = self.with_company(company_id).get_quotation_seq() vals["name"] = sequence or "/" + vals["quotation_seq_used"] = True return super().create(vals_list) @api.model @@ -43,12 +48,16 @@ def copy(self, default=None): def get_quotation_seq(self): return self.env["ir.sequence"].next_by_code("sale.quotation") + def get_sale_order_seq(self): + self.ensure_one() + return self.env["ir.sequence"].next_by_code("sale.order") + def action_confirm(self): sequence = self.env["ir.sequence"].search( [("code", "=", "sale.quotation")], limit=1 ) for order in self: - if sequence and self.name[: len(sequence.prefix)] != sequence.prefix: + if not self.quotation_seq_used: continue if order.state not in ("draft", "sent") or order.company_id.keep_name_so: continue @@ -56,10 +65,6 @@ def action_confirm(self): quo = order.origin + ", " + order.name else: quo = order.name - sequence = ( - self.with_company(order.company_id.id) - .env["ir.sequence"] - .next_by_code("sale.order") - ) - order.write({"origin": quo, "name": sequence}) + sequence = order.with_company(order.company_id.id).get_sale_order_seq() + order.write({"origin": quo, "name": sequence, "quotation_seq_used": False}) return super().action_confirm()