From 8187e01e90929103cc58ac411b4d000c805eef6d Mon Sep 17 00:00:00 2001 From: david Date: Wed, 28 Jul 2021 14:43:15 +0200 Subject: [PATCH 01/31] [MIG] sale_coupon_auto_refresh: Migration to 13.0 TT30943 --- sale_coupon_auto_refresh/README.rst | 106 +++++ sale_coupon_auto_refresh/__init__.py | 1 + sale_coupon_auto_refresh/__manifest__.py | 15 + sale_coupon_auto_refresh/i18n/es.po | 72 +++ .../i18n/sale_coupon_auto_refresh.pot | 63 +++ sale_coupon_auto_refresh/models/__init__.py | 3 + .../models/res_company.py | 11 + .../models/res_config_settings.py | 11 + sale_coupon_auto_refresh/models/sale_order.py | 30 ++ sale_coupon_auto_refresh/readme/CONFIGURE.rst | 4 + .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 1 + sale_coupon_auto_refresh/readme/ROADMAP.rst | 2 + sale_coupon_auto_refresh/readme/USAGE.rst | 5 + .../static/description/index.html | 449 ++++++++++++++++++ sale_coupon_auto_refresh/tests/__init__.py | 1 + .../tests/test_sale_coupon_auto_refresh.py | 70 +++ .../views/res_config_settings_views.xml | 33 ++ 18 files changed, 880 insertions(+) create mode 100644 sale_coupon_auto_refresh/README.rst create mode 100644 sale_coupon_auto_refresh/__init__.py create mode 100644 sale_coupon_auto_refresh/__manifest__.py create mode 100644 sale_coupon_auto_refresh/i18n/es.po create mode 100644 sale_coupon_auto_refresh/i18n/sale_coupon_auto_refresh.pot create mode 100644 sale_coupon_auto_refresh/models/__init__.py create mode 100644 sale_coupon_auto_refresh/models/res_company.py create mode 100644 sale_coupon_auto_refresh/models/res_config_settings.py create mode 100644 sale_coupon_auto_refresh/models/sale_order.py create mode 100644 sale_coupon_auto_refresh/readme/CONFIGURE.rst create mode 100644 sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst create mode 100644 sale_coupon_auto_refresh/readme/DESCRIPTION.rst create mode 100644 sale_coupon_auto_refresh/readme/ROADMAP.rst create mode 100644 sale_coupon_auto_refresh/readme/USAGE.rst create mode 100644 sale_coupon_auto_refresh/static/description/index.html create mode 100644 sale_coupon_auto_refresh/tests/__init__.py create mode 100644 sale_coupon_auto_refresh/tests/test_sale_coupon_auto_refresh.py create mode 100644 sale_coupon_auto_refresh/views/res_config_settings_views.xml diff --git a/sale_coupon_auto_refresh/README.rst b/sale_coupon_auto_refresh/README.rst new file mode 100644 index 00000000..d65f4909 --- /dev/null +++ b/sale_coupon_auto_refresh/README.rst @@ -0,0 +1,106 @@ +==================== +Auto Refresh Coupons +==================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--promotion-lightgray.png?logo=github + :target: https://github.com/OCA/sale-promotion/tree/13.0/sale_coupon_auto_refresh + :alt: OCA/sale-promotion +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-promotion-13-0/sale-promotion-13-0-sale_coupon_auto_refresh + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/296/13.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to auto apply the programs without the need of the user intervention. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +You can set this feature on or off in every company. To do so: + +#. Go to *Sales > Configuration > Settings* +#. In the *Pricing* section you'll find the option *Auto refresh promotions*. + +Usage +===== + +You need to configure a *Promotion program* with the option *Promo Code Usage* set to +*Automatically applied*. This can be done in *Sales > Products > Promotion programs*. + +Now, make a quotation that fulfill the promotion condition and the promotion will be +automatically applied. + +Known issues / Roadmap +====================== + +* This module should be ported to OCA in v13 as Odoo released enterprise modules and + they are now on the community side. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * David Vidal + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-chienandalu| image:: https://github.com/chienandalu.png?size=40px + :target: https://github.com/chienandalu + :alt: chienandalu + +Current `maintainer `__: + +|maintainer-chienandalu| + +This module is part of the `OCA/sale-promotion `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_coupon_auto_refresh/__init__.py b/sale_coupon_auto_refresh/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/sale_coupon_auto_refresh/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_coupon_auto_refresh/__manifest__.py b/sale_coupon_auto_refresh/__manifest__.py new file mode 100644 index 00000000..e4e20c5f --- /dev/null +++ b/sale_coupon_auto_refresh/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Auto Refresh Coupons", + "summary": "Allows to auto-apply the coupons with no user intervention", + "version": "13.0.1.0.0", + "development_status": "Production/Stable", + "category": "Website", + "website": "https://github.com/OCA/sale-promotion", + "author": "Tecnativa, Odoo Community Association (OCA)", + "maintainers": ["chienandalu"], + "license": "AGPL-3", + "depends": ["sale_coupon"], + "data": ["views/res_config_settings_views.xml"], +} diff --git a/sale_coupon_auto_refresh/i18n/es.po b/sale_coupon_auto_refresh/i18n/es.po new file mode 100644 index 00000000..27b25902 --- /dev/null +++ b/sale_coupon_auto_refresh/i18n/es.po @@ -0,0 +1,72 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_coupon_auto_refresh +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "" +"" +msgstr "" +"" + +#. module: sale_coupon_auto_refresh +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_res_company__auto_refresh_coupon +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_res_config_settings__auto_refresh_coupon +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_sale_order__auto_refresh_coupon +msgid "Auto Refresh Coupons" +msgstr "Auto actualizar ofertas" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "Auto refresh coupons" +msgstr "Auto actualizar ofertas" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "Auto-refresh coupons" +msgstr "Auto actualizar ofertas" + +#. module: sale_coupon_auto_refresh +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_res_company__auto_refresh_coupon +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_res_config_settings__auto_refresh_coupon +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_sale_order__auto_refresh_coupon +msgid "Autorefresh coupon lines in the backorder" +msgstr "Auto actualizar las líneas de descuento en backorder" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_res_company +msgid "Companies" +msgstr "Compañías" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_res_config_settings +msgid "Config Settings" +msgstr "Ajustes" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_sale_order +msgid "Sale Order" +msgstr "Pedido de Venta" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "" +"When the sale order is updated, auto-refresh the promotion programs which " +"are set to auto apply" +msgstr "" +"Cuando se actualice un pedido de venta, auto actualizar los programas de " +"oferta configurados para auto aplicarse" diff --git a/sale_coupon_auto_refresh/i18n/sale_coupon_auto_refresh.pot b/sale_coupon_auto_refresh/i18n/sale_coupon_auto_refresh.pot new file mode 100644 index 00000000..fa515ecb --- /dev/null +++ b/sale_coupon_auto_refresh/i18n/sale_coupon_auto_refresh.pot @@ -0,0 +1,63 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_coupon_auto_refresh +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_res_company__auto_refresh_coupon +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_res_config_settings__auto_refresh_coupon +#: model:ir.model.fields,field_description:sale_coupon_auto_refresh.field_sale_order__auto_refresh_coupon +msgid "Auto Refresh Coupons" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "Auto refresh coupons" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "Auto-refresh coupons" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_res_company__auto_refresh_coupon +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_res_config_settings__auto_refresh_coupon +#: model:ir.model.fields,help:sale_coupon_auto_refresh.field_sale_order__auto_refresh_coupon +msgid "Autorefresh coupon lines in the backorder" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_res_company +msgid "Companies" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_res_config_settings +msgid "Config Settings" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model:ir.model,name:sale_coupon_auto_refresh.model_sale_order +msgid "Sale Order" +msgstr "" + +#. module: sale_coupon_auto_refresh +#: model_terms:ir.ui.view,arch_db:sale_coupon_auto_refresh.res_config_settings_view_form +msgid "When the sale order is updated, auto-refresh the promotion programs which are set to auto apply" +msgstr "" diff --git a/sale_coupon_auto_refresh/models/__init__.py b/sale_coupon_auto_refresh/models/__init__.py new file mode 100644 index 00000000..eb28f621 --- /dev/null +++ b/sale_coupon_auto_refresh/models/__init__.py @@ -0,0 +1,3 @@ +from . import res_config_settings +from . import res_company +from . import sale_order diff --git a/sale_coupon_auto_refresh/models/res_company.py b/sale_coupon_auto_refresh/models/res_company.py new file mode 100644 index 00000000..4bfd5efb --- /dev/null +++ b/sale_coupon_auto_refresh/models/res_company.py @@ -0,0 +1,11 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class Company(models.Model): + _inherit = "res.company" + + auto_refresh_coupon = fields.Boolean( + string="Auto Refresh Coupons", help="Autorefresh coupon lines in the backorder", + ) diff --git a/sale_coupon_auto_refresh/models/res_config_settings.py b/sale_coupon_auto_refresh/models/res_config_settings.py new file mode 100644 index 00000000..e31d6349 --- /dev/null +++ b/sale_coupon_auto_refresh/models/res_config_settings.py @@ -0,0 +1,11 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + auto_refresh_coupon = fields.Boolean( + related="company_id.auto_refresh_coupon", readonly=False, + ) diff --git a/sale_coupon_auto_refresh/models/sale_order.py b/sale_coupon_auto_refresh/models/sale_order.py new file mode 100644 index 00000000..ffe1d2e9 --- /dev/null +++ b/sale_coupon_auto_refresh/models/sale_order.py @@ -0,0 +1,30 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, fields, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + # Used in UI to hide the manual button + auto_refresh_coupon = fields.Boolean(related="company_id.auto_refresh_coupon",) + + def _auto_refresh_coupons(self): + self.filtered( + lambda x: x.state in ("draft", "sent") and x.auto_refresh_coupon + ).recompute_coupon_lines() + + @api.model_create_multi + def create(self, vals_list): + """Create or refresh coupon lines on create""" + orders = super().create(vals_list) + orders.with_context(skip_auto_refresh_coupons=True)._auto_refresh_coupons() + return orders + + def write(self, vals): + """Create or refresh coupon lines after saving""" + res = super().write(vals) + if self.env.context.get("skip_auto_refresh_coupons"): + return res + self.with_context(skip_auto_refresh_coupons=True)._auto_refresh_coupons() + return res diff --git a/sale_coupon_auto_refresh/readme/CONFIGURE.rst b/sale_coupon_auto_refresh/readme/CONFIGURE.rst new file mode 100644 index 00000000..04585969 --- /dev/null +++ b/sale_coupon_auto_refresh/readme/CONFIGURE.rst @@ -0,0 +1,4 @@ +You can set this feature on or off in every company. To do so: + +#. Go to *Sales > Configuration > Settings* +#. In the *Pricing* section you'll find the option *Auto refresh promotions*. diff --git a/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst b/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..94b6ba95 --- /dev/null +++ b/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * David Vidal diff --git a/sale_coupon_auto_refresh/readme/DESCRIPTION.rst b/sale_coupon_auto_refresh/readme/DESCRIPTION.rst new file mode 100644 index 00000000..c89a3786 --- /dev/null +++ b/sale_coupon_auto_refresh/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows to auto apply the programs without the need of the user intervention. diff --git a/sale_coupon_auto_refresh/readme/ROADMAP.rst b/sale_coupon_auto_refresh/readme/ROADMAP.rst new file mode 100644 index 00000000..51340aae --- /dev/null +++ b/sale_coupon_auto_refresh/readme/ROADMAP.rst @@ -0,0 +1,2 @@ +* This module should be ported to OCA in v13 as Odoo released enterprise modules and + they are now on the community side. diff --git a/sale_coupon_auto_refresh/readme/USAGE.rst b/sale_coupon_auto_refresh/readme/USAGE.rst new file mode 100644 index 00000000..7dd8eb0d --- /dev/null +++ b/sale_coupon_auto_refresh/readme/USAGE.rst @@ -0,0 +1,5 @@ +You need to configure a *Promotion program* with the option *Promo Code Usage* set to +*Automatically applied*. This can be done in *Sales > Products > Promotion programs*. + +Now, make a quotation that fulfill the promotion condition and the promotion will be +automatically applied. diff --git a/sale_coupon_auto_refresh/static/description/index.html b/sale_coupon_auto_refresh/static/description/index.html new file mode 100644 index 00000000..0dfee264 --- /dev/null +++ b/sale_coupon_auto_refresh/static/description/index.html @@ -0,0 +1,449 @@ + + + + + + +Auto Refresh Coupons + + + +
+

