From bd309a13458fb637741fd13d20df4a4d7301d92c Mon Sep 17 00:00:00 2001 From: eLBati Date: Tue, 30 Oct 2018 14:12:40 +0100 Subject: [PATCH] ADD sale_commission_geo_assign --- sale_commission_geo_assign/README.rst | 98 ++++ sale_commission_geo_assign/__init__.py | 5 + sale_commission_geo_assign/__manifest__.py | 24 + sale_commission_geo_assign/models/__init__.py | 4 + .../models/res_partner.py | 54 +++ .../readme/CONFIGURE.rst | 5 + .../readme/DESCRIPTION.rst | 3 + sale_commission_geo_assign/readme/USAGE.rst | 7 + .../static/description/index.html | 430 ++++++++++++++++++ sale_commission_geo_assign/tests/__init__.py | 3 + .../tests/test_sale_commission_geo_assign.py | 55 +++ .../views/res_partner_view.xml | 23 + sale_commission_geo_assign/wizard/__init__.py | 4 + .../wizard/wizard_geo_assign_partner.py | 34 ++ .../wizard/wizard_geo_assign_partner_view.xml | 26 ++ 15 files changed, 775 insertions(+) create mode 100644 sale_commission_geo_assign/README.rst create mode 100644 sale_commission_geo_assign/__init__.py create mode 100644 sale_commission_geo_assign/__manifest__.py create mode 100644 sale_commission_geo_assign/models/__init__.py create mode 100644 sale_commission_geo_assign/models/res_partner.py create mode 100644 sale_commission_geo_assign/readme/CONFIGURE.rst create mode 100644 sale_commission_geo_assign/readme/DESCRIPTION.rst create mode 100644 sale_commission_geo_assign/readme/USAGE.rst create mode 100644 sale_commission_geo_assign/static/description/index.html create mode 100644 sale_commission_geo_assign/tests/__init__.py create mode 100644 sale_commission_geo_assign/tests/test_sale_commission_geo_assign.py create mode 100644 sale_commission_geo_assign/views/res_partner_view.xml create mode 100644 sale_commission_geo_assign/wizard/__init__.py create mode 100644 sale_commission_geo_assign/wizard/wizard_geo_assign_partner.py create mode 100644 sale_commission_geo_assign/wizard/wizard_geo_assign_partner_view.xml diff --git a/sale_commission_geo_assign/README.rst b/sale_commission_geo_assign/README.rst new file mode 100644 index 000000000..44b1ad43d --- /dev/null +++ b/sale_commission_geo_assign/README.rst @@ -0,0 +1,98 @@ +=================================== +Sales commissions - Geo assignation +=================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png + :target: https://odoo-community.org/page/development-status + :alt: Production/Stable +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcommission-lightgray.png?logo=github + :target: https://github.com/OCA/commission/tree/10.0/sale_commission_geo_assign + :alt: OCA/commission +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/commission-10-0/commission-10-0-sale_commission_geo_assign + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/165/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Configure sales agents assigning them to a specific geographical area. + +Then, automatically assign agents to customers, according to their geographical area. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Go to + +Sales > Commissions Management > Agents + +For every agent, you can set Countries, States or ZIP range + +Usage +===== + +Go to + +Sales > Customers + +Select the customers you want to assign agents to and click + +Action > Geo assign agents + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Agile Business Group + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +.. |maintainer-eLBati| image:: https://github.com/eLBati.png?size=40px + :target: https://github.com/eLBati + :alt: eLBati + +Current `maintainer `__: + +|maintainer-eLBati| + +This module is part of the `OCA/commission `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_commission_geo_assign/__init__.py b/sale_commission_geo_assign/__init__.py new file mode 100644 index 000000000..76ad87577 --- /dev/null +++ b/sale_commission_geo_assign/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import models +from . import wizard diff --git a/sale_commission_geo_assign/__manifest__.py b/sale_commission_geo_assign/__manifest__.py new file mode 100644 index 000000000..8ac08d761 --- /dev/null +++ b/sale_commission_geo_assign/__manifest__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +{ + "name": "Sales commissions - Geo assignation", + "summary": "Assign agents to partners according to their location", + "version": "10.0.1.0.0", + "development_status": "Production/Stable", + "category": "Sales Management", + "website": "https://github.com/OCA/commission", + "author": "Agile Business Group, Odoo Community Association (OCA)", + "maintainers": ["eLBati"], + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": [ + "sale_commission", + ], + "data": [ + "views/res_partner_view.xml", + "wizard/wizard_geo_assign_partner_view.xml", + ], +} diff --git a/sale_commission_geo_assign/models/__init__.py b/sale_commission_geo_assign/models/__init__.py new file mode 100644 index 000000000..07fe7cae6 --- /dev/null +++ b/sale_commission_geo_assign/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import res_partner diff --git a/sale_commission_geo_assign/models/res_partner.py b/sale_commission_geo_assign/models/res_partner.py new file mode 100644 index 000000000..3b5721778 --- /dev/null +++ b/sale_commission_geo_assign/models/res_partner.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import models, fields, api + + +class Partner(models.Model): + _inherit = 'res.partner' + + country_ids = fields.Many2many( + 'res.country', string='Countries', + help='Countries where this agent operates' + ) + state_ids = fields.Many2many( + 'res.country.state', string='States', + help='States where this agent operates' + ) + zip_from = fields.Char( + 'Zip From', help='ZIP range where this agent operates') + zip_to = fields.Char( + 'Zip To', help='ZIP range where this agent operates') + + @api.onchange('country_ids') + def onchange_countries(self): + if self.country_ids: + domain = [('country_id', 'in', self.country_ids.ids)] + else: + domain = [] + return {'domain': { + 'state_ids': domain + }} + + @api.multi + def verify_agent(self, partner): + self.ensure_one() + if ( + not self.country_ids and not self.state_ids and + not self.zip_from and not self.zip_to + ): + # if no criteria set on agent, agent is excluded + return False + if self.country_ids and partner.country_id not in self.country_ids: + return False + if self.state_ids and partner.state_id not in self.state_ids: + return False + if self.zip_from and ( + partner.zip or '' + ).upper() < self.zip_from.upper(): + return False + if self.zip_to and ( + partner.zip or '' + ).upper() > self.zip_to.upper(): + return False + return True diff --git a/sale_commission_geo_assign/readme/CONFIGURE.rst b/sale_commission_geo_assign/readme/CONFIGURE.rst new file mode 100644 index 000000000..73c10dace --- /dev/null +++ b/sale_commission_geo_assign/readme/CONFIGURE.rst @@ -0,0 +1,5 @@ +Go to + +Sales > Commissions Management > Agents + +For every agent, you can set Countries, States or ZIP range diff --git a/sale_commission_geo_assign/readme/DESCRIPTION.rst b/sale_commission_geo_assign/readme/DESCRIPTION.rst new file mode 100644 index 000000000..80481ed63 --- /dev/null +++ b/sale_commission_geo_assign/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +Configure sales agents assigning them to a specific geographical area. + +Then, automatically assign agents to customers, according to their geographical area. diff --git a/sale_commission_geo_assign/readme/USAGE.rst b/sale_commission_geo_assign/readme/USAGE.rst new file mode 100644 index 000000000..bdd227399 --- /dev/null +++ b/sale_commission_geo_assign/readme/USAGE.rst @@ -0,0 +1,7 @@ +Go to + +Sales > Customers + +Select the customers you want to assign agents to and click + +Action > Geo assign agents diff --git a/sale_commission_geo_assign/static/description/index.html b/sale_commission_geo_assign/static/description/index.html new file mode 100644 index 000000000..1c0668a57 --- /dev/null +++ b/sale_commission_geo_assign/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +Sales commissions - Geo assignation + + + +
+

