diff --git a/web_easy_switch_company/__init__.py b/web_easy_switch_company/__init__.py new file mode 100644 index 00000000..a14558d4 --- /dev/null +++ b/web_easy_switch_company/__init__.py @@ -0,0 +1,7 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See __openerp__.py file for Copyright and Licence Informations. +################################################################################ + +import model +import controllers diff --git a/web_easy_switch_company/__openerp__.py b/web_easy_switch_company/__openerp__.py new file mode 100644 index 00000000..8b274345 --- /dev/null +++ b/web_easy_switch_company/__openerp__.py @@ -0,0 +1,66 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See Copyright and Licence Informations undermentioned. +################################################################################ + +{ + 'name': 'Multicompany - Easy Switch Company', + 'version': '1.0', + 'category': 'web', + 'description': """ +Add menu to allow user to switch to another company more easily +=============================================================== + +Functionnalities : +------------------ + * Add a new menu in the top bar to switch to another company more easily; + * Remove the old behaviour to switch company; + * Remove the display of the current company after the name of the user, + the information is redundant and because the name of the company is displayed + only if company_id != 1 by default. + +Documentations : +---------------- + * Video : http://www.youtube.com/watch?v=Cpm6dg-IEQQ + +Technical informations : +------------------------ + * Create a field function 'logo_topbar' in res_company to have a good resized logo; + +Limits : +-------- + * It would be interesting to show the structure of the companies; + +Copyright and Licence : +----------------------- + * 2014, Groupement Régional Alimentaire de Proximité + * Licence : AGPL-3 (http://www.gnu.org/licenses/) + +Contacts : +---------- + * Sylvain LE GAL (https://twitter.com/legalsylvain); + * for any help or question about this module. + """, + 'author': 'GRAP', + 'website': 'http://www.grap.coop', + 'license': 'AGPL-3', + 'depends': [ + 'web', + ], + 'data': [ + 'view/res_users_view.xml', + ], + 'demo': [], + 'js': [ + 'static/src/js/switch_company.js', + ], + 'css': [], + 'qweb': [ + 'static/src/xml/switch_company.xml', + ], + 'images': [], + 'post_load': '', + 'application': False, + 'installable': True, + 'auto_install': False, +} diff --git a/web_easy_switch_company/controllers/__init__.py b/web_easy_switch_company/controllers/__init__.py new file mode 100644 index 00000000..f02a4515 --- /dev/null +++ b/web_easy_switch_company/controllers/__init__.py @@ -0,0 +1,6 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See __openerp__.py file for Copyright and Licence Informations. +################################################################################ + +import main diff --git a/web_easy_switch_company/controllers/main.py b/web_easy_switch_company/controllers/main.py new file mode 100644 index 00000000..d9a7c18f --- /dev/null +++ b/web_easy_switch_company/controllers/main.py @@ -0,0 +1,13 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See __openerp__.py file for Copyright and Licence Informations. +################################################################################ + +import openerp + +class WebEasySwitchCompanyController(openerp.addons.web.http.Controller): + _cp_path = '/web_easy_switch_company/switch' + + @openerp.addons.web.http.jsonrequest + def change_current_company(self, req, company_id): + req.session.model('res.users').change_current_company(company_id) diff --git a/web_easy_switch_company/model/__init__.py b/web_easy_switch_company/model/__init__.py new file mode 100644 index 00000000..cdcfac2d --- /dev/null +++ b/web_easy_switch_company/model/__init__.py @@ -0,0 +1,7 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See __openerp__.py file for Copyright and Licence Informations. +################################################################################ + +import res_users +import res_company diff --git a/web_easy_switch_company/model/res_company.py b/web_easy_switch_company/model/res_company.py new file mode 100644 index 00000000..8b1fa532 --- /dev/null +++ b/web_easy_switch_company/model/res_company.py @@ -0,0 +1,32 @@ +## -*- encoding: utf-8 -*- +################################################################################# +## See __openerp__.py file for Copyright and Licence Informations. +################################################################################# + +from openerp.osv.orm import Model +from openerp.osv import fields +from openerp.tools import image_resize_image + +class res_company(Model): + _inherit = 'res.company' + + ### Custom Section + def _switch_company_get_companies_from_partner(self, cr, uid, ids, context=None): + return self.pool['res.company'].search(cr, uid, [('partner_id', 'in', ids)], context=context) + + ### Fields function Section + def _get_logo_topbar(self, cr, uid, ids, _field_name, _args, context=None): + result = dict.fromkeys(ids, False) + for record in self.browse(cr, uid, ids, context=context): + size = (48, 48) + result[record.id] = image_resize_image(record.partner_id.image, size) + return result + + ### Columns Section + _columns = { + 'logo_topbar': fields.function(_get_logo_topbar, string="Logo displayed in the switch company menu", + type="binary", store={ + 'res.company': (lambda s, c, u, i, x: i, ['partner_id'], 10), + 'res.partner': (_switch_company_get_companies_from_partner, ['image'], 10), + }), + } diff --git a/web_easy_switch_company/model/res_users.py b/web_easy_switch_company/model/res_users.py new file mode 100644 index 00000000..09dba3d8 --- /dev/null +++ b/web_easy_switch_company/model/res_users.py @@ -0,0 +1,13 @@ +# -*- encoding: utf-8 -*- +################################################################################ +# See __openerp__.py file for Copyright and Licence Informations. +################################################################################ + +from openerp.osv.orm import Model + +class res_users(Model): + _inherit = 'res.users' + + ### Custom Function Section + def change_current_company(self, cr, uid, company_id, context=None): + return self.write(cr, uid, uid, {'company_id': company_id}) diff --git a/web_easy_switch_company/static/src/img/icon.png b/web_easy_switch_company/static/src/img/icon.png new file mode 100644 index 00000000..ee270eb3 Binary files /dev/null and b/web_easy_switch_company/static/src/img/icon.png differ diff --git a/web_easy_switch_company/static/src/img/selection-off.png b/web_easy_switch_company/static/src/img/selection-off.png new file mode 100644 index 00000000..c8e922cc Binary files /dev/null and b/web_easy_switch_company/static/src/img/selection-off.png differ diff --git a/web_easy_switch_company/static/src/img/selection-on.png b/web_easy_switch_company/static/src/img/selection-on.png new file mode 100644 index 00000000..8faf7d4d Binary files /dev/null and b/web_easy_switch_company/static/src/img/selection-on.png differ diff --git a/web_easy_switch_company/static/src/js/switch_company.js b/web_easy_switch_company/static/src/js/switch_company.js new file mode 100644 index 00000000..279b5724 --- /dev/null +++ b/web_easy_switch_company/static/src/js/switch_company.js @@ -0,0 +1,115 @@ +/******************************************************************************/ +/* See __openerp__.py file for Copyright and Licence Informations. */ +/******************************************************************************/ + +openerp.web_easy_switch_company = function (instance) { + + /*************************************************************************** + Create an new 'SwitchCompanyWidget' widget that allow users to switch + from a company to another more easily. + ***************************************************************************/ + instance.web.SwitchCompanyWidget = instance.web.Widget.extend({ + + template:'web_easy_switch_company.SwitchCompanyWidget', + + /*********************************************************************** + Overload section + ***********************************************************************/ + + /** + * Overload 'init' function to initialize the values of the widget. + */ + init: function(parent){ + this._super(parent); + this.companies = []; + this.current_company_id = 0; + this.current_company_name = ''; + }, + + /** + * Overload 'start' function to load datas from DB. + */ + start: function () { + this._super(); + this._load_data(); + }, + + /** + * Overload 'renderElement' function to set events on company items. + */ + renderElement: function() { + var self = this; + this._super(); + this.$el.find('.easy_switch_company_company_item').on('click', function(ev) { + var company_id = $(ev.target).data("company-id"); + if (company_id != self.current_company_id){ + var func = '/web_easy_switch_company/switch/change_current_company'; + var param = {'company_id': company_id} + self.rpc(func, param).done(function(res) { + window.location.reload() + }); + } + }); + }, + + /*********************************************************************** + Custom section + ***********************************************************************/ + + /** + * helper function to load data from the server + */ + _fetch: function(model, fields, domain, ctx){ + return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all(); + }, + + /** + * - Load data of the companies allowed to the current users; + * - Launch the rendering of the current widget; + */ + _load_data: function(){ + var self = this; + // Request for current users information + this._fetch('res.users',['company_id','company_ids'],[['id','=',this.session.uid]]).then(function(res_users){ + self.current_company_id = res_users[0].company_id[0]; + self.current_company_name = res_users[0].company_id[1]; + // Request for other companies + self._fetch('res.company',['name',],[['id','in', res_users[0].company_ids]]).then(function(res_company){ + for ( var i=0 ; i < res_company.length; i++) { + res_company[i]['logo_topbar'] = self.session.url( + '/web/binary/image', { + model:'res.company', + field: 'logo_topbar', + id: res_company[i].id + }); + if (res_company[i].id == self.current_company_id){ + res_company[i]['logo_state'] = '/web_easy_switch_company/static/src/img/selection-on.png'; + } + else{ + res_company[i]['logo_state'] = '/web_easy_switch_company/static/src/img/selection-off.png'; + } + self.companies.push(res_company[i]); + } + // Update rendering + self.renderElement(); + }); + }); + }, + + }); + + /*************************************************************************** + Extend 'UserMenu' Widget to insert a 'SwitchCompanyWidget' widget. + ***************************************************************************/ + instance.web.UserMenu = instance.web.UserMenu.extend({ + + init: function(parent) { + this._super(parent); + var switch_button = new instance.web.SwitchCompanyWidget(); + switch_button.appendTo(instance.webclient.$el.find('.oe_systray')); + }, + + }); + +}; + diff --git a/web_easy_switch_company/static/src/xml/switch_company.xml b/web_easy_switch_company/static/src/xml/switch_company.xml new file mode 100644 index 00000000..ac3880d4 --- /dev/null +++ b/web_easy_switch_company/static/src/xml/switch_company.xml @@ -0,0 +1,29 @@ + + + + + + diff --git a/web_easy_switch_company/view/res_users_view.xml b/web_easy_switch_company/view/res_users_view.xml new file mode 100644 index 00000000..af433676 --- /dev/null +++ b/web_easy_switch_company/view/res_users_view.xml @@ -0,0 +1,15 @@ + + + + + + res.users.form + res.users + + + + + + + +