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.

394 lines
16 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. import base64
  2. from datetime import datetime
  3. import re
  4. from openerp import http
  5. from openerp.http import request
  6. from openerp.tools.translate import _
  7. # Only use for behavior, don't stock it
  8. _TECHNICAL = ['view_from', 'view_callback']
  9. # Allow in description
  10. _BLACKLIST = ['id', 'create_uid', 'create_date', 'write_uid', 'write_date',
  11. 'user_id', 'active']
  12. _COOP_FORM_FIELD = ['email', 'confirm_email', 'firstname', 'lastname',
  13. 'birthdate', 'iban', 'share_product_id', 'no_registre',
  14. 'address', 'city', 'zip_code', 'country_id', 'phone',
  15. 'lang', 'nb_parts', 'total_parts', 'error_msg']
  16. _COMPANY_FORM_FIELD = ['is_company', 'company_register_number', 'company_name',
  17. 'company_email', 'confirm_email', 'email', 'firstname',
  18. 'lastname', 'birthdate', 'iban', 'share_product_id',
  19. 'no_registre', 'address', 'city', 'zip_code',
  20. 'country_id', 'phone', 'lang', 'nb_parts',
  21. 'total_parts', 'error_msg', 'company_type']
  22. class WebsiteSubscription(http.Controller):
  23. @http.route(['/page/become_cooperator',
  24. '/become_cooperator'],
  25. type='http', auth="public", website=True)
  26. def display_become_cooperator_page(self, **kwargs):
  27. values = {}
  28. logged = False
  29. if request.env.user.login != 'public':
  30. logged = True
  31. partner = request.env.user.partner_id
  32. if partner.is_company:
  33. return self.display_become_company_cooperator_page()
  34. values = self.fill_values(values, False, logged, True)
  35. for field in _COOP_FORM_FIELD:
  36. if kwargs.get(field):
  37. values[field] = kwargs.pop(field)
  38. values.update(kwargs=kwargs.items())
  39. return request.render("easy_my_coop_website.becomecooperator", values)
  40. @http.route(['/page/become_company_cooperator',
  41. '/become_company_cooperator'],
  42. type='http', auth="public", website=True)
  43. def display_become_company_cooperator_page(self, **kwargs):
  44. values = {}
  45. logged = False
  46. if request.env.user.login != 'public':
  47. logged = True
  48. values = self.fill_values(values, True, logged, True)
  49. for field in _COMPANY_FORM_FIELD:
  50. if kwargs.get(field):
  51. values[field] = kwargs.pop(field)
  52. values.update(kwargs=kwargs.items())
  53. return request.render("easy_my_coop_website.becomecompanycooperator",
  54. values)
  55. def preRenderThanks(self, values, kwargs):
  56. """ Allow to be overrided """
  57. return {
  58. '_values': values,
  59. '_kwargs': kwargs,
  60. }
  61. def get_subscription_response(self, values, kwargs):
  62. values = self.preRenderThanks(values, kwargs)
  63. return request.render("easy_my_coop_website.cooperator_thanks", values)
  64. def get_date_string(self, birthdate):
  65. if birthdate:
  66. return datetime.strftime(birthdate, "%d/%m/%Y")
  67. return False
  68. def get_values_from_user(self, values, is_company):
  69. # the subscriber is connected
  70. if request.env.user.login != 'public':
  71. values['logged'] = 'on'
  72. partner = request.env.user.partner_id
  73. if partner.member or partner.old_member:
  74. values['already_cooperator'] = 'on'
  75. if partner.bank_ids:
  76. values['iban'] = partner.bank_ids[0].acc_number
  77. values['address'] = partner.street
  78. values['zip_code'] = partner.zip
  79. values['city'] = partner.city
  80. values['country_id'] = partner.country_id.id
  81. if is_company:
  82. # company values
  83. values['company_register_number'] = partner.company_register_number
  84. values['company_name'] = partner.name
  85. values['company_email'] = partner.email
  86. values['company_type'] = partner.legal_form
  87. # contact person values
  88. representative = partner.get_representative()
  89. values['firstname'] = representative.firstname
  90. values['lastname'] = representative.lastname
  91. values['gender'] = representative.gender
  92. values['email'] = representative.email
  93. values['contact_person_function'] = representative.function
  94. values['birthdate'] = self.get_date_string(representative.birthdate_date)
  95. values['lang'] = representative.lang
  96. values['phone'] = representative.phone
  97. else:
  98. values['firstname'] = partner.firstname
  99. values['lastname'] = partner.lastname
  100. values['email'] = partner.email
  101. values['gender'] = partner.gender
  102. values['birthdate'] = self.get_date_string(partner.birthdate_date)
  103. values['lang'] = partner.lang
  104. values['phone'] = partner.phone
  105. return values
  106. def fill_values(self, values, is_company, logged, load_from_user=False):
  107. sub_req_obj = request.env['subscription.request']
  108. company = request.website.company_id
  109. products = self.get_products_share(is_company)
  110. if load_from_user:
  111. values = self.get_values_from_user(values, is_company)
  112. if is_company:
  113. values['is_company'] = 'on'
  114. if logged:
  115. values['logged'] = 'on'
  116. values['countries'] = self.get_countries()
  117. values['langs'] = self.get_langs()
  118. values['products'] = products
  119. fields_desc = sub_req_obj.sudo().fields_get(['company_type', 'gender'])
  120. values['company_types'] = fields_desc['company_type']['selection']
  121. values['genders'] = fields_desc['gender']['selection']
  122. values['company'] = company
  123. if not values.get('share_product_id'):
  124. for product in products:
  125. if product.default_share_product is True:
  126. values['share_product_id'] = product.id
  127. break
  128. if not values.get('share_product_id', False) and products:
  129. values['share_product_id'] = products[0].id
  130. if not values.get('country_id'):
  131. if company.default_country_id:
  132. values['country_id'] = company.default_country_id.id
  133. else:
  134. values['country_id'] = '21'
  135. if not values.get('activities_country_id'):
  136. if company.default_country_id:
  137. values['activities_country_id'] = company.default_country_id.id
  138. else:
  139. values['activities_country_id'] = '21'
  140. if not values.get('lang'):
  141. if company.default_lang_id:
  142. values['lang'] = company.default_lang_id.code
  143. comp = request.env['res.company']._company_default_get()
  144. values.update({
  145. 'display_data_policy': comp.display_data_policy_approval,
  146. 'data_policy_required': comp.data_policy_approval_required,
  147. 'data_policy_text': comp.data_policy_approval_text,
  148. 'display_internal_rules': comp.display_internal_rules_approval,
  149. 'internal_rules_required': comp.internal_rules_approval_required,
  150. 'internal_rules_text': comp.internal_rules_approval_text,
  151. })
  152. return values
  153. def get_products_share(self, is_company):
  154. product_obj = request.env['product.template']
  155. products = product_obj.sudo().get_web_share_products(is_company)
  156. return products
  157. def get_countries(self):
  158. countries = request.env['res.country'].sudo().search([])
  159. return countries
  160. def get_langs(self):
  161. langs = request.env['res.lang'].sudo().search([])
  162. return langs
  163. def get_selected_share(self, kwargs):
  164. prod_obj = request.env['product.template']
  165. product_id = kwargs.get("share_product_id")
  166. return prod_obj.sudo().browse(int(product_id)).product_variant_ids[0]
  167. def validation(self, kwargs, logged, values, post_file):
  168. user_obj = request.env['res.users']
  169. sub_req_obj = request.env['subscription.request']
  170. redirect = "easy_my_coop_website.becomecooperator"
  171. email = kwargs.get('email')
  172. is_company = kwargs.get("is_company") == 'on'
  173. if is_company:
  174. is_company = True
  175. redirect = "easy_my_coop_website.becomecompanycooperator"
  176. email = kwargs.get('company_email')
  177. if 'g-recaptcha-response' not in kwargs or kwargs['g-recaptcha-response'] == '':
  178. values = self.fill_values(values, is_company, logged)
  179. values.update(kwargs)
  180. values["error_msg"] = _("the captcha has not been validated,"
  181. " please fill in the captcha")
  182. return request.render(redirect, values)
  183. elif not request.website.is_captcha_valid(
  184. kwargs['g-recaptcha-response']):
  185. values = self.fill_values(values, is_company, logged)
  186. values.update(kwargs)
  187. values["error_msg"] = _("the captcha has not been validated,"
  188. " please fill in the captcha")
  189. return request.render(redirect, values)
  190. # Check that required field from model subscription_request exists
  191. required_fields = sub_req_obj.sudo().get_required_field()
  192. error = set(field for field in required_fields if not values.get(field)) #noqa
  193. if error:
  194. values = self.fill_values(values, is_company, logged)
  195. values["error_msg"] = _("Some mandatory fields have not "
  196. "been filled")
  197. values = dict(values, error=error, kwargs=kwargs.items())
  198. return request.render(redirect, values)
  199. if not logged and email:
  200. user = user_obj.sudo().search([('login', '=', email)])
  201. if user:
  202. values = self.fill_values(values, is_company, logged)
  203. values.update(kwargs)
  204. values["error_msg"] = _("There is an existing account for this"
  205. " mail address. Please login before "
  206. "fill in the form")
  207. return request.render(redirect, values)
  208. else:
  209. confirm_email = kwargs.get('confirm_email')
  210. if email != confirm_email:
  211. values = self.fill_values(values, is_company, logged)
  212. values.update(kwargs)
  213. values["error_msg"] = _("The email and the confirmation "
  214. "email doesn't match.Please check "
  215. "the given mail addresses")
  216. return request.render(redirect, values)
  217. company = request.website.company_id
  218. if company.allow_id_card_upload:
  219. if not post_file:
  220. values = self.fill_values(values, is_company, logged)
  221. values.update(kwargs)
  222. values["error_msg"] = _("You need to upload a"
  223. " scan of your id card")
  224. return request.render(redirect, values)
  225. iban = kwargs.get("iban")
  226. valid = sub_req_obj.check_iban(iban)
  227. if not valid:
  228. values = self.fill_values(values, is_company, logged)
  229. values["error_msg"] = _("You iban account number"
  230. "is not valid")
  231. return request.render(redirect, values)
  232. # check the subscription's amount
  233. max_amount = company.subscription_maximum_amount
  234. if logged:
  235. partner = request.env.user.partner_id
  236. if partner.member:
  237. max_amount = max_amount - partner.total_value
  238. if company.unmix_share_type:
  239. share = self.get_selected_share(kwargs)
  240. if int(partner.cooperator_type) != share.id:
  241. values = self.fill_values(values, is_company, logged)
  242. values["error_msg"] = (_("You can't subscribe two "
  243. "different types of share"))
  244. return request.render(redirect, values)
  245. total_amount = float(kwargs.get('total_parts'))
  246. if max_amount > 0 and total_amount > max_amount:
  247. values = self.fill_values(values, is_company, logged)
  248. values["error_msg"] = (_("You can't subscribe for an amount that "
  249. "exceed ")
  250. + str(max_amount)
  251. + company.currency_id.symbol)
  252. return request.render(redirect, values)
  253. return True
  254. @http.route(['/subscription/get_share_product'],
  255. type='json',
  256. auth="public",
  257. methods=['POST'], website=True)
  258. def get_share_product(self, share_product_id, **kw):
  259. product_template = request.env['product.template']
  260. product = product_template.sudo().browse(int(share_product_id))
  261. return {
  262. product.id: {
  263. 'list_price': product.list_price,
  264. 'min_qty': product.minimum_quantity,
  265. 'force_min_qty': product.force_min_qty
  266. }
  267. }
  268. @http.route(['/subscription/subscribe_share'],
  269. type='http',
  270. auth="public", website=True)
  271. def share_subscription(self, **kwargs):
  272. sub_req_obj = request.env['subscription.request']
  273. attach_obj = request.env['ir.attachment']
  274. # List of file to add to ir_attachment once we have the ID
  275. post_file = []
  276. # Info to add after the message
  277. post_description = []
  278. values = {}
  279. for field_name, field_value in kwargs.items():
  280. if hasattr(field_value, 'filename'):
  281. post_file.append(field_value)
  282. elif (field_name in sub_req_obj._fields
  283. and field_name not in _BLACKLIST):
  284. values[field_name] = field_value
  285. # allow to add some free fields or blacklisted field like ID
  286. elif field_name not in _TECHNICAL:
  287. post_description.append("%s: %s" % (field_name, field_value))
  288. logged = kwargs.get("logged") == 'on'
  289. is_company = kwargs.get("is_company") == 'on'
  290. response = self.validation(kwargs, logged, values, post_file)
  291. if response is not True:
  292. return response
  293. already_coop = False
  294. if logged:
  295. partner = request.env.user.partner_id
  296. values['partner_id'] = partner.id
  297. already_coop = partner.member
  298. elif kwargs.get("already_cooperator") == 'on':
  299. already_coop = True
  300. values["already_cooperator"] = already_coop
  301. values["is_company"] = is_company
  302. if kwargs.get('data_policy_approved', 'off') == 'on':
  303. values['data_policy_approved'] = True
  304. if kwargs.get('internal_rules_approved', 'off') == 'on':
  305. values['internal_rules_approved'] = True
  306. lastname = kwargs.get("lastname").upper()
  307. firstname = kwargs.get("firstname").title()
  308. values["name"] = firstname + " " + lastname
  309. values["lastname"] = lastname
  310. values["firstname"] = firstname
  311. values["birthdate"] = datetime.strptime(kwargs.get("birthdate"),
  312. "%d/%m/%Y").date()
  313. values["source"] = "website"
  314. values["share_product_id"] = self.get_selected_share(kwargs).id
  315. if is_company:
  316. if kwargs.get("company_register_number", is_company):
  317. values["company_register_number"] = re.sub('[^0-9a-zA-Z]+',
  318. '',
  319. kwargs.get("company_register_number"))
  320. subscription_id = sub_req_obj.sudo().create_comp_sub_req(values)
  321. else:
  322. subscription_id = sub_req_obj.sudo().create(values)
  323. if subscription_id:
  324. for field_value in post_file:
  325. attachment_value = {
  326. 'name': field_value.filename,
  327. 'res_name': field_value.filename,
  328. 'res_model': 'subscription.request',
  329. 'res_id': subscription_id,
  330. 'datas': base64.encodestring(field_value.read()),
  331. 'datas_fname': field_value.filename,
  332. }
  333. attach_obj.sudo().create(attachment_value)
  334. return self.get_subscription_response(values, kwargs)