Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.0] sales_team_operating_unit #35

Merged
merged 14 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crm_operating_unit/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This module introduces the following features:

* Adds the Operating Unit (OU) to the Lead.

* The users default Operating Unit (OU) is proposed at the time of creating the Lead / Opportunity.
* The user's default Operating Unit (OU) is proposed at the time of creating the Lead / Opportunity.

* Security rules are defined to ensure that users can only see the Opportunity / Lead of that Operating Units in which they are allowed access to.

Expand Down
5 changes: 2 additions & 3 deletions crm_operating_unit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.a
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from . import models
7 changes: 3 additions & 4 deletions crm_operating_unit/__openerp__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
Expand All @@ -13,7 +12,7 @@
"license": "LGPL-3",
"website": "http://www.eficent.com",
"category": "Sales",
"depends": ["crm", "operating_unit"],
"depends": ["crm", "sales_team_operating_unit"],
"data": [
"views/crm_lead_view.xml",
"security/crm_security.xml",
Expand Down
5 changes: 2 additions & 3 deletions crm_operating_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from . import crm_lead
21 changes: 14 additions & 7 deletions crm_operating_unit/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from openerp import fields, models
from openerp import api, fields, models


class CRMLead(models.Model):

_inherit = 'crm.lead'

@api.model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't add this code here, as there's no dependency to the other module. Do it in the proper module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, there's dependency. I think it's ok, isn't it?

def _get_default_operating_unit(self):
team_id = self.env['crm.team']._get_default_team_id()
team = self.env['crm.team'].sudo().browse(team_id)
if team.operating_unit_id:
return team.operating_unit_id
else:
return self.env['res.users'].operating_unit_default_get(self._uid)

operating_unit_id = fields.Many2one('operating.unit', 'Operating Unit',
default=lambda self:
self.env['res.users'].
operating_unit_default_get(self._uid))
related='team_id.operating_unit_id',
default=_get_default_operating_unit)
5 changes: 2 additions & 3 deletions crm_operating_unit/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from . import test_crm_operating_unit
76 changes: 47 additions & 29 deletions crm_operating_unit/tests/test_crm_operating_unit.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L. -
# Jordi Ballester Alomar
# © 2015 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from openerp.tests import common


class TestPurchaseOperatingUnit(common.TransactionCase):
class TestCrmOperatingUnit(common.TransactionCase):

def setUp(self):
super(TestPurchaseOperatingUnit, self).setUp()
super(TestCrmOperatingUnit, self).setUp()
self.res_users_model = self.env['res.users']
self.crm_lead_model = self.env['crm.lead']
# Groups
self.grp_sale_mngr = self.env.ref('base.group_sale_manager')
self.grp_user = self.env.ref('base.group_user')
# Company
self.crm_team_model = self.registry('crm.team')
self.group_sale_manager = self.env.ref('base.group_sale_manager')
self.group_user = self.env.ref('base.group_user')
self.company = self.env.ref('base.main_company')
# Main Operating Unit
self.ou1 = self.env.ref('operating_unit.main_operating_unit')
# B2C Operating Unit
self.b2c = self.env.ref('operating_unit.b2c_operating_unit')
self.main_OU = self.env.ref('operating_unit.main_operating_unit')
self.b2c_OU = self.env.ref('operating_unit.b2c_operating_unit')
# Create User 1 with Main OU
self.user1 = self._create_user('user_1', [self.grp_sale_mngr,
self.grp_user], self.company,
[self.ou1])
self.user1 = self._create_user('user_1', [self.group_sale_manager,
self.group_user],
self.company, [self.main_OU])
# Create User 2 with B2C OU
self.user2 = self._create_user('user_2', [self.grp_sale_mngr,
self.grp_user], self.company,
[self.b2c])
self.user2 = self._create_user('user_2', [self.group_sale_manager,
self.group_user],
self.company, [self.b2c_OU])

self.team1 = self._create_crm_team(self.user1.id, self.main_OU)
self.team2 = self._create_crm_team(self.user2.id, self.b2c_OU)

# Create CRM Leads
self.lead1 = self._create_crm_lead(self.user1.id, self.ou1)
self.lead2 = self._create_crm_lead(self.user2.id, self.b2c)
self.lead1 = self._create_crm_lead(self.user1.id, self.team1)
self.lead2 = self._create_crm_lead(self.user2.id, self.team2)

