diff --git a/addons/project_purchase/models/project_project.py b/addons/project_purchase/models/project_project.py index 11a2b8ce2f4..640da6112fb 100644 --- a/addons/project_purchase/models/project_project.py +++ b/addons/project_purchase/models/project_project.py @@ -169,7 +169,11 @@ class Project(models.Model): purchase_order_line_invoice_line_ids.extend(pol_read['invoice_lines'].ids) currency = self.env['res.currency'].browse(pol_read['currency_id']).with_prefetch(currency_ids) price_unit = currency._convert(pol_read['price_unit'], self.currency_id, self.company_id) - analytic_contribution = pol_read['analytic_distribution'][str(self.analytic_account_id.id)] / 100. + # an analytic account can appear several time in an analytic distribution with different repartition percentage + analytic_contribution = sum( + percentage for ids, percentage in pol_read['analytic_distribution'].items() + if str(self.analytic_account_id.id) in ids.split(',') + ) / 100. amount_invoiced -= price_unit * pol_read['qty_invoiced'] * analytic_contribution if pol_read['qty_invoiced'] > 0 else 0.0 if pol_read['qty_to_invoice'] > 0: amount_to_invoice -= price_unit * pol_read['qty_to_invoice'] * analytic_contribution diff --git a/addons/project_purchase/tests/test_project_profitability.py b/addons/project_purchase/tests/test_project_profitability.py index 0e6a65d6a7b..8f92b27c79a 100644 --- a/addons/project_purchase/tests/test_project_profitability.py +++ b/addons/project_purchase/tests/test_project_profitability.py @@ -507,3 +507,36 @@ class TestProjectPurchaseProfitability(TestProjectProfitabilityCommon, TestPurch purchase_bill = purchase_order.invoice_ids # get the bill from the purchase purchase_bill.invoice_date = datetime.today() purchase_bill.action_post() + + def test_cross_analytics_contribution(self): + cross_plan = self.env['account.analytic.plan'].create({'name': 'Cross Plan'}) + cross_account = self.env['account.analytic.account'].create({ + 'name': "Cross Analytic Account", + 'plan_id': cross_plan.id, + "company_id": self.env.company.id, + }) + cross_distribution = 42 + + cross_order = self.env['purchase.order'].create({ + 'name': 'Cross Purchase Order', + "partner_id": self.partner_a.id, + "company_id": self.env.company.id, + 'order_line': [ + Command.create({ + 'analytic_distribution': { + f"{self.project.analytic_account_id.id},{cross_account.id}": cross_distribution, + }, + "product_id": self.product_order.id, + "product_qty": 1, + "price_unit": self.product_order.standard_price, + "currency_id": self.env.company.currency_id.id, + }), + ], + }) + + cross_order.button_confirm() + items = self.project._get_profitability_items() + self.assertEqual( + items['costs']['data'][0]['to_bill'], + self.product_order.standard_price *-cross_distribution/100 + )