diff --git a/membership_sale_prorate/README.rst b/membership_sale_prorate/README.rst new file mode 100644 index 00000000..dc9d9f74 --- /dev/null +++ b/membership_sale_prorate/README.rst @@ -0,0 +1,76 @@ +======================= +Membership Sale Prorate +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:481e8310c0b91adbd533f0662f28937fa890d6ca476e734e32059244e832f3fb + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fvertical--association-lightgray.png?logo=github + :target: https://github.com/OCA/vertical-association/tree/16.0/membership_sale_prorate + :alt: OCA/vertical-association +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/vertical-association-16-0/vertical-association-16-0-membership_sale_prorate + :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/vertical-association&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module calculates and sets the quantity for sale order lines having prorate membership products so that the sale order line price is calculated accordingly + +**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 +~~~~~~~ + +* Onestein + +Contributors +~~~~~~~~~~~~ + +* `Onestein `_ + +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/vertical-association `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/membership_sale_prorate/__init__.py b/membership_sale_prorate/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/membership_sale_prorate/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/membership_sale_prorate/__manifest__.py b/membership_sale_prorate/__manifest__.py new file mode 100644 index 00000000..2cafe63b --- /dev/null +++ b/membership_sale_prorate/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2024 Onestein () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Membership Sale Prorate", + "summary": "Calculates and sets the quantity for sale order lines having " + "prorate membership products so that the sale order line price " + "is calculated accordingly", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "category": "Association", + "author": "Onestein, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/vertical-association", + "depends": [ + "membership", + "membership_prorate", + "sale", + ], + "data": [], + "auto_install": True, +} diff --git a/membership_sale_prorate/models/__init__.py b/membership_sale_prorate/models/__init__.py new file mode 100644 index 00000000..8eb9d1d4 --- /dev/null +++ b/membership_sale_prorate/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order_line diff --git a/membership_sale_prorate/models/sale_order_line.py b/membership_sale_prorate/models/sale_order_line.py new file mode 100644 index 00000000..3513dd3c --- /dev/null +++ b/membership_sale_prorate/models/sale_order_line.py @@ -0,0 +1,59 @@ +# Copyright 2024 Onestein () +from datetime import timedelta + +from odoo import api, fields, models +from odoo.tests import Form + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + def _prepare_sale_line_prorate_vals(self, sale_line): + product = sale_line.product_id + date_order = ( + sale_line.order_id.date_order + and sale_line.order_id.date_order.date() + or fields.Date.today() + ) + date_from, date_to = self.env["account.move.line"]._get_membership_interval( + product, date_order + ) + if not date_from: + return {"quantity": 1.0} + if date_order < date_from: + date_order = date_from + if date_order > date_to: + date_order = date_to + theoretical_duration = date_to - date_from + timedelta(1) + real_duration = date_to - date_order + timedelta(1) + if theoretical_duration != real_duration: + return { + "quantity": round( + float(real_duration.days) / theoretical_duration.days, 2 + ), + } + + @api.model_create_multi + def create(self, vals_list): + sale_lines = super().create(vals_list) + for sale_line in sale_lines.filtered( + lambda r: r.product_id.membership and r.product_id.membership_prorate + ): + # Change quantity accordingly the prorate + sale_line_vals = self._prepare_sale_line_prorate_vals(sale_line) + if sale_line_vals: + quantity = sale_line_vals["quantity"] + order = sale_line.order_id + with Form(order) as sale_form: + index = next( + ( + index + for (index, d) in enumerate(sale_form.order_line._records) + if d["id"] == sale_line.id + ), + None, + ) + if index is not None: + with sale_form.order_line.edit(index) as line_form: + line_form.product_uom_qty = quantity + return sale_lines diff --git a/membership_sale_prorate/readme/CONTRIBUTORS.rst b/membership_sale_prorate/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..85125abb --- /dev/null +++ b/membership_sale_prorate/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* `Onestein `_ diff --git a/membership_sale_prorate/readme/DESCRIPTION.rst b/membership_sale_prorate/readme/DESCRIPTION.rst new file mode 100644 index 00000000..f6b4c88f --- /dev/null +++ b/membership_sale_prorate/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module calculates and sets the quantity for sale order lines having prorate membership products so that the sale order line price is calculated accordingly diff --git a/membership_sale_prorate/static/description/index.html b/membership_sale_prorate/static/description/index.html new file mode 100644 index 00000000..1be12b7d --- /dev/null +++ b/membership_sale_prorate/static/description/index.html @@ -0,0 +1,423 @@ + + + + + +Membership Sale Prorate + + + +
+

