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.

152 lines
5.6 KiB

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