diff --git a/sale_loyalty_order_info/README.rst b/sale_loyalty_order_info/README.rst new file mode 100644 index 000000000..e840eeef4 --- /dev/null +++ b/sale_loyalty_order_info/README.rst @@ -0,0 +1,85 @@ +================== +Loyalty Order Info +================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8080e371d5afb278f95c163f92a7cf5f74c249c0fe7d8aff1052a6f86aef75e2 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/16.0/sale_loyalty_order_info + :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-16-0/sale-promotion-16-0-sale_loyalty_order_info + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module provides more info on the applied loyalty rewards on a sale order. +It provides: +* The total reward amount, computed tax included; +* A list of the applied promo codes; +* All generated coupons from this sale order; +* All programs applied to the sale order. + +The main goal is to use these info for reporting, so they are not added in views, as it would trigger additional computations. + +**Table of contents** + +.. contents:: + :local: + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Acsone + +Contributors +~~~~~~~~~~~~ + +* `Acsone `_ + + * Marie Lejeune + +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. + +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_loyalty_order_info/__init__.py b/sale_loyalty_order_info/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/sale_loyalty_order_info/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_loyalty_order_info/__manifest__.py b/sale_loyalty_order_info/__manifest__.py new file mode 100644 index 000000000..0c610f523 --- /dev/null +++ b/sale_loyalty_order_info/__manifest__.py @@ -0,0 +1,12 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Loyalty Order Info", + "summary": "Add info on sale order about applied loyalties", + "version": "16.0.1.0.0", + "category": "Sale", + "website": "https://github.com/OCA/sale-promotion", + "author": "Acsone, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["sale_loyalty"], +} diff --git a/sale_loyalty_order_info/models/__init__.py b/sale_loyalty_order_info/models/__init__.py new file mode 100644 index 000000000..6aacb7531 --- /dev/null +++ b/sale_loyalty_order_info/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order diff --git a/sale_loyalty_order_info/models/sale_order.py b/sale_loyalty_order_info/models/sale_order.py new file mode 100644 index 000000000..b9fb899a8 --- /dev/null +++ b/sale_loyalty_order_info/models/sale_order.py @@ -0,0 +1,57 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import api, fields, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + reward_amount_tax_incl = fields.Float(compute="_compute_reward_total_tax_incl") + promo_codes = fields.Char(compute="_compute_promo_codes") + generated_coupon_ids = fields.One2many( + "loyalty.card", + "order_id", + "Generated Coupons", + help="The coupons generated from this order.", + ) + program_ids = fields.Many2many( + "loyalty.program", string="Applied programs", compute="_compute_programs" + ) + + def _get_reward_lines(self): + self.ensure_one() + return self.order_line.filtered("is_reward_line") + + @api.depends("order_line") + def _compute_reward_total_tax_incl(self): + for order in self: + reward_amount_tax_incl = 0 + for line in order._get_reward_lines(): + if line.reward_id.reward_type != "product": + reward_amount_tax_incl += line.price_subtotal + line.price_tax + else: + # Free product are 'regular' product lines with + # a price_subtotal and price_tax of 0 + reward_amount_tax_incl -= line.product_id.taxes_id.compute_all( + line.product_id.lst_price, + product=line.product_id, + quantity=line.product_uom_qty, + )["total_included"] + order.reward_amount_tax_incl = reward_amount_tax_incl + + @api.depends("order_line", "applied_coupon_ids", "code_enabled_rule_ids") + def _compute_promo_codes(self): + for order in self: + codes = order.applied_coupon_ids.mapped( + "code" + ) + order.code_enabled_rule_ids.mapped("code") + if codes: + order.promo_codes = str(codes) # stock the list of codes in a string + else: + order.promo_codes = "[]" + + @api.depends("order_line", "applied_coupon_ids", "code_enabled_rule_ids") + def _compute_programs(self): + self.program_ids = self.env["loyalty.program"] + for order in self: + order.program_ids = order.order_line.reward_id.program_id diff --git a/sale_loyalty_order_info/readme/CONTRIBUTORS.rst b/sale_loyalty_order_info/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..d5621d3b7 --- /dev/null +++ b/sale_loyalty_order_info/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Acsone `_ + + * Marie Lejeune diff --git a/sale_loyalty_order_info/readme/DESCRIPTION.rst b/sale_loyalty_order_info/readme/DESCRIPTION.rst new file mode 100644 index 000000000..a4290a83f --- /dev/null +++ b/sale_loyalty_order_info/readme/DESCRIPTION.rst @@ -0,0 +1,8 @@ +This module provides more info on the applied loyalty rewards on a sale order. +It provides: +* The total reward amount, computed tax included; +* A list of the applied promo codes; +* All generated coupons from this sale order; +* All programs applied to the sale order. + +The main goal is to use these info for reporting, so they are not added in views, as it would trigger additional computations. diff --git a/sale_loyalty_order_info/static/description/index.html b/sale_loyalty_order_info/static/description/index.html new file mode 100644 index 000000000..cfe04fb1c --- /dev/null +++ b/sale_loyalty_order_info/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +Loyalty Order Info + + + +
+

Loyalty Order Info

+ + +

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

+

This module provides more info on the applied loyalty rewards on a sale order. +It provides: +* The total reward amount, computed tax included; +* A list of the applied promo codes; +* All generated coupons from this sale order; +* All programs applied to the sale order.

