portal.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import http, _
  4. from odoo.osv import expression
  5. from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager
  6. from odoo.exceptions import AccessError, MissingError
  7. from collections import OrderedDict
  8. from odoo.http import request
  9. class PortalAccount(CustomerPortal):
  10. def _prepare_home_portal_values(self, counters):
  11. values = super()._prepare_home_portal_values(counters)
  12. if 'invoice_count' in counters:
  13. invoice_count = request.env['account.move'].search_count(self._get_invoices_domain()) \
  14. if request.env['account.move'].check_access_rights('read', raise_exception=False) else 0
  15. values['invoice_count'] = invoice_count
  16. return values
  17. # ------------------------------------------------------------
  18. # My Invoices
  19. # ------------------------------------------------------------
  20. def _invoice_get_page_view_values(self, invoice, access_token, **kwargs):
  21. values = {
  22. 'page_name': 'invoice',
  23. 'invoice': invoice,
  24. }
  25. return self._get_page_view_values(invoice, access_token, values, 'my_invoices_history', False, **kwargs)
  26. def _get_invoices_domain(self):
  27. return [('state', 'not in', ('cancel', 'draft')), ('move_type', 'in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'))]
  28. def _get_account_searchbar_sortings(self):
  29. return {
  30. 'date': {'label': _('Date'), 'order': 'invoice_date desc'},
  31. 'duedate': {'label': _('Due Date'), 'order': 'invoice_date_due desc'},
  32. 'name': {'label': _('Reference'), 'order': 'name desc'},
  33. 'state': {'label': _('Status'), 'order': 'state'},
  34. }
  35. def _get_account_searchbar_filters(self):
  36. return {
  37. 'all': {'label': _('All'), 'domain': []},
  38. 'invoices': {'label': _('Invoices'), 'domain': [('move_type', 'in', ('out_invoice', 'out_refund'))]},
  39. 'bills': {'label': _('Bills'), 'domain': [('move_type', 'in', ('in_invoice', 'in_refund'))]},
  40. }
  41. @http.route(['/my/invoices', '/my/invoices/page/<int:page>'], type='http', auth="user", website=True)
  42. def portal_my_invoices(self, page=1, date_begin=None, date_end=None, sortby=None, filterby=None, **kw):
  43. values = self._prepare_my_invoices_values(page, date_begin, date_end, sortby, filterby)
  44. # pager
  45. pager = portal_pager(**values['pager'])
  46. # content according to pager and archive selected
  47. invoices = values['invoices'](pager['offset'])
  48. request.session['my_invoices_history'] = invoices.ids[:100]
  49. values.update({
  50. 'invoices': invoices,
  51. 'pager': pager,
  52. })
  53. return request.render("account.portal_my_invoices", values)
  54. def _prepare_my_invoices_values(self, page, date_begin, date_end, sortby, filterby, domain=None, url="/my/invoices"):
  55. values = self._prepare_portal_layout_values()
  56. AccountInvoice = request.env['account.move']
  57. domain = expression.AND([
  58. domain or [],
  59. self._get_invoices_domain(),
  60. ])
  61. searchbar_sortings = self._get_account_searchbar_sortings()
  62. # default sort by order
  63. if not sortby:
  64. sortby = 'date'
  65. order = searchbar_sortings[sortby]['order']
  66. searchbar_filters = self._get_account_searchbar_filters()
  67. # default filter by value
  68. if not filterby:
  69. filterby = 'all'
  70. domain += searchbar_filters[filterby]['domain']
  71. if date_begin and date_end:
  72. domain += [('create_date', '>', date_begin), ('create_date', '<=', date_end)]
  73. values.update({
  74. 'date': date_begin,
  75. # content according to pager and archive selected
  76. # lambda function to get the invoices recordset when the pager will be defined in the main method of a route
  77. 'invoices': lambda pager_offset: AccountInvoice.search(domain, order=order, limit=self._items_per_page, offset=pager_offset),
  78. 'page_name': 'invoice',
  79. 'pager': { # vals to define the pager.
  80. "url": url,
  81. "url_args": {'date_begin': date_begin, 'date_end': date_end, 'sortby': sortby},
  82. "total": AccountInvoice.search_count(domain),
  83. "page": page,
  84. "step": self._items_per_page,
  85. },
  86. 'default_url': url,
  87. 'searchbar_sortings': searchbar_sortings,
  88. 'sortby': sortby,
  89. 'searchbar_filters': OrderedDict(sorted(searchbar_filters.items())),
  90. 'filterby': filterby,
  91. })
  92. return values
  93. @http.route(['/my/invoices/<int:invoice_id>'], type='http', auth="public", website=True)
  94. def portal_my_invoice_detail(self, invoice_id, access_token=None, report_type=None, download=False, **kw):
  95. try:
  96. invoice_sudo = self._document_check_access('account.move', invoice_id, access_token)
  97. except (AccessError, MissingError):
  98. return request.redirect('/my')
  99. if report_type in ('html', 'pdf', 'text'):
  100. return self._show_report(model=invoice_sudo, report_type=report_type, report_ref='account.account_invoices', download=download)
  101. values = self._invoice_get_page_view_values(invoice_sudo, access_token, **kw)
  102. return request.render("account.portal_invoice_page", values)
  103. # ------------------------------------------------------------
  104. # My Home
  105. # ------------------------------------------------------------
  106. def details_form_validate(self, data, partner_creation=False):
  107. error, error_message = super(PortalAccount, self).details_form_validate(data)
  108. # prevent VAT/name change if invoices exist
  109. partner = request.env['res.users'].browse(request.uid).partner_id
  110. # Skip this test if we're creating a new partner as we won't ever block him from filling values.
  111. if not partner_creation and not partner.can_edit_vat():
  112. if 'vat' in data and (data['vat'] or False) != (partner.vat or False):
  113. error['vat'] = 'error'
  114. error_message.append(_('Changing VAT number is not allowed once invoices have been issued for your account. Please contact us directly for this operation.'))
  115. if 'name' in data and (data['name'] or False) != (partner.name or False):
  116. error['name'] = 'error'
  117. error_message.append(_('Changing your name is not allowed once invoices have been issued for your account. Please contact us directly for this operation.'))
  118. if 'company_name' in data and (data['company_name'] or False) != (partner.company_name or False):
  119. error['company_name'] = 'error'
  120. error_message.append(_('Changing your company name is not allowed once invoices have been issued for your account. Please contact us directly for this operation.'))
  121. return error, error_message
  122. def extra_details_form_validate(self, data, additional_required_fields, error, error_message):
  123. """ Ensure that all additional required fields have a value in the data """
  124. for field in additional_required_fields:
  125. if field.name not in data or not data[field.name]:
  126. error[field.name] = 'error'
  127. error_message.append(_('The field %s must be filled.', field.field_description.lower()))
  128. return error, error_message