Skip to content

Commit

Permalink
[FIX] project: account invoices/bills in project update dashboard
Browse files Browse the repository at this point in the history
Current behaviour:
In the project update dashboard, invoices and bills that are created
directly (without sales order or purchase order) are not taken into account
in the calculations for profits/costs.

Expected behaviour:
They should be taken into account, as long as the line on them has the
analytic account line on it.

Steps to reproduce:
- Install Project, Sales, Purchase, Accounting
- Activate "Analytic Accounting" in Settings
- Create a new Project and create a new analytic account for the project
- Create a Bill with a product (storable/consumable) and the analytic account
on the line
- Post the bill
- Go to your project update dashboard and notice the cost of the bill
is not taken into account, but we do have a smart button that goes to the bills

Reason for the problem:
Profits/Costs calculations are done based on the sales/purchase orders,
missing the "orphaned" invoices and bills. A side effect of this problem,
storable products on sales are also not taken into account, since Sales Orders
don't have an analytic account per line before version 16, unless that product
is a service (all services intrinsically have an analytic account)

Fix:
Get all invoices/bills that have lines with the project's analytic account,
and removing from those the lines that were already accounted for.

Affected versions:
- saas-15.3
- 16.0
- master

opw-3058198

closes odoo#108110

X-original-commit: 09cd4f5
Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
Signed-off-by: Piryns Victor (pivi) <pivi@odoo.com>
  • Loading branch information
pivi-odoo committed Dec 16, 2022
1 parent 16cb4af commit 3d3f410
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 9 deletions.
7 changes: 7 additions & 0 deletions addons/project_purchase/i18n/project_purchase.pot
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ msgstr ""
msgid "# Purchase Orders"
msgstr ""

#. module: project_purchase
#: code:addons/project_purchase/models/project.py:0
#: code:addons/project_purchase/models/project.py:0
#, python-format
msgid "Other Costs"
msgstr ""

#. module: project_purchase
#: model:ir.model,name:project_purchase.model_project_project
msgid "Project"
Expand Down
49 changes: 48 additions & 1 deletion addons/project_purchase/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ def _get_profitability_aal_domain(self):
def _get_profitability_labels(self):
labels = super()._get_profitability_labels()
labels['purchase_order'] = _lt('Purchase Orders')
labels['other_purchase_costs'] = _lt('Other Costs')
return labels

def _get_profitability_sequence_per_invoice_type(self):
sequence_per_invoice_type = super()._get_profitability_sequence_per_invoice_type()
sequence_per_invoice_type['purchase_order'] = 9
sequence_per_invoice_type['other_purchase_costs'] = 10
return sequence_per_invoice_type

