diff options
| author | Kinjal Mehta <kme@odoo.com> | 2017-06-09 11:37:35 +0530 |
|---|---|---|
| committer | Simon Lejeune <sle@openerp.com> | 2018-01-10 13:53:53 +0100 |
| commit | a0e7c0d91c63f879051b6ef02947b8c0ecb57ef8 (patch) | |
| tree | 3361518d364d6271fbf6332458ca58b4888fbb6b | |
| parent | bbc9b329b5962c0e208a7abe33d5d853aaa49fc2 (diff) | |
[IMP] maintenance: multi company aware
Add a company field on the maintenance equipment, equipment categories, maintenance request
and maintenance team (maintenance users from a company do not see equipments, categories
and requests from another company and Equipment Manager can create the maintenance teams).
| -rw-r--r-- | addons/maintenance/models/maintenance.py | 10 | ||||
| -rw-r--r-- | addons/maintenance/security/ir.model.access.csv | 3 | ||||
| -rw-r--r-- | addons/maintenance/security/maintenance.xml | 28 | ||||
| -rw-r--r-- | addons/maintenance/tests/__init__.py | 1 | ||||
| -rw-r--r-- | addons/maintenance/tests/test_maintenance_multicompany.py | 177 | ||||
| -rw-r--r-- | addons/maintenance/views/maintenance_views.xml | 10 |
6 files changed, 225 insertions, 4 deletions
diff --git a/addons/maintenance/models/maintenance.py b/addons/maintenance/models/maintenance.py index f17f7a0714e..7ff99b5a2aa 100644 --- a/addons/maintenance/models/maintenance.py +++ b/addons/maintenance/models/maintenance.py @@ -31,6 +31,8 @@ class MaintenanceEquipmentCategory(models.Model): self.fold = False if self.equipment_count else True name = fields.Char('Category Name', required=True, translate=True) + company_id = fields.Many2one('res.company', string='Company', required=True, + default=lambda self: self.env.user.company_id) technician_user_id = fields.Many2one('res.users', 'Responsible', track_visibility='onchange', default=lambda self: self.env.uid, oldname='user_id') color = fields.Integer('Color Index') note = fields.Text('Comments', translate=True) @@ -120,6 +122,8 @@ class MaintenanceEquipment(models.Model): return recs.name_get() name = fields.Char('Equipment Name', required=True, translate=True) + company_id = fields.Many2one('res.company', string='Company', required=True, + default=lambda self: self.env.user.company_id) active = fields.Boolean(default=True) technician_user_id = fields.Many2one('res.users', string='Technician', track_visibility='onchange', oldname='user_id') owner_user_id = fields.Many2one('res.users', string='Owner', track_visibility='onchange') @@ -269,9 +273,11 @@ class MaintenanceRequest(models.Model): return super(MaintenanceRequest, self)._track_subtype(init_values) def _get_default_team_id(self): - return self.env['maintenance.team'].search([], limit=1) + return self.env['maintenance.team'].search([('company_id', '=', self.env.user.company_id.id)], limit=1).id name = fields.Char('Subjects', required=True) + company_id = fields.Many2one('res.company', string='Company', required=True, + default=lambda self: self.env.user.company_id) description = fields.Text('Description') request_date = fields.Date('Request Date', track_visibility='onchange', default=fields.Date.context_today, help="Date requested for the maintenance to happen") @@ -361,6 +367,8 @@ class MaintenanceTeam(models.Model): name = fields.Char(required=True) active = fields.Boolean(default=True) + company_id = fields.Many2one('res.company', string='Company', required=True, + default=lambda self: self.env.user.company_id) member_ids = fields.Many2many('res.users', 'maintenance_team_users_rel', string="Team Members") color = fields.Integer("Color Index", default=0) request_ids = fields.One2many('maintenance.request', 'maintenance_team_id', copy=False) diff --git a/addons/maintenance/security/ir.model.access.csv b/addons/maintenance/security/ir.model.access.csv index f066a807a55..264be46b68e 100644 --- a/addons/maintenance/security/ir.model.access.csv +++ b/addons/maintenance/security/ir.model.access.csv @@ -6,4 +6,5 @@ access_equipment_category_user,equipment.category.user,model_maintenance_equipme access_equipment_category_admin_user,equipment.category system user,model_maintenance_equipment_category,group_equipment_manager,1,1,1,1 access_maintenance_stage_user,maintenance.stage.user,model_maintenance_stage,base.group_user,1,0,0,0 access_maintenance_stage_admin_user,equipment.request.stage system user,model_maintenance_stage,group_equipment_manager,1,1,1,1 -access_maintenance_team_user,maintenance.team.user,model_maintenance_team,base.group_user,1,0,0,0
\ No newline at end of file +access_maintenance_team_user,maintenance.team.user,model_maintenance_team,base.group_user,1,0,0,0 +access_maintenance_team_admin_user,maintenance.team.admin.user,model_maintenance_team,group_equipment_manager,1,1,1,1
\ No newline at end of file diff --git a/addons/maintenance/security/maintenance.xml b/addons/maintenance/security/maintenance.xml index a567eb8ddab..1214b91bd67 100644 --- a/addons/maintenance/security/maintenance.xml +++ b/addons/maintenance/security/maintenance.xml @@ -37,4 +37,32 @@ <field name="groups" eval="[(4, ref('group_equipment_manager'))]"/> </record> + <record id="maintenance_request_comp_rule" model="ir.rule"> + <field name="name">Maintenance Request Multi-company rule</field> + <field name="model_id" ref="model_maintenance_request"/> + <field name="global" eval="True"/> + <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field> + </record> + + <record id="maintenance_equipment_comp_rule" model="ir.rule"> + <field name="name">Maintenance Equipment Multi-company rule</field> + <field name="model_id" ref="model_maintenance_equipment"/> + <field name="global" eval="True"/> + <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field> + </record> + + <record id="maintenance_team_comp_rule" model="ir.rule"> + <field name="name">Maintenance Team Multi-company rule</field> + <field name="model_id" ref="model_maintenance_team"/> + <field name="global" eval="True"/> + <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field> + </record> + + <record id="maintenance_equipment_category_comp_rule" model="ir.rule"> + <field name="name">Maintenance Equipment Category Multi-company rule</field> + <field name="model_id" ref="model_maintenance_equipment_category"/> + <field name="global" eval="True"/> + <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field> + </record> + </odoo> diff --git a/addons/maintenance/tests/__init__.py b/addons/maintenance/tests/__init__.py index ac9f331eab9..e65a25e0b3f 100644 --- a/addons/maintenance/tests/__init__.py +++ b/addons/maintenance/tests/__init__.py @@ -1,3 +1,4 @@ # -*- coding: utf-8 -*- from . import test_maintenance +from . import test_maintenance_multicompany diff --git a/addons/maintenance/tests/test_maintenance_multicompany.py b/addons/maintenance/tests/test_maintenance_multicompany.py new file mode 100644 index 00000000000..d9a233e71bf --- /dev/null +++ b/addons/maintenance/tests/test_maintenance_multicompany.py @@ -0,0 +1,177 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import time + +from odoo.tests.common import TransactionCase +from odoo.exceptions import AccessError + + +class TestEquipmentMulticompany(TransactionCase): + + def test_00_equipment_multicompany_user(self): + """Test Check maintenance with equipment manager and user in multi company environment""" + + # Use full models + Equipment = self.env['maintenance.equipment'] + MaintenanceRequest = self.env['maintenance.request'] + Category = self.env['maintenance.equipment.category'] + ResUsers = self.env['res.users'] + ResCompany = self.env['res.company'] + MaintenanceTeam = self.env['maintenance.team'] + + # Use full reference. + group_user = self.env.ref('base.group_user') + group_manager = self.env.ref('maintenance.group_equipment_manager') + + # Company A + company_a = ResCompany.create({ + 'name': 'Company A', + 'currency_id': self.env.ref('base.USD').id, + }) + + # Create one child company having parent company is 'Your company' + company_b = ResCompany.create({ + 'name': 'Company B', + 'currency_id': self.env.ref('base.USD').id, + 'parent_id': company_a.id, + }) + + # Create equipment manager. + equipment_manager = ResUsers.create({ + 'name': 'Equipment Manager', + 'company_id': company_a.id, + 'login': 'e_equipment_manager', + 'password': 'e_equipment_manager', + 'email': 'eqmanager@yourcompany.example.com', + 'groups_id': [(6, 0, [group_manager.id])], + 'company_ids': [(6, 0, [company_a.id, company_b.id])] + }) + + # Create equipment user + user = ResUsers.create({ + 'name': 'Normal User/Employee', + 'company_id': company_b.id, + 'login': 'emp', + 'password': 'emp', + 'email': 'empuser@yourcompany.example.com', + 'groups_id': [(6, 0, [group_user.id])], + 'company_ids': [(6, 0, [company_b.id])] + }) + + # create a maintenance team for company A user + team = MaintenanceTeam.sudo(equipment_manager).create({ + 'name': 'Metrology', + 'company_id': company_a.id, + }) + # create a maintenance team for company B user + teamb = MaintenanceTeam.sudo(equipment_manager).create({ + 'name': 'Subcontractor', + 'company_id': company_b.id, + }) + + # User should not able to create equipment category. + with self.assertRaises(AccessError): + Category.sudo(user).create({ + 'name': 'Software', + 'company_id': company_b.id, + 'technician_user_id': user.id, + }) + + # create equipment category for equipment manager + category_1 = Category.sudo(equipment_manager).create({ + 'name': 'Monitors', + 'company_id': company_b.id, + 'technician_user_id': equipment_manager.id, + }) + + # create equipment category for equipment manager + Category.sudo(equipment_manager).create({ + 'name': 'Computers', + 'company_id': company_b.id, + 'technician_user_id': equipment_manager.id, + }) + + # create equipment category for equipment user + Category.sudo(equipment_manager).create({ + 'name': 'Phones', + 'company_id': company_a.id, + 'technician_user_id': equipment_manager.id, + }) + + # Check category for user equipment_manager and user + self.assertEquals(Category.sudo(equipment_manager).search_count([]), 3) + self.assertEquals(Category.sudo(user).search_count([]), 2) + + # User should not able to create equipment. + with self.assertRaises(AccessError): + Equipment.sudo(user).create({ + 'name': 'Samsung Monitor 15', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': user.id, + }) + + Equipment.sudo(equipment_manager).create({ + 'name': 'Acer Laptop', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': user.id, + }) + + # create an equipment for user + Equipment.sudo(equipment_manager).create({ + 'name': 'HP Laptop', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': equipment_manager.id, + }) + # Now there are total 2 equipments created and can view by equipment_manager user + self.assertEquals(Equipment.sudo(equipment_manager).search_count([]), 2) + + # And there is total 1 equipment can be view by Normal User ( Which user is followers) + self.assertEquals(Equipment.sudo(user).search_count([]), 1) + + # create an equipment team BY user + with self.assertRaises(AccessError): + MaintenanceTeam.sudo(user).create({ + 'name': 'Subcontractor', + 'company_id': company_b.id, + }) + + # create an equipment category BY user + with self.assertRaises(AccessError): + Category.sudo(user).create({ + 'name': 'Computers', + 'company_id': company_b.id, + 'technician_user_id': user.id, + }) + + # create an maintenance stage BY user + with self.assertRaises(AccessError): + self.env['maintenance.stage'].sudo(user).create({ + 'name': 'identify corrective maintenance requirements', + }) + + # Create an maintenance request for ( User Follower ). + MaintenanceRequest.sudo(user).create({ + 'name': 'Some keys are not working', + 'company_id': company_b.id, + 'technician_user_id': user.id, + 'owner_user_id': user.id, + }) + + # Create an maintenance request for equipment_manager (Admin Follower) + MaintenanceRequest.sudo(equipment_manager).create({ + 'name': 'Battery drains fast', + 'company_id': company_a.id, + 'technician_user_id': equipment_manager.id, + 'owner_user_id': equipment_manager.id, + }) + + # Now here is total 1 maintenance request can be view by Normal User + self.assertEquals(MaintenanceRequest.sudo(equipment_manager).search_count([]), 2) + self.assertEquals(MaintenanceRequest.sudo(user).search_count([]), 1) diff --git a/addons/maintenance/views/maintenance_views.xml b/addons/maintenance/views/maintenance_views.xml index 8df390e8c60..f5c0f90ff5f 100644 --- a/addons/maintenance/views/maintenance_views.xml +++ b/addons/maintenance/views/maintenance_views.xml @@ -76,6 +76,7 @@ <group> <group> <field name="owner_user_id" string="Requested By"/> + <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> <field name="equipment_id" context="{'default_category_id':category_id}"/> <field name="category_id" groups="maintenance.group_equipment_manager" attrs="{'invisible': [('equipment_id', '=', False)]}"/> <field name="request_date" readonly="True"/> @@ -325,6 +326,7 @@ <group> <group> <field name="category_id" options="{"no_open": True}"/> + <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> <field name="owner_user_id" string="Owner"/> </group> <group> @@ -582,6 +584,7 @@ <group> <group col="4"> <field name="technician_user_id"/> + <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> </group> <group name="group_alias" attrs="{'invisible': [('alias_domain', '=', False)]}" groups="base.group_no_one"> <label for="alias_name" string="Email Alias"/> @@ -727,7 +730,7 @@ </field> </record> - <!-- maitenance.team: views --> + <!-- maintenance.team: views --> <record id="maintenance_team_view_form" model="ir.ui.view"> <field name="name">maintenance.team.form</field> <field name="model">maintenance.team</field> @@ -747,7 +750,10 @@ </div> <group> <group> - <field name="member_ids" widget="many2many_tags" options="{'color_field': 'color'}"/> + <field name="member_ids" widget="many2many_tags" options="{'color_field': 'color', 'no_create': True}"/> + </group> + <group> + <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> </group> </group> </sheet> |
