From 5b0ed1c20bf4fcf22ce53bc5b80e9d8732807bcb Mon Sep 17 00:00:00 2001 From: alvaro-domatix Date: Wed, 27 May 2026 18:41:27 +0000 Subject: [PATCH] [ADD] subscription_oca_sms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New bridge module between `subscription_oca` and the community `sms` addon. Ships two ready-to-use SMS templates (payment reminder, payment failure) and two corresponding actions on the subscription form so a salesman can fire an SMS notification to the customer in one click. * Two `sms.template` records loaded as data, both targeting `sale.subscription` and rendered with the partner's name and the subscription's next invoice date. * `_send_sms_template(xmlid)` resolves the template, refuses to send if the partner has no phone number, then drives an `sms.composer` in comment mode via its public `action_send_sms()`. * Form view: two header buttons reusing `_send_sms_template` under the hood. Lives in its own addon directory because `sms` is LGPL-3 and not acceptable in the core module dependencies (§0.3 of the project guidelines). --- requirements.txt | 2 + subscription_oca_sms/README.rst | 105 ++++ subscription_oca_sms/__init__.py | 1 + subscription_oca_sms/__manifest__.py | 19 + .../data/sms_template_data.xml | 18 + subscription_oca_sms/models/__init__.py | 1 + .../models/sale_subscription.py | 66 +++ subscription_oca_sms/pyproject.toml | 3 + subscription_oca_sms/readme/CONTRIBUTORS.md | 2 + subscription_oca_sms/readme/DESCRIPTION.md | 4 + subscription_oca_sms/readme/USAGE.md | 16 + .../static/description/index.html | 454 ++++++++++++++++++ subscription_oca_sms/tests/__init__.py | 1 + .../tests/test_subscription_sms.py | 140 ++++++ .../views/sale_subscription_views.xml | 29 ++ 15 files changed, 861 insertions(+) create mode 100644 requirements.txt create mode 100644 subscription_oca_sms/README.rst create mode 100644 subscription_oca_sms/__init__.py create mode 100644 subscription_oca_sms/__manifest__.py create mode 100644 subscription_oca_sms/data/sms_template_data.xml create mode 100644 subscription_oca_sms/models/__init__.py create mode 100644 subscription_oca_sms/models/sale_subscription.py create mode 100644 subscription_oca_sms/pyproject.toml create mode 100644 subscription_oca_sms/readme/CONTRIBUTORS.md create mode 100644 subscription_oca_sms/readme/DESCRIPTION.md create mode 100644 subscription_oca_sms/readme/USAGE.md create mode 100644 subscription_oca_sms/static/description/index.html create mode 100644 subscription_oca_sms/tests/__init__.py create mode 100644 subscription_oca_sms/tests/test_subscription_sms.py create mode 100644 subscription_oca_sms/views/sale_subscription_views.xml diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000..7dcadefb7d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +# generated from manifests external_dependencies +phonenumbers diff --git a/subscription_oca_sms/README.rst b/subscription_oca_sms/README.rst new file mode 100644 index 0000000000..18dc561479 --- /dev/null +++ b/subscription_oca_sms/README.rst @@ -0,0 +1,105 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +==================== +Subscription OCA SMS +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:4d098a9398ec3318bfe33e897e1021985f354663730d1b215d75d95aa2a26cf8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-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%2Fcontract-lightgray.png?logo=github + :target: https://github.com/OCA/contract/tree/19.0/subscription_oca_sms + :alt: OCA/contract +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/contract-19-0/contract-19-0-subscription_oca_sms + :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/contract&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Bridge module between ``subscription_oca`` and the community ``sms`` +addon. Ships two ready-to-use SMS templates (payment reminder, payment +failure) and two corresponding actions on the subscription form so a +salesman can send SMS notifications to the customer in one click. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +On a subscription form, salespeople (group *Sales / User*) get two +buttons next to *Close subscription*: + +- **SMS payment reminder** — sends the customer an SMS reminding them + of the next invoice date. +- **SMS payment failure** — notifies the customer that a payment could + not be processed. + +Both are **manual, one-click** actions: the salesperson decides when to +send. The buttons only appear when the customer has a valid phone +number. The SMS body is automatically translated to the customer's +language if a translation of the template exists. + +Sending SMS relies on Odoo's SMS gateway (IAP), which has a per-message +cost. Make sure the gateway is configured and has credit before using +these actions. + +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 +------- + +* Domatix + +Contributors +------------ + +- Domatix: + + - Álvaro López + +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/contract `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/subscription_oca_sms/__init__.py b/subscription_oca_sms/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/subscription_oca_sms/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/subscription_oca_sms/__manifest__.py b/subscription_oca_sms/__manifest__.py new file mode 100644 index 0000000000..46a3fbe875 --- /dev/null +++ b/subscription_oca_sms/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2026 Domatix - Alvaro +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Subscription OCA SMS", + "summary": "Send SMS notifications from subscriptions.", + "version": "19.0.1.0.0", + "development_status": "Beta", + "category": "Subscription Management", + "website": "https://github.com/OCA/contract", + "license": "AGPL-3", + "author": "Domatix, Odoo Community Association (OCA)", + "depends": ["subscription_oca", "sms", "phone_validation"], + "external_dependencies": {"python": ["phonenumbers"]}, + "data": [ + "data/sms_template_data.xml", + "views/sale_subscription_views.xml", + ], + "installable": True, +} diff --git a/subscription_oca_sms/data/sms_template_data.xml b/subscription_oca_sms/data/sms_template_data.xml new file mode 100644 index 0000000000..f3784edb02 --- /dev/null +++ b/subscription_oca_sms/data/sms_template_data.xml @@ -0,0 +1,18 @@ + + + + Subscription: Payment reminder + + Hi {{ object.partner_id.name }}, your subscription {{ object.name }} renews on {{ format_date(object.recurring_next_date) }}. + + + + Subscription: Payment failure + + Hi {{ object.partner_id.name }}, the payment for {{ object.name }} failed. Please update your payment method. + + diff --git a/subscription_oca_sms/models/__init__.py b/subscription_oca_sms/models/__init__.py new file mode 100644 index 0000000000..9119ef94dd --- /dev/null +++ b/subscription_oca_sms/models/__init__.py @@ -0,0 +1 @@ +from . import sale_subscription diff --git a/subscription_oca_sms/models/sale_subscription.py b/subscription_oca_sms/models/sale_subscription.py new file mode 100644 index 0000000000..e60d1946de --- /dev/null +++ b/subscription_oca_sms/models/sale_subscription.py @@ -0,0 +1,66 @@ +# Copyright 2026 Domatix - Alvaro +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class SaleSubscription(models.Model): + _inherit = "sale.subscription" + + can_send_sms = fields.Boolean( + string="Can send SMS", + compute="_compute_can_send_sms", + help="Technical field: true when the customer has a valid phone number.", + ) + + @api.depends("partner_id.phone") + def _compute_can_send_sms(self): + for subscription in self: + partner = subscription.partner_id + subscription.can_send_sms = bool( + partner and partner._phone_format(fname="phone") + ) + + def _send_sms_template(self, template_xmlid): + self.ensure_one() + template = self.env.ref(template_xmlid, raise_if_not_found=False) + if not template: + raise UserError( + self.env._( + "SMS template %(xmlid)s is not available.", + xmlid=template_xmlid, + ) + ) + # Validate against the *sanitized* number, mirroring what the SMS + # composer does internally (it resolves and formats partner_id.phone). + # A non-empty but invalid number would otherwise pass a naive check + # and fail later inside the composer with a less helpful message. + if not self.partner_id._phone_format(fname="phone"): + raise UserError( + self.env._( + "Cannot send SMS: %(partner)s has no valid phone number.", + partner=self.partner_id.display_name, + ) + ) + composer = ( + self.env["sms.composer"] + .with_context(active_id=self.id, active_model="sale.subscription") + .create( + { + "composition_mode": "comment", + "res_model": "sale.subscription", + "template_id": template.id, + } + ) + ) + composer.action_send_sms() + + def action_send_sms_payment_reminder(self): + self.ensure_one() + self._send_sms_template("subscription_oca_sms.sms_template_payment_reminder") + self.message_post(body=self.env._("Payment reminder SMS sent.")) + + def action_send_sms_payment_failure(self): + self.ensure_one() + self._send_sms_template("subscription_oca_sms.sms_template_payment_failure") + self.message_post(body=self.env._("Payment failure SMS sent.")) diff --git a/subscription_oca_sms/pyproject.toml b/subscription_oca_sms/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/subscription_oca_sms/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/subscription_oca_sms/readme/CONTRIBUTORS.md b/subscription_oca_sms/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..1f64d19814 --- /dev/null +++ b/subscription_oca_sms/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Domatix: + - Álvaro López \<\> diff --git a/subscription_oca_sms/readme/DESCRIPTION.md b/subscription_oca_sms/readme/DESCRIPTION.md new file mode 100644 index 0000000000..4ee0c9d870 --- /dev/null +++ b/subscription_oca_sms/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +Bridge module between `subscription_oca` and the community `sms` +addon. Ships two ready-to-use SMS templates (payment reminder, payment +failure) and two corresponding actions on the subscription form so a +salesman can send SMS notifications to the customer in one click. diff --git a/subscription_oca_sms/readme/USAGE.md b/subscription_oca_sms/readme/USAGE.md new file mode 100644 index 0000000000..6a1db71739 --- /dev/null +++ b/subscription_oca_sms/readme/USAGE.md @@ -0,0 +1,16 @@ +On a subscription form, salespeople (group *Sales / User*) get two +buttons next to *Close subscription*: + +- **SMS payment reminder** — sends the customer an SMS reminding them of + the next invoice date. +- **SMS payment failure** — notifies the customer that a payment could + not be processed. + +Both are **manual, one-click** actions: the salesperson decides when to +send. The buttons only appear when the customer has a valid phone +number. The SMS body is automatically translated to the customer's +language if a translation of the template exists. + +Sending SMS relies on Odoo's SMS gateway (IAP), which has a per-message +cost. Make sure the gateway is configured and has credit before using +these actions. diff --git a/subscription_oca_sms/static/description/index.html b/subscription_oca_sms/static/description/index.html new file mode 100644 index 0000000000..4ca1eea44b --- /dev/null +++ b/subscription_oca_sms/static/description/index.html @@ -0,0 +1,454 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Subscription OCA SMS

