[IMP] Reportings Review
[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.Order = module.Order.extend({
58         lineResume: function(){
59             var resume = {};
60             this.get('orderLines').each(function(item){
61                 var line = item.export_as_JSON();
62                 if( typeof resume[line.product_id] === 'undefined'){
63                     resume[line.product_id] = line.qty;
64                 }else{
65                     resume[line.product_id] += line.qty;
66                 }
67             });
68             return resume;
69         },
70         saveChanges: function(){
71             this.old_resume = this.lineResume();
72         },
73         computeChanges: function(categories){
74             var current = this.lineResume();
75             var old     = this.old_resume || {};
76             var json    = this.export_as_JSON();
77             var add = [];
78             var rem = [];
79
80             for( product in current){
81                 if (typeof old[product] === 'undefined'){
82                     add.push({
83                         'id': product,
84                         'name': this.pos.db.get_product_by_id(product).display_name,
85                         'quantity': current[product],
86                     });
87                 }else if( old[product] < current[product]){
88                     add.push({
89                         'id': product,
90                         'name': this.pos.db.get_product_by_id(product).display_name,
91                         'quantity': current[product] - old[product],
92                     });
93                 }else if( old[product] > current[product]){
94                     rem.push({
95                         'id': product,
96                         'name': this.pos.db.get_product_by_id(product).display_name,
97                         'quantity': old[product] - current[product],
98                     });
99                 }
100             }
101
102             for( product in old){
103                 if(typeof current[product] === 'undefined'){
104                     rem.push({
105                         'id': product,
106                         'name': this.pos.db.get_product_by_id(product).display_name,
107                         'quantity': old[product], 
108                     });
109                 }
110             }
111
112             if(categories && categories.length > 0){
113                 // filter the added and removed orders to only contains
114                 // products that belong to one of the categories supplied as a parameter
115
116                 var self = this;
117                 function product_in_category(product_id){
118                     var cat = self.pos.db.get_product_by_id(product_id).pos_categ_id[0];
119                     while(cat){
120                         for(var i = 0; i < categories.length; i++){
121                             if(cat === categories[i]){
122                                 return true;
123                             }
124                         }
125                         cat = self.pos.db.get_category_parent_id(cat);
126                     }
127                     return false;
128                 }
129
130                 var _add = [];
131                 var _rem = [];
132                 
133                 for(var i = 0; i < add.length; i++){
134                     if(product_in_category(add[i].id)){
135                         _add.push(add[i]);
136                     }
137                 }
138                 add = _add;
139
140                 for(var i = 0; i < rem.length; i++){
141                     if(product_in_category(rem[i].id)){
142                         _rem.push(rem[i]);
143                     }
144                 }
145                 rem = _rem;
146             }
147
148             return {
149                 'new': add,
150                 'cancelled': rem,
151                 'table': json.table || 'unknown table',
152                 'name': json.name  || 'unknown order',
153             };
154             
155         },
156         printChanges: function(){
157             var printers = this.pos.printers;
158             for(var i = 0; i < printers.length; i++){
159                 var changes = this.computeChanges(printers[i].config.product_categories_ids);
160                 if ( changes['new'].length > 0 || changes['cancelled'].length > 0){
161                     var receipt = QWeb.render('OrderChangeReceipt',{changes:changes, widget:this});
162                     printers[i].print(receipt);
163                 }
164             }
165         },
166         hasChangesToPrint: function(){
167             var printers = this.pos.printers;
168             for(var i = 0; i < printers.length; i++){
169                 var changes = this.computeChanges(printers[i].config.product_categories_ids);
170                 if ( changes['new'].length > 0 || changes['cancelled'].length > 0){
171                     return true;
172                 }
173             }
174             return false;
175         },
176     });
177
178     module.PosWidget.include({
179         build_widgets: function(){
180             var self = this;
181             this._super();
182
183             if(this.pos.printers.length){
184                 var submitorder = $(QWeb.render('SubmitOrderButton'));
185
186                 submitorder.click(function(){
187                     var order = self.pos.get('selectedOrder');
188                     if(order.hasChangesToPrint()){
189                         order.printChanges();
190                         order.saveChanges();
191                         self.pos_widget.order_widget.update_summary();
192                     }
193                 });
194                 
195                 submitorder.appendTo(this.$('.control-buttons'));
196                 this.$('.control-buttons').removeClass('oe_hidden');
197             }
198         },
199         
200     });
201
202     module.OrderWidget.include({
203         update_summary: function(){
204             this._super();
205             var order = this.pos.get('selectedOrder');
206
207             if(order.hasChangesToPrint()){
208                 this.pos_widget.$('.order-submit').addClass('highlight');
209             }else{
210                 this.pos_widget.$('.order-submit').removeClass('highlight');
211             }
212         },
213     });
214
215 }