c4343cbf2d610e7674d0d294cdee0ef26000609f
[odoo/odoo.git] / addons / pos_restaurant / static / src / js / multiprint.js
1 function openerp_restaurant_multiprint(instance,module){
2     var QWeb = instance.web.qweb;
3         var _t = instance.web._t;
4
5     module.Printer = instance.web.Class.extend(openerp.PropertiesMixin,{
6         init: function(parent,options){
7             openerp.PropertiesMixin.init.call(this,parent);
8             var self = this;
9             options = options || {};
10             var url = options.url || 'http://localhost:8069';
11             this.connection = new instance.web.Session(undefined,url, { use_cors: true});
12             this.host       = url;
13             this.receipt_queue = [];
14         },
15         print: function(receipt){
16             var self = this;
17             if(receipt){
18                 this.receipt_queue.push(receipt);
19             }
20             var aborted = false;
21             function send_printing_job(){
22                 if(self.receipt_queue.length > 0){
23                     var r = self.receipt_queue.shift();
24                     self.connection.rpc('/hw_proxy/print_xml_receipt',{receipt: r},{timeout: 5000})
25                         .then(function(){
26                             send_printing_job();
27                         },function(){
28                             self.receipt_queue.unshift(r);
29                         });
30                 }
31             }
32             send_printing_job();
33         },
34     });
35
36     module.PosModel.prototype.models.push({
37         model: 'restaurant.printer',
38         fields: ['name','proxy_ip','product_categories_ids'],
39         domain: null,
40         loaded: function(self,printers){
41             var active_printers = {};
42             for (var i = 0; i < self.config.printer_ids.length; i++) {
43                 active_printers[self.config.printer_ids[i]] = true;
44             }
45
46             self.printers = [];
47             for(var i = 0; i < printers.length; i++){
48                 if(active_printers[printers[i].id]){
49                     var printer = new module.Printer(self,{url:'http://'+printers[i].proxy_ip+':8069'});
50                     printer.config = printers[i];
51                     self.printers.push(printer);
52                 }
53             }
54         },
55     });
56
57     module.Orderline = module.Orderline.extend({
58         get_line_diff_hash: function(){
59             if (this.get_note()) {
60                 return this.get_product().id + '|' + this.get_note();
61             } else {
62                 return '' + this.get_product().id;
63             }
64         },
65     });
66
67     var _super_order = module.Order.prototype;
68     module.Order = module.Order.extend({
69         lineResume: function(){
70             var resume = {};
71
72             this.orderlines.each(function(line){
73                 var line_hash = line.get_line_diff_hash();
74                 var qty  = Number(line.get_quantity());
75                 var note = line.get_note();
76                 var product_id = line.get_product().id;
77
78                 if (typeof resume[line_hash] === 'undefined') {
79                     resume[line_hash] = { qty: qty, note: note, product_id: product_id };
80                 } else {
81                     resume[line_hash].qty += qty;
82                 }
83             });
84             return resume;
85         },
86         saveChanges: function(){
87             this.saved_resume = this.build_line_resume();
88             this.trigger('change',this);
89         },
90         computeChanges: function(categories){
91             var current_res = this.build_line_resume();
92             var old_res     = this.saved_resume || {};
93             var json        = this.export_as_JSON();
94             var add = [];
95             var rem = [];
96
97             for( product in current){
98                 if (typeof old[product] === 'undefined'){
99                     add.push({
100                         'id': product,
101                         'name': this.pos.db.get_product_by_id(product).display_name,
102                         'quantity': current[product],
103                     });
104                 }else if( old[product] < current[product]){
105                     add.push({
106                         'id': product,
107                         'name': this.pos.db.get_product_by_id(product).display_name,
108                         'quantity': current[product] - old[product],
109                     });
110                 }else if( old[product] > current[product]){
111                     rem.push({
112                         'id': product,
113                         'name': this.pos.db.get_product_by_id(product).display_name,
114                         'quantity': old[product] - current[product],
115                     });
116                 }
117             }
118
119             for( product in old){
120                 if(typeof current[product] === 'undefined'){
121                     rem.push({
122                         'id': product,
123                         'name': this.pos.db.get_product_by_id(product).display_name,
124                         'quantity': old[product], 
125                     });
126                 }
127             }
128
129             if(categories && categories.length > 0){
130                 // filter the added and removed orders to only contains
131                 // products that belong to one of the categories supplied as a parameter
132
133                 var self = this;
134                 function product_in_category(product_id){
135                     var cat = self.pos.db.get_product_by_id(product_id).pos_categ_id[0];
136                     while(cat){
137                         for(var i = 0; i < categories.length; i++){
138                             if(cat === categories[i]){
139                                 return true;
140                             }
141                         }
142                         cat = self.pos.db.get_category_parent_id(cat);
143                     }
144                     return false;
145                 }
146
147                 var _add = [];
148                 var _rem = [];
149                 
150                 for(var i = 0; i < add.length; i++){
151                     if(product_in_category(add[i].id)){
152                         _add.push(add[i]);
153                     }
154                 }
155                 add = _add;
156
157                 for(var i = 0; i < rem.length; i++){
158                     if(product_in_category(rem[i].id)){
159                         _rem.push(rem[i]);
160                     }
161                 }
162                 rem = _rem;
163             }
164
165             var d = new Date();
166             var hours   = '' + d.getHours();
167                 hours   = hours.length < 2 ? ('0' + hours) : hours;
168             var minutes = '' + d.getMinutes();
169                 minutes = minutes.length < 2 ? ('0' + minutes) : minutes;
170
171             return {
172                 'new': add,
173                 'cancelled': rem,
174                 'table': json.table || false,
175                 'floor': json.floor || false,
176                 'name': json.name  || 'unknown order',
177                 'time': {
178                     'hours':   hours,
179                     'minutes': minutes,
180                 },
181             };
182             
183         },
184         printChanges: function(){
185             var printers = this.pos.printers;
186             for(var i = 0; i < printers.length; i++){
187                 var changes = this.computeChanges(printers[i].config.product_categories_ids);
188                 if ( changes['new'].length > 0 || changes['cancelled'].length > 0){
189                     var receipt = QWeb.render('OrderChangeReceipt',{changes:changes, widget:this});
190                     printers[i].print(receipt);
191                 }
192             }
193         },
194         hasChangesToPrint: function(){
195             var printers = this.pos.printers;
196             for(var i = 0; i < printers.length; i++){
197                 var changes = this.computeChanges(printers[i].config.product_categories_ids);
198                 if ( changes['new'].length > 0 || changes['cancelled'].length > 0){
199                     return true;
200                 }
201             }
202             return false;
203         },
204         export_as_JSON: function(){
205             var json = _super_order.export_as_JSON.apply(this,arguments);
206             json.multiprint_resume = this.saved_resume;
207             return json;
208         },
209         init_from_JSON: function(json){
210             _super_order.init_from_JSON.apply(this,arguments);
211             this.saved_resume = json.multiprint_resume;
212         },
213     });
214
215     module.PosWidget.include({
216         build_widgets: function(){
217             var self = this;
218             this._super();
219
220             if(this.pos.printers.length){
221                 var submitorder = $(QWeb.render('SubmitOrderButton'));
222
223                 submitorder.click(function(){
224                     var order = self.pos.get('selectedOrder');
225                     if(order.hasChangesToPrint()){
226                         order.printChanges();
227                         order.saveChanges();
228                         self.pos_widget.order_widget.update_summary();
229                     }
230                 });
231                 
232                 submitorder.appendTo(this.$('.control-buttons'));
233                 this.$('.control-buttons').removeClass('oe_hidden');
234             }
235         },
236         
237     });
238
239     module.OrderWidget.include({
240         update_summary: function(){
241             this._super();
242             var order = this.pos.get('selectedOrder');
243
244             if(order.hasChangesToPrint()){
245                 this.pos_widget.$('.order-submit').addClass('highlight');
246             }else{
247                 this.pos_widget.$('.order-submit').removeClass('highlight');
248             }
249         },
250     });
251
252 }