1 function openerp_restaurant_multiprint(instance,module){
2 var QWeb = instance.web.qweb;
3 var _t = instance.web._t;
5 module.Printer = instance.web.Class.extend(openerp.PropertiesMixin,{
6 init: function(parent,options){
7 openerp.PropertiesMixin.init.call(this,parent);
9 options = options || {};
10 var url = options.url || 'http://localhost:8069';
11 this.connection = new instance.web.Session(undefined,url, { use_cors: true});
13 this.receipt_queue = [];
15 print: function(receipt){
18 this.receipt_queue.push(receipt);
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})
28 self.receipt_queue.unshift(r);
36 module.PosModel.prototype.models.push({
37 model: 'restaurant.printer',
38 fields: ['name','proxy_ip','product_categories_ids'],
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;
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);
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();
62 return '' + this.get_product().id;
67 var _super_order = module.Order.prototype;
68 module.Order = module.Order.extend({
69 lineResume: function(){
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;
78 if (typeof resume[line_hash] === 'undefined') {
79 resume[line_hash] = { qty: qty, note: note, product_id: product_id };
81 resume[line_hash].qty += qty;
86 saveChanges: function(){
87 this.saved_resume = this.build_line_resume();
88 this.trigger('change',this);
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();
97 for( product in current){
98 if (typeof old[product] === 'undefined'){
101 'name': this.pos.db.get_product_by_id(product).display_name,
102 'quantity': current[product],
104 }else if( old[product] < current[product]){
107 'name': this.pos.db.get_product_by_id(product).display_name,
108 'quantity': current[product] - old[product],
110 }else if( old[product] > current[product]){
113 'name': this.pos.db.get_product_by_id(product).display_name,
114 'quantity': old[product] - current[product],
119 for( product in old){
120 if(typeof current[product] === 'undefined'){
123 'name': this.pos.db.get_product_by_id(product).display_name,
124 'quantity': old[product],
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
134 function product_in_category(product_id){
135 var cat = self.pos.db.get_product_by_id(product_id).pos_categ_id[0];
137 for(var i = 0; i < categories.length; i++){
138 if(cat === categories[i]){
142 cat = self.pos.db.get_category_parent_id(cat);
150 for(var i = 0; i < add.length; i++){
151 if(product_in_category(add[i].id)){
157 for(var i = 0; i < rem.length; i++){
158 if(product_in_category(rem[i].id)){
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;
174 'table': json.table || false,
175 'floor': json.floor || false,
176 'name': json.name || 'unknown order',
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);
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){
204 export_as_JSON: function(){
205 var json = _super_order.export_as_JSON.apply(this,arguments);
206 json.multiprint_resume = this.saved_resume;
209 init_from_JSON: function(json){
210 _super_order.init_from_JSON.apply(this,arguments);
211 this.saved_resume = json.multiprint_resume;
215 module.PosWidget.include({
216 build_widgets: function(){
220 if(this.pos.printers.length){
221 var submitorder = $(QWeb.render('SubmitOrderButton'));
223 submitorder.click(function(){
224 var order = self.pos.get('selectedOrder');
225 if(order.hasChangesToPrint()){
226 order.printChanges();
228 self.pos_widget.order_widget.update_summary();
232 submitorder.appendTo(this.$('.control-buttons'));
233 this.$('.control-buttons').removeClass('oe_hidden');
239 module.OrderWidget.include({
240 update_summary: function(){
242 var order = this.pos.get('selectedOrder');
244 if(order.hasChangesToPrint()){
245 this.pos_widget.$('.order-submit').addClass('highlight');
247 this.pos_widget.$('.order-submit').removeClass('highlight');