def _create_user(self, login, groups, company, operating_units,
context=None):
def _create_user(self, login, groups, company, operating_units):
""" Create a user. """
group_ids = [group.id for group in groups]
user = self.res_users_model.create({
'name': 'Test User',
'name': login,
'login': login,
'password': 'demo',
'email': 'test@yourcompany.com',
Expand All @@ -49,11 +48,24 @@ def _create_user(self, login, groups, company, operating_units,
})
return user

def _create_crm_lead(self, uid, operating_unit):
def _create_crm_team(self, uid, operating_unit):
"""Create a sale order."""
crm = self.crm_lead_model.sudo(uid).create({
'name': 'CRM LEAD',
context = {'mail_create_nosubscribe': True}
crm = self.crm_team_model.create(self.cr, uid, {
'name': 'CRM team',
'operating_unit_id': operating_unit.id,
'user_id': uid}, context=context)
return crm

def _create_crm_lead(self, uid, team):
"""Create a sale order."""
operating_unit_id = self.crm_lead_model.sudo(uid).\
_get_default_operating_unit()
crm = self.crm_lead_model.create({
'name': 'CRM LEAD',
'user_id': uid,
'operating_unit_id': operating_unit_id.id,
'team_id': team
})
return crm

Expand All @@ -63,6 +75,12 @@ def test_crm_lead(self):

lead = self.crm_lead_model.sudo(self.user2.id).search(
[('id', '=', self.lead1.id),
('operating_unit_id', '=', self.ou1.id)])
('operating_unit_id', '=', self.main_OU.id)])
self.assertEqual(lead.ids, [], 'User 2 should not have access to '
'%s' % self.ou1.name)
'%s' % self.main_OU.name)

def test_team_ou(self):
new_lead = self._create_crm_lead(self.user2.id, self.team2)
self.assertEqual(
new_lead.operating_unit_id, self.b2c_OU,
'User 2 lead should have %s as operating unit' % self.b2c_OU.name)
61 changes: 61 additions & 0 deletions sales_team_operating_unit/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.. image:: https://img.shields.io/badge/license-LGPLv3-blue.svg
:target: https://www.gnu.org/licenses/lgpl.html
:alt: License: LGPL-3

=========================
Sales Team Operating Unit
=========================

This module introduces the following features:

* Adds the Operating Unit (OU) to the Sales Team.

* The user's default Operating Unit (OU) is proposed at the time of creating
the Sales Team.

* Security rules are defined to ensure that users can only see the Sales Team
of that Operating Units in which they are allowed access to.


Usage
=====

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/213/9.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/operating-unit/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.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Jordi Ballester Alomar <jordi.ballester@eficent.com>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

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.

To contribute to this module, please visit https://odoo-community.org.
5 changes: 5 additions & 0 deletions sales_team_operating_unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import models
20 changes: 20 additions & 0 deletions sales_team_operating_unit/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
"name": "Sales Team Operating Unit",
"version": "9.0.1.0.0",
"author": "Eficent, "
"Serpent Consulting Services Pvt. Ltd.,"
"Odoo Community Association (OCA)",
"license": "LGPL-3",
"website": "http://www.eficent.com",
"category": "Sales",
"depends": ["sales_team", "operating_unit"],
"data": [
"views/crm_team_view.xml",
"security/crm_security.xml",
],
'installable': True,
}
5 changes: 5 additions & 0 deletions sales_team_operating_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import crm_team
15 changes: 15 additions & 0 deletions sales_team_operating_unit/models/crm_team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L.
# © 2015 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from openerp import fields, models


class CRMTeam(models.Model):

_inherit = 'crm.team'

operating_unit_id = fields.Many2one('operating.unit', 'Operating Unit',
default=lambda self:
self.env['res.users'].
operating_unit_default_get(self._uid))
20 changes: 20 additions & 0 deletions sales_team_operating_unit/security/crm_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 Eficent Business and IT Consulting Services S.L.
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0) -->
<odoo>
<data noupdate="0">

<record id="ir_rule_crm_team_allowed_operating_units"
model="ir.rule">
<field name="model_id" ref="sales_team.model_crm_team"/>
<field name="domain_force">['|',('operating_unit_id','=',False),('operating_unit_id','in',[g.id for g in user.operating_unit_ids])]</field>
<field name="name">Sales Teams from allowed operating units</field>
<field name="global" eval="True"/>
<field eval="0" name="perm_unlink"/>
<field eval="0" name="perm_write"/>
<field eval="1" name="perm_read"/>
<field eval="0" name="perm_create"/>
</record>

</data>
</odoo>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions sales_team_operating_unit/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import test_crm_team_operating_unit
Loading