| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import Command, api, fields, models |
| 4 |
|
| 5 |
|
| 6 |
class HrExpense(models.Model): |
| 7 |
_inherit = "hr.expense" |
| 8 |
|
| 9 |
sale_order_id = fields.Many2one( |
| 10 |
'sale.order', |
| 11 |
string='Customer to Reinvoice', |
| 12 |
compute='_compute_sale_order_id', |
| 13 |
store=True, |
| 14 |
readonly=False, |
| 15 |
index='btree_not_null', |
| 16 |
tracking=True, |
| 17 |
# NOTE: only confirmed SO can be selected, but this domain in activated throught the name search with the `sale_expense_all_order` |
| 18 |
# context key. So, this domain is not the one applied. |
| 19 |
domain="[('state', '=', 'sale')]", |
| 20 |
check_company=True, |
| 21 |
help="If the category has an expense policy, it will be reinvoiced on this sales order") |
| 22 |
sale_order_line_id = fields.Many2one( |
| 23 |
comodel_name='sale.order.line', |
| 24 |
compute='_compute_sale_order_id', |
| 25 |
store=True, |
| 26 |
readonly=True, |
| 27 |
index='btree_not_null', |
| 28 |
) |
| 29 |
can_be_reinvoiced = fields.Boolean("Can be reinvoiced", compute='_compute_can_be_reinvoiced') |
| 30 |
|
| 31 |
@api.depends('product_id.expense_policy') |
| 32 |
def _compute_can_be_reinvoiced(self): |
| 33 |
for expense in self: |
| 34 |
expense.can_be_reinvoiced = expense.product_id.expense_policy in ['sales_price', 'cost'] |
| 35 |
|
| 36 |
@api.depends('can_be_reinvoiced') |
| 37 |
def _compute_sale_order_id(self): |
| 38 |
for expense in self.filtered(lambda e: not e.can_be_reinvoiced): |
| 39 |
expense.sale_order_id = False |
| 40 |
expense.sale_order_line_id = False |
| 41 |
|
| 42 |
@api.onchange('sale_order_id') |
| 43 |
def _onchange_sale_order_id(self): |
| 44 |
to_reset = self.filtered(lambda line: not self.env.is_protected(self._fields['analytic_distribution'], line)) |
| 45 |
to_reset.invalidate_recordset(['analytic_distribution']) |
| 46 |
self.env.add_to_compute(self._fields['analytic_distribution'], to_reset) |
| 47 |
|
| 48 |
def _sale_expense_reset_sol_quantities(self): |
| 49 |
""" |
| 50 |
Resets the quantity of a SOL created by a reinvoiced expense to 0 when the expense or its move is reset to an unfinished state |
| 51 |
|
| 52 |
Note: Resetting the qty_delivered will raise if the product is a storable product and sale_stock is installed, |
| 53 |
but it's fine as it doesn't make much sense to have a stored product in an expense. |
| 54 |
""" |
| 55 |
self.check_access('write') |
| 56 |
# If we can edit the expense, we may not be able to edit the sol without sudoing. |
| 57 |
self.sudo().sale_order_line_id.write({ |
| 58 |
'qty_delivered': 0.0, |
| 59 |
'product_uom_qty': 0.0, |
| 60 |
'expense_ids': [Command.clear()], |
| 61 |
}) |
| 62 |
|
| 63 |
def _get_split_values(self): |
| 64 |
# EXTENDS hr_expense |
| 65 |
vals = super()._get_split_values() |
| 66 |
for split_value in vals: |
| 67 |
split_value['sale_order_id'] = self.sale_order_id.id |
| 68 |
return vals |
| 69 |
|
| 70 |
def action_post(self): |
| 71 |
# EXTENDS hr_expense |
| 72 |
# When posting expense, we need the analytic entries to be generated, because reinvoicing uses analytic accounts. |
| 73 |
# We then ensure the proper analytic acocunt is given in the distribution and if not, |
| 74 |
# we create an account and set the distribution to it. |
| 75 |
for expense in self: |
| 76 |
if expense.sale_order_id and not expense.analytic_distribution: |
| 77 |
analytic_account = self.env['account.analytic.account'].create(expense.sale_order_id._prepare_analytic_account_data()) |
| 78 |
expense.analytic_distribution = {analytic_account.id: 100} |
| 79 |
return super().action_post() |
| 80 |
|
| 81 |
def action_open_sale_order(self): |
| 82 |
self.ensure_one() |
| 83 |
return { |
| 84 |
'type': 'ir.actions.act_window', |
| 85 |
'res_model': 'sale.order', |
| 86 |
'views': [(self.env.ref("sale.view_order_form").id, 'form')], |
| 87 |
'view_mode': 'form', |
| 88 |
'target': 'current', |
| 89 |
'name': self.sale_order_id.display_name, |
| 90 |
'res_id': self.sale_order_id.id, |
| 91 |
} |