RFC (Registro Federal de Contribuyentes) party.py Mexico

29 views
Skip to first unread message

ferr...@gmail.com

unread,
Jul 11, 2016, 10:35:03 AM7/11/16
to tryton-contrib
Hello, I updated party.py from the party module to include the Mexican's tax number RFC (VAT code equivalent). It requires python-stdnum v1.3.

*** /tmp/ediff27895fd    2016-07-11 09:17:23.966602026 -0500
--- /usr/lib/python2.7/dist-packages/trytond/modules/party/party.py    2016-07-09 21:15:31.914255012 -0500
***************
*** 3,8 ****
--- 3,9 ----
  from itertools import groupby
 
  import stdnum.eu.vat as vat
+ import stdnum.mx.rfc as rfc
  import stdnum.exceptions
  from sql import Null
  from sql.functions import CharLength
***************
*** 126,132 ****
 
      @classmethod
      def _vat_types(cls):
!         return ['eu_vat']
 
      def get_vat_code(self, name):
          types = self._vat_types()
--- 127,133 ----
 
      @classmethod
      def _vat_types(cls):
!         return ['eu_vat', 'mx_rfc']
 
      def get_vat_code(self, name):
          types = self._vat_types()
***************
*** 266,271 ****
--- 267,274 ----
                  type = None
                  if vat.is_valid(code):
                      type = 'eu_vat'
+                 if rfc.is_valid(code):
+                     type = 'mx_rfc'
                  identifiers.append(
                      cls(party=party_id, code=code, type=type))
              cls.save(identifiers)
***************
*** 276,281 ****
--- 279,285 ----
      def get_types(cls):
          return [
              (None, ''),
+             ('mx_rfc', 'RFC'),
              ('eu_vat', 'VAT'),
              ]
 
***************
*** 286,301 ****
                  return vat.compact(self.code)
              except stdnum.exceptions.ValidationError:
                  pass
          return self.code
 
      def pre_validate(self):
          super(PartyIdentifier, self).pre_validate()
          self.check_code()
 
      def check_code(self):
          if self.type == 'eu_vat':
              if not vat.is_valid(self.code):
!                 if self.party.id > 0:
                      party = self.party.rec_name
                  else:
                      party = ''
--- 290,323 ----
                  return vat.compact(self.code)
              except stdnum.exceptions.ValidationError:
                  pass
+
+         if self.type == 'mx_rfc':
+             try:
+                 return rfc.compact(self.code)
+             except stdnum.exceptions.ValidationError:
+                 pass
          return self.code
 
      def pre_validate(self):
          super(PartyIdentifier, self).pre_validate()
          self.check_code()
 
+     @fields.depends('type', 'party', 'code')
      def check_code(self):
          if self.type == 'eu_vat':
              if not vat.is_valid(self.code):
!                 if self.party and self.party.id > 0:
!                     party = self.party.rec_name
!                 else:
!                     party = ''
!                 self.raise_user_error('invalid_vat', {
!                         'code': self.code,
!                         'party': party,
!                          })
!
!         if self.type == 'mx_rfc':
!             if not rfc.is_valid(self.code):
!                 if self.party and self.party.id > 0:
                      party = self.party.rec_name
                  else:
                      party = ''
***************
*** 345,350 ****
--- 367,379 ----
          parties = Party.browse(Transaction().context.get('active_ids'))
          for party in parties:
              for identifier in party.identifiers:
+                 if identifier.type == 'mx_rfc':
+                     if not rfc.is_valid(identifier.code):
+                         parties_failed.append(party.id)
+                     else:
+                         parties_succeed.append(party.id)
+                     continue
+
                  if identifier.type != 'eu_vat':
                      continue
                  try:

Cédric Krier

unread,
Jul 13, 2016, 10:35:06 PM7/13/16
to tryton-contrib
On 2016-07-11 07:20, ferr...@gmail.com wrote:
> Hello, I updated party.py from the party module to include the Mexican's
> tax number RFC (VAT code equivalent). It requires python-stdnum v1.3.

Instead of changing the code of the standard module, it is better to
create a new module that extend the party module to add the MX
validation. Normally, the design of the Identifier object is done such
customization should be easy.

You can take a look at [1] to learn how to create new module

[1] http://downloads.tryton.org/TUL2014/crash-course.pdf

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/
Reply all
Reply to author
Forward
0 new messages