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.

158 lines
5.6 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 openerp import http
  4. from openerp.addons.website_portal_v10.controllers.main import WebsiteAccount
  5. from openerp.exceptions import AccessError, MissingError
  6. from openerp.http import request
  7. from werkzeug.exceptions import Forbidden, NotFound
  8. class TaxShelterWebsiteAccount(WebsiteAccount):
  9. @http.route()
  10. def account(self):
  11. """ Add Tax Shelter Certificate to main account page """
  12. response = super(TaxShelterWebsiteAccount, self).account()
  13. partner = request.env.user.partner_id
  14. tax_shelter_mgr = request.env["tax.shelter.certificate"]
  15. tax_shelter_count = tax_shelter_mgr.sudo().search_count(
  16. [("partner_id", "in", [partner.commercial_partner_id.id])]
  17. )
  18. response.qcontext.update({"tax_shelter_count": tax_shelter_count})
  19. return response
  20. @http.route(
  21. [
  22. "/my/tax_shelter_certificate",
  23. "/my/tax_shelter_certificate/page/<int:page>",
  24. ],
  25. type="http",
  26. auth="user",
  27. website=True,
  28. )
  29. def portal_my_tax_shelter_certificate(
  30. self, page=1, date_begin=None, date_end=None, **kw
  31. ):
  32. """Render a page that lits the tax shelter report:
  33. * Tax Shelter Certificates
  34. * Shares Certifcates
  35. """
  36. values = self._prepare_portal_layout_values()
  37. partner = request.env.user.partner_id
  38. tax_shelter_mgr = request.env["tax.shelter.certificate"]
  39. domain = [("partner_id", "in", [partner.commercial_partner_id.id])]
  40. if date_begin and date_end:
  41. domain += [
  42. ("create_date", ">=", date_begin),
  43. ("create_date", "<", date_end),
  44. ]
  45. # count for pager
  46. tax_shelter_count = tax_shelter_mgr.sudo().search_count(domain)
  47. # pager
  48. pager = request.website.pager(
  49. url="/my/tax_shelter_certificate",
  50. url_args={"date_begin": date_begin, "date_end": date_end},
  51. total=tax_shelter_count,
  52. page=page,
  53. step=self._items_per_page,
  54. )
  55. # content according to pager and archive selected
  56. tax_shelters = tax_shelter_mgr.sudo().search(
  57. domain, limit=self._items_per_page, offset=pager["offset"]
  58. )
  59. tax_shelters = tax_shelters.sorted(
  60. key=lambda r: r.declaration_id.fiscal_year, reverse=True
  61. )
  62. values.update(
  63. {
  64. "date": date_begin,
  65. "tax_shelters": tax_shelters,
  66. "page_name": "invoice",
  67. "pager": pager,
  68. "default_url": "/my/tax_shelter_certificate",
  69. }
  70. )
  71. return request.website.render(
  72. "easy_my_coop_website_taxshelter.portal_my_tax_shelter", values
  73. )
  74. @http.route(
  75. ["/my/taxshelter_certificate/pdf/<int:oid>"],
  76. type="http",
  77. auth="user",
  78. website=True,
  79. )
  80. def get_taxshelter_certificate_pdf(self, oid=-1):
  81. """Render the Tax Shelter Certificate pdf of the given Tax
  82. Shelter Report
  83. """
  84. # Get the subscription certificate and raise an error if the user
  85. # is not allowed to access to it or if the object is not found.
  86. partner = request.env.user.partner_id
  87. tax_shelter_mgr = request.env["tax.shelter.certificate"]
  88. tax_shelter = tax_shelter_mgr.sudo().browse(oid)
  89. try:
  90. if tax_shelter.partner_id != partner:
  91. raise Forbidden()
  92. except AccessError:
  93. raise Forbidden()
  94. except MissingError:
  95. raise NotFound()
  96. # Get the pdf
  97. report_mgr = request.env["report"]
  98. pdf = report_mgr.sudo().get_pdf(
  99. tax_shelter,
  100. "easy_my_coop_taxshelter_report.tax_shelter_subscription_report",
  101. )
  102. filename = "Tax Shelter Certificate - {} - {}".format(
  103. partner.name, tax_shelter.declaration_id.fiscal_year
  104. )
  105. return self._render_pdf(pdf, filename)
  106. @http.route(
  107. ["/my/share_certificate/pdf/<int:oid>"],
  108. type="http",
  109. auth="user",
  110. website=True,
  111. )
  112. def get_share_certificate_pdf(self, oid=-1):
  113. """Render the Share Certificate pdf of the given Tax Shelter
  114. Report
  115. """
  116. # Get the share certificate and raise an error if the user
  117. # is not allowed to access to it or if the object is not found.
  118. partner = request.env.user.partner_id
  119. tax_shelter_mgr = request.env["tax.shelter.certificate"]
  120. tax_shelter = tax_shelter_mgr.sudo().browse(oid)
  121. try:
  122. if tax_shelter.partner_id != partner:
  123. raise Forbidden()
  124. except AccessError:
  125. raise Forbidden()
  126. except MissingError:
  127. raise NotFound()
  128. # Get the pdf
  129. report_mgr = request.env["report"]
  130. pdf = report_mgr.sudo().get_pdf(
  131. tax_shelter,
  132. "easy_my_coop_taxshelter_report.tax_shelter_shares_report",
  133. )
  134. filename = "Share Certificate - {} - {}".format(
  135. partner.name, tax_shelter.declaration_id.fiscal_year
  136. )
  137. return self._render_pdf(pdf, filename)
  138. def _render_pdf(self, pdf, filename):
  139. """Render a http response for a pdf"""
  140. pdfhttpheaders = [
  141. ("Content-Disposition", 'inline; filename="%s.pdf"' % filename),
  142. ("Content-Type", "application/pdf"),
  143. ("Content-Length", len(pdf)),
  144. ]
  145. return request.make_response(pdf, headers=pdfhttpheaders)