diff --git a/sign_oca_sms/README.rst b/sign_oca_sms/README.rst new file mode 100644 index 00000000..65419290 --- /dev/null +++ b/sign_oca_sms/README.rst @@ -0,0 +1,187 @@ +============ +Sign Oca Sms +============ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:357363f01f3db6c4376b426136118e16318057d64555f49d654f1405abcfa6c1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fsign-lightgray.png?logo=github + :target: https://github.com/OCA/sign/tree/18.0/sign_oca_sms + :alt: OCA/sign +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sign-18-0/sign-18-0-sign_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/sign&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Sign OCA SMS Integration +------------------------ + +This module extends the functionality of Sign OCA by adding SMS +notification capabilities for document signing requests. It allows users +to send signature requests via SMS, in addition to the standard email +method, providing more flexibility in how signers are notified and can +access documents for signing. + +Key Features +~~~~~~~~~~~~ + +- Send signature requests via SMS, email, or both +- Configure preferred contact method for each signer +- Track SMS delivery status +- Customizable SMS templates + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Configuration Steps +~~~~~~~~~~~~~~~~~~~ + +1. **Configure SMS Template** + + - Navigate to **Settings > Technical > Phone > SMS > SMS > + Templates** + - Locate the template named "Sign OCA: Sign Request Notification" + - Customize the SMS message as needed using the available + placeholders + +Technical Notes +~~~~~~~~~~~~~~~ + +- The module adds a new selection field ``send_method`` with options: + + - Email (default) + - SMS + - Email & SMS + +- SMS messages include a secure link to access the document for signing + +Usage +===== + +Sending Signature Requests via SMS +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. **Initiating a Signature Request** + + - Navigate to **Sign > New Request** + - Select or upload the document to be signed + - Add signers as you normally would + - In the sending options, choose your preferred method: + + - **Email**: Send via email only (default) + - **SMS**: Send via SMS only + - **Email & SMS**: Send both email and SMS notifications + +2. **Adding Signer Phone Numbers** + + - When adding a signer, ensure their contact information includes a + valid phone number + - The system will use the mobile number if available, falling back to + the phone number + - You can also specify a custom phone number for each signer in the + signer details + +3. **Customizing the SMS Message** + + - The default SMS message includes: + + - Recipient's name + - Name of the person requesting the signature + - Secure link to access the document + - Message (if provided) + + - To customize the message: + + - Go to **Settings > Technical > Phone > SMS > Templates** + - Find and edit "Sign OCA: Sign Request Notification" + - Use available placeholders to personalize the message + +Tracking and Managing SMS Notifications +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- **Resending Notifications**: + + - Open the signature request + - Click on the "Resend" button + - Select the preferred sending method + +Best Practices +~~~~~~~~~~~~~~ + +1. **Consent and Privacy** + + - Ensure you have consent to contact signers via SMS + - Include an opt-out mechanism in your communications + +2. **Message Length** + + - Keep SMS messages concise to avoid multiple message charges + - The secure link is automatically shortened by the system + +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 +------- + +* Kencove +* ForgeFlow + +Contributors +------------ + +- ForgeFlow (https://www.forgeflow.com) + + - BernatPForgeFlow + - Maria de Luna maria.de.luna@forgeflow.com + +- `Trobz `__: + + - Do Anh Duy + +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/sign `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sign_oca_sms/__init__.py b/sign_oca_sms/__init__.py new file mode 100644 index 00000000..aee8895e --- /dev/null +++ b/sign_oca_sms/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/sign_oca_sms/__manifest__.py b/sign_oca_sms/__manifest__.py new file mode 100644 index 00000000..be94545d --- /dev/null +++ b/sign_oca_sms/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2023 ForgeFlow S.L. (http://www.forgeflow.com) +# Copyright 2025 Kencove (https://www.kencove.com). +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "Sign Oca Sms", + "version": "18.0.1.0.0", + "category": "Sales/Sign", + "summary": "Kencove Sign Customizations", + "author": "Kencove, ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/sign", + "license": "AGPL-3", + "depends": [ + "sms", + "sign_oca", + "html_text", + ], + "data": [ + "wizards/sign_oca_generate.xml", + "security/ir.model.access.csv", + "data/sms_template.xml", + ], + "installable": True, +} diff --git a/sign_oca_sms/data/sms_template.xml b/sign_oca_sms/data/sms_template.xml new file mode 100644 index 00000000..432062d3 --- /dev/null +++ b/sign_oca_sms/data/sms_template.xml @@ -0,0 +1,14 @@ + + + + + Sign OCA: Sign Request Notification + Hello {{ object.display_name }}, {{ ctx['requested_by_user'] }} has requested your signature on the following documents: {{ ctx['link'] }}, {{ ctx['message'] }} + {{ object.lang }} + + + diff --git a/sign_oca_sms/models/__init__.py b/sign_oca_sms/models/__init__.py new file mode 100644 index 00000000..e1b740ce --- /dev/null +++ b/sign_oca_sms/models/__init__.py @@ -0,0 +1 @@ +from . import sign_oca_request diff --git a/sign_oca_sms/models/sign_oca_request.py b/sign_oca_sms/models/sign_oca_request.py new file mode 100644 index 00000000..8377ab27 --- /dev/null +++ b/sign_oca_sms/models/sign_oca_request.py @@ -0,0 +1,87 @@ +# Copyright 2023 ForgeFlow S.L. (http://www.forgeflow.com) +# Copyright 2025 Kencove (https://www.kencove.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from odoo import fields, models +from odoo.exceptions import ValidationError + + +class SignOcaRequest(models.Model): + _inherit = "sign.oca.request" + + def get_sms_message(self, signer, requested_by_user, message, link): + message = self.env._( + "Hello %(display_name)s, %(requested_by_user)s has requested " + "your signature on the following documents: %(link)s . %(message)s", + display_name=signer.display_name, + requested_by_user=requested_by_user, + link=link, + message=message, + ) + return message + + def _send_sign_sms( + self, message, link, signer_partner, requested_by_user, partner_phone_field + ): + if not message: + message = "

