From 519f7d9bd96624cde04e3cd16fdbcd6b58fe4229 Mon Sep 17 00:00:00 2001 From: Andrea Date: Wed, 20 Apr 2022 16:34:53 +0200 Subject: [PATCH 1/2] [13.0][FIX/IMP] Make RMA Operation settings company dependent --- rma/migrations/12.0.3.0.0/post-migration.py | 101 ++++++++++++++++++++ rma/models/product.py | 3 + rma/models/product_category.py | 2 + rma/tests/__init__.py | 1 + rma/tests/test_migrations.py | 64 +++++++++++++ rma/tests/test_rma.py | 40 +++++++- 6 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 rma/migrations/12.0.3.0.0/post-migration.py create mode 100644 rma/tests/test_migrations.py diff --git a/rma/migrations/12.0.3.0.0/post-migration.py b/rma/migrations/12.0.3.0.0/post-migration.py new file mode 100644 index 00000000..ca594680 --- /dev/null +++ b/rma/migrations/12.0.3.0.0/post-migration.py @@ -0,0 +1,101 @@ + +import logging + +_logger = logging.getLogger(__name__) + + +def set_rma_customer_operation_property(cr): + """ + """ + cr.execute( + """ + WITH rma_customer_operation_id_field AS ( + SELECT id FROM ir_model_fields WHERE model='product.template' AND name='rma_customer_operation_id' + ) + INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) + SELECT 'rma_customer_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.template,', t.id), CONCAT('rma.operation,', ro.id) + FROM product_template t JOIN rma_operation ro ON t.rma_customer_operation_id = ro.id + , rma_customer_operation_id_field rco + WHERE ro.company_id IS NOT NULL + AND NOT EXISTS(SELECT 1 + FROM ir_property + WHERE fields_id=rco.id + AND company_id=ro.company_id + AND res_id=CONCAT('product.template,', t.id)) + """) + _logger.info("Added %s rma_customer_operation_id_field product properties", cr.rowcount) + + +def set_rma_supplier_operation_property(cr): + """ + """ + cr.execute( + """ + WITH rma_supplier_operation_id_field AS ( + SELECT id FROM ir_model_fields WHERE model='product.template' AND name='rma_supplier_operation_id' + ) + INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) + SELECT 'rma_supplier_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.template,', t.id), CONCAT('rma.operation,', ro.id) + FROM product_template t JOIN rma_operation ro ON t.rma_supplier_operation_id = ro.id + , rma_supplier_operation_id_field rco + WHERE ro.company_id IS NOT NULL + AND NOT EXISTS(SELECT 1 + FROM ir_property + WHERE fields_id=rco.id + AND company_id=ro.company_id + AND res_id=CONCAT('product.template,', t.id)) + """) + _logger.info("Added %s rma_supplier_operation_id_field product properties", cr.rowcount) + + +def set_rma_customer_operation_category_property(cr): + """ + """ + cr.execute( + """ + WITH rma_customer_operation_id_field AS ( + SELECT id FROM ir_model_fields WHERE model='product.category' AND name='rma_customer_operation_id' + ) + INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) + SELECT 'rma_customer_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.category,', pc.id), CONCAT('rma.operation,', ro.id) + FROM product_category pc JOIN rma_operation ro ON pc.rma_customer_operation_id = ro.id + , rma_customer_operation_id_field rco + WHERE ro.company_id IS NOT NULL + AND NOT EXISTS(SELECT 1 + FROM ir_property + WHERE fields_id=rco.id + AND company_id=ro.company_id + AND res_id=CONCAT('product.category,', pc.id)) + """) + _logger.info("Added %s rma_customer_operation_id_field product category properties", cr.rowcount) + + +def set_rma_supplier_operation_category_property(cr): + """ + """ + cr.execute( + """ + WITH rma_supplier_operation_id_field AS ( + SELECT id FROM ir_model_fields WHERE model='product.category' AND name='rma_supplier_operation_id' + ) + INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) + SELECT 'rma_supplier_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.category,', pc.id), CONCAT('rma.operation,', ro.id) + FROM product_category pc JOIN rma_operation ro ON pc.rma_supplier_operation_id = ro.id + , rma_supplier_operation_id_field rco + WHERE ro.company_id IS NOT NULL + AND NOT EXISTS(SELECT 1 + FROM ir_property + WHERE fields_id=rco.id + AND company_id=ro.company_id + AND res_id=CONCAT('product.category,', pc.id)) + """) + _logger.info("Added %s rma_supplier_operation_id_field product category properties", cr.rowcount) + + +def migrate(cr, version=None): + if not version: + return + set_rma_customer_operation_property(cr) + set_rma_supplier_operation_property(cr) + set_rma_customer_operation_category_property(cr) + set_rma_supplier_operation_category_property(cr) diff --git a/rma/models/product.py b/rma/models/product.py index 418e6860..ff958128 100644 --- a/rma/models/product.py +++ b/rma/models/product.py @@ -8,11 +8,14 @@ class ProductTemplate(models.Model): _inherit = "product.template" rma_customer_operation_id = fields.Many2one( + company_dependent=True, comodel_name="rma.operation", string="Default RMA Customer Operation" ) rma_supplier_operation_id = fields.Many2one( + company_dependent=True, comodel_name="rma.operation", string="Default RMA Supplier Operation" ) rma_approval_policy = fields.Selection( related="categ_id.rma_approval_policy", readonly=True ) + diff --git a/rma/models/product_category.py b/rma/models/product_category.py index b528731c..be91b24e 100644 --- a/rma/models/product_category.py +++ b/rma/models/product_category.py @@ -19,8 +19,10 @@ class ProductCategory(models.Model): "this policy will request the RMA manager approval.", ) rma_customer_operation_id = fields.Many2one( + company_dependent=True, comodel_name="rma.operation", string="Default RMA Customer Operation" ) rma_supplier_operation_id = fields.Many2one( + company_dependent=True, comodel_name="rma.operation", string="Default RMA Supplier Operation" ) diff --git a/rma/tests/__init__.py b/rma/tests/__init__.py index e485a52f..79b6dc13 100644 --- a/rma/tests/__init__.py +++ b/rma/tests/__init__.py @@ -2,3 +2,4 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) from . import test_rma +from . import test_migrations diff --git a/rma/tests/test_migrations.py b/rma/tests/test_migrations.py new file mode 100644 index 00000000..b7c8ed00 --- /dev/null +++ b/rma/tests/test_migrations.py @@ -0,0 +1,64 @@ +import imp +import os + +from odoo.modules import get_module_resource +from odoo.tests.common import TransactionCase +from odoo.tools import file_open + + +class TestMigrations(TransactionCase): + def test_migration_12_0_3_0_0(self): + """Test the migration scripts to 12.0.3.0.0 """ + + # Recreate pre-migration condition + self.env.cr.execute(""" + ALTER TABLE product_template + ADD COLUMN IF NOT EXISTS rma_customer_operation_id INTEGER """) + self.env.cr.execute(""" + ALTER TABLE product_template + ADD COLUMN IF NOT EXISTS rma_supplier_operation_id INTEGER """) + self.env.cr.execute(""" + ALTER TABLE product_category + ADD COLUMN IF NOT EXISTS rma_customer_operation_id INTEGER """) + self.env.cr.execute(""" + ALTER TABLE product_category + ADD COLUMN IF NOT EXISTS rma_supplier_operation_id INTEGER """) + + rma_operation = self.env["rma.operation"].search([], limit=1) + self.env.cr.execute( + "UPDATE product_template SET rma_customer_operation_id = %d" % rma_operation.id + ) + self.env.cr.execute( + "UPDATE product_template SET rma_supplier_operation_id = %d" % rma_operation.id + ) + self.env.cr.execute( + "UPDATE product_category SET rma_customer_operation_id = %d" % rma_operation.id + ) + self.env.cr.execute( + "UPDATE product_category SET rma_supplier_operation_id = %d" % rma_operation.id + ) + + # Properties are not set + product = self.env.ref("product.product_product_5") + product_tmpl = product.product_tmpl_id + product_categ = product.categ_id + self.assertFalse(product_tmpl.rma_customer_operation_id) + self.assertFalse(product_tmpl.rma_supplier_operation_id) + self.assertFalse(product_categ.rma_customer_operation_id) + self.assertFalse(product_categ.rma_supplier_operation_id) + + # Run the migration script + pyfile = get_module_resource( + "rma", "migrations", "12.0.3.0.0", "post-migration.py") + name, ext = os.path.splitext(os.path.basename(pyfile)) + fp, pathname = file_open(pyfile, pathinfo=True) + mod = imp.load_module(name, fp, pathname, (".py", "r", imp.PY_SOURCE)) + mod.migrate(self.env.cr, "12.0.2.3.0") + + # Properties are set + product_tmpl.refresh() + product_categ.refresh() + self.assertEqual(product_tmpl.rma_customer_operation_id, rma_operation) + self.assertEqual(product_tmpl.rma_supplier_operation_id, rma_operation) + self.assertEqual(product_categ.rma_customer_operation_id, rma_operation) + self.assertEqual(product_categ.rma_supplier_operation_id, rma_operation) diff --git a/rma/tests/test_rma.py b/rma/tests/test_rma.py index faa3dd4d..cc07c520 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -980,4 +980,42 @@ class TestRma(common.SavepointCase): ) for line in self.rma_supplier_id.rma_line_ids: line.action_rma_done() - self.assertEqual(line.mapped("state"), ["done"], "Wrong State") + self.assertEquals(line.state, 'done', + "Wrong State") + + def test_05_rma_order_line(self): + """ Property rma_customer_operation_id on product or product category + correctly handled inside _onchange_product_id() + """ + rma_operation = self.env["rma.operation"].search([], limit=1) + self.assertTrue(rma_operation) + + # Case of product template + self.rma_customer_id.rma_line_ids\ + .mapped("product_id")\ + .write({"rma_customer_operation_id": rma_operation.id}) + for line in self.rma_customer_id.rma_line_ids: + data = {'product_id': line.product_id.id} + new_line = self.rma_line.new(data) + self.assertFalse(new_line.operation_id) + self.assertTrue(new_line.product_id.rma_customer_operation_id) + self.assertFalse(new_line.product_id.categ_id.rma_customer_operation_id) + new_line._onchange_product_id() + self.assertEqual(new_line.operation_id, rma_operation) + + # Case of product category + self.rma_customer_id.rma_line_ids\ + .mapped("product_id")\ + .write({"rma_customer_operation_id": False}) + self.rma_customer_id.rma_line_ids \ + .mapped("product_id.categ_id") \ + .write({"rma_customer_operation_id": rma_operation.id}) + + for line in self.rma_customer_id.rma_line_ids: + data = {'product_id': line.product_id.id} + new_line = self.rma_line.new(data) + self.assertFalse(new_line.operation_id) + self.assertFalse(new_line.product_id.rma_customer_operation_id) + self.assertTrue(new_line.product_id.categ_id.rma_customer_operation_id) + new_line._onchange_product_id() + self.assertEqual(new_line.operation_id, rma_operation) From e329a96f4ee5fd17b8abed8ab2804128357282dd Mon Sep 17 00:00:00 2001 From: DavidFIB Date: Wed, 18 May 2022 17:11:45 +0200 Subject: [PATCH 2/2] [14.0][FIX/IMP] Make RMA Operation settings company dependent --- rma/__manifest__.py | 2 +- rma/migrations/12.0.3.0.0/post-migration.py | 101 -------------------- rma/models/product.py | 7 +- rma/models/product_category.py | 6 +- rma/tests/__init__.py | 1 - rma/tests/test_migrations.py | 64 ------------- rma/tests/test_rma.py | 29 +++--- 7 files changed, 23 insertions(+), 187 deletions(-) delete mode 100644 rma/migrations/12.0.3.0.0/post-migration.py delete mode 100644 rma/tests/test_migrations.py diff --git a/rma/__manifest__.py b/rma/__manifest__.py index 545b1b5e..546821de 100644 --- a/rma/__manifest__.py +++ b/rma/__manifest__.py @@ -10,7 +10,7 @@ "in odoo", "author": "ForgeFlow", "website": "https://github.com/ForgeFlow/stock-rma", - "depends": ["stock", "mail", "web"], + "depends": ["stock", "mail", "web", "account"], "demo": ["demo/stock_demo.xml"], "data": [ "security/rma.xml", diff --git a/rma/migrations/12.0.3.0.0/post-migration.py b/rma/migrations/12.0.3.0.0/post-migration.py deleted file mode 100644 index ca594680..00000000 --- a/rma/migrations/12.0.3.0.0/post-migration.py +++ /dev/null @@ -1,101 +0,0 @@ - -import logging - -_logger = logging.getLogger(__name__) - - -def set_rma_customer_operation_property(cr): - """ - """ - cr.execute( - """ - WITH rma_customer_operation_id_field AS ( - SELECT id FROM ir_model_fields WHERE model='product.template' AND name='rma_customer_operation_id' - ) - INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) - SELECT 'rma_customer_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.template,', t.id), CONCAT('rma.operation,', ro.id) - FROM product_template t JOIN rma_operation ro ON t.rma_customer_operation_id = ro.id - , rma_customer_operation_id_field rco - WHERE ro.company_id IS NOT NULL - AND NOT EXISTS(SELECT 1 - FROM ir_property - WHERE fields_id=rco.id - AND company_id=ro.company_id - AND res_id=CONCAT('product.template,', t.id)) - """) - _logger.info("Added %s rma_customer_operation_id_field product properties", cr.rowcount) - - -def set_rma_supplier_operation_property(cr): - """ - """ - cr.execute( - """ - WITH rma_supplier_operation_id_field AS ( - SELECT id FROM ir_model_fields WHERE model='product.template' AND name='rma_supplier_operation_id' - ) - INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) - SELECT 'rma_supplier_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.template,', t.id), CONCAT('rma.operation,', ro.id) - FROM product_template t JOIN rma_operation ro ON t.rma_supplier_operation_id = ro.id - , rma_supplier_operation_id_field rco - WHERE ro.company_id IS NOT NULL - AND NOT EXISTS(SELECT 1 - FROM ir_property - WHERE fields_id=rco.id - AND company_id=ro.company_id - AND res_id=CONCAT('product.template,', t.id)) - """) - _logger.info("Added %s rma_supplier_operation_id_field product properties", cr.rowcount) - - -def set_rma_customer_operation_category_property(cr): - """ - """ - cr.execute( - """ - WITH rma_customer_operation_id_field AS ( - SELECT id FROM ir_model_fields WHERE model='product.category' AND name='rma_customer_operation_id' - ) - INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) - SELECT 'rma_customer_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.category,', pc.id), CONCAT('rma.operation,', ro.id) - FROM product_category pc JOIN rma_operation ro ON pc.rma_customer_operation_id = ro.id - , rma_customer_operation_id_field rco - WHERE ro.company_id IS NOT NULL - AND NOT EXISTS(SELECT 1 - FROM ir_property - WHERE fields_id=rco.id - AND company_id=ro.company_id - AND res_id=CONCAT('product.category,', pc.id)) - """) - _logger.info("Added %s rma_customer_operation_id_field product category properties", cr.rowcount) - - -def set_rma_supplier_operation_category_property(cr): - """ - """ - cr.execute( - """ - WITH rma_supplier_operation_id_field AS ( - SELECT id FROM ir_model_fields WHERE model='product.category' AND name='rma_supplier_operation_id' - ) - INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) - SELECT 'rma_supplier_operation_id', 'many2one', rco.id, ro.company_id, CONCAT('product.category,', pc.id), CONCAT('rma.operation,', ro.id) - FROM product_category pc JOIN rma_operation ro ON pc.rma_supplier_operation_id = ro.id - , rma_supplier_operation_id_field rco - WHERE ro.company_id IS NOT NULL - AND NOT EXISTS(SELECT 1 - FROM ir_property - WHERE fields_id=rco.id - AND company_id=ro.company_id - AND res_id=CONCAT('product.category,', pc.id)) - """) - _logger.info("Added %s rma_supplier_operation_id_field product category properties", cr.rowcount) - - -def migrate(cr, version=None): - if not version: - return - set_rma_customer_operation_property(cr) - set_rma_supplier_operation_property(cr) - set_rma_customer_operation_category_property(cr) - set_rma_supplier_operation_category_property(cr) diff --git a/rma/models/product.py b/rma/models/product.py index ff958128..c4055c5b 100644 --- a/rma/models/product.py +++ b/rma/models/product.py @@ -9,13 +9,14 @@ class ProductTemplate(models.Model): rma_customer_operation_id = fields.Many2one( company_dependent=True, - comodel_name="rma.operation", string="Default RMA Customer Operation" + comodel_name="rma.operation", + string="Default RMA Customer Operation", ) rma_supplier_operation_id = fields.Many2one( company_dependent=True, - comodel_name="rma.operation", string="Default RMA Supplier Operation" + comodel_name="rma.operation", + string="Default RMA Supplier Operation", ) rma_approval_policy = fields.Selection( related="categ_id.rma_approval_policy", readonly=True ) - diff --git a/rma/models/product_category.py b/rma/models/product_category.py index be91b24e..db832c2e 100644 --- a/rma/models/product_category.py +++ b/rma/models/product_category.py @@ -20,9 +20,11 @@ class ProductCategory(models.Model): ) rma_customer_operation_id = fields.Many2one( company_dependent=True, - comodel_name="rma.operation", string="Default RMA Customer Operation" + comodel_name="rma.operation", + string="Default RMA Customer Operation", ) rma_supplier_operation_id = fields.Many2one( company_dependent=True, - comodel_name="rma.operation", string="Default RMA Supplier Operation" + comodel_name="rma.operation", + string="Default RMA Supplier Operation", ) diff --git a/rma/tests/__init__.py b/rma/tests/__init__.py index 79b6dc13..e485a52f 100644 --- a/rma/tests/__init__.py +++ b/rma/tests/__init__.py @@ -2,4 +2,3 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) from . import test_rma -from . import test_migrations diff --git a/rma/tests/test_migrations.py b/rma/tests/test_migrations.py deleted file mode 100644 index b7c8ed00..00000000 --- a/rma/tests/test_migrations.py +++ /dev/null @@ -1,64 +0,0 @@ -import imp -import os - -from odoo.modules import get_module_resource -from odoo.tests.common import TransactionCase -from odoo.tools import file_open - - -class TestMigrations(TransactionCase): - def test_migration_12_0_3_0_0(self): - """Test the migration scripts to 12.0.3.0.0 """ - - # Recreate pre-migration condition - self.env.cr.execute(""" - ALTER TABLE product_template - ADD COLUMN IF NOT EXISTS rma_customer_operation_id INTEGER """) - self.env.cr.execute(""" - ALTER TABLE product_template - ADD COLUMN IF NOT EXISTS rma_supplier_operation_id INTEGER """) - self.env.cr.execute(""" - ALTER TABLE product_category - ADD COLUMN IF NOT EXISTS rma_customer_operation_id INTEGER """) - self.env.cr.execute(""" - ALTER TABLE product_category - ADD COLUMN IF NOT EXISTS rma_supplier_operation_id INTEGER """) - - rma_operation = self.env["rma.operation"].search([], limit=1) - self.env.cr.execute( - "UPDATE product_template SET rma_customer_operation_id = %d" % rma_operation.id - ) - self.env.cr.execute( - "UPDATE product_template SET rma_supplier_operation_id = %d" % rma_operation.id - ) - self.env.cr.execute( - "UPDATE product_category SET rma_customer_operation_id = %d" % rma_operation.id - ) - self.env.cr.execute( - "UPDATE product_category SET rma_supplier_operation_id = %d" % rma_operation.id - ) - - # Properties are not set - product = self.env.ref("product.product_product_5") - product_tmpl = product.product_tmpl_id - product_categ = product.categ_id - self.assertFalse(product_tmpl.rma_customer_operation_id) - self.assertFalse(product_tmpl.rma_supplier_operation_id) - self.assertFalse(product_categ.rma_customer_operation_id) - self.assertFalse(product_categ.rma_supplier_operation_id) - - # Run the migration script - pyfile = get_module_resource( - "rma", "migrations", "12.0.3.0.0", "post-migration.py") - name, ext = os.path.splitext(os.path.basename(pyfile)) - fp, pathname = file_open(pyfile, pathinfo=True) - mod = imp.load_module(name, fp, pathname, (".py", "r", imp.PY_SOURCE)) - mod.migrate(self.env.cr, "12.0.2.3.0") - - # Properties are set - product_tmpl.refresh() - product_categ.refresh() - self.assertEqual(product_tmpl.rma_customer_operation_id, rma_operation) - self.assertEqual(product_tmpl.rma_supplier_operation_id, rma_operation) - self.assertEqual(product_categ.rma_customer_operation_id, rma_operation) - self.assertEqual(product_categ.rma_supplier_operation_id, rma_operation) diff --git a/rma/tests/test_rma.py b/rma/tests/test_rma.py index cc07c520..310c5888 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -980,39 +980,38 @@ class TestRma(common.SavepointCase): ) for line in self.rma_supplier_id.rma_line_ids: line.action_rma_done() - self.assertEquals(line.state, 'done', - "Wrong State") + self.assertEqual(line.state, "done", "Wrong State") def test_05_rma_order_line(self): - """ Property rma_customer_operation_id on product or product category + """Property rma_customer_operation_id on product or product category correctly handled inside _onchange_product_id() """ rma_operation = self.env["rma.operation"].search([], limit=1) self.assertTrue(rma_operation) # Case of product template - self.rma_customer_id.rma_line_ids\ - .mapped("product_id")\ - .write({"rma_customer_operation_id": rma_operation.id}) + self.rma_customer_id.rma_line_ids.mapped("product_id").sudo().write( + {"rma_customer_operation_id": rma_operation.id} + ) for line in self.rma_customer_id.rma_line_ids: - data = {'product_id': line.product_id.id} + data = {"product_id": line.product_id.id} new_line = self.rma_line.new(data) self.assertFalse(new_line.operation_id) self.assertTrue(new_line.product_id.rma_customer_operation_id) - self.assertFalse(new_line.product_id.categ_id.rma_customer_operation_id) + self.assertTrue(new_line.product_id.categ_id.rma_customer_operation_id) new_line._onchange_product_id() self.assertEqual(new_line.operation_id, rma_operation) # Case of product category - self.rma_customer_id.rma_line_ids\ - .mapped("product_id")\ - .write({"rma_customer_operation_id": False}) - self.rma_customer_id.rma_line_ids \ - .mapped("product_id.categ_id") \ - .write({"rma_customer_operation_id": rma_operation.id}) + self.rma_customer_id.rma_line_ids.mapped("product_id").sudo().write( + {"rma_customer_operation_id": False} + ) + self.rma_customer_id.rma_line_ids.mapped("product_id.categ_id").sudo().write( + {"rma_customer_operation_id": rma_operation.id} + ) for line in self.rma_customer_id.rma_line_ids: - data = {'product_id': line.product_id.id} + data = {"product_id": line.product_id.id} new_line = self.rma_line.new(data) self.assertFalse(new_line.operation_id) self.assertFalse(new_line.product_id.rma_customer_operation_id)