/
screens.js
1916 lines (1694 loc) · 67.1 KB
/
screens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
odoo.define('point_of_sale.screens', function (require) {
"use strict";
// This file contains the Screens definitions. Screens are the
// content of the right pane of the pos, containing the main functionalities.
//
// Screens must be defined and named in chrome.js before use.
//
// Screens transitions are controlled by the Gui.
// gui.set_startup_screen() sets the screen displayed at startup
// gui.set_default_screen() sets the screen displayed for new orders
// gui.show_screen() shows a screen
// gui.back() goes to the previous screen
//
// Screen state is saved in the order. When a new order is selected,
// a screen is displayed based on the state previously saved in the order.
// this is also done in the Gui with:
// gui.show_saved_screen()
//
// All screens inherit from ScreenWidget. The only addition from the base widgets
// are show() and hide() which shows and hides the screen but are also used to
// bind and unbind actions on widgets and devices. The gui guarantees
// that only one screen is shown at the same time and that show() is called after all
// hide()s
//
// Each Screens must be independant from each other, and should have no
// persistent state outside the models. Screen state variables are reset at
// each screen display. A screen can be called with parameters, which are
// to be used for the duration of the screen only.
var PosBaseWidget = require('point_of_sale.BaseWidget');
var gui = require('point_of_sale.gui');
var models = require('point_of_sale.models');
var core = require('web.core');
var Model = require('web.DataModel');
var utils = require('web.utils');
var formats = require('web.formats');
var QWeb = core.qweb;
var _t = core._t;
var round_pr = utils.round_precision;
/*--------------------------------------*\
| THE SCREEN WIDGET |
\*======================================*/
// The screen widget is the base class inherited
// by all screens.
var ScreenWidget = PosBaseWidget.extend({
init: function(parent,options){
this._super(parent,options);
this.hidden = false;
},
barcode_product_screen: 'products', //if defined, this screen will be loaded when a product is scanned
// what happens when a product is scanned :
// it will add the product to the order and go to barcode_product_screen.
barcode_product_action: function(code){
var self = this;
if (self.pos.scan_product(code)) {
if (self.barcode_product_screen) {
self.gui.show_screen(self.barcode_product_screen);
}
} else {
this.barcode_error_action(code);
}
},
// what happens when a cashier id barcode is scanned.
// the default behavior is the following :
// - if there's a user with a matching barcode, put it as the active 'cashier', go to cashier mode, and return true
// - else : do nothing and return false. You probably want to extend this to show and appropriate error popup...
barcode_cashier_action: function(code){
var users = this.pos.users;
for(var i = 0, len = users.length; i < len; i++){
if(users[i].barcode === code.code){
this.pos.set_cashier(users[i]);
this.chrome.widget.username.renderElement();
return true;
}
}
this.barcode_error_action(code);
return false;
},
// what happens when a client id barcode is scanned.
// the default behavior is the following :
// - if there's a user with a matching barcode, put it as the active 'client' and return true
// - else : return false.
barcode_client_action: function(code){
var partner = this.pos.db.get_partner_by_barcode(code.code);
if(partner){
this.pos.get_order().set_client(partner);
return true;
}
this.barcode_error_action(code);
return false;
},
// what happens when a discount barcode is scanned : the default behavior
// is to set the discount on the last order.
barcode_discount_action: function(code){
var last_orderline = this.pos.get_order().get_last_orderline();
if(last_orderline){
last_orderline.set_discount(code.value);
}
},
// What happens when an invalid barcode is scanned : shows an error popup.
barcode_error_action: function(code) {
var show_code;
if (code.code.length > 32) {
show_code = code.code.substring(0,29)+'...';
} else {
show_code = code.code;
}
this.gui.show_popup('error-barcode',show_code);
},
// this method shows the screen and sets up all the widget related to this screen. Extend this method
// if you want to alter the behavior of the screen.
show: function(){
var self = this;
this.hidden = false;
if(this.$el){
this.$el.removeClass('oe_hidden');
}
this.pos.barcode_reader.set_action_callback({
'cashier': _.bind(self.barcode_cashier_action, self),
'product': _.bind(self.barcode_product_action, self),
'client' : _.bind(self.barcode_client_action, self),
'discount': _.bind(self.barcode_discount_action, self),
'error' : _.bind(self.barcode_error_action, self),
});
},
// this method is called when the screen is closed to make place for a new screen. this is a good place
// to put your cleanup stuff as it is guaranteed that for each show() there is one and only one close()
close: function(){
if(this.pos.barcode_reader){
this.pos.barcode_reader.reset_action_callbacks();
}
},
// this methods hides the screen. It's not a good place to put your cleanup stuff as it is called on the
// POS initialization.
hide: function(){
this.hidden = true;
if(this.$el){
this.$el.addClass('oe_hidden');
}
},
// we need this because some screens re-render themselves when they are hidden
// (due to some events, or magic, or both...) we must make sure they remain hidden.
// the good solution would probably be to make them not re-render themselves when they
// are hidden.
renderElement: function(){
this._super();
if(this.hidden){
if(this.$el){
this.$el.addClass('oe_hidden');
}
}
},
});
/*--------------------------------------*\
| THE DOM CACHE |
\*======================================*/
// The Dom Cache is used by various screens to improve
// their performances when displaying many time the
// same piece of DOM.
//
// It is a simple map from string 'keys' to DOM Nodes.
//
// The cache empties itself based on usage frequency
// stats, so you may not always get back what
// you put in.
var DomCache = core.Class.extend({
init: function(options){
options = options || {};
this.max_size = options.max_size || 2000;
this.cache = {};
this.access_time = {};
this.size = 0;
},
cache_node: function(key,node){
var cached = this.cache[key];
this.cache[key] = node;
this.access_time[key] = new Date().getTime();
if(!cached){
this.size++;
while(this.size >= this.max_size){
var oldest_key = null;
var oldest_time = new Date().getTime();
for(key in this.cache){
var time = this.access_time[key];
if(time <= oldest_time){
oldest_time = time;
oldest_key = key;
}
}
if(oldest_key){
delete this.cache[oldest_key];
delete this.access_time[oldest_key];
}
this.size--;
}
}
return node;
},
clear_node: function(key) {
var cached = this.cache[key];
if (cached) {
delete this.cache[key];
delete this.access_time[key];
this.size --;
}
},
get_node: function(key){
var cached = this.cache[key];
if(cached){
this.access_time[key] = new Date().getTime();
}
return cached;
},
});
/*--------------------------------------*\
| THE SCALE SCREEN |
\*======================================*/
// The scale screen displays the weight of
// a product on the electronic scale.
var ScaleScreenWidget = ScreenWidget.extend({
template:'ScaleScreenWidget',
next_screen: 'products',
previous_screen: 'products',
show: function(){
this._super();
var self = this;
var queue = this.pos.proxy_queue;
this.set_weight(0);
this.renderElement();
this.hotkey_handler = function(event){
if(event.which === 13){
self.order_product();
self.gui.show_screen(self.next_screen);
}else if(event.which === 27){
self.gui.show_screen(self.previous_screen);
}
};
$('body').on('keypress',this.hotkey_handler);
this.$('.back').click(function(){
self.gui.show_screen(self.previous_screen);
});
this.$('.next,.buy-product').click(function(){
self.order_product();
self.gui.show_screen(self.next_screen);
});
queue.schedule(function(){
return self.pos.proxy.scale_read().then(function(weight){
self.set_weight(weight.weight);
});
},{duration:50, repeat: true});
},
get_product: function(){
return this.gui.get_current_screen_param('product');
},
order_product: function(){
this.pos.get_order().add_product(this.get_product(),{ quantity: this.weight });
},
get_product_name: function(){
var product = this.get_product();
return (product ? product.display_name : undefined) || 'Unnamed Product';
},
get_product_price: function(){
var product = this.get_product();
return (product ? product.price : 0) || 0;
},
set_weight: function(weight){
this.weight = weight;
this.$('.weight').text(this.get_product_weight_string());
this.$('.computed-price').text(this.get_computed_price_string());
},
get_product_weight_string: function(){
var product = this.get_product();
var defaultstr = (this.weight || 0).toFixed(3) + ' Kg';
if(!product || !this.pos){
return defaultstr;
}
var unit_id = product.uom_id;
if(!unit_id){
return defaultstr;
}
var unit = this.pos.units_by_id[unit_id[0]];
var weight = round_pr(this.weight || 0, unit.rounding);
var weightstr = weight.toFixed(Math.ceil(Math.log(1.0/unit.rounding) / Math.log(10) ));
weightstr += ' Kg';
return weightstr;
},
get_computed_price_string: function(){
return this.format_currency(this.get_product_price() * this.weight);
},
close: function(){
this._super();
$('body').off('keypress',this.hotkey_handler);
this.pos.proxy_queue.clear();
},
});
gui.define_screen({name: 'scale', widget: ScaleScreenWidget});
/*--------------------------------------*\
| THE PRODUCT SCREEN |
\*======================================*/
// The product screen contains the list of products,
// The category selector and the order display.
// It is the default screen for orders and the
// startup screen for shops.
//
// There product screens uses many sub-widgets,
// the code follows.
/* ------------ The Numpad ------------ */
// The numpad that edits the order lines.
var NumpadWidget = PosBaseWidget.extend({
template:'NumpadWidget',
init: function(parent) {
this._super(parent);
this.state = new models.NumpadState();
},
start: function() {
this.state.bind('change:mode', this.changedMode, 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));
},
clickDeleteLastChar: function() {
return this.state.deleteLastChar();
},
clickSwitchSign: function() {
return this.state.switchSign();
},
clickAppendNewChar: function(event) {
var newChar;
newChar = event.currentTarget.innerText || event.currentTarget.textContent;
return this.state.appendNewChar(newChar);
},
clickChangeMode: function(event) {
var newMode = event.currentTarget.attributes['data-mode'].nodeValue;
return this.state.changeMode(newMode);
},
changedMode: function() {
var mode = this.state.get('mode');
$('.selected-mode').removeClass('selected-mode');
$(_.str.sprintf('.mode-button[data-mode="%s"]', mode), this.$el).addClass('selected-mode');
},
});
/* ---------- The Action Pad ---------- */
// The action pad contains the payment button and the
// customer selection button
var ActionpadWidget = PosBaseWidget.extend({
template: 'ActionpadWidget',
init: function(parent, options) {
var self = this;
this._super(parent, options);
this.pos.bind('change:selectedClient', function() {
self.renderElement();
});
},
renderElement: function() {
var self = this;
this._super();
this.$('.pay').click(function(){
self.gui.show_screen('payment');
});
this.$('.set-customer').click(function(){
self.gui.show_screen('clientlist');
});
}
});
/* --------- The Order Widget --------- */
// Displays the current Order.
var OrderWidget = PosBaseWidget.extend({
template:'OrderWidget',
init: function(parent, options) {
var self = this;
this._super(parent,options);
this.numpad_state = options.numpad_state;
this.numpad_state.reset();
this.numpad_state.bind('set_value', this.set_value, this);
this.pos.bind('change:selectedOrder', this.change_selected_order, this);
this.line_click_handler = function(event){
self.click_line(this.orderline, event);
};
if (this.pos.get_order()) {
this.bind_order_events();
}
},
click_line: function(orderline, event) {
this.pos.get_order().select_orderline(orderline);
this.numpad_state.reset();
},
set_value: function(val) {
var order = this.pos.get_order();
if (order.get_selected_orderline()) {
var mode = this.numpad_state.get('mode');
if( mode === 'quantity'){
order.get_selected_orderline().set_quantity(val);
}else if( mode === 'discount'){
order.get_selected_orderline().set_discount(val);
}else if( mode === 'price'){
order.get_selected_orderline().set_unit_price(val);
}
}
},
change_selected_order: function() {
if (this.pos.get_order()) {
this.bind_order_events();
this.numpad_state.reset();
this.renderElement();
}
},
orderline_add: function(){
this.numpad_state.reset();
this.renderElement('and_scroll_to_bottom');
},
orderline_remove: function(line){
this.remove_orderline(line);
this.numpad_state.reset();
this.update_summary();
},
orderline_change: function(line){
this.rerender_orderline(line);
this.update_summary();
},
bind_order_events: function() {
var order = this.pos.get_order();
order.unbind('change:client', this.update_summary, this);
order.bind('change:client', this.update_summary, this);
order.unbind('change', this.update_summary, this);
order.bind('change', this.update_summary, this);
var lines = order.orderlines;
lines.unbind('add', this.orderline_add, this);
lines.bind('add', this.orderline_add, this);
lines.unbind('remove', this.orderline_remove, this);
lines.bind('remove', this.orderline_remove, this);
lines.unbind('change', this.orderline_change, this);
lines.bind('change', this.orderline_change, this);
},
render_orderline: function(orderline){
var el_str = QWeb.render('Orderline',{widget:this, line:orderline});
var el_node = document.createElement('div');
el_node.innerHTML = _.str.trim(el_str);
el_node = el_node.childNodes[0];
el_node.orderline = orderline;
el_node.addEventListener('click',this.line_click_handler);
orderline.node = el_node;
return el_node;
},
remove_orderline: function(order_line){
if(this.pos.get_order().get_orderlines().length === 0){
this.renderElement();
}else{
order_line.node.parentNode.removeChild(order_line.node);
}
},
rerender_orderline: function(order_line){
var node = order_line.node;
var replacement_line = this.render_orderline(order_line);
node.parentNode.replaceChild(replacement_line,node);
},
// overriding the openerp framework replace method for performance reasons
replace: function($target){
this.renderElement();
var target = $target[0];
target.parentNode.replaceChild(this.el,target);
},
renderElement: function(scrollbottom){
var order = this.pos.get_order();
if (!order) {
return;
}
var orderlines = order.get_orderlines();
var el_str = QWeb.render('OrderWidget',{widget:this, order:order, orderlines:orderlines});
var el_node = document.createElement('div');
el_node.innerHTML = _.str.trim(el_str);
el_node = el_node.childNodes[0];
var list_container = el_node.querySelector('.orderlines');
for(var i = 0, len = orderlines.length; i < len; i++){
var orderline = this.render_orderline(orderlines[i]);
list_container.appendChild(orderline);
}
if(this.el && this.el.parentNode){
this.el.parentNode.replaceChild(el_node,this.el);
}
this.el = el_node;
this.update_summary();
if(scrollbottom){
this.el.querySelector('.order-scroller').scrollTop = 100 * orderlines.length;
}
},
update_summary: function(){
var order = this.pos.get_order();
if (!order.get_orderlines().length) {
return;
}
var total = order ? order.get_total_with_tax() : 0;
var taxes = order ? total - order.get_total_without_tax() : 0;
this.el.querySelector('.summary .total > .value').textContent = this.format_currency(total);
this.el.querySelector('.summary .total .subentry .value').textContent = this.format_currency(taxes);
},
});
/* ------ The Product Categories ------ */
// Display and navigate the product categories.
// Also handles searches.
// - set_category() to change the displayed category
// - reset_category() to go to the root category
// - perform_search() to search for products
// - clear_search() does what it says.
var ProductCategoriesWidget = PosBaseWidget.extend({
template: 'ProductCategoriesWidget',
init: function(parent, options){
var self = this;
this._super(parent,options);
this.product_type = options.product_type || 'all'; // 'all' | 'weightable'
this.onlyWeightable = options.onlyWeightable || false;
this.category = this.pos.root_category;
this.breadcrumb = [];
this.subcategories = [];
this.product_list_widget = options.product_list_widget || null;
this.category_cache = new DomCache();
this.start_categ_id = this.pos.config.iface_start_categ_id ? this.pos.config.iface_start_categ_id[0] : 0;
this.set_category(this.pos.db.get_category_by_id(this.start_categ_id));
this.switch_category_handler = function(event){
self.set_category(self.pos.db.get_category_by_id(Number(this.dataset.categoryId)));
self.renderElement();
};
this.clear_search_handler = function(event){
self.clear_search();
};
var search_timeout = null;
this.search_handler = function(event){
clearTimeout(search_timeout);
var query = this.value;
search_timeout = setTimeout(function(){
self.perform_search(self.category, query, event.which === 13);
},70);
};
},
// changes the category. if undefined, sets to root category
set_category : function(category){
var db = this.pos.db;
if(!category){
this.category = db.get_category_by_id(db.root_category_id);
}else{
this.category = category;
}
this.breadcrumb = [];
var ancestors_ids = db.get_category_ancestors_ids(this.category.id);
for(var i = 1; i < ancestors_ids.length; i++){
this.breadcrumb.push(db.get_category_by_id(ancestors_ids[i]));
}
if(this.category.id !== db.root_category_id){
this.breadcrumb.push(this.category);
}
this.subcategories = db.get_category_by_id(db.get_category_childs_ids(this.category.id));
},
get_image_url: function(category){
return window.location.origin + '/web/image?model=pos.category&field=image_medium&id='+category.id;
},
render_category: function( category, with_image ){
var cached = this.category_cache.get_node(category.id);
if(!cached){
if(with_image){
var image_url = this.get_image_url(category);
var category_html = QWeb.render('CategoryButton',{
widget: this,
category: category,
image_url: this.get_image_url(category),
});
category_html = _.str.trim(category_html);
var category_node = document.createElement('div');
category_node.innerHTML = category_html;
category_node = category_node.childNodes[0];
}else{
var category_html = QWeb.render('CategorySimpleButton',{
widget: this,
category: category,
});
category_html = _.str.trim(category_html);
var category_node = document.createElement('div');
category_node.innerHTML = category_html;
category_node = category_node.childNodes[0];
}
this.category_cache.cache_node(category.id,category_node);
return category_node;
}
return cached;
},
replace: function($target){
this.renderElement();
var target = $target[0];
target.parentNode.replaceChild(this.el,target);
},
renderElement: function(){
var el_str = QWeb.render(this.template, {widget: this});
var el_node = document.createElement('div');
el_node.innerHTML = el_str;
el_node = el_node.childNodes[1];
if(this.el && this.el.parentNode){
this.el.parentNode.replaceChild(el_node,this.el);
}
this.el = el_node;
var withpics = this.pos.config.iface_display_categ_images;
var list_container = el_node.querySelector('.category-list');
if (list_container) {
if (!withpics) {
list_container.classList.add('simple');
} else {
list_container.classList.remove('simple');
}
for(var i = 0, len = this.subcategories.length; i < len; i++){
list_container.appendChild(this.render_category(this.subcategories[i],withpics));
}
}
var buttons = el_node.querySelectorAll('.js-category-switch');
for(var i = 0; i < buttons.length; i++){
buttons[i].addEventListener('click',this.switch_category_handler);
}
var products = this.pos.db.get_product_by_category(this.category.id);
this.product_list_widget.set_product_list(products); // FIXME: this should be moved elsewhere ...
this.el.querySelector('.searchbox input').addEventListener('keypress',this.search_handler);
this.el.querySelector('.search-clear').addEventListener('click',this.clear_search_handler);
if(this.pos.config.iface_vkeyboard && this.chrome.widget.keyboard){
this.chrome.widget.keyboard.connect($(this.el.querySelector('.searchbox input')));
}
},
// resets the current category to the root category
reset_category: function(){
this.set_category(this.pos.db.get_category_by_id(this.start_categ_id));
this.renderElement();
},
// empties the content of the search box
clear_search: function(){
var products = this.pos.db.get_product_by_category(this.category.id);
this.product_list_widget.set_product_list(products);
var input = this.el.querySelector('.searchbox input');
input.value = '';
input.focus();
},
perform_search: function(category, query, buy_result){
var products;
if(query){
products = this.pos.db.search_product_in_category(category.id,query);
if(buy_result && products.length === 1){
this.pos.get_order().add_product(products[0]);
this.clear_search();
}else{
this.product_list_widget.set_product_list(products);
}
}else{
products = this.pos.db.get_product_by_category(this.category.id);
this.product_list_widget.set_product_list(products);
}
},
});
/* --------- The Product List --------- */
// Display the list of products.
// - change the list with .set_product_list()
// - click_product_action(), passed as an option, tells
// what to do when a product is clicked.
var ProductListWidget = PosBaseWidget.extend({
template:'ProductListWidget',
init: function(parent, options) {
var self = this;
this._super(parent,options);
this.model = options.model;
this.productwidgets = [];
this.weight = options.weight || 0;
this.show_scale = options.show_scale || false;
this.next_screen = options.next_screen || false;
this.click_product_handler = function(){
var product = self.pos.db.get_product_by_id(this.dataset.productId);
options.click_product_action(product);
};
this.product_list = options.product_list || [];
this.product_cache = new DomCache();
},
set_product_list: function(product_list){
this.product_list = product_list;
this.renderElement();
},
get_product_image_url: function(product){
return window.location.origin + '/web/image?model=product.product&field=image_medium&id='+product.id;
},
replace: function($target){
this.renderElement();
var target = $target[0];
target.parentNode.replaceChild(this.el,target);
},
render_product: function(product){
var cached = this.product_cache.get_node(product.id);
if(!cached){
var image_url = this.get_product_image_url(product);
var product_html = QWeb.render('Product',{
widget: this,
product: product,
image_url: this.get_product_image_url(product),
});
var product_node = document.createElement('div');
product_node.innerHTML = product_html;
product_node = product_node.childNodes[1];
this.product_cache.cache_node(product.id,product_node);
return product_node;
}
return cached;
},
renderElement: function() {
var el_str = QWeb.render(this.template, {widget: this});
var el_node = document.createElement('div');
el_node.innerHTML = el_str;
el_node = el_node.childNodes[1];
if(this.el && this.el.parentNode){
this.el.parentNode.replaceChild(el_node,this.el);
}
this.el = el_node;
var list_container = el_node.querySelector('.product-list');
for(var i = 0, len = this.product_list.length; i < len; i++){
var product_node = this.render_product(this.product_list[i]);
product_node.addEventListener('click',this.click_product_handler);
list_container.appendChild(product_node);
}
},
});
/* -------- The Action Buttons -------- */
// Above the numpad and the actionpad, buttons
// for extra actions and controls by point of
// sale extensions modules.
var action_button_classes = [];
var define_action_button = function(classe, options){
options = options || {};
var classes = action_button_classes;
var index = classes.length;
var i;
if (options.after) {
for (i = 0; i < classes.length; i++) {
if (classes[i].name === options.after) {
index = i + 1;
}
}
} else if (options.before) {
for (i = 0; i < classes.length; i++) {
if (classes[i].name === options.after) {
index = i;
break;
}
}
}
classes.splice(i,0,classe);
};
var ActionButtonWidget = PosBaseWidget.extend({
template: 'ActionButtonWidget',
label: _t('Button'),
renderElement: function(){
var self = this;
this._super();
this.$el.click(function(){
self.button_click();
});
},
button_click: function(){},
highlight: function(highlight){
this.$el.toggleClass('highlight',!!highlight);
},
// alternative highlight color
altlight: function(altlight){
this.$el.toggleClass('altlight',!!altlight);
},
});
/* -------- The Product Screen -------- */
var ProductScreenWidget = ScreenWidget.extend({
template:'ProductScreenWidget',
start: function(){
var self = this;
this.actionpad = new ActionpadWidget(this,{});
this.actionpad.replace(this.$('.placeholder-ActionpadWidget'));
this.numpad = new NumpadWidget(this,{});
this.numpad.replace(this.$('.placeholder-NumpadWidget'));
this.order_widget = new OrderWidget(this,{
numpad_state: this.numpad.state,
});
this.order_widget.replace(this.$('.placeholder-OrderWidget'));
this.product_list_widget = new ProductListWidget(this,{
click_product_action: function(product){ self.click_product(product); },
product_list: this.pos.db.get_product_by_category(0)
});
this.product_list_widget.replace(this.$('.placeholder-ProductListWidget'));
this.product_categories_widget = new ProductCategoriesWidget(this,{
product_list_widget: this.product_list_widget,
});
this.product_categories_widget.replace(this.$('.placeholder-ProductCategoriesWidget'));
this.action_buttons = {};
var classes = action_button_classes;
for (var i = 0; i < classes.length; i++) {
var classe = classes[i];
if ( !classe.condition || classe.condition.call(this) ) {
var widget = new classe.widget(this,{});
widget.appendTo(this.$('.control-buttons'));
this.action_buttons[classe.name] = widget;
}
}
if (_.size(this.action_buttons)) {
this.$('.control-buttons').removeClass('oe_hidden');
}
},
click_product: function(product) {
if(product.to_weight && this.pos.config.iface_electronic_scale){
this.gui.show_screen('scale',{product: product});
}else{
this.pos.get_order().add_product(product);
}
},
show: function(){
this._super();
this.product_categories_widget.reset_category();
this.numpad.state.reset();
},
close: function(){
this._super();
if(this.pos.config.iface_vkeyboard && this.chrome.widget.keyboard){
this.chrome.widget.keyboard.hide();
}
},
});
gui.define_screen({name:'products', widget: ProductScreenWidget});
/*--------------------------------------*\
| THE CLIENT LIST |
\*======================================*/
// The clientlist displays the list of customer,
// and allows the cashier to create, edit and assign
// customers.
var ClientListScreenWidget = ScreenWidget.extend({
template: 'ClientListScreenWidget',
init: function(parent, options){
this._super(parent, options);
this.partner_cache = new DomCache();
},
auto_back: true,
show: function(){
var self = this;
this._super();
this.renderElement();
this.details_visible = false;
this.old_client = this.pos.get_order().get_client();
this.new_client = this.old_client;
this.$('.back').click(function(){
self.gui.back();
});
this.$('.next').click(function(){
self.save_changes();
self.gui.back(); // FIXME HUH ?
});
this.$('.new-customer').click(function(){
self.display_client_details('edit',{
'country_id': self.pos.company.country_id,
});
});
var partners = this.pos.db.get_partners_sorted(1000);
this.render_list(partners);
this.reload_partners();
if( this.old_client ){
this.display_client_details('show',this.old_client,0);
}
this.$('.client-list-contents').delegate('.client-line','click',function(event){
self.line_select(event,$(this),parseInt($(this).data('id')));
});
var search_timeout = null;
if(this.pos.config.iface_vkeyboard && this.chrome.widget.keyboard){
this.chrome.widget.keyboard.connect(this.$('.searchbox input'));
}