| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import models, Command |
| 4 |
|
| 5 |
|
| 6 |
class AccountMoveLine(models.Model): |
| 7 |
_inherit = 'account.move.line' |
| 8 |
|
| 9 |
def _sale_can_be_reinvoice(self): |
| 10 |
""" determine if the generated analytic line should be reinvoiced or not. |
| 11 |
For Expense flow, if the product has a 'reinvoice policy' and a Sales Order is set on the expense, then we will reinvoice the AAL |
| 12 |
""" |
| 13 |
self.ensure_one() |
| 14 |
if self.expense_id: # expense flow is different from vendor bill reinvoice flow |
| 15 |
return ( |
| 16 |
self.expense_id.product_id.expense_policy in {'sales_price', 'cost'} |
| 17 |
and self.expense_id.sale_order_id |
| 18 |
and self.display_type == 'product' |
| 19 |
) |
| 20 |
return super()._sale_can_be_reinvoice() |
| 21 |
|
| 22 |
def _get_so_mapping_from_expense(self): |
| 23 |
mapping_from_expense = {} |
| 24 |
for move_line in self.filtered(lambda move_line: move_line.expense_id): |
| 25 |
mapping_from_expense[move_line.id] = move_line.expense_id.sale_order_id or None |
| 26 |
return mapping_from_expense |
| 27 |
|
| 28 |
def _sale_determine_order(self): |
| 29 |
# EXTENDS sale |
| 30 |
# For move lines created from expense, we override the normal behavior. |
| 31 |
mapping_from_invoice = super()._sale_determine_order() |
| 32 |
mapping_from_invoice.update(self._get_so_mapping_from_expense()) |
| 33 |
return mapping_from_invoice |
| 34 |
|
| 35 |
def _sale_prepare_sale_line_values(self, order, price): |
| 36 |
# EXTENDS sale |
| 37 |
# Add expense quantity to sales order line and update the sales order price because it will be charged to the customer in the end. |
| 38 |
res = super()._sale_prepare_sale_line_values(order, price) |
| 39 |
if self.expense_id: |
| 40 |
res.update({ |
| 41 |
'name': self.name, |
| 42 |
'expense_ids': [Command.set(self.expense_id.ids)], |
| 43 |
'product_uom_qty': self.expense_id.quantity, |
| 44 |
'analytic_distribution': self.analytic_distribution, |
| 45 |
}) |
| 46 |
return res |
| 47 |
|
| 48 |
def _sale_create_reinvoice_sale_line(self): |
| 49 |
# EXTENDS sale |
| 50 |
# We force each reinvoiced expense to be on their own sale order line, |
| 51 |
# else we cannot properly edit the quantities if the user manually override anything |
| 52 |
expensed_lines = self.filtered('expense_id') |
| 53 |
res = super(AccountMoveLine, self - expensed_lines)._sale_create_reinvoice_sale_line() |
| 54 |
res.update(super(AccountMoveLine, expensed_lines.with_context({'force_split_lines': True}))._sale_create_reinvoice_sale_line()) |
| 55 |
return res |