| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
from odoo.fields import Domain |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrder(models.Model): |
| 8 |
_inherit = 'sale.order' |
| 9 |
|
| 10 |
expense_ids = fields.One2many( |
| 11 |
comodel_name='hr.expense', |
| 12 |
inverse_name='sale_order_id', |
| 13 |
string='Expenses', |
| 14 |
domain=[('state', 'in', ('posted', 'in_payment', 'paid'))], |
| 15 |
readonly=True, |
| 16 |
) |
| 17 |
expense_count = fields.Integer("# of Expenses", compute='_compute_expense_count', compute_sudo=True) |
| 18 |
|
| 19 |
@api.model |
| 20 |
def _search_display_name(self, operator, value): |
| 21 |
""" For expense, we want to show all sales order but only their display_name (no ir.rule applied), this is the only way to do it. """ |
| 22 |
if ( |
| 23 |
self.env.context.get('sale_expense_all_order') |
| 24 |
and self.env.user.has_group('sales_team.group_sale_salesman') |
| 25 |
and not self.env.user.has_group('sales_team.group_sale_salesman_all_leads') |
| 26 |
): |
| 27 |
if operator in Domain.NEGATIVE_OPERATORS: |
| 28 |
return NotImplemented |
| 29 |
domain = super()._search_display_name(operator, value) |
| 30 |
company_domain = Domain('state', '=', 'sale') & ('company_id', 'in', self.env.companies.ids) |
| 31 |
query = self.sudo()._search(domain & company_domain) |
| 32 |
return Domain('id', 'in', query) |
| 33 |
return super()._search_display_name(operator, value) |
| 34 |
|
| 35 |
@api.depends('expense_ids') |
| 36 |
def _compute_expense_count(self): |
| 37 |
for sale_order in self: |
| 38 |
sale_order.expense_count = len(sale_order.order_line.expense_ids) |