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.

150 lines
5.5 KiB

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