|
@@ -1,399 +1,95 @@
|
|
|
-
|
|
|
-(function () {
|
|
|
- var instance = openerp
|
|
|
- openerp.print_engine = {}
|
|
|
-
|
|
|
- String.prototype.add = function (string) {
|
|
|
- return this.concat(string);
|
|
|
- }
|
|
|
-
|
|
|
- String.prototype.center = function (width, padding) {
|
|
|
- padding = padding || '';
|
|
|
- padding = padding.substr(0, 1);
|
|
|
-
|
|
|
- if (this.length < width) {
|
|
|
- var length = width - this.length;
|
|
|
- var remain = (length % 2 == 0) ? '' : padding;
|
|
|
- var pads = padding.repeat(parseInt(length / 2));
|
|
|
-
|
|
|
- return pads + this + pads + remain;
|
|
|
- } else {
|
|
|
- return this;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String.prototype.lineFeed = function () {
|
|
|
- return this + '\n';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.SocketManager = instance.web.Class.extend({
|
|
|
+openerp.print_engine = function (instance, local) {
|
|
|
+ local.SocketManager = instance.web.Class.extend({
|
|
|
init: function () {
|
|
|
this.socket = null;
|
|
|
- this.state = 'offline';
|
|
|
- this.attemps = 0;
|
|
|
-
|
|
|
this.start();
|
|
|
},
|
|
|
start: function () {
|
|
|
- var self = this;
|
|
|
- this.getSockets().then(function () {
|
|
|
- self.connect();
|
|
|
- });
|
|
|
+ this.open_socket();
|
|
|
},
|
|
|
- getSockets: function () {
|
|
|
- var defer = $.Deferred();
|
|
|
+ open_socket: function () {
|
|
|
var self = this;
|
|
|
|
|
|
- $.get('/print_engine/sockets').done(function (data) {
|
|
|
- self.sockets = data.sockets;
|
|
|
- defer.resolve(data.sockets);
|
|
|
- }).fail(function (error){
|
|
|
- defer.reject(error);
|
|
|
- });
|
|
|
-
|
|
|
- return defer;
|
|
|
- },
|
|
|
- urlize: function (info) {
|
|
|
- return info.protocol + '://' + info.host + ':' + info.port + info.path;
|
|
|
- },
|
|
|
- connect: function () {
|
|
|
- if (this.sockets.length === 0) {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- var self = this;
|
|
|
- var url = this.urlize(this.sockets[0]);
|
|
|
-
|
|
|
- if (this.socket) {
|
|
|
- this.socket = null;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- this.socket = new WebSocket(url);
|
|
|
-
|
|
|
- this.socket.onopen = function (e) {
|
|
|
- self.handleOpen(e);
|
|
|
- };
|
|
|
- this.socket.onclose = function (e) {
|
|
|
- self.handleClose(e);
|
|
|
- };
|
|
|
- this.socket.onerror = function (e) {
|
|
|
- self.handleError(e);
|
|
|
- };
|
|
|
- this.socket.onmessage = function (e) {
|
|
|
- self.handleMessage(e);
|
|
|
- };
|
|
|
- } catch(e) {
|
|
|
- // Ignore this error
|
|
|
- }
|
|
|
- },
|
|
|
- reconnect: function () {
|
|
|
- this.state = 'reconnecting';
|
|
|
-
|
|
|
- $('#printer-status').removeClass();
|
|
|
- $('#printer-status').addClass('printer-status-reconnect');
|
|
|
-
|
|
|
- var self = this;
|
|
|
-
|
|
|
- if (this.attemps === 3) {
|
|
|
- this.state = 'offline';
|
|
|
-
|
|
|
- $('#printer-status').removeClass();
|
|
|
- $('#printer-status').addClass('printer-status-offline');
|
|
|
-
|
|
|
- return;
|
|
|
+ var set_socket = function (socket) {
|
|
|
+ var protocol = window.location.protocol;
|
|
|
+ self.socket = io(protocol + '//' + socket.host + ':' + socket.port, {
|
|
|
+ path: socket.path
|
|
|
+ });
|
|
|
+ self.socket.on('connect', self.handle_connect);
|
|
|
+ self.socket.on('connect_error', self.handle_connect_error);
|
|
|
+ self.socket.on('show-printers', self.handle_printers);
|
|
|
}
|
|
|
|
|
|
- setTimeout(function () {
|
|
|
- self.connect();
|
|
|
-
|
|
|
- self.attemps = self.attemps + 1;
|
|
|
- }, 3000);
|
|
|
+ this.get_socket_config().then(set_socket);
|
|
|
},
|
|
|
- handleOpen: function (e) {
|
|
|
- this.state = 'online';
|
|
|
-
|
|
|
+ handle_connect: function () {
|
|
|
$('#printer-status').removeClass();
|
|
|
$('#printer-status').addClass('printer-status-online');
|
|
|
},
|
|
|
- handleClose: function (e) {
|
|
|
- this.reconnect();
|
|
|
- },
|
|
|
- handleMessage: function (e) {
|
|
|
- var obj = JSON.parse(e.data);
|
|
|
-
|
|
|
- if (obj.printers) {
|
|
|
- this.sendToServer('/print_engine/update', e.data).then(function () {
|
|
|
- instance.client.action_manager.inner_widget.views['form'].controller.reload();
|
|
|
- instance.web.notification.do_notify('Información', 'Impresoras actualizadas con éxito');
|
|
|
+ handle_connect_error: function () {
|
|
|
+ $('#printer-status').removeClass();
|
|
|
+ $('#printer-status').addClass('printer-status-offline');
|
|
|
+ },
|
|
|
+ handle_printers: function (printers) {
|
|
|
+ console.log(printers);
|
|
|
+ },
|
|
|
+ get_socket_config: function () {
|
|
|
+ var url = '/print_engine/socket_config';
|
|
|
+ return this.get_from_server(url);
|
|
|
+ },
|
|
|
+ get_from_server: function (url) {
|
|
|
+ instance.web.blockUI();
|
|
|
+ return $.Deferred(function (d) {
|
|
|
+ instance.web.jsonRpc(url, 'call').done(function () {
|
|
|
+ instance.web.unblockUI();
|
|
|
+ d.resolve.apply(d, arguments);
|
|
|
+ }).fail(function (e) {
|
|
|
+ instance.web.unblockUI();
|
|
|
+ d.reject.apply(d, arguments);
|
|
|
});
|
|
|
- }
|
|
|
- },
|
|
|
- handleError: function (e) {
|
|
|
- // Ignore this error
|
|
|
- },
|
|
|
- sendToSocket: function (data) {
|
|
|
- if (this.state !== 'online') {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!data) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- this.socket.send(JSON.stringify(data));
|
|
|
- },
|
|
|
- sendToServer: function (url, data) {
|
|
|
- var defer = $.Deferred();
|
|
|
-
|
|
|
- var json = {
|
|
|
- jsonrpc: '2.0',
|
|
|
- method: 'call',
|
|
|
- params: {
|
|
|
- data: data
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $.ajax({
|
|
|
- type: 'POST',
|
|
|
- url: url,
|
|
|
- dataType: 'json',
|
|
|
- data: JSON.stringify(json),
|
|
|
- beforeSend: function(xhr) {
|
|
|
- xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
- },
|
|
|
- success: function (ok) {
|
|
|
- defer.resolve(ok);
|
|
|
- },
|
|
|
});
|
|
|
-
|
|
|
- return defer;
|
|
|
- },
|
|
|
- discoveryPrinters: function () {
|
|
|
- var commands = {
|
|
|
- action: 'list'
|
|
|
- }
|
|
|
-
|
|
|
- this.sendToSocket(commands);
|
|
|
- },
|
|
|
- print(printer, data) {
|
|
|
- if (this.state !== 'online') {
|
|
|
- instance.web.notification.do_warn('Atención', 'La impresora está fuera de servicio');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!printer) {
|
|
|
- instance.web.notification.do_warn('Atención', 'La impresora no se encuentra');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!data) {
|
|
|
- instance.web.notification.do_warn('Atención', 'No se encuentran datos para imprimir');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- var commands = {
|
|
|
- action: 'print',
|
|
|
- printer: printer,
|
|
|
- data: data
|
|
|
- }
|
|
|
-
|
|
|
- this.sendToSocket(commands)
|
|
|
- },
|
|
|
- printTicket(company, city, street, state, phone, website, datetime, symbol, items, total, received, customer, ruc, cashier, ref) {
|
|
|
- if (this.sockets.length == 0) {
|
|
|
- instance.web.notification.do_warn('Atención', 'No se encuentran sockets definidos');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (this.sockets[0].printers.length == 0) {
|
|
|
- instance.web.notification.do_warn('Atención', 'No se encuentran impresoras definidas');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- var printer = null;
|
|
|
- for (var j = 0; j < this.sockets[0].printers.length; j++) {
|
|
|
- if (this.sockets[0].printers[j].isDefault) {
|
|
|
- printer = this.sockets[0].printers[j];
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!printer) {
|
|
|
- instance.web.notification.do_warn('Atención', 'No hay ninguna impresora por defecto');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- company = company || 'SIN NOMBRE';
|
|
|
- var buffer = '';
|
|
|
- buffer = buffer.add(company.center(40, ' ').toUpperCase()).lineFeed();
|
|
|
-
|
|
|
- if (city) {
|
|
|
- buffer = buffer.add(city.center(40, ' ')).lineFeed();
|
|
|
- }
|
|
|
-
|
|
|
- if (street) {
|
|
|
- buffer = buffer.add(street.center(40, ' ')).lineFeed();
|
|
|
- }
|
|
|
-
|
|
|
- if (state) {
|
|
|
- buffer = buffer.add(state.center(40, ' ')).lineFeed();
|
|
|
- }
|
|
|
-
|
|
|
- if (phone) {
|
|
|
- buffer = buffer.add(phone.center(40, ' ')).lineFeed();
|
|
|
- }
|
|
|
-
|
|
|
- if (website) {
|
|
|
- buffer = buffer.add(website.center(40, ' ')).lineFeed()
|
|
|
- }
|
|
|
-
|
|
|
- buffer = buffer.lineFeed().lineFeed();
|
|
|
- buffer = buffer.add('TICKET DE VENTA'.center(40, ' '));
|
|
|
- buffer = buffer.lineFeed();
|
|
|
-
|
|
|
- if (datetime) {
|
|
|
- buffer = buffer.add(('Fecha: ' + datetime).center(40, ' ')).lineFeed().lineFeed();
|
|
|
- }
|
|
|
-
|
|
|
- symbol = symbol || '$';
|
|
|
- ruc = ruc || '';
|
|
|
- cashier = cashier || '';
|
|
|
- ref = ref || '';
|
|
|
-
|
|
|
- buffer = buffer.add('-'.center(40, '-')).lineFeed();
|
|
|
- buffer = buffer.add('DESCRIPCION').lineFeed();
|
|
|
- buffer = buffer.add('PRECIO CANTIDAD SUBTOTAL').lineFeed();
|
|
|
- buffer = buffer.add('-'.center(40, '-')).lineFeed();
|
|
|
-
|
|
|
- if (!!items && items.length > 0) {
|
|
|
- for (var i = 0; i < items.length; i++) {
|
|
|
- buffer = buffer.add(items[i][0].toUpperCase()).lineFeed();
|
|
|
- buffer = buffer.add(items[i][1] + '\t' + items[i][2] + '\t' + items[i][3]).lineFeed();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- buffer = buffer.add('-'.center(40, '-')).lineFeed();
|
|
|
-
|
|
|
- buffer = buffer.add('TOTAL (' + symbol + '): ' + total).lineFeed();
|
|
|
- buffer = buffer.add('RECIBIDO (' + symbol + '): ' + received).lineFeed();
|
|
|
- buffer = buffer.add('VUELTO (' + symbol + '): ' + (received - total)).lineFeed();
|
|
|
-
|
|
|
- buffer = buffer.add('-'.center(40, '-')).lineFeed();
|
|
|
-
|
|
|
- buffer = buffer.add('CLIENTE: ' + customer).lineFeed();
|
|
|
- buffer = buffer.add('RUC : ' + ruc).lineFeed().lineFeed();
|
|
|
-
|
|
|
- buffer = buffer.add('-'.center(40, '-')).lineFeed();
|
|
|
- buffer = buffer.add('ATENDIDO POR: ' + cashier).lineFeed();
|
|
|
- buffer = buffer.add('REFERENCIA : ' + ref).lineFeed().lineFeed();
|
|
|
-
|
|
|
- buffer = buffer.add('GRACIAS POR SU PREFERENCIA!!!'.center(40, ' ')).lineFeed().lineFeed()
|
|
|
-
|
|
|
- buffer = buffer.add('Diseñado por Eiru Software'.center(40, ' ')).lineFeed();
|
|
|
- buffer = buffer.add('www.eiru.com.py'.center(40, ' ')).lineFeed().lineFeed().lineFeed().lineFeed().lineFeed().lineFeed()
|
|
|
-
|
|
|
- this.print(printer.name, buffer);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.PrinterTopWidget = instance.web.Widget.extend({
|
|
|
+ local.PrinterTopWidget = instance.web.Widget.extend({
|
|
|
template: 'printEngine.PrinterTopWidget',
|
|
|
init: function (parent) {
|
|
|
this._super(parent)
|
|
|
},
|
|
|
start: function () {
|
|
|
- this.$el.click(this, this.selectDefaultPrinter);
|
|
|
- },
|
|
|
- selectDefaultPrinter: function (e) {
|
|
|
- e.preventDefault();
|
|
|
- var self = e.data;
|
|
|
-
|
|
|
- instance.client.action_manager.do_action({
|
|
|
- context: self.session.user_context,
|
|
|
- name: 'Impresora por defecto',
|
|
|
- type: 'ir.actions.act_window',
|
|
|
- res_model: 'print.engine.printer',
|
|
|
- views: [[false, 'list'], [false, 'form']],
|
|
|
- domain : [['socket_id.user_id', '=', self.session.user_context.uid]],
|
|
|
- target: 'new',
|
|
|
- flags: {
|
|
|
- action_buttons: true
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.ping = function () {
|
|
|
- if (!instance.print_engine.socket_manager) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- instance.web.notification.do_notify('Información', 'Pong')
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.discovery_printers = function (element, action) {
|
|
|
- if (!instance.print_engine.socket_manager) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if(!confirm('Ésta acción quitará la impresora por defecto')) {
|
|
|
- return;
|
|
|
+ // this.$el.click(this, this.selectDefaultPrinter);
|
|
|
}
|
|
|
+ });
|
|
|
|
|
|
- instance.print_engine.socket_manager.discoveryPrinters();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.test_printer = function (element, action) {
|
|
|
- if (!instance.print_engine.socket_manager) {
|
|
|
- return;
|
|
|
+ if (instance.web) {
|
|
|
+ if (instance.web.UserMenu) {
|
|
|
+ instance.web.UserMenu.include({
|
|
|
+ do_update: function(){
|
|
|
+ var printer = new local.PrinterTopWidget(this);
|
|
|
+ printer.appendTo($('.oe_systray'));
|
|
|
+ local.socket = new local.SocketManager()
|
|
|
+ return this._super.apply(this, arguments);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- instance.print_engine.socket_manager.test();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- instance.print_engine.print = function (element, action) {
|
|
|
- if (!instance.print_engine.socket_manager) {
|
|
|
- return;
|
|
|
+ if (instance.web.ActionManager) {
|
|
|
+ instance.web.ActionManager.include({
|
|
|
+ ir_actions_report_xml: function (action, options) {
|
|
|
+ var url = '/print_engine/get_pdf';
|
|
|
+
|
|
|
+ instance.web.jsonRpc(url, 'call', {
|
|
|
+ model: action.model,
|
|
|
+ report_name: action.report_name,
|
|
|
+ ids: action.id,
|
|
|
+ context: action.context
|
|
|
+ }).done(function (data) {
|
|
|
+ console.log(data);
|
|
|
+ }).fail(function (e) {
|
|
|
+ console.log(e);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
- instance.print_engine.socket_manager.print();
|
|
|
- }
|
|
|
-
|
|
|
- instance.web.client_actions.add('print_engine.ping', 'instance.print_engine.ping')
|
|
|
- instance.web.client_actions.add('print_engine.discovery_printers', 'instance.print_engine.discovery_printers')
|
|
|
- instance.web.client_actions.add('print_engine.test_printer', 'instance.print_engine.test_printer')
|
|
|
-
|
|
|
-
|
|
|
- if (instance.web && instance.web.UserMenu) {
|
|
|
- instance.web.UserMenu.include({
|
|
|
- do_update: function(){
|
|
|
- var printer = new openerp.print_engine.PrinterTopWidget(this);
|
|
|
- printer.appendTo($('.oe_systray'));
|
|
|
-
|
|
|
- instance.print_engine.socket_manager = new instance.print_engine.SocketManager()
|
|
|
-
|
|
|
- return this._super.apply(this, arguments);
|
|
|
- }
|
|
|
- });
|
|
|
}
|
|
|
-})()
|
|
|
+}
|