Cédric Krier pushed to branch branch/default at Tryton / Tryton


Commits:
7a9f94d3 by Cédric Krier at 2023-01-30T00:11:21+01:00
Notify for duplicate identifier

Closes #12049
- - - - -


4 changed files:

- modules/party/CHANGELOG
- modules/party/message.xml
- modules/party/party.py
- + modules/party/tests/scenario_party_identifier_notifications.rst


Changes:

=====================================
modules/party/CHANGELOG
=====================================
@@ -1,3 +1,5 @@
+* Notify for duplicate identifier
+
 Version 6.6.0 - 2022-10-31
 --------------------------
 * Bug fixes (see mercurial logs for details)


=====================================
modules/party/message.xml
=====================================
@@ -18,6 +18,9 @@
         <record model="ir.message" id="msg_invalid_code">
             <field name="text">The %(type)s "%(code)s" for party "%(party)s" 
is not valid.</field>
         </record>
+        <record model="ir.message" id="msg_party_identifier_duplicate">
+            <field name="text">The party "%(party)s" has the same %(type)s 
"%(code)s".</field>
+        </record>
         <record model="ir.message" id="msg_vies_unavailable">
             <field name="text">The VIES service is unavailable, try again 
later.</field>
         </record>


=====================================
modules/party/party.py
=====================================
@@ -710,6 +710,30 @@
                             code=self.code,
                             party=party))
 
+    @fields.depends(methods=['_notify_duplicate'])
+    def on_change_notify(self):
+        notifications = super().on_change_notify()
+        notifications.extend(self._notify_duplicate())
+        return notifications
+
+    @fields.depends('code', methods=['on_change_with_code'])
+    def _notify_duplicate(self):
+        cls = self.__class__
+        if self.code:
+            code = self.on_change_with_code()
+            others = cls.search([
+                    ('id', '!=', self.id),
+                    ('type', '!=', None),
+                    ('code', '=', code),
+                    ], limit=1)
+            if others:
+                other, = others
+                yield ('warning', gettext(
+                        'party.msg_party_identifier_duplicate',
+                        party=other.party.rec_name,
+                        type=other.type_string,
+                        code=other.code))
+
 
 class CheckVIESResult(ModelView):
     'Check VIES'


=====================================
modules/party/tests/scenario_party_identifier_notifications.rst
=====================================
@@ -0,0 +1,42 @@
+==============================
+Party Identifier Notifications
+==============================
+
+Imports::
+
+    >>> from proteus import Model
+    >>> from trytond.tests.tools import activate_modules
+
+Activate modules::
+
+    >>> config = activate_modules('party')
+
+    >>> Party = Model.get('party.party')
+
+Create first party::
+
+    >>> party1 = Party(name="Party 1")
+    >>> identifier = party1.identifiers.new(type='be_vat')
+    >>> identifier.code = "500923836"
+    >>> party1.save()
+
+Create second party::
+
+    >>> party2 = Party(name="Party 2")
+    >>> identifier = party2.identifiers.new(type='be_vat')
+    >>> identifier.code = "500923836"
+
+Check notifications::
+
+    >>> len(identifier.notifications())
+    1
+
+Change identifier::
+
+    >>> identifier.type = None
+    >>> identifier.code = "foo"
+
+Check notifications::
+
+    >>> len(identifier.notifications())
+    0



View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/7a9f94d3a7c8e8e02abd5c0545d367fb14f4e171

-- 
View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/7a9f94d3a7c8e8e02abd5c0545d367fb14f4e171
You're receiving this email because of your account on foss.heptapod.net.


Reply via email to