diff --git a/addons/pos_restaurant/restaurant.py b/addons/pos_restaurant/restaurant.py index 69a1e3f4137..6b51fcfa24e 100644 --- a/addons/pos_restaurant/restaurant.py +++ b/addons/pos_restaurant/restaurant.py @@ -43,18 +43,42 @@ class restaurant_table(osv.osv): 'name': fields.char('Table Name', size=32, required=True, help='An internal identification of a table'), 'floor_id': fields.many2one('restaurant.floor','Floor'), 'shape': fields.selection([('square','Square'),('round','Round')],'Shape', required=True), - 'position_h': fields.integer('Horizontal Position', help="The table's horizontal position from the left side to the table's center, in percentage of the floor's width"), - 'position_v': fields.integer('Vertical Position', help="The table's vertical position from the top to the table's center, in percentage of the floor's height"), - 'width': fields.integer('Width', help="The table's width in percentage of the floor's width"), - 'height': fields.integer('Height', help="The table's height in percentage of the floor's height"), + 'position_h': fields.float('Horizontal Position', help="The table's horizontal position from the left side to the table's center, in percentage of the floor's width"), + 'position_v': fields.float('Vertical Position', help="The table's vertical position from the top to the table's center, in percentage of the floor's height"), + 'width': fields.float('Width', help="The table's width in percentage of the floor's width"), + 'height': fields.float('Height', help="The table's height in percentage of the floor's height"), 'color': fields.char('Color', size=32, help="The table's color"), + 'active': fields.boolean('Active',help='If false, the table is deactivated and will not be available in the point of sale'), } + _defaults = { 'shape': 'square', - 'height': 10, - 'width': 10, + 'position_h': 10, + 'position_v': 10, + 'height': 50, + 'width': 50, + 'active': True, } + def create_from_ui(self, cr, uid, table, context=None): + """ create or modify a table from the point of sale UI. + table contains the table's fields. If it contains an + id, it will modify the existing table. It then + returns the id of the table. """ + + if table.get('floor_id',False): + floor_id = table['floor_id'][0] + table['floor_id'] = floor_id + + if table.get('id',False): # Modifiy existing table + table_id = table['id'] + del table['id'] + self.write(cr, uid, [table_id], table, context=context) + else: + table_id = self.create(cr, uid, table, context=context) + + return table_id + class restaurant_printer(osv.osv): _name = 'restaurant.printer' diff --git a/addons/pos_restaurant/static/src/css/restaurant.css b/addons/pos_restaurant/static/src/css/restaurant.css index 57be4136eae..162b29c710a 100644 --- a/addons/pos_restaurant/static/src/css/restaurant.css +++ b/addons/pos_restaurant/static/src/css/restaurant.css @@ -44,6 +44,7 @@ position: relative; width: auto; background: rgba(0,0,0,0.1); + background-repeat: no-repeat; } .floor-map .table{ @@ -78,6 +79,10 @@ cursor: pointer; border: solid 1px rgba(0,0,0,0.2); } +.floor-map .edit-button.editing.active { + background: #444; + color: white; +} .floor-map .edit-bar { position: absolute; top: 0; diff --git a/addons/pos_restaurant/static/src/js/floors.js b/addons/pos_restaurant/static/src/js/floors.js index 89143e34575..01ef7eab1c1 100644 --- a/addons/pos_restaurant/static/src/js/floors.js +++ b/addons/pos_restaurant/static/src/js/floors.js @@ -1,4 +1,5 @@ function openerp_restaurant_floors(instance,module){ + var _t = instance.web._t; module.PosModel.prototype.models.push({ model: 'restaurant.floor', @@ -27,10 +28,10 @@ function openerp_restaurant_floors(instance,module){ }, }); - module.TableWidget = instance.web.Widget.extend({ + module.TableWidget = module.PosBaseWidget.extend({ template: 'TableWidget', init: function(parent, options){ - this._super(parent); + this._super(parent, options) this.table = options.table; this.selected = false; this.moved = false; @@ -47,23 +48,26 @@ function openerp_restaurant_floors(instance,module){ }, click_handler: function(){ var self = this; - setTimeout(function(){ // in a setTimeout to debounce with drag&drop - if (!self.dragging) { - if (self.moved) { - self.moved = false; - } else if (!self.selected) { - self.getParent().select_table(self); - } else { - self.getParent().deselect_tables(); - } - } - },50); + var floorplan = this.getParent(); + if (floorplan.editing) { + setTimeout(function(){ // in a setTimeout to debounce with drag&drop + if (!self.dragging) { + if (self.moved) { + self.moved = false; + } else if (!self.selected) { + self.getParent().select_table(self); + } else { + self.getParent().deselect_tables(); + } + } + },50); + } }, dragstart_handler: function(event,$el,drag){ if (this.selected && !this.handle_dragging) { this.dragging = true; this.dragpos = { x: drag.offsetX, y: drag.offsetY }; - } + } }, dragend_handler: function(event,$el){ this.dragging = false; @@ -103,13 +107,13 @@ function openerp_restaurant_floors(instance,module){ var cl = this.handle.classList; - var tw = this.table.width; - var th = this.table.height; + var MIN_SIZE = 40; + + var tw = Math.max(MIN_SIZE, this.table.width); + var th = Math.max(MIN_SIZE, this.table.height); var tx = this.table.position_h; var ty = this.table.position_v; - var MIN_SIZE = 40; - if (cl.contains('left') && tw - dx >= MIN_SIZE) { tw -= dx; tx += dx; @@ -180,6 +184,49 @@ function openerp_restaurant_floors(instance,module){ deselect: function() { this.selected = false; this.renderElement(); + this.save_changes(); + }, + save_changes: function(){ + var self = this; + var model = new instance.web.Model('restaurant.table'); + var fields = _.find(this.pos.models,function(model){ return model.model === 'restaurant.table'; }).fields; + + model.call('create_from_ui',[this.table]).then(function(table_id){ + model.query(fields).filter([['id','=',table_id]]).first().then(function(table){ + for (field in table) { + self.table[field] = table[field]; + } + self.renderElement(); + }); + }); + }, + trash: function(){ + var self = this; + var model = new instance.web.Model('restaurant.table'); + return model.call('create_from_ui',[{'active':false,'id':this.table.id}]).then(function(table_id){ + // Removing all references from the table and the table_widget in in the UI ... + // This should probably be cleaned. + for (var i = 0; i < self.pos.floors.length; i++) { + var floor = self.pos.floors[i]; + for (var j = 0; j < floor.tables.length; j++) { + if (floor.tables[j].id === table_id) { + floor.tables.splice(j,1); + break; + } + } + } + var floorplan = self.getParent(); + for (var i = 0; i < floorplan.table_widgets.length; i++) { + if (floorplan.table_widgets[i] === self) { + floorplan.table_widgets.splice(i,1); + } + } + if (floorplan.selected_table === self) { + floorplan.selected_table = null; + } + floorplan.update_toolbar(); + self.destroy(); + }); }, renderElement: function(){ var self = this; @@ -207,6 +254,7 @@ function openerp_restaurant_floors(instance,module){ this.floor = this.pos.floors[0]; this.table_widgets = []; this.selected_table = null; + this.editing = false; }, show: function(){ this._super(); @@ -214,11 +262,17 @@ function openerp_restaurant_floors(instance,module){ }, hide: function(){ this._super(); + if (this.editing) { + this.toggle_editing(); + } this.pos_widget.$('.order-selector').removeClass('oe_invisible'); }, click_floor_button: function(event,$el){ var floor = this.pos.floors_by_id[$el.data('id')]; if (floor !== this.floor) { + if (this.editing) { + this.toggle_editing(); + } this.floor = floor; this.selected_table = null; this.renderElement(); @@ -231,8 +285,7 @@ function openerp_restaurant_floors(instance,module){ for (var i = 0; i < this.table_widgets.length; i++) { var table = this.table_widgets[i]; if (table.selected) { - table.selected = false; - table.renderElement(); + table.deselect(); } } this.selected_table = null; @@ -275,7 +328,7 @@ function openerp_restaurant_floors(instance,module){ var self = this; if (this.selected_table) { this.pos_widget.screen_selector.show_popup('textinput',{ - 'message':'Table Name ?', + 'message':_t('Table Name ?'), 'value': this.selected_table.table.name, 'confirm': function(value) { self.selected_table.set_table_name(value); @@ -288,6 +341,7 @@ function openerp_restaurant_floors(instance,module){ var tw = this.create_table(this.selected_table.table); tw.table.position_h += 10; tw.table.position_v += 10; + tw.save_changes(); this.select_table(tw); } }, @@ -308,7 +362,8 @@ function openerp_restaurant_floors(instance,module){ table[p] = params[p]; } - table.floor_id = this.floor.id; + delete table['id']; + table.floor_id = [this.floor.id,'']; this.floor.tables.push(table); var tw = new module.TableWidget(this,{table: table}); @@ -316,7 +371,34 @@ function openerp_restaurant_floors(instance,module){ this.table_widgets.push(tw); return tw; }, + tool_trash_table: function(){ + var self = this; + if (this.selected_table) { + this.pos_widget.screen_selector.show_popup('confirm',{ + 'message':_t('Are you sure ?'), + 'comment':_t('Removing a table cannot be undone'), + 'confirm': function(){ + self.selected_table.trash(); + }, + }); + } + }, + toggle_editing: function(){ + this.editing = !this.editing; + this.update_toolbar(); + + if (!this.editing) { + this.deselect_tables(); + } + }, update_toolbar: function(){ + + if (this.editing) { + this.$('.edit-bar').removeClass('oe_hidden'); + } else { + this.$('.edit-bar').addClass('oe_hidden'); + } + if (this.selected_table) { this.$('.needs-selection').removeClass('disabled'); var table = this.selected_table.table; @@ -375,6 +457,10 @@ function openerp_restaurant_floors(instance,module){ this.$('.edit-button.rename').click(function(event){ self.tool_rename_table(); }); + + this.$('.edit-button.trash').click(function(event){ + self.tool_trash_table(); + }); this.$('.color-picker .close-picker').click(function(event){ self.tool_colorpicker_close(); @@ -387,6 +473,10 @@ function openerp_restaurant_floors(instance,module){ event.stopPropagation(); }); + this.$('.edit-button.editing').click(function(){ + self.toggle_editing(); + }); + this.$('.floor-map').click(function(event){ if (event.target === self.$('.floor-map')[0]) { self.deselect_tables(); diff --git a/addons/pos_restaurant/static/src/xml/floors.xml b/addons/pos_restaurant/static/src/xml/floors.xml index 668aeaaf7c3..8b2e2a5d4fe 100644 --- a/addons/pos_restaurant/static/src/xml/floors.xml +++ b/addons/pos_restaurant/static/src/xml/floors.xml @@ -41,7 +41,7 @@