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.

154 lines
5.6 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. tax_shelters = tax_shelters.sorted(
  58. key=lambda r: r.declaration_id.fiscal_year,
  59. reverse=True
  60. )
  61. values.update({
  62. 'date': date_begin,
  63. 'tax_shelters': tax_shelters,
  64. 'page_name': 'invoice',
  65. 'pager': pager,
  66. 'default_url': '/my/tax_shelter_certificate',
  67. })
  68. return request.website.render(
  69. "easy_my_coop_website_taxshelter.portal_my_tax_shelter",
  70. values
  71. )
  72. @http.route(['/my/subscription_certificate/pdf/<int:oid>'],
  73. type='http', auth="user", website=True)
  74. def get_subscription_certificate_pdf(self, oid=-1):
  75. """Render the Subscription Certificate pdf of the given
  76. Tax Shelter Report
  77. """
  78. # Get the subscription certificate and raise an error if the user
  79. # is not allowed to access to it or if the object is not found.
  80. partner = request.env.user.partner_id
  81. tax_shelter_mgr = request.env['tax.shelter.certificate'].sudo()
  82. tax_shelter = tax_shelter_mgr.browse(oid)
  83. try:
  84. if tax_shelter.partner_id != partner:
  85. raise Forbidden()
  86. except AccessError:
  87. raise Forbidden()
  88. except MissingError:
  89. raise NotFound()
  90. # Get the pdf
  91. report_mgr = request.env['report'].sudo()
  92. pdf = report_mgr.get_pdf(
  93. tax_shelter,
  94. 'easy_my_coop_taxshelter_report.tax_shelter_subscription_report'
  95. )
  96. filename = "Subscription Certificate - %s - %s" % (
  97. partner.name,
  98. tax_shelter.declaration_id.fiscal_year
  99. )
  100. return self._render_pdf(pdf, filename)
  101. @http.route(['/my/share_certificate/pdf/<int:oid>'],
  102. type='http', auth="user", website=True)
  103. def get_share_certificate_pdf(self, oid=-1):
  104. """Render the Share Certificate pdf of the given Tax Shelter
  105. Report
  106. """
  107. # Get the share certificate and raise an error if the user
  108. # is not allowed to access to it or if the object is not found.
  109. partner = request.env.user.partner_id
  110. tax_shelter_mgr = request.env['tax.shelter.certificate'].sudo()
  111. tax_shelter = tax_shelter_mgr.browse(oid)
  112. try:
  113. if tax_shelter.partner_id != partner:
  114. raise Forbidden()
  115. except AccessError:
  116. raise Forbidden()
  117. except MissingError:
  118. raise NotFound()
  119. # Get the pdf
  120. report_mgr = request.env['report'].sudo()
  121. pdf = report_mgr.get_pdf(
  122. tax_shelter,
  123. 'easy_my_coop_taxshelter_report.tax_shelter_shares_report'
  124. )
  125. filename = "Share Certificate - %s - %s" % (
  126. partner.name,
  127. tax_shelter.declaration_id.fiscal_year
  128. )
  129. return self._render_pdf(pdf, filename)
  130. def _render_pdf(self, pdf, filename):
  131. """Render a http response for a pdf"""
  132. pdfhttpheaders = [
  133. ('Content-Disposition', 'inline; filename="%s.pdf"' % filename),
  134. ('Content-Type', 'application/pdf'),
  135. ('Content-Length', len(pdf))
  136. ]
  137. return request.make_response(pdf, headers=pdfhttpheaders)