Skip to content

Commit

Permalink
[IMP] purchase_order_uninvoiced_amount: Be compatible with purchase_d…
Browse files Browse the repository at this point in the history
…iscount

If we use such module, the final price is not the PO line price unit, so we need to
extract the unit price from the subtotal divided by the quantity instead for
getting a proper result on both cases.

TT25368
  • Loading branch information
pedrobaeza committed Sep 4, 2020
1 parent 2682884 commit 531c241
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions purchase_order_uninvoiced_amount/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, fields, models
Expand All @@ -11,10 +12,14 @@ class PurchaseOrder(models.Model):
def _compute_amount_uninvoiced(self):
for order in self:
amount_uninvoiced = order.amount_untaxed

for line in order.order_line.filtered(lambda x: (x.qty_invoiced != 0)):
amount_uninvoiced -= line.qty_invoiced * line.price_unit

for line in order.order_line.filtered("qty_invoiced"):
# we use this way for being compatible with purchase_discount
price_unit = (
line.product_qty and
line.price_subtotal / line.product_qty or
line.price_unit
)
amount_uninvoiced -= line.qty_invoiced * price_unit
order.update({
'amount_uninvoiced': order.currency_id.round(amount_uninvoiced),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo.tests.common import SavepointCase
Expand Down Expand Up @@ -105,3 +106,7 @@ def test_create_purchase_create_and_invoiced_with_all_units(self):
self._create_invoice_from_purchase(purchase)
self.assertEquals(purchase.amount_uninvoiced, 0,
"The purchase amount uninvoiced must be 0")

def test_create_purchase_qty_0(self):
purchase = self._create_purchase(0, 0)
self.assertEquals(purchase.amount_uninvoiced, 0)

0 comments on commit 531c241

Please sign in to comment.