diff --git a/rma_operating_unit/README.rst b/rma_operating_unit/README.rst index b09234cb..43429a5c 100644 --- a/rma_operating_unit/README.rst +++ b/rma_operating_unit/README.rst @@ -30,4 +30,4 @@ Contributors Maintainer ---------- -This module is maintained by Eficent. \ No newline at end of file +This module is maintained by Eficent. diff --git a/rma_operating_unit/__openerp__.py b/rma_operating_unit/__manifest__.py similarity index 73% rename from rma_operating_unit/__openerp__.py rename to rma_operating_unit/__manifest__.py index 0f79c0ee..5010d934 100644 --- a/rma_operating_unit/__openerp__.py +++ b/rma_operating_unit/__manifest__.py @@ -3,8 +3,8 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). { - "name": "Operating Unit in RMA Orders", - "version": "9.0.1.0.0", + "name": "Operating Unit in RMA Groups", + "version": "10.0.1.0.0", "author": "Eficent", "license": "LGPL-3", "website": "http://www.eficent.com", @@ -12,7 +12,8 @@ "depends": ["rma", "operating_unit"], "data": [ "security/rma_security.xml", - "views/rma_order_view.xml" + "views/rma_order_view.xml", + "views/rma_order_line_view.xml", ], 'installable': True, } diff --git a/rma_operating_unit/models/__init__.py b/rma_operating_unit/models/__init__.py index 9cf2d984..935b7adb 100644 --- a/rma_operating_unit/models/__init__.py +++ b/rma_operating_unit/models/__init__.py @@ -2,3 +2,4 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). from . import rma_order +from . import rma_order_line diff --git a/rma_operating_unit/models/rma_order.py b/rma_operating_unit/models/rma_order.py index b5757cb4..d11f992d 100644 --- a/rma_operating_unit/models/rma_order.py +++ b/rma_operating_unit/models/rma_order.py @@ -2,14 +2,26 @@ # © 2017 Eficent Business and IT Consulting Services S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from openerp import api, fields, models, _ -from openerp.exceptions import ValidationError +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError class RmaOrder(models.Model): _inherit = "rma.order" + @api.multi + @api.constrains('rma_line_ids', 'rma_line_ids.operating_unit') + def _check_operating_unit(self): + for rma in self: + bad_lines = rma.rma_line_ids.filtered( + lambda l: l.operating_unit_id != rma.operating_unit_id) + if bad_lines: + raise ValidationError( + _('The operating unit of the rma lines have to match the' + ' one of the group')) + return True + @api.model def _default_operating_unit(self): return self.env.user.default_operating_unit_id diff --git a/rma_operating_unit/models/rma_order_line.py b/rma_operating_unit/models/rma_order_line.py new file mode 100644 index 00000000..c96020c9 --- /dev/null +++ b/rma_operating_unit/models/rma_order_line.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# © 2018 Eficent Business and IT Consulting Services S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo import api, fields, models + + +class RmaOrderLine(models.Model): + + _inherit = "rma.order.line" + + @api.model + def _default_operating_unit(self): + if self.rma_id.operating_unit_id: + return self.rma_id.operating_unit_id.id + return self.env.user.default_operating_unit_id + + operating_unit_id = fields.Many2one( + comodel_name='operating.unit', + string='Operating Unit', + default=_default_operating_unit, + ) diff --git a/rma_operating_unit/tests/test_rma_operating_unit.py b/rma_operating_unit/tests/test_rma_operating_unit.py index 1534cb7b..94f3bf2e 100644 --- a/rma_operating_unit/tests/test_rma_operating_unit.py +++ b/rma_operating_unit/tests/test_rma_operating_unit.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # © 2017 Eficent Business and IT Consulting Services S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from openerp.tests import common +from odoo.tests import common +from odoo import exceptions class TestRmaOperatingUnit(common.TransactionCase): @@ -10,10 +11,14 @@ class TestRmaOperatingUnit(common.TransactionCase): super(TestRmaOperatingUnit, self).setUp() self.res_users_model = self.env['res.users'] self.rma_model = self.env['rma.order'] + self.rma_line_model = self.env['rma.order.line'] self.company = self.env.ref('base.main_company') self.partner = self.env.ref('base.res_partner_1') self.grp_rma_manager = self.env.ref('rma.group_rma_manager') + self.grp_ou = self.env.ref("operating_unit.group_multi_operating_unit") + self.grp_stock = self.env.ref("stock.group_stock_manager") + self.product = self.env.ref('product.product_product_12') # Main Operating Unit self.main_OU = self.env.ref('operating_unit.main_operating_unit') @@ -22,15 +27,18 @@ class TestRmaOperatingUnit(common.TransactionCase): # Users self.user1 = self._create_user('user_1', - [self.grp_rma_manager], + [self.grp_rma_manager, self.grp_ou, + self.grp_stock], self.company, [self.main_OU, self.b2c_OU]) self.user2 = self._create_user('user_2', - [self.grp_rma_manager], + [self.grp_rma_manager, self.grp_ou, + self.grp_stock], self.company, [self.b2c_OU]) self.user3 = self._create_user('user_3', - [self.grp_rma_manager], + [self.grp_rma_manager, self.grp_ou, + self.grp_stock], self.company, [self.main_OU, self.b2c_OU]) @@ -66,6 +74,25 @@ class TestRmaOperatingUnit(common.TransactionCase): }) return rma_order + def _create_rma_line(self, rma, uid, operating_unit): + """Creates an RMA""" + rma_order_line = self.rma_line_model.sudo(uid).create({ + 'operating_unit_id': operating_unit.id, + 'rma_id': rma.id, + 'partner_id': self.partner.id, + 'in_route_id': 1, + 'out_route_id': 1, + 'in_warehouse_id': 1, + 'out_warehouse_id': 1, + 'location_id': 1, + 'receipt_policy': 'ordered', + 'delivery_policy': 'ordered', + 'name': self.product.name, + 'product_id': self.product.id, + 'uom_id': self.product.uom_id.id + }) + return rma_order_line + def test_security(self): # User 2 is only assigned to Operating Unit B2C, and cannot # access RMA of Main Operating Unit. @@ -75,3 +102,10 @@ class TestRmaOperatingUnit(common.TransactionCase): self.main_OU.id)]) self.assertEqual(record.ids, [], 'User 2 should not have access to ' 'OU %s.' % self.main_OU.name) + + def test_constraint(self): + # RMA group should contain rma lines for the same OU + with self.assertRaises(exceptions.ValidationError): + self._create_rma_line(self.rma_order1, self.user1.id, self.main_OU) + self._create_rma_line(self.rma_order1, self.user1.id, self.b2c_OU) + self.rma_order1._check_operating_unit() diff --git a/rma_operating_unit/views/rma_order_line_view.xml b/rma_operating_unit/views/rma_order_line_view.xml new file mode 100644 index 00000000..6695d38c --- /dev/null +++ b/rma_operating_unit/views/rma_order_line_view.xml @@ -0,0 +1,69 @@ + + + + + rma.order.line.tree + rma.order.line + + + + + + + + + + rma.order.line.supplier.tree + rma.order.line + + + + + + + + + + rma.order.supplier.tree + rma.order + + + + + + + + + rma.order.line.supplier.form + rma.order.line + + + + + + + + + + rma.order.line.form + rma.order.line + + + + + + + + + + rma.order.line.select + rma.order.line + + + + + + + + diff --git a/rma_operating_unit/views/rma_order_view.xml b/rma_operating_unit/views/rma_order_view.xml index 770398e2..40f5f22d 100644 --- a/rma_operating_unit/views/rma_order_view.xml +++ b/rma_operating_unit/views/rma_order_view.xml @@ -1,67 +1,64 @@ - - - - - rma.order.tree - rma.order - - - - - + + + rma.order.tree + rma.order + + + + - + + - - rma.order.supplier.tree - rma.order - - - - - + + rma.order.supplier.tree + rma.order + + + + - + + - - rma.order.supplier.form - rma.order - - - - - - + + rma.order.supplier.form + rma.order + + + + + + + + + + + rma.order.form + rma.order + + + + + + + + + + + rma.order.select + rma.order + + + + - + + - - rma.order.form - rma.order - - - - - - - - - - - rma.order.select - rma.order - - - - - - - - - - +