diff --git a/stock_available_base_exclude_location/README.rst b/stock_available_base_exclude_location/README.rst new file mode 100644 index 000000000..f2d1df552 --- /dev/null +++ b/stock_available_base_exclude_location/README.rst @@ -0,0 +1,92 @@ +===================================== +Stock Available Base Exclude Location +===================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |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/10.0/stock_available_base_exclude_location + :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-10-0/stock-logistics-warehouse-10-0-stock_available_base_exclude_location + :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/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module is a technical base module to allow defining excluded locations +on an Odoo model. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +In new module, inherit from "stock.exclude.location.mixin" model on +the wanted model. + +Then, when querying for product availability, add to context the key +"excluded_location_ids" with your model "stock_excluded_location_ids" +property. + +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 +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Denis Roussel + +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. + +.. |maintainer-rousseldenis| image:: https://github.com/rousseldenis.png?size=40px + :target: https://github.com/rousseldenis + :alt: rousseldenis + +Current `maintainer `__: + +|maintainer-rousseldenis| + +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_available_base_exclude_location/__init__.py b/stock_available_base_exclude_location/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/stock_available_base_exclude_location/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_available_base_exclude_location/__manifest__.py b/stock_available_base_exclude_location/__manifest__.py new file mode 100644 index 000000000..4a539f3fb --- /dev/null +++ b/stock_available_base_exclude_location/__manifest__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Stock Available Base Exclude Location', + 'summary': """ + Base module to exclude locations for product available quantities""", + 'version': '10.0.1.0.0', + 'category': 'Warehouse', + 'maintainers': ['rousseldenis'], + 'license': 'AGPL-3', + 'author': 'ACSONE SA/NV,Odoo Community Association (OCA)', + 'website': 'https://acsone.eu', + 'depends': [ + "stock" + ], +} diff --git a/stock_available_base_exclude_location/models/__init__.py b/stock_available_base_exclude_location/models/__init__.py new file mode 100644 index 000000000..53274ab95 --- /dev/null +++ b/stock_available_base_exclude_location/models/__init__.py @@ -0,0 +1,2 @@ +from . import product_product +from . import stock_exclude_location_mixin diff --git a/stock_available_base_exclude_location/models/product_product.py b/stock_available_base_exclude_location/models/product_product.py new file mode 100644 index 000000000..b62e027e6 --- /dev/null +++ b/stock_available_base_exclude_location/models/product_product.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.osv import expression +from odoo import models + + +class ProductProduct(models.Model): + + _inherit = "product.product" + + def _get_domain_locations_new( + self, location_ids, company_id=False, compute_child=True): + """ + This is used to exclude locations if needed + :param location_ids: + :param company_id: + :param compute_child: + :return: + """ + domain_quant_loc, domain_move_in_loc, domain_move_out_loc = super( + ProductProduct, self)._get_domain_locations_new( + location_ids=location_ids, + company_id=company_id, + compute_child=compute_child) + excluded_location_ids = self.env.context.get("excluded_location_ids") + if excluded_location_ids: + domain_quant_loc = expression.AND([ + [("location_id", "not in", excluded_location_ids.ids)], + domain_quant_loc, + ]) + domain_move_in_loc = expression.AND([ + [("location_dest_id", "not in", excluded_location_ids.ids)], + domain_quant_loc, + ]) + domain_move_out_loc = expression.AND([ + [("location_id", "not in", excluded_location_ids.ids)], + domain_quant_loc, + ]) + return domain_quant_loc, domain_move_in_loc, domain_move_out_loc diff --git a/stock_available_base_exclude_location/models/stock_exclude_location_mixin.py b/stock_available_base_exclude_location/models/stock_exclude_location_mixin.py new file mode 100644 index 000000000..e77b02025 --- /dev/null +++ b/stock_available_base_exclude_location/models/stock_exclude_location_mixin.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockExcludeLocationMixin(models.AbstractModel): + + _name = 'stock.exclude.location.mixin' + + stock_excluded_location_ids = fields.Many2many( + comodel_name="stock.location", + help="Fill in this field to exclude locations for product available" + "quantities." + ) diff --git a/stock_available_base_exclude_location/readme/CONTRIBUTORS.rst b/stock_available_base_exclude_location/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..9179ee4b8 --- /dev/null +++ b/stock_available_base_exclude_location/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Denis Roussel diff --git a/stock_available_base_exclude_location/readme/DESCRIPTION.rst b/stock_available_base_exclude_location/readme/DESCRIPTION.rst new file mode 100644 index 000000000..9fc10b0ca --- /dev/null +++ b/stock_available_base_exclude_location/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module is a technical base module to allow defining excluded locations +on an Odoo model. diff --git a/stock_available_base_exclude_location/readme/USAGE.rst b/stock_available_base_exclude_location/readme/USAGE.rst new file mode 100644 index 000000000..0fb2bad63 --- /dev/null +++ b/stock_available_base_exclude_location/readme/USAGE.rst @@ -0,0 +1,6 @@ +In new module, inherit from "stock.exclude.location.mixin" model on +the wanted model. + +Then, when querying for product availability, add to context the key +"excluded_location_ids" with your model "stock_excluded_location_ids" +property. diff --git a/stock_available_base_exclude_location/static/description/icon.png b/stock_available_base_exclude_location/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/stock_available_base_exclude_location/static/description/icon.png differ diff --git a/stock_available_base_exclude_location/static/description/index.html b/stock_available_base_exclude_location/static/description/index.html new file mode 100644 index 000000000..0c682ff13 --- /dev/null +++ b/stock_available_base_exclude_location/static/description/index.html @@ -0,0 +1,431 @@ + + + + + + +Stock Available Base Exclude Location + + + +
+