" + message = self.env["ir.fields.converter"].text_from_html(message) + template = self.env.ref( + "sign_oca_sms.sign_oca_sms_template_notification" + ).with_context( + **{ + "requested_by_user": requested_by_user, + "message": message, + "link": link, + } + ) + body = template._render_field("body", signer_partner.ids, compute_lang=True)[ + signer_partner.id + ] + + composer = ( + self.env["sms.composer"] + .with_context( + default_composition_mode="comment", + default_res_id=signer_partner.id, + default_res_model="res.partner", + default_template_id=False, + ) + .create( + { + "body": body, + "number_field_name": partner_phone_field, + } + ) + ) + + composer.action_send_sms() + + def action_send_sms(self, sign_now=False, message=""): + self.ensure_one() + if self.state != "1_draft": + return + self._set_action_log("validate") + self.state = "2_signed" + if any(not signer.phone_field for signer in self.signer_ids): + raise ValidationError(self.env._("Please Select the Signer's Phone")) + for signer in self.signer_ids: + signer._portal_ensure_token() + if sign_now and signer.partner_id == self.env.user.partner_id: + continue + + link = self.get_base_url() + signer.access_url + self._send_sign_sms( + message, + link, + signer.partner_id, + self.create_uid.name, + signer.phone_field, + ) + + +class SignOcaRequestSigner(models.Model): + _inherit = "sign.oca.request.signer" + + phone_field = fields.Char() diff --git a/sign_oca_sms/pyproject.toml b/sign_oca_sms/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/sign_oca_sms/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sign_oca_sms/readme/CONFIGURE.md b/sign_oca_sms/readme/CONFIGURE.md new file mode 100644 index 00000000..e0f8c9f4 --- /dev/null +++ b/sign_oca_sms/readme/CONFIGURE.md @@ -0,0 +1,14 @@ +### Configuration Steps + +1. **Configure SMS Template** + - Navigate to **Settings > Technical > Phone > SMS > SMS > Templates** + - Locate the template named "Sign OCA: Sign Request Notification" + - Customize the SMS message as needed using the available placeholders + +### Technical Notes + +- The module adds a new selection field `send_method` with options: + - Email (default) + - SMS + - Email & SMS +- SMS messages include a secure link to access the document for signing diff --git a/sign_oca_sms/readme/CONTRIBUTORS.md b/sign_oca_sms/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..8a045b19 --- /dev/null +++ b/sign_oca_sms/readme/CONTRIBUTORS.md @@ -0,0 +1,6 @@ +- ForgeFlow () + - BernatPForgeFlow \<\> + - Maria de Luna + +- [Trobz](https://www.trobz.com): + - Do Anh Duy \<\> diff --git a/sign_oca_sms/readme/DESCRIPTION.md b/sign_oca_sms/readme/DESCRIPTION.md new file mode 100644 index 00000000..c2acb6b1 --- /dev/null +++ b/sign_oca_sms/readme/DESCRIPTION.md @@ -0,0 +1,10 @@ +## Sign OCA SMS Integration + +This module extends the functionality of Sign OCA by adding SMS notification capabilities for document signing requests. It allows users to send signature requests via SMS, in addition to the standard email method, providing more flexibility in how signers are notified and can access documents for signing. + +### Key Features + +- Send signature requests via SMS, email, or both +- Configure preferred contact method for each signer +- Track SMS delivery status +- Customizable SMS templates diff --git a/sign_oca_sms/readme/USAGE.md b/sign_oca_sms/readme/USAGE.md new file mode 100644 index 00000000..c047c1d9 --- /dev/null +++ b/sign_oca_sms/readme/USAGE.md @@ -0,0 +1,43 @@ +### Sending Signature Requests via SMS + +1. **Initiating a Signature Request** + - Navigate to **Sign > New Request** + - Select or upload the document to be signed + - Add signers as you normally would + - In the sending options, choose your preferred method: + - **Email**: Send via email only (default) + - **SMS**: Send via SMS only + - **Email & SMS**: Send both email and SMS notifications + +2. **Adding Signer Phone Numbers** + - When adding a signer, ensure their contact information includes a valid phone number + - The system will use the mobile number if available, falling back to the phone number + - You can also specify a custom phone number for each signer in the signer details + +3. **Customizing the SMS Message** + - The default SMS message includes: + - Recipient's name + - Name of the person requesting the signature + - Secure link to access the document + - Message (if provided) + - To customize the message: + - Go to **Settings > Technical > Phone > SMS > Templates** + - Find and edit "Sign OCA: Sign Request Notification" + - Use available placeholders to personalize the message + +### Tracking and Managing SMS Notifications + +- **Resending Notifications**: + - Open the signature request + - Click on the "Resend" button + - Select the preferred sending method + +### Best Practices + +1. **Consent and Privacy** + - Ensure you have consent to contact signers via SMS + - Include an opt-out mechanism in your communications + +2. **Message Length** + - Keep SMS messages concise to avoid multiple message charges + - The secure link is automatically shortened by the system diff --git a/sign_oca_sms/security/ir.model.access.csv b/sign_oca_sms/security/ir.model.access.csv new file mode 100644 index 00000000..95812091 --- /dev/null +++ b/sign_oca_sms/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +edit_sign_phone,edit_sign_phone,model_sign_oca_signer_phone,sign_oca.sign_oca_group_user,1,1,1,1 diff --git a/sign_oca_sms/static/description/icon.png b/sign_oca_sms/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/sign_oca_sms/static/description/icon.png differ diff --git a/sign_oca_sms/static/description/index.html b/sign_oca_sms/static/description/index.html new file mode 100644 index 00000000..0737adb7 --- /dev/null +++ b/sign_oca_sms/static/description/index.html @@ -0,0 +1,541 @@ + + + + + +Sign Oca Sms + + + +
+