+ +

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

+

Bridge module between subscription_oca and the community sms +addon. Ships two ready-to-use SMS templates (payment reminder, payment +failure) and two corresponding actions on the subscription form so a +salesman can send SMS notifications to the customer in one click.

+

Table of contents

+ +
+

Usage

+

On a subscription form, salespeople (group Sales / User) get two +buttons next to Close subscription:

+
    +
  • SMS payment reminder — sends the customer an SMS reminding them +of the next invoice date.
  • +
  • SMS payment failure — notifies the customer that a payment could +not be processed.
  • +
+

Both are manual, one-click actions: the salesperson decides when to +send. The buttons only appear when the customer has a valid phone +number. The SMS body is automatically translated to the customer’s +language if a translation of the template exists.

+

Sending SMS relies on Odoo’s SMS gateway (IAP), which has a per-message +cost. Make sure the gateway is configured and has credit before using +these actions.

+
+
+

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

+
    +
  • Domatix
  • +
+
+
+

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

+

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

+
+
+
+
+ + diff --git a/subscription_oca_sms/tests/__init__.py b/subscription_oca_sms/tests/__init__.py new file mode 100644 index 0000000000..9d8ac56bb7 --- /dev/null +++ b/subscription_oca_sms/tests/__init__.py @@ -0,0 +1 @@ +from . import test_subscription_sms diff --git a/subscription_oca_sms/tests/test_subscription_sms.py b/subscription_oca_sms/tests/test_subscription_sms.py new file mode 100644 index 0000000000..a60783e935 --- /dev/null +++ b/subscription_oca_sms/tests/test_subscription_sms.py @@ -0,0 +1,140 @@ +# Copyright 2026 Domatix - Alvaro +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from unittest.mock import patch + +from odoo.exceptions import UserError + +from odoo.addons.base.tests.common import BaseCommon + +SEND_SMS = "odoo.addons.sms.wizard.sms_composer.SmsComposer.action_send_sms" + + +class TestSubscriptionSMS(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.pricelist = cls.env["product.pricelist"].create({"name": "SMS PL"}) + cls.partner_with_phone = cls.env["res.partner"].create( + {"name": "SMS partner", "phone": "+34600000000"} + ) + cls.partner_no_phone = cls.env["res.partner"].create({"name": "Silent partner"}) + cls.template = cls.env["sale.subscription.template"].create( + { + "name": "SMS tmpl", + "code": "S", + "recurring_rule_type": "months", + } + ) + cls.stage = cls.env["sale.subscription.stage"].search( + [("type", "=", "in_progress")], limit=1 + ) + + def _new_subscription(self, partner=None): + return self.env["sale.subscription"].create( + { + "partner_id": (partner or self.partner_with_phone).id, + "template_id": self.template.id, + "pricelist_id": self.pricelist.id, + "stage_id": self.stage.id, + } + ) + + # --- data smoke --------------------------------------------------- + + def test_payment_reminder_template_exists(self): + template = self.env.ref( + "subscription_oca_sms.sms_template_payment_reminder", + raise_if_not_found=False, + ) + self.assertTrue(template) + self.assertEqual(template.model, "sale.subscription") + + def test_payment_failure_template_exists(self): + template = self.env.ref( + "subscription_oca_sms.sms_template_payment_failure", + raise_if_not_found=False, + ) + self.assertTrue(template) + self.assertEqual(template.model, "sale.subscription") + + # --- guard -------------------------------------------------------- + + def test_send_sms_raises_when_no_phone(self): + sub = self._new_subscription(partner=self.partner_no_phone) + self.assertFalse(sub.can_send_sms) + with self.assertRaises(UserError): + sub.action_send_sms_payment_reminder() + + def test_send_sms_raises_when_phone_invalid(self): + # A non-empty but unparseable number must be rejected by our guard, + # not slip through to a cryptic error inside the composer. + partner = self.env["res.partner"].create( + {"name": "Bad number", "phone": "not-a-number"} + ) + sub = self._new_subscription(partner=partner) + self.assertFalse(sub.can_send_sms) + with self.assertRaises(UserError): + sub.action_send_sms_payment_reminder() + + def test_can_send_sms_true_with_valid_phone(self): + sub = self._new_subscription() + self.assertTrue(sub.can_send_sms) + + # --- behaviour: who/what gets sent -------------------------------- + + def test_reminder_uses_correct_template_and_record(self): + # Mock only the external gateway call, but capture the composer it + # was called on, so we verify the *real* logic (which template, which + # record) instead of just the chatter note. + sub = self._new_subscription() + captured = {} + + def capture(composer): + captured["template_id"] = composer.template_id + captured["res_id"] = composer.res_id + captured["res_model"] = composer.res_model + return True + + with patch(SEND_SMS, autospec=True, side_effect=capture): + sub.action_send_sms_payment_reminder() + self.assertEqual(captured["res_model"], "sale.subscription") + self.assertEqual(captured["res_id"], sub.id) + self.assertEqual( + captured["template_id"], + self.env.ref("subscription_oca_sms.sms_template_payment_reminder"), + ) + + def test_failure_uses_correct_template_and_record(self): + sub = self._new_subscription() + captured = {} + + def capture(composer): + captured["template_id"] = composer.template_id + captured["res_id"] = composer.res_id + return True + + with patch(SEND_SMS, autospec=True, side_effect=capture): + sub.action_send_sms_payment_failure() + self.assertEqual(captured["res_id"], sub.id) + self.assertEqual( + captured["template_id"], + self.env.ref("subscription_oca_sms.sms_template_payment_failure"), + ) + + def test_reminder_body_renders_subscription_data(self): + # Guards against a silent break if partner_id/name placeholders drift. + sub = self._new_subscription() + template = self.env.ref("subscription_oca_sms.sms_template_payment_reminder") + body = template._render_field("body", sub.ids)[sub.id] + self.assertIn(sub.partner_id.name, body) + self.assertIn(sub.name, body) + + def test_send_payment_reminder_logs_chatter(self): + sub = self._new_subscription() + with patch(SEND_SMS, return_value=True): + sub.action_send_sms_payment_reminder() + self.assertTrue( + any("Payment reminder SMS sent" in (m.body or "") for m in sub.message_ids) + ) diff --git a/subscription_oca_sms/views/sale_subscription_views.xml b/subscription_oca_sms/views/sale_subscription_views.xml new file mode 100644 index 0000000000..e88e832c92 --- /dev/null +++ b/subscription_oca_sms/views/sale_subscription_views.xml @@ -0,0 +1,29 @@ + + + + sale.subscription.form.sms + sale.subscription + + + + +