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.

159 lines
5.1 KiB

6 years ago
  1. # Copyright 2017-2018 Coop IT Easy SCRLfs <remy@gcoopiteasy.be>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from werkzeug.exceptions import Forbidden
  4. from odoo import http
  5. from odoo.exceptions import AccessError, MissingError
  6. from odoo.http import request
  7. from odoo.addons.portal.controllers.portal import (
  8. CustomerPortal,
  9. pager as portal_pager,
  10. )
  11. class PortalTaxShelter(CustomerPortal):
  12. def _prepare_portal_layout_values(self):
  13. values = super()._prepare_portal_layout_values()
  14. partner = request.env.user.partner_id
  15. tax_shelter_count = (
  16. request.env["tax.shelter.certificate"]
  17. .sudo()
  18. .search_count(
  19. [("partner_id", "in", [partner.commercial_partner_id.id])]
  20. )
  21. )
  22. values["tax_shelter_count"] = tax_shelter_count
  23. return values
  24. def _taxshelter_certificate_get_page_view_values(
  25. self, taxshelter_certificate, access_token, **kwargs
  26. ):
  27. values = {
  28. "page_name": "taxshelter",
  29. "taxshelter": taxshelter_certificate,
  30. }
  31. return self._get_page_view_values(
  32. taxshelter_certificate,
  33. access_token,
  34. values,
  35. "my_taxshelter_certificates_history",
  36. False,
  37. **kwargs,
  38. )
  39. @http.route(
  40. [
  41. "/my/tax_shelter_certificates",
  42. "/my/tax_shelter_certificates/page/<int:page>",
  43. ],
  44. type="http",
  45. auth="user",
  46. website=True,
  47. )
  48. def portal_my_tax_shelter_certificates(
  49. self, page=1, date_begin=None, date_end=None, **kw
  50. ):
  51. """Render a page that lits the tax shelter report:
  52. * Tax Shelter Certificates
  53. * Shares Certifcates
  54. """
  55. values = self._prepare_portal_layout_values()
  56. TaxShelterCertificate = request.env["tax.shelter.certificate"]
  57. partner = request.env.user.partner_id
  58. domain = [("partner_id", "in", [partner.commercial_partner_id.id])]
  59. if date_begin and date_end:
  60. domain += [
  61. ("create_date", ">=", date_begin),
  62. ("create_date", "<", date_end),
  63. ]
  64. # count for pager
  65. tax_shelter_count = TaxShelterCertificate.sudo().search_count(domain)
  66. # pager
  67. pager = portal_pager(
  68. url="/my/tax_shelter_certificates",
  69. url_args={"date_begin": date_begin, "date_end": date_end},
  70. total=tax_shelter_count,
  71. page=page,
  72. step=self._items_per_page,
  73. )
  74. # content according to pager and archive selected
  75. tax_shelters = TaxShelterCertificate.sudo().search(
  76. domain, limit=self._items_per_page, offset=pager["offset"]
  77. )
  78. tax_shelters = tax_shelters.sorted(
  79. key=lambda r: r.declaration_id.fiscal_year, reverse=True
  80. )
  81. request.session[
  82. "my_taxshelter_certificates_history"
  83. ] = tax_shelters.ids[:100]
  84. values.update(
  85. {
  86. "date": date_begin,
  87. "tax_shelters": tax_shelters,
  88. "page_name": "taxshelter",
  89. "pager": pager,
  90. "default_url": "/my/tax_shelter_certificates",
  91. }
  92. )
  93. return request.render(
  94. "easy_my_coop_website_taxshelter.portal_my_tax_shelter", values
  95. )
  96. # Black adds a trailing comma after last argument of function definition
  97. # this syntax is invalid for python < 3.6
  98. # Exclude for formatting while not fixed, follow status here:
  99. # https://github.com/psf/black/issues/1657
  100. # fmt: off
  101. @http.route(
  102. ["/my/tax_shelter_certificates/<int:certificate_id>"],
  103. type="http",
  104. auth="public",
  105. website=True,
  106. )
  107. def portal_taxshelter_certificate(
  108. self,
  109. certificate_id,
  110. access_token=None,
  111. report_type=None,
  112. download=False,
  113. query_string=None,
  114. **kw
  115. ):
  116. # fmt: on
  117. partner = request.env.user.partner_id
  118. try:
  119. taxshelter_certificate_sudo = self._document_check_access(
  120. "tax.shelter.certificate", certificate_id, access_token
  121. )
  122. if taxshelter_certificate_sudo.partner_id != partner:
  123. raise Forbidden()
  124. except (AccessError, MissingError):
  125. return request.redirect("/my")
  126. if report_type in ("html", "pdf", "text") and query_string in (
  127. "subscription",
  128. "shares",
  129. ):
  130. report_ref = (
  131. "easy_my_coop_taxshelter_report.action_tax_shelter_%s_report"
  132. % (query_string)
  133. )
  134. return self._show_report(
  135. model=taxshelter_certificate_sudo,
  136. report_type=report_type,
  137. report_ref=report_ref,
  138. download=download,
  139. )
  140. values = self._taxshelter_certificate_get_page_view_values(
  141. taxshelter_certificate_sudo, access_token, **kw
  142. )
  143. return request.render(
  144. "easy_my_coop_website_taxshelter.portal_taxshelter_page", values
  145. )