+

The main goal is to use these info for reporting, so they are not added in views, as it would trigger additional computations.

+

Table of contents

+ +
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Acsone
  • +
+
+
+

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.

+

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_loyalty_order_info/tests/__init__.py b/sale_loyalty_order_info/tests/__init__.py new file mode 100644 index 000000000..6f699d0d8 --- /dev/null +++ b/sale_loyalty_order_info/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_order diff --git a/sale_loyalty_order_info/tests/test_sale_order.py b/sale_loyalty_order_info/tests/test_sale_order.py new file mode 100644 index 000000000..60eb8ab37 --- /dev/null +++ b/sale_loyalty_order_info/tests/test_sale_order.py @@ -0,0 +1,190 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon + + +class TestSaleOrder(TestSaleCouponCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Deactivate default promotion program + cls.immediate_promotion_program.active = False + + cls.order = cls.env["sale.order"].create({"partner_id": cls.steve.id}) + + def _create_discount_program(self, product): + return self.env["loyalty.program"].create( + { + "name": "50% on order if product bought", + "program_type": "promotion", + "trigger": "auto", + "applies_on": "current", + "company_id": self.env.company.id, + "rule_ids": [ + ( + 0, + 0, + { + "product_ids": [(4, product.id)], + "minimum_qty": 1, + }, + ) + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "discount": 50, + "required_points": 1, + }, + ), + ], + } + ) + + def _create_discount_code_program(self): + return self.env["loyalty.program"].create( + { + "name": "50% on order with code 'PROMOTION'", + "program_type": "promo_code", + "trigger": "with_code", + "applies_on": "current", + "company_id": self.env.company.id, + "rule_ids": [ + ( + 0, + 0, + { + "code": "PROMOTION", + }, + ) + ], + "reward_ids": [ + ( + 0, + 0, + { + "reward_type": "discount", + "discount": 50, + "required_points": 1, + }, + ), + ], + } + ) + + def _create_discount_next_order(self): + return self.env["loyalty.program"].create( + { + "name": "15% on next order if 50$ spent", + "program_type": "next_order_coupons", + "trigger": "auto", + "applies_on": "future", + "company_id": self.env.company.id, + "rule_ids": [ + ( + 0, + 0, + { + "minimum_amount": 50, + }, + ) + ], + "reward_ids": [ + ( + 0, + 0, + { + "discount": 15, + }, + ) + ], + } + ) + + def test_empty_order(self): + self.assertEqual(self.order.reward_amount_tax_incl, 0) + self.assertEqual(self.order.promo_codes, "[]") + self.assertFalse(self.order.generated_coupon_ids) + self.assertFalse(self.order.program_ids) + + def test_reward_amount_tax_incl(self): + program = self._create_discount_program(self.product_A) + self.order.order_line = [ + ( + 0, + 0, + { + "product_id": self.product_A.id, + }, + ) + ] + self.order._update_programs_and_rewards() + claimable_rewards = self.order._get_claimable_rewards() + coupon = next(iter(claimable_rewards)) + self.order._apply_program_reward(claimable_rewards[coupon], coupon) + self.assertAlmostEqual( + self.order.reward_amount, + -0.5 * self.product_A.lst_price, + 2, + "Untaxed reward amount should be 50% of untaxed unit price of product A", + ) + self.assertAlmostEqual( + self.order.reward_amount_tax_incl, + -0.5 + * self.product_A.taxes_id.compute_all(self.product_A.lst_price)[ + "total_included" + ], + 2, + "Tax included reward amount should be 50% of tax included unit price of product A", + ) + self.assertEqual(self.order.program_ids.ids, [program.id]) + + def test_promo_codes(self): + program = self._create_discount_code_program() + self.order.order_line = [ + ( + 0, + 0, + { + "product_id": self.product_A.id, + }, + ) + ] + # Apply the coupon to make it allowed in the rewards list + self.env["sale.loyalty.coupon.wizard"].create( + { + "order_id": self.order.id, + "coupon_code": "PROMOTION", + } + ).action_apply() + # Apply the coupon reward + self.env["sale.loyalty.reward.wizard"].create( + { + "order_id": self.order.id, + "selected_reward_id": program.reward_ids.id, + } + ).action_apply() + self.assertEqual(self.order.promo_codes, "['PROMOTION']") + self.assertEqual(self.order.program_ids.ids, [program.id]) + + def test_generated_programs(self): + program = self._create_discount_next_order() + self.assertGreater(self.product_A.lst_price, 50) + self.order.order_line = [ + ( + 0, + 0, + { + "product_id": self.product_A.id, + }, + ) + ] + self.order._update_programs_and_rewards() + self.assertTrue(self.order.generated_coupon_ids) + self.assertEqual(self.order.generated_coupon_ids.ids, program.coupon_ids.ids) diff --git a/setup/sale_loyalty_order_info/odoo/addons/sale_loyalty_order_info b/setup/sale_loyalty_order_info/odoo/addons/sale_loyalty_order_info new file mode 120000 index 000000000..202578336 --- /dev/null +++ b/setup/sale_loyalty_order_info/odoo/addons/sale_loyalty_order_info @@ -0,0 +1 @@ +../../../../sale_loyalty_order_info \ No newline at end of file diff --git a/setup/sale_loyalty_order_info/setup.py b/setup/sale_loyalty_order_info/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/sale_loyalty_order_info/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)