diff --git a/addons/stock_landed_costs/models/account_move.py b/addons/stock_landed_costs/models/account_move.py index 00e1caffc56..9e53d51d11f 100644 --- a/addons/stock_landed_costs/models/account_move.py +++ b/addons/stock_landed_costs/models/account_move.py @@ -46,6 +46,11 @@ class AccountMove(models.Model): views = [(self.env.ref('stock_landed_costs.view_stock_landed_cost_tree2').id, 'tree'), (False, 'form'), (False, 'kanban')] return dict(action, domain=domain, context=context, views=views) + def _post(self, soft=True): + posted = super()._post(soft) + posted.sudo().landed_costs_ids.reconcile_landed_cost() + return posted + class AccountMoveLine(models.Model): _inherit = 'account.move.line' diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index 0bd746bff12..f3e4e53c15c 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -177,7 +177,11 @@ class StockLandedCost(models.Model): cost.write(cost_vals) if cost.account_move_id: move._post() + cost.reconcile_landed_cost() + return True + def reconcile_landed_cost(self): + for cost in self: if cost.vendor_bill_id and cost.vendor_bill_id.state == 'posted' and cost.company_id.anglo_saxon_accounting: all_amls = cost.vendor_bill_id.line_ids | cost.account_move_id.line_ids for product in cost.cost_lines.product_id: @@ -185,8 +189,6 @@ class StockLandedCost(models.Model): input_account = accounts['stock_input'] all_amls.filtered(lambda aml: aml.account_id == input_account and not aml.reconciled).reconcile() - return True - def get_valuation_lines(self): self.ensure_one() lines = [] diff --git a/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py b/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py index b79af4b7154..47f42a53ce4 100644 --- a/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py +++ b/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py @@ -5,6 +5,7 @@ from odoo.addons.stock_landed_costs.tests.common import TestStockLandedCostsComm 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.fields import Date from odoo.tests import tagged, Form @@ -433,3 +434,70 @@ class TestLandedCostsWithPurchaseAndInv(TestStockValuationLCCommon): # Check nothing was posted in the stock valuation account. 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_invoice_after_lc_amls(self): + self.env.company.anglo_saxon_accounting = True + self.landed_cost.landed_cost_ok = True + self.landed_cost.categ_id.property_cost_method = 'fifo' + self.landed_cost.categ_id.property_valuation = 'real_time' + + # Create PO + po = self.env['purchase.order'].create({ + 'partner_id': self.partner_a.id, + 'currency_id': self.company_data['currency'].id, + 'order_line': [ + (0, 0, { + 'name': self.product_a.name, + 'product_id': self.product_a.id, + 'product_qty': 1.0, + 'product_uom': self.product_a.uom_po_id.id, + 'price_unit': 100.0, + 'taxes_id': False, + }), + (0, 0, { + 'name': self.landed_cost.name, + 'product_id': self.landed_cost.id, + 'product_qty': 1.0, + 'price_unit': 100.0, + }), + ], + }) + po.button_confirm() + + receipt = po.picking_ids + receipt.move_ids.quantity_done = 1 + receipt.button_validate() + po.order_line[1].qty_received = 1 + + po.action_create_invoice() + bill = po.invoice_ids + + # Create and validate LC + lc = self.env['stock.landed.cost'].create(dict( + picking_ids=[(6, 0, [receipt.id])], + account_journal_id=self.stock_journal.id, + cost_lines=[ + (0, 0, { + 'name': 'equal split', + 'split_method': 'equal', + 'price_unit': 100, + 'product_id': self.landed_cost.id, + }), + ], + )) + lc.compute_landed_cost() + lc.button_validate() + + user = self.env['res.users'].create({ + 'name': 'User h', + 'login': 'usher', + 'email': 'usher@yourcompany.com', + 'groups_id': [(6, 0, [self.env.ref('account.group_account_invoice').id])] + }) + # Post the bill + bill.landed_costs_ids = [(6, 0, lc.id)] + bill.invoice_date = Date.today() + bill.with_user(user)._post() + + landed_cost_aml = bill.invoice_line_ids.filtered(lambda l: l.product_id == self.landed_cost) + self.assertTrue(landed_cost_aml.reconciled)