Sales commissions - Geo assignation

+ + +

Production/Stable License: LGPL-3 OCA/commission Translate me on Weblate Try me on Runbot

+

Configure sales agents assigning them to a specific geographical area.

+

Then, automatically assign agents to customers, according to their geographical area.

+

Table of contents

+ +
+

Configuration

+

Go to

+

Sales > Commissions Management > Agents

+

For every agent, you can set Countries, States or ZIP range

+
+
+

Usage

+

Go to

+

Sales > Customers

+

Select the customers you want to assign agents to and click

+

Action > Geo assign agents

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Agile Business Group
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

eLBati

+

This module is part of the OCA/commission project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/sale_commission_geo_assign/tests/__init__.py b/sale_commission_geo_assign/tests/__init__.py new file mode 100644 index 000000000..d217b0d45 --- /dev/null +++ b/sale_commission_geo_assign/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_sale_commission_geo_assign diff --git a/sale_commission_geo_assign/tests/test_sale_commission_geo_assign.py b/sale_commission_geo_assign/tests/test_sale_commission_geo_assign.py new file mode 100644 index 000000000..3604a39e0 --- /dev/null +++ b/sale_commission_geo_assign/tests/test_sale_commission_geo_assign.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +from odoo import exceptions +import odoo.tests.common as common + + +class TestSaleCommissionGeoAssign(common.TransactionCase): + + def setUp(self): + super(TestSaleCommissionGeoAssign, self).setUp() + self.partner_model = self.env['res.partner'] + self.wizard_model = self.env['wizard.geo.assign.partner'] + self.genova = self.env.ref('base.state_it_ge') + + def test_geo_assign(self): + c1 = self.partner_model.create({ + 'name': 'c1', + 'zip': '16137', + }) + c2 = self.partner_model.create({ + 'name': 'c2', + 'state_id': self.genova.id, + }) + agent1 = self.partner_model.create({ + 'name': 'agent1', + 'agent': True, + 'state_ids': [(4, self.genova.id)] + }) + wizard = self.wizard_model.with_context( + active_ids=[c1.id, c2.id]).create({}) + wizard.geo_assign_partner() + self.assertTrue(len(c2.agents) == 1) + self.assertTrue(agent1.id == c2.agents.ids[0]) + self.assertFalse(c1.agents) + + wizard = self.wizard_model.with_context( + active_ids=[c1.id, c2.id]).create({}) + with self.assertRaises(exceptions.UserError): + wizard.geo_assign_partner() + wizard.check_existing_agents = False + wizard.geo_assign_partner() + self.assertTrue(len(c2.agents) == 1) + self.assertTrue(agent1.id == c2.agents.ids[0]) + self.assertFalse(c1.agents) + + agent2 = self.partner_model.create({ + 'name': 'agent2', + 'agent': True, + 'zip_from': '16100', + 'zip_to': '17100', + }) + wizard.geo_assign_partner() + self.assertTrue(len(c2.agents) == 1) + self.assertTrue(len(c1.agents) == 1) + self.assertTrue(agent2.id == c1.agents.ids[0]) diff --git a/sale_commission_geo_assign/views/res_partner_view.xml b/sale_commission_geo_assign/views/res_partner_view.xml new file mode 100644 index 000000000..d59a91f2d --- /dev/null +++ b/sale_commission_geo_assign/views/res_partner_view.xml @@ -0,0 +1,23 @@ + + + + view_partner_form_agent_geo_assign + res.partner + + + + + + + + + + + + + + + + + + diff --git a/sale_commission_geo_assign/wizard/__init__.py b/sale_commission_geo_assign/wizard/__init__.py new file mode 100644 index 000000000..aeb46487c --- /dev/null +++ b/sale_commission_geo_assign/wizard/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from . import wizard_geo_assign_partner diff --git a/sale_commission_geo_assign/wizard/wizard_geo_assign_partner.py b/sale_commission_geo_assign/wizard/wizard_geo_assign_partner.py new file mode 100644 index 000000000..397f52e98 --- /dev/null +++ b/sale_commission_geo_assign/wizard/wizard_geo_assign_partner.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import models, api, fields, _ +from odoo.exceptions import UserError + + +class WizardGeoAssign(models.TransientModel): + _name = 'wizard.geo.assign.partner' + + check_existing_agents = fields.Boolean( + "Check existing agents", + help="If checked, partners with already assigned agents will be " + "blocked. Otherwise, found agents will be added", + default=True + ) + + @api.multi + def geo_assign_partner(self): + self.ensure_one() + partner_model = self.env['res.partner'] + agents = partner_model.search([('agent', '=', True)]) + if not agents: + raise UserError(_("No agents found in the system")) + partners = partner_model.browse(self.env.context.get('active_ids')) + for partner in partners: + if partner.agents and self.check_existing_agents: + raise UserError(_( + "Partner %s already has agents. You should remove them" + " or deselect 'Check existing agents'" + ) % partner.name) + for agent in agents: + if agent.verify_agent(partner): + partner.agents = [(4, agent.id)] diff --git a/sale_commission_geo_assign/wizard/wizard_geo_assign_partner_view.xml b/sale_commission_geo_assign/wizard/wizard_geo_assign_partner_view.xml new file mode 100644 index 000000000..f7126f902 --- /dev/null +++ b/sale_commission_geo_assign/wizard/wizard_geo_assign_partner_view.xml @@ -0,0 +1,26 @@ + + + + + + Geo assign agents + wizard.geo.assign.partner + +
+ + + + +
+
+
+