From 23e007f72912e403ea2e2e67f59865569b8021d9 Mon Sep 17 00:00:00 2001 From: Juany Davila Date: Thu, 1 Dec 2022 10:12:35 -0500 Subject: [PATCH] [ADD] stock_valuation_layer_total_value: add to 14.0 --- stock_valuation_layer_total_value/README.rst | 85 +++++++++++++++++++ stock_valuation_layer_total_value/__init__.py | 2 + .../__manifest__.py | 18 ++++ .../models/__init__.py | 2 + .../models/stock_valuation_layer.py | 35 ++++++++ .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 2 + .../readme/USAGE.rst | 5 ++ .../test/__init__.py | 1 + .../test_stock_valuation_layer_total_value.py | 84 ++++++++++++++++++ .../views/stock_valuation_layer.xml | 33 +++++++ 11 files changed, 269 insertions(+) create mode 100644 stock_valuation_layer_total_value/README.rst create mode 100644 stock_valuation_layer_total_value/__init__.py create mode 100644 stock_valuation_layer_total_value/__manifest__.py create mode 100644 stock_valuation_layer_total_value/models/__init__.py create mode 100644 stock_valuation_layer_total_value/models/stock_valuation_layer.py create mode 100644 stock_valuation_layer_total_value/readme/CONTRIBUTORS.rst create mode 100644 stock_valuation_layer_total_value/readme/DESCRIPTION.rst create mode 100644 stock_valuation_layer_total_value/readme/USAGE.rst create mode 100644 stock_valuation_layer_total_value/test/__init__.py create mode 100644 stock_valuation_layer_total_value/test/test_stock_valuation_layer_total_value.py create mode 100644 stock_valuation_layer_total_value/views/stock_valuation_layer.xml diff --git a/stock_valuation_layer_total_value/README.rst b/stock_valuation_layer_total_value/README.rst new file mode 100644 index 000000000000..e321bdc0cfcc --- /dev/null +++ b/stock_valuation_layer_total_value/README.rst @@ -0,0 +1,85 @@ +==================== +Stock Secondary Unit +==================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png + :target: https://odoo-community.org/page/development-status + :alt: Production/Stable +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--warehouse-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-warehouse/tree/14.0/stock_secondary_unit + :alt: OCA/stock-logistics-warehouse +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-warehouse-14-0/stock-logistics-warehouse-14-0-stock_secondary_unit + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/153/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module shows the value of the original valuation layer after doing +a landed cost. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module you need to: + +1.- Generate a stock valuation layer (ex: create a purchase of the product) +2.- Generate another valuation layer that is linked to the first one (This can be done using landed cost) +3.- Now you have on the original valuation the original cost and the updated cost (the one generated on the second valuation) + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Jordi Ballester +* Juany Davila + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/stock-logistics-warehouse `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_valuation_layer_total_value/__init__.py b/stock_valuation_layer_total_value/__init__.py new file mode 100644 index 000000000000..3275ac2adf3d --- /dev/null +++ b/stock_valuation_layer_total_value/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import models diff --git a/stock_valuation_layer_total_value/__manifest__.py b/stock_valuation_layer_total_value/__manifest__.py new file mode 100644 index 000000000000..b7c6ce970887 --- /dev/null +++ b/stock_valuation_layer_total_value/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2022 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Stock Valuation Layer Total Value", + "summary": "Show total value on tree and form view", + "version": "14.0.0.0.0", + "development_status": "Production/Stable", + "category": "stock", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "author": "Forgeflow, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": ["stock_account"], + "data": [ + "views/stock_valuation_layer.xml", + ], +} diff --git a/stock_valuation_layer_total_value/models/__init__.py b/stock_valuation_layer_total_value/models/__init__.py new file mode 100644 index 000000000000..8d0ac8b415a5 --- /dev/null +++ b/stock_valuation_layer_total_value/models/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import stock_valuation_layer diff --git a/stock_valuation_layer_total_value/models/stock_valuation_layer.py b/stock_valuation_layer_total_value/models/stock_valuation_layer.py new file mode 100644 index 000000000000..cc7c42236c6d --- /dev/null +++ b/stock_valuation_layer_total_value/models/stock_valuation_layer.py @@ -0,0 +1,35 @@ +# Copyright 2022 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, fields, models + + +class StockValuationLayer(models.Model): + _inherit = "stock.valuation.layer" + + total_value_with_additional_costs = fields.Float( + string="Total value (with additional costs)", + compute="_compute_original_layer_values", + help="This is the sum of the total value's layer and total value of child layers", + store=True, + ) + unit_price_with_extra_cost = fields.Float( + string="New unit price (with additional costs)", + compute="_compute_original_layer_values", + help="This is the unit cost after the additional costs are added", + store=True, + ) + + @api.depends("stock_valuation_layer_ids") + def _compute_original_layer_values(self): + for rec in self: + if len(rec.stock_valuation_layer_ids): + children_value = sum(rec.stock_valuation_layer_ids.mapped("value")) + total_value = rec.value + children_value + new_unit_price = ( + (total_value / rec.quantity) if rec.quantity else rec.unit_cost + ) + else: + total_value = rec.value + new_unit_price = rec.unit_cost + rec.total_value_with_additional_costs = total_value + rec.unit_price_with_extra_cost = new_unit_price diff --git a/stock_valuation_layer_total_value/readme/CONTRIBUTORS.rst b/stock_valuation_layer_total_value/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..a969a75de672 --- /dev/null +++ b/stock_valuation_layer_total_value/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Jordi Ballester +* Juany Davila diff --git a/stock_valuation_layer_total_value/readme/DESCRIPTION.rst b/stock_valuation_layer_total_value/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..248417938aa7 --- /dev/null +++ b/stock_valuation_layer_total_value/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module shows the value of the original valuation layer after doing +a landed cost. diff --git a/stock_valuation_layer_total_value/readme/USAGE.rst b/stock_valuation_layer_total_value/readme/USAGE.rst new file mode 100644 index 000000000000..f4471aec51a6 --- /dev/null +++ b/stock_valuation_layer_total_value/readme/USAGE.rst @@ -0,0 +1,5 @@ +To use this module you need to: + +1.- Generate a stock valuation layer (ex: create a purchase of the product) +2.- Generate another valuation layer that is linked to the first one (This can be done using landed cost) +3.- Now you have on the original valuation the original cost and the updated cost (the one generated on the second valuation) diff --git a/stock_valuation_layer_total_value/test/__init__.py b/stock_valuation_layer_total_value/test/__init__.py new file mode 100644 index 000000000000..56b8aac5a918 --- /dev/null +++ b/stock_valuation_layer_total_value/test/__init__.py @@ -0,0 +1 @@ +from . import test_stock_valuation_layer_total_value diff --git a/stock_valuation_layer_total_value/test/test_stock_valuation_layer_total_value.py b/stock_valuation_layer_total_value/test/test_stock_valuation_layer_total_value.py new file mode 100644 index 000000000000..7fc8f7aaf005 --- /dev/null +++ b/stock_valuation_layer_total_value/test/test_stock_valuation_layer_total_value.py @@ -0,0 +1,84 @@ +# Copyright 2022 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ( + ValuationReconciliationTestCommon, +) + + +class TestValuationLayerTotalValue(ValuationReconciliationTestCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.stock_account_product_categ = cls.env["product.category"].create( + { + "name": "Test category", + "property_valuation": "real_time", + "property_cost_method": "fifo", + "property_stock_valuation_account_id": cls.company_data[ + "default_account_stock_valuation" + ].id, + "property_stock_account_input_categ_id": cls.company_data[ + "default_account_stock_in" + ].id, + "property_stock_account_output_categ_id": cls.company_data[ + "default_account_stock_out" + ].id, + } + ) + cls.product1 = cls.env["product.product"].create( + { + "name": "product1", + "type": "product", + "categ_id": cls.stock_account_product_categ.id, + } + ) + + def _make_in_move(self, product, quantity, unit_cost=None, create_picking=False): + """Helper to create and validate a receipt move.""" + unit_cost = unit_cost or product.standard_price + in_move = self.env["stock.move"].create( + { + "name": "in %s units @ %s per unit" % (str(quantity), str(unit_cost)), + "product_id": product.id, + "location_id": self.env.ref("stock.stock_location_suppliers").id, + "location_dest_id": self.company_data[ + "default_warehouse" + ].lot_stock_id.id, + "product_uom": self.env.ref("uom.product_uom_unit").id, + "product_uom_qty": quantity, + "price_unit": unit_cost, + "picking_type_id": self.company_data["default_warehouse"].in_type_id.id, + } + ) + + if create_picking: + picking = self.env["stock.picking"].create( + { + "picking_type_id": in_move.picking_type_id.id, + "location_id": in_move.location_id.id, + "location_dest_id": in_move.location_dest_id.id, + } + ) + in_move.write({"picking_id": picking.id}) + + in_move._action_confirm() + in_move._action_assign() + in_move.move_line_ids.qty_done = quantity + in_move._action_done() + + return in_move.with_context(svl=True) + + def test_valuation_layer_values(self): + move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True) + move2 = self._make_in_move(self.product1, 5, unit_cost=15, create_picking=True) + original_svl = move1.stock_valuation_layer_ids + new_svl = move2.stock_valuation_layer_ids + original_svl.write({"stock_valuation_layer_ids": [(4, new_svl.id)]}) + self.assertEqual( + original_svl.total_value_with_additional_costs, + original_svl.total_value_with_additional_costs + new_svl.value, + ) + self.assertEqual( + original_svl.unit_price_with_extra_cost, + original_svl.total_value_with_additional_costs / original_svl.quantity, + ) diff --git a/stock_valuation_layer_total_value/views/stock_valuation_layer.xml b/stock_valuation_layer_total_value/views/stock_valuation_layer.xml new file mode 100644 index 000000000000..02bfe6a65b36 --- /dev/null +++ b/stock_valuation_layer_total_value/views/stock_valuation_layer.xml @@ -0,0 +1,33 @@ + + + + + Stock Valuation Layer + stock.valuation.layer + + + + + + + + + + + + + + + + Stock Valuation Layer + stock.valuation.layer + + + + + + + + +