Sign Oca Sms

+ + +

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

+
+

Sign OCA SMS Integration

+

This module extends the functionality of Sign OCA by adding SMS +notification capabilities for document signing requests. It allows users +to send signature requests via SMS, in addition to the standard email +method, providing more flexibility in how signers are notified and can +access documents for signing.

+
+

Key Features

+
    +
  • Send signature requests via SMS, email, or both
  • +
  • Configure preferred contact method for each signer
  • +
  • Track SMS delivery status
  • +
  • Customizable SMS templates
  • +
+

Table of contents

+ + +
+
+

Configuration Steps

+
    +
  1. Configure SMS Template
      +
    • Navigate to Settings > Technical > Phone > SMS > SMS > +Templates
    • +
    • Locate the template named “Sign OCA: Sign Request Notification”
    • +
    • Customize the SMS message as needed using the available +placeholders
    • +
    +
  2. +
+
+
+

Technical Notes

+
    +
  • The module adds a new selection field send_method with options:
      +
    • Email (default)
    • +
    • SMS
    • +
    • Email & SMS
    • +
    +
  • +
  • SMS messages include a secure link to access the document for signing
  • +
+
+

Usage

+
+
+
+

Sending Signature Requests via SMS

+
    +
  1. Initiating a Signature Request
      +
    • Navigate to Sign > New Request
    • +
    • Select or upload the document to be signed
    • +
    • Add signers as you normally would
    • +
    • In the sending options, choose your preferred method:
        +
      • Email: Send via email only (default)
      • +
      • SMS: Send via SMS only
      • +
      • Email & SMS: Send both email and SMS notifications
      • +
      +
    • +
    +
  2. +
  3. Adding Signer Phone Numbers
      +
    • When adding a signer, ensure their contact information includes a +valid phone number
    • +
    • The system will use the mobile number if available, falling back to +the phone number
    • +
    • You can also specify a custom phone number for each signer in the +signer details
    • +
    +
  4. +
  5. Customizing the SMS Message
      +
    • The default SMS message includes:
        +
      • Recipient’s name
      • +
      • Name of the person requesting the signature
      • +
      • Secure link to access the document
      • +
      • Message (if provided)
      • +
      +
    • +
    • To customize the message:
        +
      • Go to Settings > Technical > Phone > SMS > Templates
      • +
      • Find and edit “Sign OCA: Sign Request Notification”
      • +
      • Use available placeholders to personalize the message
      • +
      +
    • +
    +
  6. +
