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.

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