From 5508ca1626c54eb2bc3abbf4aa9dcd58303e3821 Mon Sep 17 00:00:00 2001 From: AaronHForgeFlow Date: Fri, 4 Mar 2022 10:56:07 +0100 Subject: [PATCH] [15.0][IMP] Tests for stock valuation [FIX] rma: rma_custmer_user has no write permissions in partner, so compute method fails. [IMP] rma: use rma user in tests [FIX] rma_account: move_line_id field string [IMP] rma, rma_account, rma_sale, rma_purchase: tests for stock valuation [FIX] account_move_line_rma_order_line: minor lint, make auto-install --- rma_purchase/models/procurement.py | 12 +-- rma_purchase/tests/__init__.py | 1 + .../tests/test_rma_stock_account_purchase.py | 81 +++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 rma_purchase/tests/test_rma_stock_account_purchase.py diff --git a/rma_purchase/models/procurement.py b/rma_purchase/models/procurement.py index 854181f5..63993911 100644 --- a/rma_purchase/models/procurement.py +++ b/rma_purchase/models/procurement.py @@ -37,16 +37,18 @@ class StockRule(models.Model): if moves: # TODO: Should we be smart in the choice of the move? layers = moves.mapped("stock_valuation_layer_ids") - cost = layers[-1].unit_cost - res["price_unit"] = cost + if layers: + cost = layers[-1].unit_cost + res["price_unit"] = cost elif line.account_move_line_id.purchase_line_id: purchase_lines = line.account_move_line_id.purchase_line_id moves = purchase_lines.mapped("move_ids") if moves: layers = moves.mapped("stock_valuation_layer_ids") - cost = layers[-1].unit_cost - # TODO: Should we be smart in the choice of the move? - res["price_unit"] = cost + if layers: + cost = layers[-1].unit_cost + # TODO: Should we be smart in the choice of the move? + res["price_unit"] = cost return res diff --git a/rma_purchase/tests/__init__.py b/rma_purchase/tests/__init__.py index c7f1b5f2..4d1e8f9e 100644 --- a/rma_purchase/tests/__init__.py +++ b/rma_purchase/tests/__init__.py @@ -1 +1,2 @@ from . import test_rma_purchase +from . import test_rma_stock_account_purchase diff --git a/rma_purchase/tests/test_rma_stock_account_purchase.py b/rma_purchase/tests/test_rma_stock_account_purchase.py new file mode 100644 index 00000000..2bb25bf1 --- /dev/null +++ b/rma_purchase/tests/test_rma_stock_account_purchase.py @@ -0,0 +1,81 @@ +# Copyright 2017-22 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo.fields import Datetime +from odoo.tests.common import Form + +# pylint: disable=odoo-addons-relative-import +from odoo.addons.rma_account.tests.test_rma_stock_account import TestRmaStockAccount + + +class TestRmaStockAccountPurchase(TestRmaStockAccount): + @classmethod + def setUpClass(cls): + super(TestRmaStockAccountPurchase, cls).setUpClass() + cls.pol_model = cls.env["purchase.order.line"] + cls.po_model = cls.env["purchase.order"] + # Create PO: + cls.product_fifo_1.standard_price = 1234 + cls.po = cls.po_model.create( + { + "partner_id": cls.partner_id.id, + } + ) + cls.pol_1 = cls.pol_model.create( + { + "name": cls.product_fifo_1.name, + "order_id": cls.po.id, + "product_id": cls.product_fifo_1.id, + "product_qty": 20.0, + "product_uom": cls.product_fifo_1.uom_id.id, + "price_unit": 100.0, + "date_planned": Datetime.now(), + } + ) + cls.po.button_confirm() + cls._do_picking(cls.po.picking_ids) + + def test_01_cost_from_po_move(self): + """ + Test the price unit is taken from the cost of the stock move associated to + the PO + """ + self.product_fifo_1.standard_price = 1234 # this should not be taken + supplier_view = self.env.ref("rma_purchase.view_rma_line_form") + rma_line = Form( + self.rma_line.with_context(supplier=1).with_user(self.rma_basic_user), + view=supplier_view.id, + ) + rma_line.partner_id = self.po.partner_id + rma_line.purchase_order_line_id = self.pol_1 + rma_line.price_unit = 4356 + rma_line = rma_line.save() + rma_line.action_rma_to_approve() + picking = self._deliver_rma(rma_line) + # The price is not the standard price, is the value of the incoming layer + # of the PO + rma_move_value = picking.move_lines.stock_valuation_layer_ids.value + po_move_value = self.po.picking_ids.mapped( + "move_lines.stock_valuation_layer_ids" + )[-1].value + self.assertEqual(-rma_move_value, po_move_value) + # Test the accounts used + account_move = picking.move_lines.stock_valuation_layer_ids.account_move_id + self.check_accounts_used(account_move, "grni", "inventory") + # Now forcing a refund to check the stock journals + rma_line.refund_policy = "ordered" + make_refund = ( + self.env["rma.refund"] + .with_context( + **{ + "customer": True, + "active_ids": rma_line.ids, + "active_model": "rma.order.line", + } + ) + .create({"description": "Test refund"}) + ) + make_refund.invoice_refund() + rma_line.refund_line_ids.move_id.action_post() + account_move = rma_line.mapped("refund_line_ids.move_id") + self.check_accounts_used(account_move, credit_account="grni")