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.

188 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017-2018 Rémy Taymans <remytaymans@gmail.com>
  3. # Copyright 2018 Odoo SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp import fields, models, http
  6. from openerp.http import request
  7. from openerp import tools
  8. from openerp.tools.translate import _
  9. from openerp.addons.website_portal_v10.controllers.main import WebsiteAccount
  10. class ExtendWebsiteAccountController(WebsiteAccount):
  11. mandatory_billing_fields = [
  12. "name",
  13. "phone",
  14. "email",
  15. "city",
  16. "country_id",
  17. "street",
  18. ]
  19. optional_billing_fields = [
  20. "zipcode",
  21. "state_id",
  22. "vat",
  23. ]
  24. @http.route(['/my/account'], type='http', auth='user', website=True)
  25. def details(self, redirect=None, **post):
  26. partner = request.env['res.users'].browse(request.uid).partner_id
  27. values = {
  28. 'error': {},
  29. 'error_message': []
  30. }
  31. self._set_mandatory_fields(post)
  32. self._set_optional_fields(post)
  33. all_fields = (
  34. self.mandatory_billing_fields
  35. + self.optional_billing_fields
  36. )
  37. if post:
  38. error, error_message = self.details_form_validate(post)
  39. values.update({'error': error, 'error_message': error_message})
  40. values.update(post)
  41. if not error:
  42. # Change zipcode to zip as it is stored as zip in the
  43. # partner
  44. if 'zipcode' in all_fields:
  45. post.update({'zip': post.pop('zipcode', '')})
  46. if partner.type == "contact":
  47. address_fields = {}
  48. if 'city' in all_fields:
  49. address_fields.update({
  50. 'city': post.pop('city'),
  51. })
  52. if 'street' in all_fields:
  53. address_fields.update({
  54. 'street': post.pop('street'),
  55. })
  56. if 'vat' in all_fields:
  57. address_fields.update({
  58. 'vat': post['vat'],
  59. })
  60. if 'zipcode' in all_fields:
  61. address_fields.update({
  62. 'zip': post.pop('zip'),
  63. })
  64. if 'country_id' in all_fields:
  65. address_fields.update({
  66. 'country_id': post.pop('country_id'),
  67. })
  68. if 'state_id' in all_fields:
  69. address_fields.update({
  70. 'state_id': post.pop('state_id')
  71. })
  72. company_fields = {}
  73. if 'company_name' in all_fields:
  74. company_fields.update({
  75. 'name': post.pop('company_name'),
  76. })
  77. if 'vat' in all_fields:
  78. company_fields.update({
  79. # The VAT must be updated on the company and on
  80. # the partner, so pop is not used.
  81. 'vat': post['vat'],
  82. })
  83. partner.commercial_partner_id.sudo().write(address_fields)
  84. partner.commercial_partner_id.sudo().write(company_fields)
  85. # Write the rest of the info in the partner
  86. partner.sudo().write(post)
  87. if redirect:
  88. return request.redirect(redirect)
  89. return request.redirect('/my/home')
  90. countries = request.env['res.country'].sudo().search([])
  91. states = request.env['res.country.state'].sudo().search([])
  92. values.update({
  93. 'partner': partner,
  94. 'countries': countries,
  95. 'states': states,
  96. 'has_check_vat': hasattr(request.env['res.partner'], 'check_vat'),
  97. 'redirect': redirect,
  98. })
  99. return request.website.render("website_portal.details", values)
  100. def _set_mandatory_fields(self, data):
  101. """Change mandatory billing fields of the form.
  102. Overwrite this function if mandatory fields must be changed
  103. depending on the value of the data or any other value.
  104. Here it mark the field 'company_name' as need or not depending
  105. on the current user.
  106. """
  107. partner = request.env['res.users'].browse(request.uid).partner_id
  108. if (partner.parent_id
  109. and 'company_name' not in self.mandatory_billing_fields):
  110. self.mandatory_billing_fields.append('company_name')
  111. if (not partner.parent_id
  112. and 'company_name' in self.mandatory_billing_fields):
  113. self.mandatory_billing_fields.remove('company_name')
  114. def _set_optional_fields(self, data):
  115. """Same as set_mandatory_fields but for optional ones.
  116. Here this does nothing.
  117. """
  118. pass
  119. def details_form_validate(self, data):
  120. """Validate the form"""
  121. error = dict()
  122. error_message = []
  123. all_fields = (
  124. self.mandatory_billing_fields
  125. + self.optional_billing_fields
  126. )
  127. # Validation
  128. for field_name in self.mandatory_billing_fields:
  129. if not data.get(field_name):
  130. error[field_name] = 'missing'
  131. # email validation
  132. if ('email' in all_fields
  133. and data.get('email')
  134. and not tools.single_email_re.match(data.get('email'))):
  135. error["email"] = 'error'
  136. error_message.append(
  137. _('Invalid Email! Please enter a valid email address.')
  138. )
  139. # vat validation
  140. if ('vat' in all_fields
  141. and data.get("vat")
  142. and hasattr(request.env["res.partner"], "check_vat")):
  143. if request.website.company_id.vat_check_vies:
  144. # force full VIES online check
  145. check_func = request.env["res.partner"].vies_vat_check
  146. else:
  147. # quick and partial off-line checksum validation
  148. check_func = request.env["res.partner"].simple_vat_check
  149. vat_country, vat_number = request.env["res.partner"]._split_vat(
  150. data.get("vat")
  151. )
  152. if not check_func(vat_country, vat_number): # simple_vat_check
  153. error["vat"] = 'error'
  154. # error message for empty required fields
  155. if [err for err in error.values() if err == 'missing']:
  156. error_message.append(_('Some required fields are empty.'))
  157. unknown = [k for k in data.iterkeys() if k not in all_fields]
  158. if unknown:
  159. error['common'] = 'Unknown field'
  160. error_message.append("Unknown field '%s'" % ','.join(unknown))
  161. return error, error_message