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.

187 lines
7.2 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.fields import Date
  10. from openerp.http import request
  11. from openerp.addons.website_portal_v10.controllers.main import WebsiteAccount
  12. class CooperatorWebsiteAccount(WebsiteAccount):
  13. def _prepare_portal_layout_values(self):
  14. values = super(CooperatorWebsiteAccount,
  15. self)._prepare_portal_layout_values()
  16. # We assume that commercial_partner_id always point to the
  17. # partner itself or to the linked partner. So there is no
  18. # need to check if the partner is a "contact" or not.
  19. coop = request.env.user.partner_id.commercial_partner_id
  20. coop_bank = request.env['res.partner.bank'].sudo().search(
  21. [('partner_id', 'in', [coop.id])],
  22. limit=1
  23. )
  24. values.update({
  25. 'coop': coop,
  26. 'coop_bank': coop_bank,
  27. })
  28. return values
  29. @http.route()
  30. def account(self):
  31. """ Add Release Capital Request to main account page """
  32. response = super(CooperatorWebsiteAccount, self).account()
  33. partner = request.env.user.partner_id
  34. invoice_mgr = request.env['account.invoice']
  35. capital_request_count = invoice_mgr.sudo().search_count([
  36. ('partner_id', 'in',
  37. [partner.commercial_partner_id.id]),
  38. ('state', 'in', ['open', 'paid', 'cancelled']),
  39. # Get only the release capital request
  40. ('release_capital_request', '=', True),
  41. ])
  42. response.qcontext.update({
  43. 'capital_request_count': capital_request_count,
  44. })
  45. return response
  46. @http.route(
  47. ['/my/release_capital_request',
  48. '/my/release_capital_request/page/<int:page>'],
  49. type='http', auth="user", website=True)
  50. def portal_my_release_capital_request(self, page=1, date_begin=None,
  51. date_end=None, **kw):
  52. """Render a page with the list of release capital request.
  53. A release capital request is an invoice with a flag that tell
  54. if it's a capital request or not.
  55. """
  56. values = self._prepare_portal_layout_values()
  57. partner = request.env.user.partner_id
  58. invoice_mgr = request.env['account.invoice']
  59. domain = [
  60. ('partner_id', 'in',
  61. [partner.commercial_partner_id.id]),
  62. ('state', 'in', ['open', 'paid', 'cancelled']),
  63. # Get only the release capital request
  64. ('release_capital_request', '=', True),
  65. ]
  66. archive_groups = self._get_archive_groups_sudo('account.invoice',
  67. domain)
  68. if date_begin and date_end:
  69. domain += [('create_date', '>=', date_begin),
  70. ('create_date', '<', date_end)]
  71. # count for pager
  72. capital_request_count = invoice_mgr.sudo().search_count(domain)
  73. # pager
  74. pager = request.website.pager(
  75. url="/my/release_capital_request",
  76. url_args={'date_begin': date_begin, 'date_end': date_end},
  77. total=capital_request_count,
  78. page=page,
  79. step=self._items_per_page
  80. )
  81. # content according to pager and archive selected
  82. invoices = invoice_mgr.sudo().search(
  83. domain, limit=self._items_per_page, offset=pager['offset'])
  84. values.update({
  85. 'date': date_begin,
  86. 'capital_requests': invoices,
  87. 'page_name': 'invoice',
  88. 'pager': pager,
  89. 'archive_groups': archive_groups,
  90. 'default_url': '/my/release_capital_request',
  91. })
  92. return request.website.render(
  93. "easy_my_coop_website_portal.portal_my_capital_releases",
  94. values
  95. )
  96. @http.route(['/my/release_capital_request/pdf/<int:oid>'],
  97. type='http', auth="user", website=True)
  98. def get_release_capital_request(self, oid=-1, **kw):
  99. """Render the pdf of the given release capital request"""
  100. # Get the release capital request and raise an error if the user
  101. # is not allowed to access to it or if the object is not found.
  102. partner = request.env.user.partner_id
  103. invoice_mgr = request.env['account.invoice']
  104. capital_request = invoice_mgr.sudo().browse(oid)
  105. try:
  106. if capital_request.partner_id != partner:
  107. raise Forbidden()
  108. except AccessError:
  109. raise Forbidden()
  110. except MissingError:
  111. raise NotFound()
  112. # Get the pdf
  113. report_mgr = request.env['report']
  114. pdf = report_mgr.sudo().get_pdf(
  115. capital_request,
  116. 'easy_my_coop.theme_invoice_G002'
  117. )
  118. filename = "Release Capital Request - %d" % capital_request.id
  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']
  126. pdf = report_mgr.sudo().get_pdf(
  127. partner,
  128. 'easy_my_coop.cooperator_certificat_G001'
  129. )
  130. filename = "Cooperator Certificate - %s" % partner.name
  131. return self._render_pdf(pdf, filename)
  132. def _render_pdf(self, pdf, filename):
  133. """Render a http response for a pdf"""
  134. pdfhttpheaders = [
  135. ('Content-Disposition', 'inline; filename="%s.pdf"' % filename),
  136. ('Content-Type', 'application/pdf'),
  137. ('Content-Length', len(pdf))
  138. ]
  139. return request.make_response(pdf, headers=pdfhttpheaders)
  140. def _get_archive_groups_sudo(self, model, domain=None, fields=None,
  141. groupby="create_date",
  142. order="create_date desc"):
  143. """Same as the one from website_portal_v10 except that it runs
  144. in root.
  145. """
  146. if not model:
  147. return []
  148. if domain is None:
  149. domain = []
  150. if fields is None:
  151. fields = ['name', 'create_date']
  152. groups = []
  153. for group in request.env[model].sudo().read_group(
  154. domain, fields=fields, groupby=groupby, orderby=order):
  155. label = group[groupby]
  156. date_begin = date_end = None
  157. for leaf in group["__domain"]:
  158. if leaf[0] == groupby:
  159. if leaf[1] == ">=":
  160. date_begin = leaf[2]
  161. elif leaf[1] == "<":
  162. date_end = leaf[2]
  163. groups.append({
  164. 'date_begin': Date.to_string(Date.from_string(date_begin)),
  165. 'date_end': Date.to_string(Date.from_string(date_end)),
  166. 'name': label,
  167. 'item_count': group[groupby + '_count']
  168. })
  169. return groups