You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.7 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) Odoo Colombia (Community).
  6. # Author David Arnold (devCO)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import models, fields, api, _
  23. class ResPartnerIDtype(models.Model):
  24. _name = 'res.partner.idtype'
  25. _description = 'Identification Document Type'
  26. _order = 'sequence'
  27. name = fields.Char(required=True)
  28. code = fields.Char(required=True)
  29. sequence = fields.Integer()
  30. active = fields.Boolean(default=True)
  31. note = fields.Text()
  32. on_company = fields.Boolean(string=u'On Company?')
  33. on_contact = fields.Boolean(string=u'On Contact?', default=True)
  34. class ResPartnerIdent(models.Model):
  35. _name = 'res.partner'
  36. _inherit = 'res.partner'
  37. dom = "['|', ('on_company', '=', is_company), ('on_contact', '!=', is_company)]"
  38. fiscal_id_type = fields.Many2one(
  39. 'res.partner.idtype', string=u'Document Type', domain=dom,
  40. )
  41. fiscal_id = fields.Char(string=u'Document ID')
  42. fiscal_id_doc = fields.Binary(string=u'Document Scan',
  43. help="Upload the supporting Document preferably as size-optimized PDF. This might "
  44. "help save disk space and PDF allows you to concatenate multiple documents.")
  45. @api.one
  46. @api.onchange(
  47. 'fiscal_id_type',
  48. 'fiscal_id',
  49. # 'is_company', # https://github.com/odoo/odoo/issues/1530
  50. )
  51. def validateformatcopy(self):
  52. # CASE: Current ID Type is not applicable on company
  53. if self.is_company and not self.fiscal_id_type.on_company:
  54. # Get the first valid ID type (remember: ordered by sequence)
  55. self.fiscal_id_type = self.env['res.partner.idtype'].search([('on_company', '=', True)], limit=1).id
  56. self.fiscal_id = None # Reset ID value
  57. # CASE: Current ID Type is not applicable on contact
  58. if not self.is_company and not self.fiscal_id_type.on_contact:
  59. # Get the first valid ID type (remember: ordered by sequence)
  60. self.fiscal_id_type = self.env['res.partner.idtype'].search([('on_contact', '=', True)], limit=1).id
  61. self.fiscal_id = None # Reset ID value
  62. # If everything is fine, call subclasses
  63. if self.fiscal_id_type and self.fiscal_id:
  64. # Function for String Operations
  65. res = self._validateandformatid(self)
  66. if res['output_type'] and res['output_id']:
  67. self.fiscal_id_type, self.fiscal_id = res['output_type'], res['output_id']
  68. # Procedure for Copying
  69. self._copyid(self)
  70. @staticmethod
  71. def _validateandformatid(self):
  72. """
  73. Hook method to be inherited for custom validation methods.
  74. :param input_type: the value of the field fiscal_id_type (id); passed on by onchange decorator
  75. :param input_id: the value of the field fiscal_id (string); passed on by onchange decorator
  76. :return: must return a dict with validated and formatted values
  77. Hint:
  78. you might not alter the output_type unless you might want to build some kind of fiscal_id_type recognition
  79. based on the input pattern into your hook method. CO###.###.###-# CO-VAT (NIT) for example.
  80. Find below a suggested basic outline.
  81. """
  82. f_type, f_id, is_company = self.fiscal_id_type, self.fiscal_id, self.is_company
  83. def default(): return {'output_type': f_type, 'output_id': f_id}
  84. return {
  85. 'CODE1': {'output_type': f_type, 'output_id': f_id},
  86. 'CODE2': {'output_type': f_type, 'output_id': f_id},
  87. # Define your cases with the index being the doctype id to match
  88. # Note you can work inline (lambda), or call subfunctions at will
  89. }.get(self.fiscal_id_type.code,default())
  90. @staticmethod
  91. def _copyid(self):
  92. """
  93. Hook Method to be inherited for custom copy methods based on the document type (id)
  94. Example Use Case: Copy some local VAT number into the VAT-Field in it's international format for compatibility.
  95. :return: It is a Procedure and therefore has no return value.
  96. Find below a suggested basic outline.
  97. """
  98. f_type, f_id, is_company = self.fiscal_id_type, self.fiscal_id, self.is_company
  99. def stringop_def(s): return s
  100. def stringop_1(s): return re.match('\\d|\\w*', s)
  101. # Define other Docstringoperatios if necessary
  102. def default():
  103. self.vat_subjected = True
  104. # self.vat is a Boolean until base_vat is installed.
  105. # self.vat = self.country_id.code + sringop_def(f_id)
  106. {
  107. 'CODE1': {# self.vat_subjected: True,
  108. # self.vat: self.country_id.code + stringop_1(f_id)
  109. },
  110. 'CODE2': {# seld.vat_subjected: True,
  111. # self.vat: self.country_id.code + stringop_1(f_id)
  112. },
  113. }.get(self.fiscal_id_type.code, default())