Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/purchase_stock/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _get_po_line_moves(self):
return moves

def _get_po_line_invoice_lines_su(self):
#TODO remove in master: un-used
return self.sudo().invoice_lines

@api.depends('move_ids.state', 'move_ids.product_uom', 'move_ids.quantity')
Expand Down
10 changes: 6 additions & 4 deletions addons/purchase_stock/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ def _get_price_unit(self):
invoiced_layer = line.sudo().invoice_lines.stock_valuation_layer_ids
# value on valuation layer is in company's currency, while value on invoice line is in order's currency
receipt_value = 0
if move_layer:
receipt_value += sum(move_layer.mapped(lambda l: l.currency_id._convert(
l.value, order.currency_id, order.company_id, l.create_date, round=False)))
for layer in move_layer:
if not layer._should_impact_price_unit_receipt_value():
continue
receipt_value += layer.currency_id._convert(
layer.value, order.currency_id, order.company_id, layer.create_date, round=False)
if invoiced_layer:
receipt_value += sum(invoiced_layer.mapped(lambda l: l.currency_id._convert(
l.value, order.currency_id, order.company_id, l.create_date, round=False)))
total_invoiced_value = 0
invoiced_qty = 0
for invoice_line in line._get_po_line_invoice_lines_su():
for invoice_line in line.sudo().invoice_lines:
if invoice_line.move_id.state != 'posted':
continue
# Adjust unit price to account for discounts before adding taxes.
Expand Down
4 changes: 4 additions & 0 deletions addons/stock_account/models/stock_valuation_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,7 @@ def _change_standart_price_accounting_entries(self, new_price):
account_moves = self.env['account.move'].sudo().create(am_vals_list)
if account_moves:
account_moves._post()

def _should_impact_price_unit_receipt_value(self):
self.ensure_one()
return True
1 change: 1 addition & 0 deletions addons/stock_landed_costs/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def _prepare_account_move_line(self, move=False):
return res

def _get_po_line_invoice_lines_su(self):
#TODO remove in master: un-used
po_line_invoices_lines = super()._get_po_line_invoice_lines_su()
move = self.sudo().invoice_lines.move_id
if move.landed_costs_ids.filtered(lambda lc: lc.state == 'done'):
Expand Down
4 changes: 4 additions & 0 deletions addons/stock_landed_costs/models/stock_valuation_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class StockValuationLayer(models.Model):
_inherit = 'stock.valuation.layer'

stock_landed_cost_id = fields.Many2one('stock.landed.cost', 'Landed Cost')

def _should_impact_price_unit_receipt_value(self):
res = super()._should_impact_price_unit_receipt_value()
return res and not self.stock_landed_cost_id.vendor_bill_id
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from odoo.addons.stock_landed_costs.tests.test_stockvaluationlayer import TestStockValuationLCCommon
from odoo.addons.stock_account.tests.test_stockvaluation import _create_accounting_data

from odoo import fields
from odoo.fields import Command, Date
from odoo.tests import tagged, Form

Expand Down Expand Up @@ -427,6 +428,74 @@ def test_invoice_after_lc(self):
price_diff_aml = self.env['account.move.line'].search([('account_id', '=', stock_valuation_account.id), ('move_id', '=', move.id)])
self.assertEqual(len(price_diff_aml), 0, "No line should have been generated in the stock valuation account about the price difference.")

def test_lc_with_avco_ordered_qty_backorder(self):
""" Make sure the landed cost added in invoices are taken into account to compute product
cost even in 'Ordered Quantity' invoice policy. """
self.env.company.anglo_saxon_accounting = True
self.landed_cost.split_method_landed_cost = 'by_quantity'
product2 = self.env['product.product'].create({
'name': 'product2',
'is_storable': True,
'categ_id': self.stock_account_product_categ.id,
})
products = self.product1 | product2
products.purchase_method = 'purchase'
products.categ_id.write({
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id,
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id,
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id,
'property_valuation': 'real_time',
'property_cost_method': 'average',
})
self.landed_cost.categ_id = products.categ_id.id

purchase_order = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'order_line': [Command.create({
'product_id': self.product1.id,
'product_qty': 100000,
'price_unit': 1,
}), Command.create({
'product_id': product2.id,
'product_qty': 50000,
'price_unit': 3,
})],
})
purchase_order.button_confirm()
purchase_order.action_create_invoice()
bill = purchase_order.invoice_ids[0]
receipt = purchase_order.picking_ids[0]
receipt.picking_type_id.create_backorder = 'always'
receipt.move_ids[0].quantity = 50000
receipt.move_ids[1].quantity = 25000
receipt.move_ids.picked = True
receipt.button_validate()
self.assertEqual(self.product1.standard_price, 1)
self.assertEqual(product2.standard_price, 3)

bill.invoice_date = fields.Date.today()
with Form(bill) as bill_form:
with bill_form.invoice_line_ids.new() as inv_line:
inv_line.product_id = self.landed_cost
inv_line.price_unit = 75000
inv_line.is_landed_costs_line = True
bill.action_post()
action = bill.button_create_landed_costs()
lc_form = Form(self.env[action['res_model']].browse(action['res_id']))
lc_form.picking_ids.add(receipt)
lc = lc_form.save()
lc.button_validate()
self.assertEqual(self.product1.standard_price, 2)
self.assertEqual(product2.standard_price, 4)

receipt2 = purchase_order.picking_ids.filtered(lambda p: p.state == 'assigned')
for move in receipt2.move_ids:
move.quantity = move.product_qty
move.picked = True
receipt2.button_validate()
self.assertEqual(self.product1.standard_price, 1.5)
self.assertEqual(product2.standard_price, 3.5)

def test_invoice_after_lc_amls(self):
self.env.company.anglo_saxon_accounting = True
self.landed_cost.landed_cost_ok = True
Expand Down Expand Up @@ -510,6 +579,7 @@ def test_lc_with_avco_ordered_qty_invoice_receipt_order(self):
})
self.landed_cost.categ_id = self.product1.categ_id.id
product = self.product1
product.uom_id = self.env.ref("uom.product_uom_kgm")

purchase_order = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
Expand Down