[IMP] point_of_sale: rewrite price access control logic

Introduced at 4e715dae94. 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
This commit is contained in:
Joren Van Onder
2017-05-22 13:47:21 -07:00
parent 4308e692c5
commit f5f60599bf
4 changed files with 14 additions and 15 deletions
+1 -2
View File
@@ -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{
+5 -8
View File
@@ -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();
@@ -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();
+1 -1
View File
@@ -1422,7 +1422,7 @@
<br />
<t t-esc="widget.pos.company.name"/><br />
Phone: <t t-esc="widget.pos.company.phone || ''"/><br />
User: <t t-esc="widget.pos.cashier ? widget.pos.cashier.name : widget.pos.user.name"/><br />
User: <t t-esc="widget.pos.get_cashier().name"/><br />
<br />
<t t-if="receipt.header">
<div style='text-align:center'>