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.

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