| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import json |
| 4 |
|
| 5 |
from odoo import models |
| 6 |
from collections import defaultdict |
| 7 |
|
| 8 |
|
| 9 |
class ProjectProject(models.Model): |
| 10 |
_inherit = 'project.project' |
| 11 |
|
| 12 |
def _get_expenses_profitability_items(self, with_action=True): |
| 13 |
expenses_read_group = self.env['hr.expense']._read_group( |
| 14 |
[('state', 'in', ['posted', 'in_payment', 'paid']), ('analytic_distribution', 'in', self.account_id.ids)], |
| 15 |
groupby=['sale_order_id', 'product_id', 'currency_id'], |
| 16 |
aggregates=['id:array_agg', 'untaxed_amount_currency:sum'], |
| 17 |
) |
| 18 |
if not expenses_read_group: |
| 19 |
return {} |
| 20 |
expenses_per_so_id = {} |
| 21 |
expense_ids = [] |
| 22 |
dict_amount_per_currency = defaultdict(lambda: 0.0) |
| 23 |
can_see_expense = with_action and self.env.user.has_group('hr_expense.group_hr_expense_team_approver') |
| 24 |
for sale_order, product, currency, ids, untaxed_amount_currency_sum in expenses_read_group: |
| 25 |
expenses_per_so_id.setdefault(sale_order.id, {})[product.id] = ids |
| 26 |
if can_see_expense: |
| 27 |
expense_ids.extend(ids) |
| 28 |
dict_amount_per_currency[currency] += untaxed_amount_currency_sum |
| 29 |
|
| 30 |
amount_billed = 0.0 |
| 31 |
for currency, untaxed_amount_currency_sum in dict_amount_per_currency.items(): |
| 32 |
amount_billed += currency._convert(untaxed_amount_currency_sum, self.currency_id, self.company_id, round=False) |
| 33 |
|
| 34 |
sol_read_group = self.env['sale.order.line'].sudo()._read_group( |
| 35 |
[ |
| 36 |
('order_id', 'in', list(expenses_per_so_id.keys())), |
| 37 |
('is_expense', '=', True), |
| 38 |
('state', '=', 'sale'), |
| 39 |
], |
| 40 |
['order_id', 'product_id', 'currency_id'], |
| 41 |
['untaxed_amount_to_invoice:sum', 'untaxed_amount_invoiced:sum'], |
| 42 |
) |
| 43 |
|
| 44 |
total_amount_expense_invoiced = total_amount_expense_to_invoice = 0.0 |
| 45 |
reinvoice_expense_ids = [] |
| 46 |
dict_invoices_amount_per_currency = defaultdict(lambda: {'to_invoice': 0.0, 'invoiced': 0.0}) |
| 47 |
set_currency_ids = {self.currency_id.id} |
| 48 |
for order, product, currency, untaxed_amount_to_invoice_sum, untaxed_amount_invoiced_sum in sol_read_group: |
| 49 |
expense_data_per_product_id = expenses_per_so_id[order.id] |
| 50 |
set_currency_ids.add(currency.id) |
| 51 |
product_id = product.id |
| 52 |
if product_id in expense_data_per_product_id: |
| 53 |
dict_invoices_amount_per_currency[currency]['to_invoice'] += untaxed_amount_to_invoice_sum |
| 54 |
dict_invoices_amount_per_currency[currency]['invoiced'] += untaxed_amount_invoiced_sum |
| 55 |
reinvoice_expense_ids += expense_data_per_product_id[product_id] |
| 56 |
for currency, revenues in dict_invoices_amount_per_currency.items(): |
| 57 |
total_amount_expense_to_invoice += currency._convert(revenues['to_invoice'], self.currency_id, self.company_id) |
| 58 |
total_amount_expense_invoiced += currency._convert(revenues['invoiced'], self.currency_id, self.company_id) |
| 59 |
|
| 60 |
section_id = 'expenses' |
| 61 |
sequence = self._get_profitability_sequence_per_invoice_type()[section_id] |
| 62 |
expense_data = { |
| 63 |
'costs': { |
| 64 |
'id': section_id, |
| 65 |
'sequence': sequence, |
| 66 |
'billed': -amount_billed, |
| 67 |
'to_bill': 0.0, |
| 68 |
}, |
| 69 |
} |
| 70 |
if reinvoice_expense_ids: |
| 71 |
expense_data['revenues'] = { |
| 72 |
'id': section_id, |
| 73 |
'sequence': sequence, |
| 74 |
'invoiced': total_amount_expense_invoiced, |
| 75 |
'to_invoice': total_amount_expense_to_invoice, |
| 76 |
} |
| 77 |
if can_see_expense: |
| 78 |
def get_action(res_ids): |
| 79 |
args = [section_id, [('id', 'in', res_ids)]] |
| 80 |
if len(res_ids) == 1: |
| 81 |
args.append(res_ids[0]) |
| 82 |
return {'name': 'action_profitability_items', 'type': 'object', 'args': json.dumps(args)} |
| 83 |
|
| 84 |
if reinvoice_expense_ids: |
| 85 |
expense_data['revenues']['action'] = get_action(reinvoice_expense_ids) |
| 86 |
if expense_ids: |
| 87 |
expense_data['costs']['action'] = get_action(expense_ids) |
| 88 |
return expense_data |
| 89 |
|
| 90 |
def _get_already_included_profitability_invoice_line_ids(self): |
| 91 |
move_line_ids = super()._get_already_included_profitability_invoice_line_ids() |
| 92 |
expenses_read_group = self.env['hr.expense']._read_group( |
| 93 |
[('state', 'in', ['posted', 'in_payment', 'paid']), ('analytic_distribution', 'in', self.account_id.ids)], |
| 94 |
groupby=['sale_order_id'], |
| 95 |
aggregates=['__count'], |
| 96 |
) |
| 97 |
if not expenses_read_group: |
| 98 |
return move_line_ids |
| 99 |
for sale_order, count in expenses_read_group: |
| 100 |
move_line_ids.extend(sale_order.invoice_ids.mapped('invoice_line_ids').ids) |
| 101 |
return move_line_ids |