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
6.0 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. def _prepare_portal_layout_values(self):
  13. values = super(CooperatorWebsiteAccount,
  14. self)._prepare_portal_layout_values()
  15. # We assume that commercial_partner_id always point to the
  16. # partner itself or to the linked partner. So there is no
  17. # need to check if the partner is a "contact" or not.
  18. coop = request.env.user.partner_id.commercial_partner_id
  19. coop_bank = request.env['res.partner.bank'].sudo().search(
  20. [('partner_id', 'in', [coop.id])],
  21. limit=1
  22. )
  23. values.update({
  24. 'coop': coop,
  25. 'coop_bank': coop_bank,
  26. })
  27. return values
  28. @http.route()
  29. def account(self):
  30. """ Add Release Capital Request to main account page """
  31. response = super(CooperatorWebsiteAccount, self).account()
  32. partner = request.env.user.partner_id
  33. invoice_mgr = request.env['account.invoice']
  34. capital_request_count = invoice_mgr.search_count([
  35. ('message_partner_ids', 'in',
  36. [partner.commercial_partner_id.id]),
  37. ('state', 'in', ['open', 'paid', 'cancelled']),
  38. # Get only the release capital request
  39. ('release_capital_request', '=', True),
  40. ])
  41. response.qcontext.update({
  42. 'capital_request_count': capital_request_count,
  43. })
  44. return response
  45. @http.route(
  46. ['/my/release_capital_request',
  47. '/my/release_capital_request/page/<int:page>'],
  48. type='http', auth="user", website=True)
  49. def portal_my_release_capital_request(self, page=1, date_begin=None,
  50. date_end=None, **kw):
  51. """Render a page with the list of release capital request.
  52. A release capital request is an invoice with a flag that tell
  53. if it's a capital request or not.
  54. """
  55. values = self._prepare_portal_layout_values()
  56. partner = request.env.user.partner_id
  57. invoice_mgr = request.env['account.invoice']
  58. domain = [
  59. ('message_partner_ids', 'in',
  60. [partner.commercial_partner_id.id]),
  61. ('state', 'in', ['open', 'paid', 'cancelled']),
  62. # Get only the release capital request
  63. ('release_capital_request', '=', True),
  64. ]
  65. archive_groups = self._get_archive_groups('account.invoice', domain)
  66. if date_begin and date_end:
  67. domain += [('create_date', '>=', date_begin),
  68. ('create_date', '<', date_end)]
  69. # count for pager
  70. capital_request_count = invoice_mgr.search_count(domain)
  71. # pager
  72. pager = request.website.pager(
  73. url="/my/release_capital_request",
  74. url_args={'date_begin': date_begin, 'date_end': date_end},
  75. total=capital_request_count,
  76. page=page,
  77. step=self._items_per_page
  78. )
  79. # content according to pager and archive selected
  80. invoices = invoice_mgr.search(
  81. domain, limit=self._items_per_page, offset=pager['offset'])
  82. values.update({
  83. 'date': date_begin,
  84. 'capital_requests': invoices,
  85. 'page_name': 'invoice',
  86. 'pager': pager,
  87. 'archive_groups': archive_groups,
  88. 'default_url': '/my/release_capital_request',
  89. })
  90. return request.website.render(
  91. "easy_my_coop_website_portal.portal_my_capital_releases",
  92. values
  93. )
  94. @http.route(['/my/release_capital_request/pdf/<int:oid>'],
  95. type='http', auth="user", website=True)
  96. def get_release_capital_request(self, oid=-1, **kw):
  97. """Render the pdf of the given release capital request"""
  98. # Get the release capital request and raise an error if the user
  99. # is not allowed to access to it or if the object is not found.
  100. partner = request.env.user.partner_id
  101. invoice_mgr = request.env['account.invoice']
  102. capital_request = invoice_mgr.sudo().browse(oid)
  103. try:
  104. if capital_request.partner_id != partner:
  105. raise Forbidden()
  106. except AccessError:
  107. raise Forbidden()
  108. except MissingError:
  109. raise NotFound()
  110. # Get the pdf
  111. report_mgr = request.env['report'].sudo()
  112. pdf = report_mgr.get_pdf(
  113. capital_request.ids,
  114. 'easy_my_coop.theme_invoice_G002'
  115. )
  116. filename = "Release Capital Request - {oid}".format(
  117. oid=capital_request.id
  118. )
  119. return self._render_pdf(pdf, filename)
  120. @http.route(['/my/cooperator_certificate/pdf'],
  121. type='http', auth="user", website=True)
  122. def get_cooperator_certificat(self, **kw):
  123. """Render the cooperator certificate pdf of the current user"""
  124. partner = request.env.user.partner_id
  125. report_mgr = request.env['report'].sudo()
  126. pdf = report_mgr.get_pdf(
  127. partner.ids,
  128. 'easy_my_coop.cooperator_certificat_G001'
  129. )
  130. filename = "Cooperator Certificate - {name}".format(
  131. name=partner.name
  132. )
  133. return self._render_pdf(pdf, filename)
  134. def _render_pdf(self, pdf, filename):
  135. """Render a http response for a pdf"""
  136. pdfhttpheaders = [
  137. ('Content-Disposition', 'inline; filename="%s.pdf"' % filename),
  138. ('Content-Type', 'application/pdf'),
  139. ('Content-Length', len(pdf))
  140. ]
  141. return request.make_response(pdf, headers=pdfhttpheaders)