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.

153 lines
5.6 KiB

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