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.

297 lines
9.9 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. # fmt: off
  179. def portal_my_invoice_detail(
  180. self,
  181. invoice_id,
  182. access_token=None,
  183. report_type=None,
  184. download=False,
  185. **kw
  186. ):
  187. # fmt: on
  188. # override in order to not retrieve release capital request as invoices
  189. try:
  190. invoice_sudo = self._document_check_access(
  191. "account.invoice", invoice_id, access_token
  192. )
  193. except (AccessError, MissingError):
  194. return request.redirect("/my")
  195. if invoice_sudo.release_capital_request:
  196. report_ref = "easy_my_coop.action_cooperator_invoices"
  197. else:
  198. report_ref = "account.account_invoices"
  199. if report_type in ("html", "pdf", "text"):
  200. return self._show_report(
  201. model=invoice_sudo,
  202. report_type=report_type,
  203. report_ref=report_ref,
  204. download=download,
  205. )
  206. values = self._invoice_get_page_view_values(
  207. invoice_sudo, access_token, **kw
  208. )
  209. PaymentProcessing.remove_payment_transaction(
  210. invoice_sudo.transaction_ids
  211. )
  212. return request.render("account.portal_invoice_page", values)
  213. @route(
  214. ["/my/cooperator_certificate/pdf"],
  215. type="http",
  216. auth="user",
  217. website=True,
  218. )
  219. def get_cooperator_certificat(self, **kw):
  220. """Render the cooperator certificate pdf of the current user"""
  221. partner = request.env.user.partner_id
  222. return self._show_report(
  223. model=partner,
  224. report_type="pdf",
  225. report_ref="easy_my_coop.action_cooperator_report_certificat",
  226. download=True,
  227. )
  228. def _render_pdf(self, pdf, filename):
  229. """Render a http response for a pdf"""
  230. pdfhttpheaders = [
  231. ("Content-Disposition", 'inline; filename="%s.pdf"' % filename),
  232. ("Content-Type", "application/pdf"),
  233. ("Content-Length", len(pdf)),
  234. ]
  235. return request.make_response(pdf, headers=pdfhttpheaders)
  236. def _get_archive_groups_sudo(
  237. self,
  238. model,
  239. domain=None,
  240. fields=None,
  241. groupby="create_date",
  242. order="create_date desc",
  243. ):
  244. """Same as the one from website_portal_v10 except that it runs
  245. in root.
  246. """
  247. if not model:
  248. return []
  249. if domain is None:
  250. domain = []
  251. if fields is None:
  252. fields = ["name", "create_date"]
  253. groups = []
  254. for group in (
  255. request.env[model]
  256. .sudo()
  257. .read_group(domain, fields=fields, groupby=groupby, orderby=order)
  258. ):
  259. label = group[groupby]
  260. date_begin = date_end = None
  261. for leaf in group["__domain"]:
  262. if leaf[0] == groupby:
  263. if leaf[1] == ">=":
  264. date_begin = leaf[2]
  265. elif leaf[1] == "<":
  266. date_end = leaf[2]
  267. groups.append(
  268. {
  269. "date_begin": Date.to_string(Date.from_string(date_begin)),
  270. "date_end": Date.to_string(Date.from_string(date_end)),
  271. "name": label,
  272. "item_count": group[groupby + "_count"],
  273. }
  274. )
  275. return groups