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.

125 lines
4.0 KiB

  1. from odoo import http
  2. from odoo.http import request
  3. from odoo.tools.translate import _
  4. class WebsiteLoanIssueSubscription(http.Controller):
  5. @http.route(
  6. ["/subscription/get_loan_issue"],
  7. type="json",
  8. auth="user",
  9. methods=["POST"],
  10. website=True,
  11. )
  12. def get_loan_issue(self, loan_issue_id, **kw):
  13. loan_issue_obj = request.env["loan.issue"]
  14. partner = request.env.user.partner_id
  15. if loan_issue_id:
  16. loan_issue = loan_issue_obj.sudo().browse(int(loan_issue_id))
  17. max_amount = loan_issue.get_max_amount(partner)
  18. return {
  19. loan_issue.id: {
  20. "max_amount": max_amount,
  21. "face_value": loan_issue.face_value,
  22. }
  23. }
  24. else:
  25. return False
  26. def missing_mandatory_info(self):
  27. partner = request.env.user.partner_id
  28. if (
  29. not partner.bank_ids
  30. or not partner.birthdate_date
  31. or not partner.street
  32. or not partner.city
  33. or not partner.zip
  34. or not partner.country_id
  35. or not partner.gender
  36. or not partner.phone
  37. ):
  38. return False
  39. if partner.is_company:
  40. if (
  41. not partner.company_name
  42. or not partner.vat
  43. ):
  44. return False
  45. return True
  46. @http.route(
  47. ["/subscription/loan_issue_form"],
  48. type="http",
  49. auth="user",
  50. website=True,
  51. )
  52. def display_loan_issue_subscription_page(self, **kwargs):
  53. values = {}
  54. if not self.missing_mandatory_info():
  55. return request.redirect("/my/account")
  56. is_company = request.env.user.partner_id.is_company
  57. values = self.fill_values(values, is_company)
  58. values.update(kwargs=kwargs.items())
  59. return request.render(
  60. "easy_my_coop_loan_website.loanissuesubscription", values
  61. )
  62. def get_loan_issues(self, is_company):
  63. loan_obj = request.env["loan.issue"]
  64. loan_issues = loan_obj.sudo().get_web_issues(is_company)
  65. return loan_issues
  66. def fill_values(self, values, is_company):
  67. company = request.website.company_id
  68. loan_issues = self.get_loan_issues(is_company)
  69. values["loan_issues"] = loan_issues
  70. values["company"] = company
  71. if not values.get("loan_issue_id"):
  72. for loan_issue in loan_issues:
  73. if loan_issue.default_issue is True:
  74. values["loan_issue_id"] = loan_issue.id
  75. break
  76. if not values.get("loan_issue_id", False) and loan_issues:
  77. values["loan_issue_id"] = loan_issues[0].id
  78. return values
  79. def validation(self, loan_issue, kwargs):
  80. sub_amount = kwargs.get("subscription_amount")
  81. redirect = "easy_my_coop_loan_website.loanissuesubscription"
  82. values = {}
  83. if not loan_issue:
  84. values["error_msg"] = _("The selected loan issue is not found")
  85. return request.render(redirect, values)
  86. if sub_amount:
  87. values["error_msg"] = _("The amount shoud be of monetary type")
  88. return request.render(redirect, values)
  89. return True
  90. @http.route(
  91. ["/subscription/subscribe_loan_issue"],
  92. type="http",
  93. auth="user",
  94. website=True,
  95. )
  96. def loan_issue_subscription(self, **kwargs):
  97. loan_obj = request.env["loan.issue"]
  98. loan_obj_line = request.env["loan.issue.line"]
  99. loan_issue = loan_obj.sudo().browse(kwargs.get("loan_issue_id"))
  100. partner = request.env.user.partner_id
  101. if self.validation(loan_issue, kwargs):
  102. values = {
  103. "loan_issue_id": loan_issue.id,
  104. "partner_id": partner.id,
  105. "amount": kwargs["subscription_amount"],
  106. "state": "subscribed",
  107. }
  108. loan_obj_line.sudo().create(values)
  109. return request.render("easy_my_coop_website.cooperator_thanks", values)