def _get_profitability_items(self, with_action=True):
Expand All @@ -128,11 +130,16 @@ def _get_profitability_items(self, with_action=True):
query.add_where('purchase_order_line.analytic_distribution ? %s', [str(self.analytic_account_id.id)])
query_string, query_param = query.select('"purchase_order_line".id', 'qty_invoiced', 'qty_to_invoice', 'product_uom_qty', 'price_unit')
self._cr.execute(query_string, query_param)
purchase_order_line_read = [pol for pol in self._cr.dictfetchall()]
purchase_order_line_read = [{
**pol,
'invoice_lines': self.env['purchase.order.line'].browse(pol['id']).invoice_lines, # One2Many cannot be queried, they are not columns
} for pol in self._cr.dictfetchall()]
purchase_order_line_invoice_line_ids = []
if purchase_order_line_read:
amount_invoiced = amount_to_invoice = 0.0
purchase_order_line_ids = []
for pol_read in purchase_order_line_read:
purchase_order_line_invoice_line_ids.extend(pol_read['invoice_lines'].ids)
price_unit = pol_read['price_unit']
amount_invoiced -= price_unit * pol_read['qty_invoiced'] if pol_read['qty_invoiced'] > 0 else 0.0
if pol_read['qty_to_invoice'] > 0:
Expand All @@ -152,4 +159,44 @@ def _get_profitability_items(self, with_action=True):
costs['data'].append(purchase_order_costs)
costs['total']['billed'] += amount_invoiced
costs['total']['to_bill'] += amount_to_invoice
# calculate the cost of bills without a purchase order
query = self.env['account.move.line'].sudo()._search([
('move_id.move_type', 'in', ['in_invoice', 'in_refund']),
('parent_state', 'in', ['draft', 'posted']),
('price_subtotal', '>', 0),
('id', 'not in', purchase_order_line_invoice_line_ids),
])
query.add_where('account_move_line.analytic_distribution ? %s', [str(self.analytic_account_id.id)])
# account_move_line__move_id is the alias of the joined table account_move in the query
# we can use it, because of the "move_id.move_type" clause in the domain of the query, which generates the join
# this is faster than a search_read followed by a browse on the move_id to retrieve the move_type of each account.move.line
query_string, query_param = query.select('price_subtotal', 'parent_state', 'account_move_line__move_id.move_type')
self._cr.execute(query_string, query_param)
bills_move_line_read = self._cr.dictfetchall()
if bills_move_line_read:
amount_invoiced = amount_to_invoice = 0.0
for moves_read in bills_move_line_read:
if moves_read['parent_state'] == 'draft':
if moves_read['move_type'] == 'in_invoice':
amount_to_invoice -= moves_read['price_subtotal']
else: # moves_read['move_type'] == 'in_refund'
amount_to_invoice += moves_read['price_subtotal']
else: # moves_read['parent_state'] == 'posted'
if moves_read['move_type'] == 'in_invoice':
amount_invoiced -= moves_read['price_subtotal']
else: # moves_read['move_type'] == 'in_refund'
amount_invoiced += moves_read['price_subtotal']
# don't display the section if the final values are both 0 (bill -> vendor credit)
if amount_invoiced != 0 or amount_to_invoice != 0:
costs = profitability_items['costs']
section_id = 'other_purchase_costs'
bills_costs = {
'id': section_id,
'sequence': self._get_profitability_sequence_per_invoice_type()[section_id],
'billed': amount_invoiced,
'to_bill': amount_to_invoice,
}
costs['data'].append(bills_costs)
costs['total']['billed'] += amount_invoiced
costs['total']['to_bill'] += amount_to_invoice
return profitability_items
4 changes: 4 additions & 0 deletions addons/project_purchase/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import test_project_profitability
182 changes: 182 additions & 0 deletions addons/project_purchase/tests/test_project_profitability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from datetime import datetime

from odoo import Command
from odoo.tests import tagged

from odoo.addons.project.tests.test_project_profitability import TestProjectProfitabilityCommon
from odoo.addons.purchase.tests.test_purchase_invoice import TestPurchaseToInvoiceCommon


@tagged('-at_install', 'post_install')
class TestProjectPurchaseProfitability(TestProjectProfitabilityCommon, TestPurchaseToInvoiceCommon):

