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.

150 lines
6.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  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', '!=', " \
  38. "is_company)]"
  39. fiscal_id_type = fields.Many2one('res.partner.idtype',
  40. string=u'Document Type', domain=dom, )
  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 "
  44. "preferably as size-optimized PDF. "
  45. "This might "
  46. "help save disk space and PDF allows "
  47. "you to concatenate multiple documents.")
  48. @api.one
  49. @api.onchange('fiscal_id_type', 'fiscal_id',
  50. # 'is_company', # https://github.com/odoo/odoo/issues/1530)
  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(
  56. [('on_company', '=', True)], limit=1).id
  57. self.fiscal_id = None # Reset ID value
  58. # CASE: Current ID Type is not applicable on contact
  59. if not self.is_company and not self.fiscal_id_type.on_contact:
  60. # Get the first valid ID type (remember: ordered by sequence)
  61. self.fiscal_id_type = self.env['res.partner.idtype'].search(
  62. [('on_contact', '=', True)], limit=1).id
  63. self.fiscal_id = None # Reset ID value
  64. # If everything is fine, call subclasses
  65. if self.fiscal_id_type and self.fiscal_id:
  66. # Function for String Operations
  67. res = self._validateandformatid(self)
  68. if res['output_type'] and res['output_id']:
  69. self.fiscal_id_type, self.fiscal_id = res['output_type'], res[
  70. 'output_id']
  71. # Procedure for Copying
  72. self._copyid(self)
  73. @staticmethod
  74. def _validateandformatid(self):
  75. """
  76. Hook method to be inherited for custom validation methods.
  77. :param input_type: the value of the field fiscal_id_type (id); passed
  78. on by onchange decorator
  79. :param input_id: the value of the field fiscal_id (string); passed on
  80. by onchange decorator
  81. :return: must return a dict with validated and formatted values
  82. Hint:
  83. you might not alter the output_type unless you might want to build
  84. some kind of fiscal_id_type recognition
  85. based on the input pattern into your hook method. CO###.###.###-#
  86. CO-VAT (NIT) for example.
  87. Find below a suggested basic outline.
  88. """
  89. f_type, f_id, is_company = self.fiscal_id_type, self.fiscal_id, \
  90. self.is_company
  91. def default(): return {'output_type': f_type, 'output_id': f_id}
  92. return {'CODE1': {'output_type': f_type, 'output_id': f_id},
  93. 'CODE2': {'output_type': f_type, 'output_id': f_id},
  94. """
  95. Define your cases with the index being the doctype id to match
  96. Note you can work inline (lambda), or call subfunctions at will
  97. """
  98. }.get(self.fiscal_id_type.code, default())
  99. @staticmethod
  100. def _copyid(self):
  101. """
  102. Hook Method to be inherited for custom copy methods based on the
  103. document type (id)
  104. Example Use Case: Copy some local VAT number into the VAT-Field in
  105. it's international format for compatibility.
  106. :return: It is a Procedure and therefore has no return value.
  107. Find below a suggested basic outline.
  108. """
  109. f_type, f_id, is_company = self.fiscal_id_type, self.fiscal_id, \
  110. self.is_company
  111. def stringop_def(s): return s
  112. def stringop_1(s): return re.match('\\d|\\w*', s)
  113. # Define other Docstringoperatios if necessary
  114. def default():
  115. self.vat_subjected = True
  116. # self.vat is a Boolean until base_vat is installed.
  117. # self.vat = self.country_id.code + sringop_def(f_id)
  118. {'CODE1': {
  119. # self.vat_subjected: True,
  120. # self.vat: self.country_id.code + stringop_1(f_id)
  121. },
  122. 'CODE2': {
  123. # seld.vat_subjected: True,
  124. # self.vat: self.country_id.code + stringop_1(f_id)
  125. },}.get(self.fiscal_id_type.code, default())