Stock Available Base Exclude Location

+ + +

Beta License: AGPL-3 OCA/stock-logistics-warehouse Translate me on Weblate Try me on Runbot

+

This module is a technical base module to allow defining excluded locations +on an Odoo model.

+

Table of contents

+ +
+

Usage

+

In new module, inherit from “stock.exclude.location.mixin” model on +the wanted model.

+

Then, when querying for product availability, add to context the key +“excluded_location_ids” with your model “stock_excluded_location_ids” +property.

+
+
+

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

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

rousseldenis

+

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_available_base_exclude_location/tests/__init__.py b/stock_available_base_exclude_location/tests/__init__.py new file mode 100644 index 000000000..d48fb9c88 --- /dev/null +++ b/stock_available_base_exclude_location/tests/__init__.py @@ -0,0 +1,2 @@ +from . import common +from . import test_stock_exclude_location diff --git a/stock_available_base_exclude_location/tests/common.py b/stock_available_base_exclude_location/tests/common.py new file mode 100644 index 000000000..4c6d2cc9c --- /dev/null +++ b/stock_available_base_exclude_location/tests/common.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import mock +from operator import attrgetter +from odoo import models + + +class TestExcludeLocationMixin(object): + # Generate xmlids + # This is needed if you want to load data tied to a test model via xid. + _test_setup_gen_xid = False + # If you extend a real model (ie: res.partner) you must enable this + # to not delete the model on tear down. + _test_teardown_no_delete = False + # You can add custom fields to real models (eg: res.partner). + # In this case you must delete them to leave registry and model clean. + # This is mandatory for relational fields that link a fake model. + _test_purge_fields = [] + + @classmethod + def _test_setup_model(cls, env): + """Initialize it.""" + with mock.patch.object(env.cr, "commit"): + cls._build_model(env.registry, env.cr) + env.registry.setup_models(env.cr) + ctx = dict(env.context, update_custom_fields=True) + env.registry.init_models(env.cr, [cls._name], ctx) + + @classmethod + def _test_teardown_model(cls, env): + """Cleanup registry and real models.""" + + for fname in cls._test_purge_fields: + model = env[cls._name] + if fname in model: + model._pop_field(fname) + + if not getattr(cls, "_test_teardown_no_delete", False): + del env.registry.models[cls._name] + # here we must remove the model from list of children of inherited + # models + parents = cls._inherit + parents = ( + [parents] + if isinstance(parents, basestring) + else (parents or []) + ) + # keep a copy to be sure to not modify the original _inherit + parents = list(parents) + parents.extend(cls._inherits.keys()) + parents.append("base") + funcs = [ + attrgetter(kind + "_children") + for kind in ["_inherits", "_inherit"] + ] + for parent in parents: + for func in funcs: + children = func(env.registry[parent]) + if cls._name in children: + # at this stage our cls is referenced as children of + # parent -> must un reference it + children.remove(cls._name) + + +class ExcludeLocationModelFake(models.Model, TestExcludeLocationMixin): + + _name = "exclude.location.fake" + _inherit = "stock.exclude.location.mixin" + _description = "Exclude Location Fake" diff --git a/stock_available_base_exclude_location/tests/test_stock_exclude_location.py b/stock_available_base_exclude_location/tests/test_stock_exclude_location.py new file mode 100644 index 000000000..f552eeea2 --- /dev/null +++ b/stock_available_base_exclude_location/tests/test_stock_exclude_location.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.tests import SavepointCase +from .common import TestExcludeLocationMixin, ExcludeLocationModelFake + + +class TestExcludeLocation(SavepointCase, TestExcludeLocationMixin): + + @classmethod + def setUpClass(cls): + super(TestExcludeLocation, cls).setUpClass() + ExcludeLocationModelFake._test_setup_model(cls.env) + cls.fake = cls.env["exclude.location.fake"].create({}) + cls.location_shop = cls.env.ref("stock.stock_location_shop0") + vals = { + "location_id": cls.location_shop.id, + "name": "Sub Location 1" + } + cls.sub_location_1 = cls.env["stock.location"].create(vals) + cls.sub_location_1._parent_store_compute() + cls.product = cls.env.ref("product.product_product_4") + + @classmethod + def tearDownClass(cls): + ExcludeLocationModelFake._test_teardown_model(cls.env) + super(TestExcludeLocation, cls).tearDownClass() + + def _add_stock_to_product(self, product, location, qty): + """ + Set the stock quantity of the product + :param product: product.product recordset + :param qty: float + """ + wizard = self.env["stock.change.product.qty"].create( + { + "product_id": product.id, + "new_quantity": qty, + "location_id": location.id, + } + ) + wizard.change_product_qty() + + def test_exclude_location(self): + # Add different levels of stock for product as : + # Shop 0: 50.0 + # Sub Level (Shop 0 / Sub Location 1): 25.0 + # Query product stock availability normally and with excluded + # location as Sub Location 1 + self._add_stock_to_product(self.product, self.location_shop, 50.0) + self._add_stock_to_product(self.product, self.sub_location_1, 25.0) + self.fake.stock_excluded_location_ids = self.sub_location_1 + qty = self.product.with_context( + excluded_location_ids=self.fake.stock_excluded_location_ids).\ + qty_available + self.assertEquals( + 50.0, + qty + ) + qty = self.product.qty_available + self.assertEquals( + 75.0, + qty + )