def test_bills_without_purchase_order_are_accounted_in_profitability(self):
"""
A bill that has an AAL on one of its line should be taken into account
for the profitability of the project.
"""
# create a bill_1 with the AAL
bill_1 = self.env['account.move'].create({
"name": "Bill_1 name",
"move_type": "in_invoice",
"state": "draft",
"partner_id": self.partner.id,
"invoice_date": datetime.today(),
"invoice_line_ids": [Command.create({
"analytic_distribution": {self.analytic_account.id: 100},
"product_id": self.product_a.id,
"quantity": 1,
"product_uom_id": self.product_a.uom_id.id,
"price_unit": self.product_a.standard_price,
})],
})
# the bill_1 is in draft, therefor it should have the cost "to_bill" same as the -product_price (untaxed)
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': -self.product_a.standard_price,
'billed': 0.0,
}],
'total': {'to_bill': -self.product_a.standard_price, 'billed': 0.0},
},
)
# post bill_1
bill_1.action_post()
# we posted the bill_1, therefore the cost "billed" should be -product_price, to_bill should be back to 0
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': 0.0,
'billed': -self.product_a.standard_price,
}],
'total': {'to_bill': 0.0, 'billed': -self.product_a.standard_price},
},
)
# create another bill, with 2 lines, 2 diff products, the second line has 2 as quantity
bill_2 = self.env['account.move'].create({
"name": "I have 2 lines",
"move_type": "in_invoice",
"state": "draft",
"partner_id": self.partner.id,
"invoice_date": datetime.today(),
"invoice_line_ids": [Command.create({
"analytic_distribution": {self.analytic_account.id: 100},
"product_id": self.product_a.id,
"quantity": 1,
"product_uom_id": self.product_a.uom_id.id,
"price_unit": self.product_a.standard_price,
}), Command.create({
"analytic_distribution": {self.analytic_account.id: 100},
"product_id": self.product_b.id,
"quantity": 2,
"product_uom_id": self.product_b.uom_id.id,
"price_unit": self.product_b.standard_price,
})],
})
# bill_2 is not posted, therefor its cost should be "to_billed" = - sum of all product_price * qty for each line
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price),
'billed': -self.product_a.standard_price,
}],
'total': {
'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price),
'billed': -self.product_a.standard_price,
},
},
)
# post bill_2
bill_2.action_post()
# bill_2 is posted, therefor its cost should be counting in "billed", with the cost of bill_1
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': 0.0,
'billed': -2 * (self.product_a.standard_price + self.product_b.standard_price),
}],
'total': {
'to_bill': 0.0,
'billed': -2 * (self.product_a.standard_price + self.product_b.standard_price),
},
},
)
# create a new purchase order
purchase_order = self.env['purchase.order'].create({
"name": "A purchase order",
"partner_id": self.partner_a.id,
"order_line": [Command.create({
"analytic_distribution": {self.analytic_account.id: 100},
"product_id": self.product_order.id,
"product_qty": 1,
"price_unit": self.product_order.standard_price,
})],
})
purchase_order.button_confirm()
# we should have a new section "purchase_order", the total should be updated,
# but the "other_purchase_costs" shouldn't change, as we don't takes into
# account bills from purchase orders, as those are already taken into calculations
# from the purchase orders (in "purchase_order" section)
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'purchase_order',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['purchase_order'],
'to_bill': -self.product_order.standard_price,
'billed': 0.0,
}, {
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': 0.0,
'billed': -2 * (self.product_a.standard_price + self.product_b.standard_price),
}],
'total': {
'to_bill': -self.product_order.standard_price,
'billed': -2 * (self.product_a.standard_price + self.product_b.standard_price),
},
},
)
purchase_order.action_create_invoice()
purchase_bill = purchase_order.invoice_ids # get the bill from the purchase
purchase_bill.invoice_date = datetime.today()
purchase_bill.action_post()
# now the bill has been posted, its costs should be accounted in the "billed" part
# of the purchase_order section, but should touch in the other_purchase_costs
self.assertDictEqual(
self.project._get_profitability_items(False)['costs'],
{
'data': [{
'id': 'purchase_order',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['purchase_order'],
'to_bill': 0.0,
'billed': -self.product_order.standard_price,
}, {
'id': 'other_purchase_costs',
'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
'to_bill': 0.0,
'billed': -2 * (self.product_a.standard_price + self.product_b.standard_price),
}],
'total': {
'to_bill': 0.0,
'billed': -(2 * self.product_a.standard_price +
2 * self.product_b.standard_price +
self.product_order.standard_price),
},
},
)
9 changes: 6 additions & 3 deletions addons/purchase/tests/test_purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from odoo import Command, fields


@tagged('post_install', '-at_install')
class TestPurchaseToInvoice(AccountTestInvoicingCommon):
class TestPurchaseToInvoiceCommon(AccountTestInvoicingCommon):

@classmethod
def setUpClass(cls):
super(TestPurchaseToInvoice, cls).setUpClass()
super(TestPurchaseToInvoiceCommon, cls).setUpClass()
uom_unit = cls.env.ref('uom.product_uom_unit')
uom_hour = cls.env.ref('uom.product_uom_hour')
cls.product_order = cls.env['product.product'].create({
Expand Down Expand Up @@ -59,6 +58,10 @@ def setUpClass(cls):
'taxes_id': False,
})


@tagged('post_install', '-at_install')
class TestPurchaseToInvoice(TestPurchaseToInvoiceCommon):

def test_vendor_bill_delivered(self):
"""Test if a order of product invoiced by delivered quantity can be
correctly invoiced."""
Expand Down
7 changes: 7 additions & 0 deletions addons/sale_project/i18n/sale_project.pot
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ msgid ""
" 'In sale order's project': Will use the sale order's configured project if defined or fallback to creating a new project based on the selected template."
msgstr ""

#. module: sale_project
#: code:addons/sale_project/models/project.py:0
#: code:addons/sale_project/models/project.py:0
#, python-format
msgid "Other Revenues"
msgstr ""

#. module: sale_project
#: code:addons/sale_project/models/project.py:0
#, python-format
Expand Down
Loading

0 comments on commit 3d3f410

Please sign in to comment.