portal.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from werkzeug.exceptions import NotFound
  4. from odoo import http, _
  5. from odoo.exceptions import AccessError, MissingError
  6. from odoo.http import request
  7. from odoo.osv import expression
  8. from odoo.addons.account.controllers.portal import PortalAccount
  9. from odoo.addons.hr_timesheet.controllers.portal import TimesheetCustomerPortal
  10. from odoo.addons.portal.controllers.portal import pager as portal_pager
  11. from odoo.addons.project.controllers.portal import ProjectCustomerPortal
  12. class PortalProjectAccount(PortalAccount, ProjectCustomerPortal):
  13. def _invoice_get_page_view_values(self, invoice, access_token, **kwargs):
  14. values = super()._invoice_get_page_view_values(invoice, access_token, **kwargs)
  15. domain = request.env['account.analytic.line']._timesheet_get_portal_domain()
  16. domain = expression.AND([
  17. domain,
  18. request.env['account.analytic.line']._timesheet_get_sale_domain(
  19. invoice.mapped('line_ids.sale_line_ids'),
  20. request.env['account.move'].browse([invoice.id])
  21. )
  22. ])
  23. values['timesheets'] = request.env['account.analytic.line'].sudo().search(domain)
  24. values['is_uom_day'] = request.env['account.analytic.line'].sudo()._is_timesheet_encode_uom_day()
  25. return values
  26. @http.route([
  27. '/my/tasks/<task_id>/orders/invoices',
  28. '/my/tasks/<task_id>/orders/invoices/page/<int:page>'],
  29. type='http', auth="user", website=True)
  30. def portal_my_tasks_invoices(self, task_id=None, page=1, date_begin=None, date_end=None, sortby=None, filterby=None, **kw):
  31. task = request.env['project.task'].search([('id', '=', task_id)])
  32. if not task:
  33. return NotFound()
  34. domain = [('id', 'in', task.sale_order_id.invoice_ids.ids)]
  35. values = self._prepare_my_invoices_values(page, date_begin, date_end, sortby, filterby, domain=domain)
  36. # pager
  37. pager = portal_pager(**values['pager'])
  38. # content according to pager and archive selected
  39. invoices = values['invoices'](pager['offset'])
  40. request.session['my_invoices_history'] = invoices.ids[:100]
  41. values.update({
  42. 'invoices': invoices,
  43. 'pager': pager,
  44. })
  45. return request.render("account.portal_my_invoices", values)
  46. class SaleTimesheetCustomerPortal(TimesheetCustomerPortal):
  47. def _get_searchbar_inputs(self):
  48. searchbar_inputs = super()._get_searchbar_inputs()
  49. searchbar_inputs.update(
  50. so={'input': 'so', 'label': _('Search in Sales Order')},
  51. sol={'input': 'sol', 'label': _('Search in Sales Order Item')},
  52. invoice={'input': 'invoice', 'label': _('Search in Invoice')})
  53. return searchbar_inputs
  54. def _get_searchbar_groupby(self):
  55. searchbar_groupby = super()._get_searchbar_groupby()
  56. searchbar_groupby.update(
  57. so={'input': 'so', 'label': _('Sales Order')},
  58. sol={'input': 'sol', 'label': _('Sales Order Item')},
  59. invoice={'input': 'invoice', 'label': _('Invoice')})
  60. return searchbar_groupby
  61. def _get_search_domain(self, search_in, search):
  62. search_domain = super()._get_search_domain(search_in, search)
  63. if search_in in ('sol', 'all'):
  64. search_domain = expression.OR([search_domain, [('so_line', 'ilike', search)]])
  65. if search_in in ('so', 'all'):
  66. search_domain = expression.OR([search_domain, [('so_line.order_id.name', 'ilike', search)]])
  67. if search_in in ('invoice', 'all'):
  68. invoices = request.env['account.move'].sudo().search([('name', 'ilike', search)])
  69. domain = request.env['account.analytic.line']._timesheet_get_sale_domain(invoices.mapped('invoice_line_ids.sale_line_ids'), invoices)
  70. search_domain = expression.OR([search_domain, domain])
  71. return search_domain
  72. def _get_groupby_mapping(self):
  73. groupby_mapping = super()._get_groupby_mapping()
  74. groupby_mapping.update(
  75. sol='so_line',
  76. so='order_id',
  77. invoice='timesheet_invoice_id')
  78. return groupby_mapping
  79. def _get_searchbar_sortings(self):
  80. searchbar_sortings = super()._get_searchbar_sortings()
  81. searchbar_sortings.update(
  82. sol={'label': _('Sales Order Item'), 'order': 'so_line'})
  83. return searchbar_sortings
  84. def _task_get_page_view_values(self, task, access_token, **kwargs):
  85. values = super()._task_get_page_view_values(task, access_token, **kwargs)
  86. values['so_accessible'] = False
  87. try:
  88. if task.sale_order_id and self._document_check_access('sale.order', task.sale_order_id.id):
  89. values['so_accessible'] = True
  90. title = _('Quotation') if task.sale_order_id.state in ['draft', 'sent'] else _('Sales Order')
  91. values['task_link_section'].append({
  92. 'access_url': task.sale_order_id.get_portal_url(),
  93. 'title': title,
  94. })
  95. except (AccessError, MissingError):
  96. pass
  97. moves = request.env['account.move']
  98. invoice_ids = task.sale_order_id.invoice_ids
  99. if invoice_ids and request.env['account.move'].check_access_rights('read', raise_exception=False):
  100. moves = request.env['account.move'].search([('id', 'in', invoice_ids.ids)])
  101. values['invoices_accessible'] = moves.ids
  102. if moves:
  103. if len(moves) == 1:
  104. task_invoice_url = moves.get_portal_url()
  105. title = _('Invoice')
  106. else:
  107. task_invoice_url = f'/my/tasks/{task.id}/orders/invoices'
  108. title = _('Invoices')
  109. values['task_link_section'].append({
  110. 'access_url': task_invoice_url,
  111. 'title': title,
  112. })
  113. return values
  114. @http.route(['/my/timesheets', '/my/timesheets/page/<int:page>'], type='http', auth="user", website=True)
  115. def portal_my_timesheets(self, page=1, sortby=None, filterby=None, search=None, search_in='all', groupby='sol', **kw):
  116. return super().portal_my_timesheets(page, sortby, filterby, search, search_in, groupby, **kw)