[FIX] stock_account: partial reception with avg cost

- Create a product where the associated product category has Costing
  Method "Average Cost (AVCO)"
- Create a PO. Order 1 unit at $1000.
- Receive that unit. (System now has 1 unit at $1000).
- Create another PO. Order 3 units at $500 each.
- Receive only 1 unit and don’t create any back order.

System has 2 units @ $625, instead of $750.

The quantity computed should take into account the quantity actually
done.

opw-1843998
This commit is contained in:
Nicolas Martinelli
2018-05-16 14:21:15 +02:00
committed by Nicolas Martinelli
parent cdabb9d74c
commit 491243d1ca
2 changed files with 38 additions and 3 deletions
+5 -3
View File
@@ -372,6 +372,7 @@ class StockMove(models.Model):
product_tot_qty_available = move.product_id.qty_available + tmpl_dict[move.product_id.id]
rounding = move.product_id.uom_id.rounding
qty_done = 0.0
if float_is_zero(product_tot_qty_available, precision_rounding=rounding):
new_std_price = move._get_price_unit()
elif float_is_zero(product_tot_qty_available + move.product_qty, precision_rounding=rounding):
@@ -379,10 +380,11 @@ class StockMove(models.Model):
else:
# Get the standard price
amount_unit = std_price_update.get((move.company_id.id, move.product_id.id)) or move.product_id.standard_price
qty = forced_qty or move.product_qty
new_std_price = ((amount_unit * product_tot_qty_available) + (move._get_price_unit() * qty)) / (product_tot_qty_available + move.product_qty)
qty_done = move.product_uom._compute_quantity(move.quantity_done, move.product_id.uom_id)
qty = forced_qty or qty_done
new_std_price = ((amount_unit * product_tot_qty_available) + (move._get_price_unit() * qty)) / (product_tot_qty_available + qty_done)
tmpl_dict[move.product_id.id] += move.product_qty
tmpl_dict[move.product_id.id] += qty_done
# Write the standard price, as SUPERUSER_ID because a warehouse manager may not have the right to write on products
move.product_id.with_context(force_company=move.company_id.id).sudo().write({'standard_price': new_std_price})
std_price_update[move.company_id.id, move.product_id.id] = new_std_price
@@ -2489,6 +2489,39 @@ class TestStockValuation(TransactionCase):
move2.move_line_ids.qty_done = 0
self.assertEqual(self.product1.stock_value, -187.5)
def test_average_perpetual_4(self):
self.product1.product_tmpl_id.cost_method = 'average'
move1 = self.env['stock.move'].create({
'name': 'Receive 1 unit at 10',
'location_id': self.supplier_location.id,
'location_dest_id': self.stock_location.id,
'product_id': self.product1.id,
'product_uom': self.uom_unit.id,
'product_uom_qty': 1.0,
'price_unit': 10,
})
move1._action_confirm()
move1._action_assign()
move1.move_line_ids.qty_done = 1.0
move1._action_done()
move2 = self.env['stock.move'].create({
'name': 'Receive 3 units at 5',
'location_id': self.supplier_location.id,
'location_dest_id': self.stock_location.id,
'product_id': self.product1.id,
'product_uom': self.uom_unit.id,
'product_uom_qty': 3.0,
'price_unit': 5,
})
move2._action_confirm()
move2._action_assign()
move2.move_line_ids.qty_done = 1.0
move2._action_done()
self.assertAlmostEqual(self.product1.standard_price, 7.5)
def test_average_negative_1(self):
""" Test edit in the past. Receive 10, send 20, edit the second move to only send 10.
"""