+
+
+

Tracking and Managing SMS Notifications

+
    +
  • Resending Notifications:
      +
    • Open the signature request
    • +
    • Click on the “Resend” button
    • +
    • Select the preferred sending method
    • +
    +
  • +
+
+
+

Best Practices

+
    +
  1. Consent and Privacy
      +
    • Ensure you have consent to contact signers via SMS
    • +
    • Include an opt-out mechanism in your communications
    • +
    +
  2. +
  3. Message Length
      +
    • Keep SMS messages concise to avoid multiple message charges
    • +
    • The secure link is automatically shortened by the system
    • +
    +
  4. +
+
+

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

+
    +
  • Kencove
  • +
  • ForgeFlow
  • +
+
+
+

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

+

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

+
+
+ + diff --git a/sign_oca_sms/tests/__init__.py b/sign_oca_sms/tests/__init__.py new file mode 100644 index 00000000..c43be9ed --- /dev/null +++ b/sign_oca_sms/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sign_oca_sms diff --git a/sign_oca_sms/tests/empty.pdf b/sign_oca_sms/tests/empty.pdf new file mode 100644 index 00000000..dfe01759 Binary files /dev/null and b/sign_oca_sms/tests/empty.pdf differ diff --git a/sign_oca_sms/tests/test_sign_oca_sms.py b/sign_oca_sms/tests/test_sign_oca_sms.py new file mode 100644 index 00000000..65046144 --- /dev/null +++ b/sign_oca_sms/tests/test_sign_oca_sms.py @@ -0,0 +1,95 @@ +# Copyright 2023 ForgeFlow S.L. (http://www.forgeflow.com) +# Copyright 2025 Kencove (https://www.kencove.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests import Form + +from odoo.addons.sign_oca.tests.test_sign import TestSign + + +class TestSignSms(TestSign): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.signer.write( + { + "phone": "+507 833 8744", + "mobile": "+254711123456", + } + ) + cls.SMS = cls.env["sms.sms"] + cls.Mail = cls.env["mail.mail"] + # clear old data + cls.SMS.search([]).unlink() + cls.Mail.search([]).unlink() + + def _create_sms_wizard(self, send_method="sms", phone_field="mobile"): + partner = self.signer + self.configure_template() + wizard_form = Form( + self.env["sign.oca.template.generate"].with_context( + default_template_id=self.template.id, + send_method=send_method, + ) + ) + # Create phone record if needed + if send_method in ("sms", "both"): + phone_record = self.env["sign.oca.signer.phone"].create( + { + "partner_id": partner.id, + "number": partner.phone + if phone_field == "phone" + else partner.mobile, + "phone_field": phone_field, + } + ) + with wizard_form.signer_ids.edit(0) as signer_line: + signer_line.partner_id = partner + signer_line.phone_id = phone_record + else: + with wizard_form.signer_ids.edit(0) as signer_line: + signer_line.partner_id = partner + + return wizard_form.save() + + def test_wizard_generate_send_sms_only(self): + sent_sms = self.SMS.search([]) + self.assertEqual(len(sent_sms), 0) + wizard = self._create_sms_wizard(send_method="sms") + action = wizard.generate() + request = self.env[action["res_model"]].browse(action["res_id"]) + self.assertEqual(len(request.signer_ids), 1) + self.assertEqual(request.signer_ids.partner_id, self.signer) + self.assertEqual(request.signer_ids.phone_field, "mobile") + sent_sms = self.SMS.search([]) + self.assertEqual(len(sent_sms), 1) + self.assertEqual(sent_sms.number, self.signer.mobile) + self.assertIn(self.signer.name, sent_sms.body) + + def test_wizard_generate_send_email_only(self): + sent_mail = self.Mail.search([]) + self.assertEqual(len(sent_mail), 0) + wizard = self._create_sms_wizard(send_method="email") + action = wizard.generate() + request = self.env[action["res_model"]].browse(action["res_id"]) + self.assertEqual(len(request.signer_ids), 1) + self.assertFalse( + request.signer_ids.phone_field, "Phone field should be empty for email-only" + ) + sent_mail = self.Mail.search([]) + self.assertEqual(len(sent_mail), 1) + + def test_wizard_generate_send_both(self): + sent_sms = self.SMS.search([]) + sent_mail = self.Mail.search([]) + self.assertEqual(len(sent_sms), 0) + self.assertEqual(len(sent_mail), 0) + wizard = self._create_sms_wizard(send_method="both") + action = wizard.generate() + # Verify SMS and Email were sent + request = self.env[action["res_model"]].browse(action["res_id"]) + self.assertEqual(request.signer_ids.phone_field, "mobile") + sent_sms = self.SMS.search([]) + sent_mail = self.Mail.search([]) + self.assertEqual(len(sent_sms), 1) + self.assertEqual(len(sent_mail), 1) diff --git a/sign_oca_sms/wizards/__init__.py b/sign_oca_sms/wizards/__init__.py new file mode 100644 index 00000000..b4830870 --- /dev/null +++ b/sign_oca_sms/wizards/__init__.py @@ -0,0 +1 @@ +from . import sign_oca_template_generate diff --git a/sign_oca_sms/wizards/sign_oca_generate.xml b/sign_oca_sms/wizards/sign_oca_generate.xml new file mode 100644 index 00000000..eb106cad --- /dev/null +++ b/sign_oca_sms/wizards/sign_oca_generate.xml @@ -0,0 +1,53 @@ + + + + + sign.oca.template.generate.form (in sign_oca) + sign.oca.template.generate + + + + + + + + + + diff --git a/sign_oca_sms/wizards/sign_oca_template_generate.py b/sign_oca_sms/wizards/sign_oca_template_generate.py new file mode 100644 index 00000000..e0a15f97 --- /dev/null +++ b/sign_oca_sms/wizards/sign_oca_template_generate.py @@ -0,0 +1,111 @@ +# Copyright 2023 CreuBlana +# Copyright 2023 ForgeFlow S.L. (http://www.forgeflow.com) +# Copyright 2025 Kencove (https://www.kencove.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class SignOcaTemplateGenerate(models.TransientModel): + _inherit = "sign.oca.template.generate" + _description = "Generate a signature request" + + send_method = fields.Selection( + selection=[("sms", "SMS"), ("email", "Email"), ("both", "Email & SMS")], + default="email", + ) + + def generate(self): + send_method = self.env.context.get("send_method") + send_methods = dict(self._fields["send_method"].selection) + if not send_method or send_method not in send_methods: + return super().generate() + + self.send_method = send_method + send_method_msg = send_methods[send_method] + request = self._generate() + if send_method == "email" or self.sign_now: + request.action_send(sign_now=self.sign_now, message=self.message) + elif send_method == "sms": + request.action_send_sms(sign_now=self.sign_now, message=self.message) + else: + request.action_send(sign_now=self.sign_now, message=self.message) + request.state = "1_draft" + request.action_send_sms(sign_now=self.sign_now, message=self.message) + request.message_post(body=f"Sign request sent via {send_method_msg}") + return request.sign() + + def _generate_vals(self): + res = super()._generate_vals() + for signer in res["signer_ids"]: + signer_dict = signer[2] + partner_id = signer_dict.get("partner_id") + signer_id = self.signer_ids.filtered( + lambda x, pid=partner_id: x.partner_id.id == pid + ) + signer_dict["phone_field"] = ( + signer_id.phone_id.phone_field if signer_id.phone_id else "" + ) + return res + + +class SignSignerPhone(models.TransientModel): + _name = "sign.oca.signer.phone" + _description = "Sign Signer Phone" + _rec_names_search = ["number"] + + display_name = fields.Char( + compute="_compute_display_name", search="_search_display_name" + ) + number = fields.Char() + partner_id = fields.Many2one("res.partner") + phone_field = fields.Selection(selection=[("mobile", "Mobile"), ("phone", "Phone")]) + + @api.depends("phone_field", "number") + def _compute_display_name(self): + for rec in self: + rec.display_name = rec.phone_field + ": " + rec.number + + def _create_or_update_number(self, partner_id): + phone_fields = ["mobile", "phone"] + for phone_field in phone_fields: + record = self.search( + [ + ("partner_id", "=", partner_id.id), + ("phone_field", "=", phone_field), + ] + ) + partner_num = getattr(partner_id, phone_field) + if not record and partner_num: + self.create( + { + "partner_id": partner_id.id, + "number": partner_num, + "phone_field": phone_field, + } + ) + elif record.number != partner_num: + record.number = partner_num + elif record.number and not partner_num: + record.unlink() + + +class SignOcaTemplateGenerateSigner(models.TransientModel): + _inherit = "sign.oca.template.generate.signer" + + phone_id = fields.Many2one("sign.oca.signer.phone") + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + for rec in res: + if rec.partner_id: + signer_phone = self.env["sign.oca.signer.phone"] + signer_phone._create_or_update_number(rec.partner_id) + return res + + @api.onchange("partner_id") + def _get_phones(self): + signer_phone = self.env["sign.oca.signer.phone"] + if self.partner_id: + signer_phone._create_or_update_number(self.partner_id)