Membership Sale Prorate

+ + +

Beta License: AGPL-3 OCA/vertical-association Translate me on Weblate Try me on Runboat

+

This module calculates and sets the quantity for sale order lines having prorate membership products so that the sale order line price is calculated accordingly

+

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

+
    +
  • Onestein
  • +
+
+ +
+

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/vertical-association project on GitHub.

+

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

+
+
+
+ + diff --git a/membership_sale_prorate/tests/__init__.py b/membership_sale_prorate/tests/__init__.py new file mode 100644 index 00000000..6ca1c1eb --- /dev/null +++ b/membership_sale_prorate/tests/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html +from . import test_membership_sale_prorate diff --git a/membership_sale_prorate/tests/test_membership_sale_prorate.py b/membership_sale_prorate/tests/test_membership_sale_prorate.py new file mode 100644 index 00000000..de825e31 --- /dev/null +++ b/membership_sale_prorate/tests/test_membership_sale_prorate.py @@ -0,0 +1,74 @@ +# Copyright 2024 Onestein () +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html +from datetime import date +from unittest.mock import patch + +from odoo import fields +from odoo.tests.common import TransactionCase + +from odoo.addons.membership_prorate.models.account_move_line import AccountMoveLine + + +class TestMembershipProrate(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.product = cls.env["product.product"].create( + { + "name": "Membership product with prorate", + "membership": True, + "membership_prorate": True, + "membership_date_from": "2017-01-01", + "membership_date_to": "2017-12-31", + } + ) + cls.partner = cls.env["res.partner"].create( + { + "name": "Test", + } + ) + + def create_membership_sale(self, date_order=False): + vals = { + "partner_id": self.partner.id, + "date_order": date_order or fields.Date.today(), + "order_line": [ + ( + 0, + None, + { + "product_id": self.product.id, + "product_uom_qty": 1, + }, + ) + ], + } + return self.env["sale.order"].create(vals) + + def test_create_sale_membership_product_wo_prorate(self): + self.product.membership_prorate = False + sale = self.create_membership_sale() + self.assertEqual(sale.order_line[0].product_uom_qty, 1.0) + + def test_create_sale_membership_product_prorate(self): + sale = self.create_membership_sale(date_order=date(2017, 7, 1)) + self.assertAlmostEqual(sale.order_line[0].product_uom_qty, 0.50, 2) + sale = self.create_membership_sale(date_order=date(2016, 7, 1)) + self.assertAlmostEqual(sale.order_line[0].product_uom_qty, 1.0, 2) + sale = self.create_membership_sale(date_order=date(2018, 7, 1)) + self.assertAlmostEqual(sale.order_line[0].product_uom_qty, 0, 2) + + def test_create_sale_membership_prorate_variable_period(self): + """It is a test for case where membership type is set to variable + on product with membership_prorate_variable_period not installed""" + + def _get_membership_interval(self, product, date): + return False, False + + with patch.object( + AccountMoveLine, + "_get_membership_interval", + _get_membership_interval, + ): + sale = self.create_membership_sale(date_order=date(2017, 7, 1)) + self.assertEqual(sale.order_line[0].product_uom_qty, 1.0) diff --git a/setup/membership_sale_prorate/odoo/addons/membership_sale_prorate b/setup/membership_sale_prorate/odoo/addons/membership_sale_prorate new file mode 120000 index 00000000..39f5f9eb --- /dev/null +++ b/setup/membership_sale_prorate/odoo/addons/membership_sale_prorate @@ -0,0 +1 @@ +../../../../membership_sale_prorate \ No newline at end of file diff --git a/setup/membership_sale_prorate/setup.py b/setup/membership_sale_prorate/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/membership_sale_prorate/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)