diff --git a/account_multi_store/models/account_journal.py b/account_multi_store/models/account_journal.py new file mode 100644 index 00000000..7003b01d --- /dev/null +++ b/account_multi_store/models/account_journal.py @@ -0,0 +1,59 @@ +<<<<<<< HEAD +||||||| MERGE BASE +======= +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## +from odoo import api, fields, models +from odoo.tools import config + + +class AccountJournal(models.Model): + _inherit = "account.journal" + + store_id = fields.Many2one( + "res.store", + "Store", + help="Store used for data analysis and also users that are not of this" + " store, can only see this journal records but can not post or modify " + " any entry on them.", + ) + + @api.model + def _search(self, domain, offset=0, limit=None, order=None): + """ + Para que los usuarios no puedan elegir diarios donde no puedan + escribir, modificamos la funcion search. No lo hacemos por regla de + permiso ya que si no pueden ver los diarios termina dando errores en + cualquier lugar que se use un campo related a algo del diario + """ + user = self.env.user + # if superadmin, do not apply + if not self.env.is_superuser() and not self.env.context.get("skip_store_filter"): + domain = domain + ["|", ("store_id", "=", False), ("store_id", "child_of", user.store_ids.ids)] + if not config["test_enable"]: + self = self.with_context(active_test=False) + return super(AccountJournal, self)._search(domain, offset, limit, order) + + def fetch(self, field_names): + # Since 17.0, fetch() resolves the ids through _search() as the current user, so the store + # filter above forbids reading journals of other stores (allowed up to 16.0). The filter + # is only meant to restrict choosing journals, not reading them by id. + # keep env.company (cache key of company-dependent fields) and env.companies (multi-company rules) + company = self.env.company + company_ids = [company.id] + [cid for cid in self.env.companies.ids if cid != company.id] + records = self.with_context(skip_store_filter=True, allowed_company_ids=company_ids) + return super(AccountJournal, records).fetch(field_names) + + @api.model + @api.readonly + @api.returns("self") + def search(self, domain, offset=0, limit=None, order=None): + user = self.env.user + journal_ids = user.mapped("store_ids.journal_ids").ids + if not self.with_user(user.id).env.is_superuser() and journal_ids and limit == 1: + domain += ["|", ("store_id", "=", False), ("id", "in", journal_ids)] + return super().search(domain, offset, limit, order) + +>>>>>>> FORWARD PORTED diff --git a/account_multi_store/tests/__init__.py b/account_multi_store/tests/__init__.py new file mode 100644 index 00000000..1e92d485 --- /dev/null +++ b/account_multi_store/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_journal_store diff --git a/account_multi_store/tests/test_account_journal_store.py b/account_multi_store/tests/test_account_journal_store.py new file mode 100644 index 00000000..f463f492 --- /dev/null +++ b/account_multi_store/tests/test_account_journal_store.py @@ -0,0 +1,43 @@ +from odoo.addons.account.tests.common import AccountTestInvoicingCommon +from odoo.exceptions import AccessError +from odoo.tests import tagged + + +@tagged("post_install", "-at_install") +class TestAccountJournalStore(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + company = cls.company_data["company"] + cls.store_own = cls.env["res.store"].create({"name": "Own store", "company_id": company.id}) + cls.store_other = cls.env["res.store"].create({"name": "Other store", "company_id": company.id}) + # The user that sets up the data works on every store + cls.env.user.store_ids = cls.store_own | cls.store_other + cls.journal_other = cls.company_data["default_journal_sale"].copy( + {"name": "Other store sales", "code": "OSS", "store_id": cls.store_other.id} + ) + cls.invoice_other = cls.init_invoice( + "out_invoice", products=cls.product_a, journal=cls.journal_other, post=True + ) + cls.store_user = cls.env["res.users"].create( + { + "name": "Store user", + "login": "store_user", + "company_id": company.id, + "company_ids": [(6, 0, company.ids)], + "store_ids": [(6, 0, cls.store_own.ids)], + "groups_id": [(6, 0, cls.env.ref("account.group_account_invoice").ids)], + } + ) + + def test_read_other_store_journal_but_not_choose_or_edit_it(self): + invoice = self.invoice_other.with_user(self.store_user) + # Opening an entry of another store reads its journal, as up to 16.0 + self.assertEqual(invoice.journal_id.display_name, self.journal_other.display_name) + invoice.read(["name", "journal_id", "amount_total"]) + # Choosing journals stays filtered by store + journals = self.env["account.journal"].with_user(self.store_user).search([]) + self.assertNotIn(self.journal_other, journals) + # Editing entries of another store stays forbidden + with self.assertRaises(AccessError): + invoice.write({"ref": "Not allowed"}) diff --git a/sale_multi_store/models/sale_order.py b/sale_multi_store/models/sale_order.py new file mode 100644 index 00000000..4a6b2421 --- /dev/null +++ b/sale_multi_store/models/sale_order.py @@ -0,0 +1,43 @@ +<<<<<<< HEAD +||||||| MERGE BASE +======= +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## +from odoo import api, fields, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + store_id = fields.Many2one( + related="warehouse_id.store_id", + store=True, + ) + + @api.model + def _search(self, args, offset=0, limit=None, order=None): + """ + Para que los usuarios no puedan elegir pickings donde no puedan + escribir, modificamos la funcion search. No lo hacemos por regla de + permiso ya que si no pueden ver los diarios termina dando errores en + cualquier lugar que se use un campo related a algo del diario + """ + user = self.env.user + # if superadmin, do not apply + if not self.env.is_superuser() and not self.env.context.get("skip_store_filter"): + args += ["|", ("store_id", "=", False), ("store_id", "child_of", user.store_ids.ids)] + return super()._search(args, offset, limit, order) + + def fetch(self, field_names): + # Since 17.0, fetch() resolves the ids through _search() as the current user, so the store + # filter above forbids reading records of other stores (allowed up to 16.0). The filter + # is only meant to restrict choosing records, not reading them by id. + # keep env.company (cache key of company-dependent fields) and env.companies (multi-company rules) + company = self.env.company + company_ids = [company.id] + [cid for cid in self.env.companies.ids if cid != company.id] + records = self.with_context(skip_store_filter=True, allowed_company_ids=company_ids) + return super(SaleOrder, records).fetch(field_names) + +>>>>>>> FORWARD PORTED diff --git a/sale_multi_store/tests/__init__.py b/sale_multi_store/tests/__init__.py new file mode 100644 index 00000000..ded80fec --- /dev/null +++ b/sale_multi_store/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_order_store diff --git a/sale_multi_store/tests/test_sale_order_store.py b/sale_multi_store/tests/test_sale_order_store.py new file mode 100644 index 00000000..c7dc36f5 --- /dev/null +++ b/sale_multi_store/tests/test_sale_order_store.py @@ -0,0 +1,39 @@ +from odoo.exceptions import AccessError +from odoo.tests import TransactionCase, tagged + + +@tagged("post_install", "-at_install") +class TestSaleOrderStore(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.store_own = cls.env["res.store"].create({"name": "Own store"}) + cls.store_other = cls.env["res.store"].create({"name": "Other store"}) + cls.warehouse_other = cls.env["stock.warehouse"].create( + {"name": "Other store warehouse", "code": "OSW", "store_id": cls.store_other.id} + ) + cls.order_other = cls.env["sale.order"].create( + { + "partner_id": cls.env["res.partner"].create({"name": "Customer"}).id, + "warehouse_id": cls.warehouse_other.id, + } + ) + cls.store_user = cls.env["res.users"].create( + { + "name": "Store user", + "login": "store_user", + "store_ids": [(6, 0, cls.store_own.ids)], + "groups_id": [(6, 0, cls.env.ref("sales_team.group_sale_salesman_all_leads").ids)], + } + ) + + def test_read_other_store_order_but_not_choose_or_edit_it(self): + order = self.order_other.with_user(self.store_user) + # Opening an order of another store by id works, as up to 16.0 + self.assertEqual(order.read(["name"])[0]["name"], self.order_other.name) + # Searching orders stays filtered by store + orders = self.env["sale.order"].with_user(self.store_user).search([]) + self.assertNotIn(self.order_other, orders) + # Editing orders of another store stays forbidden + with self.assertRaises(AccessError): + order.write({"client_order_ref": "Not allowed"}) diff --git a/stock_multi_store/models/stock_picking_type.py b/stock_multi_store/models/stock_picking_type.py new file mode 100644 index 00000000..612a2958 --- /dev/null +++ b/stock_multi_store/models/stock_picking_type.py @@ -0,0 +1,48 @@ +<<<<<<< HEAD +||||||| MERGE BASE +======= +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## +from odoo import api, fields, models + + +class StockPickingType(models.Model): + _inherit = "stock.picking.type" + + store_id = fields.Many2one( + related="warehouse_id.store_id", + store=True, + ) + + def write(self, vals): + if "active" in vals and self.mapped("store_id"): + self = self.with_context(active_test=False) + return super().write(vals) + + @api.model + def _search(self, args, offset=0, limit=None, order=None): + """ + Para que los usuarios no puedan elegir pickings donde no puedan + escribir, modificamos la funcion search. No lo hacemos por regla de + permiso ya que si no pueden ver los diarios termina dando errores en + cualquier lugar que se use un campo related a algo del diario + """ + user = self.env.user + # if superadmin, do not apply + if not self.env.is_superuser() and not self.env.context.get("skip_store_filter"): + args += ["|", ("store_id", "=", False), ("store_id", "child_of", user.store_ids.ids)] + return super()._search(args, offset, limit, order) + + def fetch(self, field_names): + # Since 17.0, fetch() resolves the ids through _search() as the current user, so the store + # filter above forbids reading records of other stores (allowed up to 16.0). The filter + # is only meant to restrict choosing records, not reading them by id. + # keep env.company (cache key of company-dependent fields) and env.companies (multi-company rules) + company = self.env.company + company_ids = [company.id] + [cid for cid in self.env.companies.ids if cid != company.id] + records = self.with_context(skip_store_filter=True, allowed_company_ids=company_ids) + return super(StockPickingType, records).fetch(field_names) + +>>>>>>> FORWARD PORTED diff --git a/stock_multi_store/tests/__init__.py b/stock_multi_store/tests/__init__.py new file mode 100644 index 00000000..6ec46f2f --- /dev/null +++ b/stock_multi_store/tests/__init__.py @@ -0,0 +1 @@ +from . import test_stock_picking_type_store diff --git a/stock_multi_store/tests/test_stock_picking_type_store.py b/stock_multi_store/tests/test_stock_picking_type_store.py new file mode 100644 index 00000000..b47dfe18 --- /dev/null +++ b/stock_multi_store/tests/test_stock_picking_type_store.py @@ -0,0 +1,40 @@ +from odoo.exceptions import AccessError +from odoo.tests import TransactionCase, tagged + + +@tagged("post_install", "-at_install") +class TestStockPickingTypeStore(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.store_own = cls.env["res.store"].create({"name": "Own store"}) + cls.store_other = cls.env["res.store"].create({"name": "Other store"}) + cls.warehouse_other = cls.env["stock.warehouse"].create( + {"name": "Other store warehouse", "code": "OSW", "store_id": cls.store_other.id} + ) + cls.picking_other = cls.env["stock.picking"].create( + { + "picking_type_id": cls.warehouse_other.out_type_id.id, + "location_id": cls.warehouse_other.lot_stock_id.id, + "location_dest_id": cls.env.ref("stock.stock_location_customers").id, + } + ) + cls.store_user = cls.env["res.users"].create( + { + "name": "Store user", + "login": "store_user", + "store_ids": [(6, 0, cls.store_own.ids)], + "groups_id": [(6, 0, cls.env.ref("stock.group_stock_user").ids)], + } + ) + + def test_read_other_store_picking_type_but_not_choose_or_edit_it(self): + picking = self.picking_other.with_user(self.store_user) + # Opening a transfer of another store reads its operation type, as up to 16.0 + self.assertEqual(picking.picking_type_id.display_name, self.warehouse_other.out_type_id.display_name) + # Choosing operation types stays filtered by store + picking_types = self.env["stock.picking.type"].with_user(self.store_user).search([]) + self.assertNotIn(self.warehouse_other.out_type_id, picking_types) + # Editing transfers of another store stays forbidden + with self.assertRaises(AccessError): + picking.write({"origin": "Not allowed"})