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.

238 lines
9.8 KiB

  1. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # Copyright 2017-2018 Rémy Taymans <remy@coopiteasy.be>
  3. # Copyright 2019 Houssine Bakkali <houssine@coopiteasy.be>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo.exceptions import AccessError, MissingError
  6. from odoo.fields import Date
  7. from odoo.http import request, route
  8. from odoo import _
  9. from odoo.addons.portal.controllers.portal import CustomerPortal
  10. from odoo.addons.portal.controllers.portal import pager as portal_pager
  11. from odoo.addons.payment.controllers.portal import PaymentProcessing
  12. class CooperatorPortalAccount(CustomerPortal):
  13. CustomerPortal.MANDATORY_BILLING_FIELDS.extend(["iban",
  14. "birthdate_date",
  15. "gender"])
  16. def _prepare_portal_layout_values(self):
  17. values = super(CooperatorPortalAccount,
  18. self)._prepare_portal_layout_values()
  19. # We assume that commercial_partner_id always point to the
  20. # partner itself or to the linked partner. So there is no
  21. # need to check if the partner is a "contact" or not.
  22. partner = request.env.user.partner_id
  23. coop = partner.commercial_partner_id
  24. partner_obj = request.env['res.partner']
  25. coop_bank = request.env['res.partner.bank'].sudo().search(
  26. [('partner_id', 'in', [coop.id])],
  27. limit=1
  28. )
  29. invoice_mgr = request.env['account.invoice']
  30. capital_request_count = invoice_mgr.search_count([
  31. ('state', 'in', ['open', 'paid', 'cancelled']),
  32. # Get only the release capital request
  33. ('release_capital_request', '=', True),
  34. ])
  35. invoice_count = invoice_mgr.search_count([
  36. ('release_capital_request', '=', False)
  37. ])
  38. iban = ''
  39. if partner.bank_ids:
  40. iban = partner.bank_ids[0].acc_number
  41. fields_desc = partner_obj.sudo().fields_get(['gender'])
  42. values.update({
  43. 'coop': coop,
  44. 'coop_bank': coop_bank,
  45. 'capital_request_count': capital_request_count,
  46. 'invoice_count': invoice_count,
  47. 'iban': iban,
  48. 'genders': fields_desc['gender']['selection']
  49. })
  50. return values
  51. def details_form_validate(self, data):
  52. error, error_message = super(CooperatorPortalAccount,
  53. self).details_form_validate(data)
  54. sub_req_obj = request.env['subscription.request']
  55. iban = data.get("iban")
  56. valid = sub_req_obj.check_iban(iban)
  57. if not valid:
  58. error['iban'] = 'error'
  59. error_message.append(_("You iban account number is not valid"))
  60. return error, error_message
  61. @route(['/my/account'], type='http', auth='user', website=True)
  62. def account(self, redirect=None, **post):
  63. res = super(CooperatorPortalAccount, self).account(
  64. redirect, **post)
  65. if not res.qcontext.get('error'):
  66. partner = request.env.user.partner_id
  67. partner_bank = request.env['res.partner.bank']
  68. iban = post.get('iban')
  69. if iban:
  70. if partner.bank_ids:
  71. bank_account = partner.bank_ids[0]
  72. bank_account.acc_number = iban
  73. else:
  74. partner_bank.sudo().create({
  75. 'partner_id': partner.id,
  76. 'acc_number': iban
  77. })
  78. return res
  79. @route(['/my/invoices', '/my/invoices/page/<int:page>'], type='http',
  80. auth="user", website=True)
  81. def portal_my_invoices(self, page=1, date_begin=None, date_end=None,
  82. sortby=None, **kw):
  83. res = super(CooperatorPortalAccount, self).portal_my_invoices(
  84. page, date_begin, date_end, sortby, **kw)
  85. invoice_obj = request.env['account.invoice']
  86. qcontext = res.qcontext
  87. if qcontext:
  88. invoices = invoice_obj.search([('release_capital_request', '=', False)])
  89. invoice_count = len(invoices)
  90. qcontext['invoices'] = invoices
  91. qcontext['pager']['invoice_count'] = invoice_count
  92. return res
  93. @route(
  94. ['/my/release_capital_request',
  95. '/my/release_capital_request/page/<int:page>'],
  96. type='http', auth="user", website=True)
  97. def portal_my_release_capital_request(self, page=1, date_begin=None,
  98. date_end=None, sortby=None, **kw):
  99. """Render a page with the list of release capital request.
  100. A release capital request is an invoice with a flag that tell
  101. if it's a capital request or not.
  102. """
  103. values = self._prepare_portal_layout_values()
  104. partner = request.env.user.partner_id
  105. invoice_mgr = request.env['account.invoice']
  106. domain = [
  107. ('partner_id', 'in',
  108. [partner.commercial_partner_id.id]),
  109. ('state', 'in', ['open', 'paid', 'cancelled']),
  110. # Get only the release capital request
  111. ('release_capital_request', '=', True),
  112. ]
  113. archive_groups = self._get_archive_groups_sudo('account.invoice',
  114. domain)
  115. if date_begin and date_end:
  116. domain += [('create_date', '>=', date_begin),
  117. ('create_date', '<', date_end)]
  118. # count for pager
  119. capital_request_count = invoice_mgr.sudo().search_count(domain)
  120. # pager
  121. pager = portal_pager(
  122. url="/my/release_capital_request",
  123. url_args={'date_begin': date_begin, 'date_end': date_end,
  124. 'sortby': sortby},
  125. total=capital_request_count,
  126. page=page,
  127. step=self._items_per_page
  128. )
  129. # content according to pager and archive selected
  130. invoices = invoice_mgr.sudo().search(
  131. domain, limit=self._items_per_page, offset=pager['offset'])
  132. values.update({
  133. 'date': date_begin,
  134. 'capital_requests': invoices,
  135. 'page_name': 'Release request',
  136. 'pager': pager,
  137. 'archive_groups': archive_groups,
  138. 'default_url': '/my/release_capital_request',
  139. })
  140. return request.render(
  141. "easy_my_coop_website_portal.portal_my_capital_releases",
  142. values
  143. )
  144. @route(['/my/invoices/<int:invoice_id>'],
  145. type='http', auth="public", website=True)
  146. def portal_my_invoice_detail(self, invoice_id, access_token=None,
  147. report_type=None, download=False, **kw):
  148. # override in order to not retrieve release capital request as invoices
  149. try:
  150. invoice_sudo = self._document_check_access('account.invoice',
  151. invoice_id,
  152. access_token)
  153. except (AccessError, MissingError):
  154. return request.redirect('/my')
  155. if invoice_sudo.release_capital_request:
  156. report_ref = 'easy_my_coop.action_cooperator_invoices'
  157. else:
  158. report_ref = 'account.account_invoices'
  159. if report_type in ('html', 'pdf', 'text'):
  160. return self._show_report(model=invoice_sudo,
  161. report_type=report_type,
  162. report_ref=report_ref,
  163. download=download)
  164. values = self._invoice_get_page_view_values(invoice_sudo, access_token,
  165. **kw)
  166. PaymentProcessing.remove_payment_transaction(invoice_sudo.transaction_ids)
  167. return request.render("account.portal_invoice_page", values)
  168. @route(['/my/cooperator_certificate/pdf'],
  169. type='http', auth="user", website=True)
  170. def get_cooperator_certificat(self, **kw):
  171. """Render the cooperator certificate pdf of the current user"""
  172. partner = request.env.user.partner_id
  173. return self._show_report(
  174. model=partner,
  175. report_type='pdf',
  176. report_ref='easy_my_coop.action_cooperator_report_certificat',
  177. download=True
  178. )
  179. def _render_pdf(self, pdf, filename):
  180. """Render a http response for a pdf"""
  181. pdfhttpheaders = [
  182. ('Content-Disposition', 'inline; filename="%s.pdf"' % filename),
  183. ('Content-Type', 'application/pdf'),
  184. ('Content-Length', len(pdf))
  185. ]
  186. return request.make_response(pdf, headers=pdfhttpheaders)
  187. def _get_archive_groups_sudo(self, model, domain=None, fields=None,
  188. groupby="create_date",
  189. order="create_date desc"):
  190. """Same as the one from website_portal_v10 except that it runs
  191. in root.
  192. """
  193. if not model:
  194. return []
  195. if domain is None:
  196. domain = []
  197. if fields is None:
  198. fields = ['name', 'create_date']
  199. groups = []
  200. for group in request.env[model].sudo().read_group(
  201. domain, fields=fields, groupby=groupby, orderby=order):
  202. label = group[groupby]
  203. date_begin = date_end = None
  204. for leaf in group["__domain"]:
  205. if leaf[0] == groupby:
  206. if leaf[1] == ">=":
  207. date_begin = leaf[2]
  208. elif leaf[1] == "<":
  209. date_end = leaf[2]
  210. groups.append({
  211. 'date_begin': Date.to_string(Date.from_string(date_begin)),
  212. 'date_end': Date.to_string(Date.from_string(date_end)),
  213. 'name': label,
  214. 'item_count': group[groupby + '_count']
  215. })
  216. return groups