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.

103 lines
3.4 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. @http.route(
  27. ["/subscription/loan_issue_form"],
  28. type="http",
  29. auth="user",
  30. website=True,
  31. )
  32. def display_loan_issue_subscription_page(self, **kwargs):
  33. values = {}
  34. partner = request.env.user.partner_id
  35. is_company = partner.is_company
  36. values = self.fill_values(values, is_company)
  37. values.update(kwargs=kwargs.items())
  38. return request.render(
  39. "easy_my_coop_loan_website.loanissuesubscription", values
  40. )
  41. def get_loan_issues(self, is_company):
  42. loan_obj = request.env["loan.issue"]
  43. loan_issues = loan_obj.sudo().get_web_issues(is_company)
  44. return loan_issues
  45. def fill_values(self, values, is_company):
  46. company = request.website.company_id
  47. loan_issues = self.get_loan_issues(is_company)
  48. values["loan_issues"] = loan_issues
  49. values["company"] = company
  50. if not values.get("loan_issue_id"):
  51. for loan_issue in loan_issues:
  52. if loan_issue.default_issue is True:
  53. values["loan_issue_id"] = loan_issue.id
  54. break
  55. if not values.get("loan_issue_id", False) and loan_issues:
  56. values["loan_issue_id"] = loan_issues[0].id
  57. return values
  58. def validation(self, loan_issue, kwargs):
  59. sub_amount = kwargs.get("subscription_amount")
  60. redirect = "easy_my_coop_loan_website.loanissuesubscription"
  61. values = {}
  62. if not loan_issue:
  63. values["error_msg"] = _("The selected loan issue is not found")
  64. return request.render(redirect, values)
  65. if sub_amount:
  66. values["error_msg"] = _("The amount shoud be of monetary type")
  67. return request.render(redirect, values)
  68. return True
  69. @http.route(
  70. ["/subscription/subscribe_loan_issue"],
  71. type="http",
  72. auth="user",
  73. website=True,
  74. )
  75. def loan_issue_subscription(self, **kwargs):
  76. loan_obj = request.env["loan.issue"]
  77. loan_obj_line = request.env["loan.issue.line"]
  78. loan_issue = loan_obj.sudo().browse(kwargs.get("loan_issue_id"))
  79. partner = request.env.user.partner_id
  80. if self.validation(loan_issue, kwargs):
  81. values = {
  82. "loan_issue_id": loan_issue.id,
  83. "partner_id": partner.id,
  84. "amount": kwargs["subscription_amount"],
  85. "state": "subscribed",
  86. }
  87. loan_obj_line.sudo().create(values)
  88. return request.render("easy_my_coop_website.cooperator_thanks", values)