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.Order = module.Order.extend({
58 lineResume: function(){
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;
65 resume[line.product_id] += line.qty;
70 saveChanges: function(){
71 this.old_resume = this.lineResume();
73 computeChanges: function(categories){
74 var current = this.lineResume();
75 var old = this.old_resume || {};
76 var json = this.export_as_JSON();
80 for( product in current){
81 if (typeof old[product] === 'undefined'){
84 'name': this.pos.db.get_product_by_id(product).display_name,
85 'quantity': current[product],
87 }else if( old[product] < current[product]){
90 'name': this.pos.db.get_product_by_id(product).display_name,
91 'quantity': current[product] - old[product],
93 }else if( old[product] > current[product]){
96 'name': this.pos.db.get_product_by_id(product).display_name,
97 'quantity': old[product] - current[product],
102 for( product in old){
103 if(typeof current[product] === 'undefined'){
106 'name': this.pos.db.get_product_by_id(product).display_name,
107 'quantity': old[product],
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
117 function product_in_category(product_id){
118 var cat = self.pos.db.get_product_by_id(product_id).pos_categ_id[0];
120 for(var i = 0; i < categories.length; i++){
121 if(cat === categories[i]){
125 cat = self.pos.db.get_category_parent_id(cat);
133 for(var i = 0; i < add.length; i++){
134 if(product_in_category(add[i].id)){
140 for(var i = 0; i < rem.length; i++){
141 if(product_in_category(rem[i].id)){
151 'table': json.table || 'unknown table',
152 'name': json.name || 'unknown order',
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);
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){
178 module.PosWidget.include({
179 build_widgets: function(){
183 if(this.pos.printers.length){
184 var submitorder = $(QWeb.render('SubmitOrderButton'));
186 submitorder.click(function(){
187 var order = self.pos.get('selectedOrder');
188 if(order.hasChangesToPrint()){
189 order.printChanges();
191 self.pos_widget.order_widget.update_summary();
195 submitorder.appendTo(this.$('.control-buttons'));
196 this.$('.control-buttons').removeClass('oe_hidden');
202 module.OrderWidget.include({
203 update_summary: function(){
205 var order = this.pos.get('selectedOrder');
207 if(order.hasChangesToPrint()){
208 this.pos_widget.$('.order-submit').addClass('highlight');
210 this.pos_widget.$('.order-submit').removeClass('highlight');