Auto Refresh Coupons

+ + +

Beta License: AGPL-3 OCA/sale-promotion Translate me on Weblate Try me on Runbot

+

This module allows to auto apply the programs without the need of the user intervention.

+

Table of contents

+ +
+

Configuration

+

You can set this feature on or off in every company. To do so:

+
    +
  1. Go to Sales > Configuration > Settings
  2. +
  3. In the Pricing section you’ll find the option Auto refresh promotions.
  4. +
+
+
+

Usage

+

You need to configure a Promotion program with the option Promo Code Usage set to +Automatically applied. This can be done in Sales > Products > Promotion programs.

+

Now, make a quotation that fulfill the promotion condition and the promotion will be +automatically applied.

+
+
+

Known issues / Roadmap

+
    +
  • This module should be ported to OCA in v13 as Odoo released enterprise modules and +they are now on the community side.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

chienandalu

+

This module is part of the OCA/sale-promotion project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/sale_coupon_auto_refresh/tests/__init__.py b/sale_coupon_auto_refresh/tests/__init__.py new file mode 100644 index 00000000..52d9c0e9 --- /dev/null +++ b/sale_coupon_auto_refresh/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_coupon_auto_refresh diff --git a/sale_coupon_auto_refresh/tests/test_sale_coupon_auto_refresh.py b/sale_coupon_auto_refresh/tests/test_sale_coupon_auto_refresh.py new file mode 100644 index 00000000..d7d7631c --- /dev/null +++ b/sale_coupon_auto_refresh/tests/test_sale_coupon_auto_refresh.py @@ -0,0 +1,70 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.tests import Form, common + + +class TestWebsiteSaleCouponAutorefresh(common.SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.pricelist = cls.env["product.pricelist"].create( + { + "name": "Test pricelist", + "item_ids": [ + ( + 0, + 0, + { + "applied_on": "3_global", + "compute_price": "formula", + "base": "list_price", + }, + ) + ], + } + ) + cls.partner = cls.env["res.partner"].create( + {"name": "Mr. Odoo", "property_product_pricelist": cls.pricelist.id} + ) + cls.product = cls.env["product.product"].create({"name": "Test"}) + coupon_program_form = Form( + cls.env["sale.coupon.program"], + view="sale_coupon.sale_coupon_program_view_promo_program_form", + ) + coupon_program_form.name = "Test Discount Program" + coupon_program_form.promo_code_usage = "no_code_needed" + coupon_program_form.discount_type = "percentage" + coupon_program_form.discount_percentage = 50 + coupon_program_form.discount_apply_on = "on_order" + coupon_program_form.rule_minimum_amount = 100 + cls.coupon_program = coupon_program_form.save() + cls.coupon_program.company_id.auto_refresh_coupon = True + + def test_sale_coupon_auto_refresh_on_create(self): + sale_form = Form(self.env["sale.order"]) + sale_form.partner_id = self.partner + with sale_form.order_line.new() as line_form: + line_form.product_id = self.product + line_form.product_uom_qty = 1 + line_form.price_unit = 150 + sale = sale_form.save() + discount_line = sale.order_line.filtered("is_reward_line") + self.assertAlmostEqual(-75, discount_line.price_unit) + + def test_sale_coupon_auto_refresh_on_update(self): + sale_form = Form(self.env["sale.order"]) + sale_form.partner_id = self.partner + with sale_form.order_line.new() as line_form: + line_form.product_id = self.product + line_form.product_uom_qty = 1 + line_form.price_unit = 1 + sale = sale_form.save() + discount_line = sale.order_line.filtered("is_reward_line") + self.assertFalse(bool(discount_line)) + with sale_form.order_line.new() as line_form: + line_form.product_id = self.product + line_form.product_uom_qty = 200 + sale_form.save() + discount_line = sale.order_line.filtered("is_reward_line") + self.assertEqual(1, len(discount_line)) + self.assertAlmostEqual(-100.5, discount_line.price_unit) diff --git a/sale_coupon_auto_refresh/views/res_config_settings_views.xml b/sale_coupon_auto_refresh/views/res_config_settings_views.xml new file mode 100644 index 00000000..cca546e1 --- /dev/null +++ b/sale_coupon_auto_refresh/views/res_config_settings_views.xml @@ -0,0 +1,33 @@ + + + + res.config.settings + + + +
+
+ +
+
+
+
+
+
+
+
From 9ce15e691ce3120e5f5b5888599fe9173831f3ef Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Wed, 28 Jul 2021 18:03:53 +0200 Subject: [PATCH 02/31] [13.0][UPD] sale_coupon_auto_refresh: performance optimization, lesser refactoring for better inheritability --- sale_coupon_auto_refresh/README.rst | 16 ++++-- sale_coupon_auto_refresh/__manifest__.py | 5 +- sale_coupon_auto_refresh/models/sale_order.py | 57 +++++++++++++++---- .../readme/CONTRIBUTORS.rst | 4 ++ .../static/description/index.html | 13 +++-- .../views/res_config_settings_views.xml | 2 +- 6 files changed, 75 insertions(+), 22 deletions(-) diff --git a/sale_coupon_auto_refresh/README.rst b/sale_coupon_auto_refresh/README.rst index d65f4909..787cf1a3 100644 --- a/sale_coupon_auto_refresh/README.rst +++ b/sale_coupon_auto_refresh/README.rst @@ -7,9 +7,9 @@ Auto Refresh Coupons !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png +.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png :target: https://odoo-community.org/page/development-status - :alt: Beta + :alt: Production/Stable .. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 @@ -72,6 +72,7 @@ Authors ~~~~~~~ * Tecnativa +* Camptocamp Contributors ~~~~~~~~~~~~ @@ -80,6 +81,10 @@ Contributors * David Vidal +* `Camptocamp `_: + + * Silvio Gregorini + Maintainers ~~~~~~~~~~~ @@ -96,10 +101,13 @@ promote its widespread use. .. |maintainer-chienandalu| image:: https://github.com/chienandalu.png?size=40px :target: https://github.com/chienandalu :alt: chienandalu +.. |maintainer-SilvioC2C| image:: https://github.com/SilvioC2C.png?size=40px + :target: https://github.com/SilvioC2C + :alt: SilvioC2C -Current `maintainer `__: +Current `maintainers `__: -|maintainer-chienandalu| +|maintainer-chienandalu| |maintainer-SilvioC2C| This module is part of the `OCA/sale-promotion `_ project on GitHub. diff --git a/sale_coupon_auto_refresh/__manifest__.py b/sale_coupon_auto_refresh/__manifest__.py index e4e20c5f..542dd968 100644 --- a/sale_coupon_auto_refresh/__manifest__.py +++ b/sale_coupon_auto_refresh/__manifest__.py @@ -1,4 +1,5 @@ # Copyright 2021 Tecnativa - David Vidal +# Copyright 2021 Camptocamp - Silvio Gregorini # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { "name": "Auto Refresh Coupons", @@ -7,8 +8,8 @@ "development_status": "Production/Stable", "category": "Website", "website": "https://github.com/OCA/sale-promotion", - "author": "Tecnativa, Odoo Community Association (OCA)", - "maintainers": ["chienandalu"], + "author": "Tecnativa, Camptocamp, Odoo Community Association (OCA)", + "maintainers": ["chienandalu", "SilvioC2C"], "license": "AGPL-3", "depends": ["sale_coupon"], "data": ["views/res_config_settings_views.xml"], diff --git a/sale_coupon_auto_refresh/models/sale_order.py b/sale_coupon_auto_refresh/models/sale_order.py index ffe1d2e9..483ef93d 100644 --- a/sale_coupon_auto_refresh/models/sale_order.py +++ b/sale_coupon_auto_refresh/models/sale_order.py @@ -1,5 +1,7 @@ # Copyright 2021 Tecnativa - David Vidal +# Copyright 2021 Camptocamp - Silvio Gregorini # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + from odoo import api, fields, models @@ -9,22 +11,55 @@ class SaleOrder(models.Model): # Used in UI to hide the manual button auto_refresh_coupon = fields.Boolean(related="company_id.auto_refresh_coupon",) - def _auto_refresh_coupons(self): - self.filtered( - lambda x: x.state in ("draft", "sent") and x.auto_refresh_coupon - ).recompute_coupon_lines() - @api.model_create_multi def create(self, vals_list): - """Create or refresh coupon lines on create""" orders = super().create(vals_list) - orders.with_context(skip_auto_refresh_coupons=True)._auto_refresh_coupons() + orders._auto_refresh_coupons() return orders def write(self, vals): - """Create or refresh coupon lines after saving""" res = super().write(vals) - if self.env.context.get("skip_auto_refresh_coupons"): - return res - self.with_context(skip_auto_refresh_coupons=True)._auto_refresh_coupons() + self._auto_refresh_coupons() + return res + + def _auto_refresh_coupons(self): + if not self.env.context.get("skip_auto_refresh_coupons"): + orders = self.filtered(type(self)._allow_recompute_coupon_lines) + if orders: + orders = orders.with_context(skip_auto_refresh_coupons=True) + orders.recompute_coupon_lines() + + def _allow_recompute_coupon_lines(self): + """Returns whether reward lines in order ``self`` can be recomputed + automatically. + + Hook method, to be overridden for custom behaviours. + + :return: True if the current SO allows automatic recomputation for + reward lines, False otherwise. + """ + self.ensure_one() + return self.auto_refresh_coupon and self.state in ("draft", "sent") + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + @api.model_create_multi + def create(self, vals_list): + lines = super().create(vals_list) + lines.mapped("order_id")._auto_refresh_coupons() + return lines + + def write(self, vals): + orders = self.mapped("order_id") + res = super().write(vals) + orders |= self.mapped("order_id") + orders._auto_refresh_coupons() + return res + + def unlink(self): + orders = self.mapped("order_id") + res = super().unlink() + orders._auto_refresh_coupons() return res diff --git a/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst b/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst index 94b6ba95..6ffed338 100644 --- a/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst +++ b/sale_coupon_auto_refresh/readme/CONTRIBUTORS.rst @@ -1,3 +1,7 @@ * `Tecnativa `_: * David Vidal + +* `Camptocamp `_: + + * Silvio Gregorini diff --git a/sale_coupon_auto_refresh/static/description/index.html b/sale_coupon_auto_refresh/static/description/index.html index 0dfee264..e91fa622 100644 --- a/sale_coupon_auto_refresh/static/description/index.html +++ b/sale_coupon_auto_refresh/static/description/index.html @@ -3,7 +3,7 @@ - + Auto Refresh Coupons