From f5f60599bfd49df5904f971b0f1bbb37a96ccdbe Mon Sep 17 00:00:00 2001 From: Joren Van Onder Date: Mon, 22 May 2017 13:30:10 -0700 Subject: [PATCH] [IMP] point_of_sale: rewrite price access control logic Introduced at 4e715dae94dc956dbfacfe3974f42ade86ad2c2a. Instead of doing hacky things like self.pos.gui.screen_instances... in the cashier widget to force the numpad UI to rerender itself it's better to use Backbone.js because: 1. we're already using it everywhere else to handle UI updates 2. easier to extend for eg. adding more cashier related access rights To do so cashier is turned into a Backbone attribute on PosModel. All references to the cashier property are replaced with get_cashier to ensure the Backbone attribute is used. Note that this is not necessary for the user property on PosModel. This contains the actual user logged into Odoo and thus never changes. This also removes the check in set_unit_price because: 1. it was wrong to begin with (no price change when restrict_price_control is enabled or you're a manager?) 2. even if fixed it would break the following for non-managers: 1. tipping 2. fix_tax_included_price --- addons/point_of_sale/static/src/js/chrome.js | 3 +-- addons/point_of_sale/static/src/js/models.js | 13 +++++-------- addons/point_of_sale/static/src/js/screens.js | 11 +++++++---- addons/point_of_sale/static/src/xml/pos.xml | 2 +- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/addons/point_of_sale/static/src/js/chrome.js b/addons/point_of_sale/static/src/js/chrome.js index 96f0e8df585..150a6930835 100644 --- a/addons/point_of_sale/static/src/js/chrome.js +++ b/addons/point_of_sale/static/src/js/chrome.js @@ -103,12 +103,11 @@ var UsernameWidget = PosBaseWidget.extend({ 'title': _t('Change Cashier'), }).then(function(user){ self.pos.set_cashier(user); - self.pos.gui.screen_instances.products.numpad.check_price_control_rights(); self.renderElement(); }); }, get_name: function(){ - var user = this.pos.cashier || this.pos.user; + var user = this.pos.get_cashier(); if(user){ return user.name; }else{ diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index ff336551bab..74ca9c38766 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -56,7 +56,6 @@ exports.PosModel = Backbone.Model.extend({ this.user = null; this.users = []; this.partners = []; - this.cashier = null; this.cashregisters = []; this.taxes = []; this.pos_session = null; @@ -73,6 +72,7 @@ exports.PosModel = Backbone.Model.extend({ 'orders': new OrderCollection(), 'selectedOrder': null, 'selectedClient': null, + 'cashier': null, }); this.get('orders').bind('remove', function(order,_unused_,options){ @@ -584,11 +584,11 @@ exports.PosModel = Backbone.Model.extend({ // returns the user who is currently the cashier for this point of sale get_cashier: function(){ - return this.cashier || this.user; + return this.get('cashier') || this.user; }, // changes the current cashier set_cashier: function(user){ - this.cashier = user; + this.set('cashier', user); }, //creates a new empty order and sets it as the current order add_new_order: function(){ @@ -1429,9 +1429,6 @@ exports.Orderline = Backbone.Model.extend({ }, // changes the base price of the product for this orderline set_unit_price: function(price){ - if (!this.pos.config.restrict_price_control || this.pos.get_cashier().role == 'manager') { - return; - } this.order.assert_editable(); this.price = round_di(parseFloat(price) || 0, this.pos.dp['Product Price']); this.trigger('change',this); @@ -1891,7 +1888,7 @@ exports.Order = Backbone.Model.extend({ statement_ids: paymentLines, pos_session_id: this.pos_session_id, partner_id: this.get_client() ? this.get_client().id : false, - user_id: this.pos.cashier ? this.pos.cashier.id : this.pos.user.id, + user_id: this.pos.get_cashier().id, uid: this.uid, sequence_number: this.sequence_number, creation_date: this.validation_date || this.creation_date, // todo: rename creation_date in master @@ -1911,7 +1908,7 @@ exports.Order = Backbone.Model.extend({ paymentlines.push(paymentline.export_for_printing()); }); var client = this.get('client'); - var cashier = this.pos.cashier || this.pos.user; + var cashier = this.pos.get_cashier(); var company = this.pos.company; var shop = this.pos.shop; var date = new Date(); diff --git a/addons/point_of_sale/static/src/js/screens.js b/addons/point_of_sale/static/src/js/screens.js index e053264277b..78b150b6844 100644 --- a/addons/point_of_sale/static/src/js/screens.js +++ b/addons/point_of_sale/static/src/js/screens.js @@ -364,17 +364,20 @@ var NumpadWidget = PosBaseWidget.extend({ this.state = new models.NumpadState(); }, start: function() { - this.check_price_control_rights(); + this.applyAccessRights(); this.state.bind('change:mode', this.changedMode, this); + this.pos.bind('change:cashier', this.applyAccessRights, this); this.changedMode(); this.$el.find('.numpad-backspace').click(_.bind(this.clickDeleteLastChar, this)); this.$el.find('.numpad-minus').click(_.bind(this.clickSwitchSign, this)); this.$el.find('.number-char').click(_.bind(this.clickAppendNewChar, this)); this.$el.find('.mode-button').click(_.bind(this.clickChangeMode, this)); }, - check_price_control_rights: function() { - var has_rights = !this.pos.config.restrict_price_control || this.pos.get_cashier().role == 'manager'; - this.$el.find('.mode-button[data-mode="price"]').toggleClass('disabled-mode', !has_rights).prop('disabled', !has_rights); + applyAccessRights: function() { + var has_price_control_rights = !this.pos.config.restrict_price_control || this.pos.get_cashier().role == 'manager'; + this.$el.find('.mode-button[data-mode="price"]') + .toggleClass('disabled-mode', !has_price_control_rights) + .prop('disabled', !has_price_control_rights); }, clickDeleteLastChar: function() { return this.state.deleteLastChar(); diff --git a/addons/point_of_sale/static/src/xml/pos.xml b/addons/point_of_sale/static/src/xml/pos.xml index 71e4ca2c1c8..e746a6f66f0 100644 --- a/addons/point_of_sale/static/src/xml/pos.xml +++ b/addons/point_of_sale/static/src/xml/pos.xml @@ -1422,7 +1422,7 @@

Phone:
- User:
+ User: