diff --git a/base_report_to_printer/__openerp__.py b/base_report_to_printer/__openerp__.py index 511c179086e..8d95719c5a0 100644 --- a/base_report_to_printer/__openerp__.py +++ b/base_report_to_printer/__openerp__.py @@ -35,6 +35,7 @@ 'security/security.xml', 'printing_data.xml', 'printing_view.xml', + 'base_report_to_printer.xml', 'wizard/update_printers.xml', ], 'installable': True, diff --git a/base_report_to_printer/base_report_to_printer.xml b/base_report_to_printer/base_report_to_printer.xml new file mode 100644 index 00000000000..c1c0bbc227e --- /dev/null +++ b/base_report_to_printer/base_report_to_printer.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/base_report_to_printer/ir_report.py b/base_report_to_printer/ir_report.py index 4b610547d5e..72382774768 100644 --- a/base_report_to_printer/ir_report.py +++ b/base_report_to_printer/ir_report.py @@ -52,6 +52,23 @@ class ReportXml(models.Model): 'user basis' ) + @api.model + def print_action_for_report_name(self, report_name): + """ Returns if the action is a direct print or pdf + + Called from js + """ + report_obj = self.env['report'] + report = report_obj._get_report_from_name(report_name) + if not report: + return {} + result = report.behaviour()[report.id] + serializable_result = { + 'action': result['action'], + 'printer_name': result['printer'].name, + } + return serializable_result + @api.multi def behaviour(self): result = {} diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py index 5749e589beb..d909922e16f 100644 --- a/base_report_to_printer/report.py +++ b/base_report_to_printer/report.py @@ -25,15 +25,30 @@ class Report(models.Model): _inherit = 'report' + def print_document(self, cr, uid, ids, report_name, html=None, + data=None, context=None): + """ Print a document, do not return the document file """ + document = super(Report, self).get_pdf(cr, uid, ids, report_name, + html=html, data=data, + context=context) + report = self._get_report_from_name(cr, uid, report_name) + behaviour = report.behaviour()[report.id] + printer = behaviour['printer'] + return printer.print_document(report, document, report.report_type) + def get_pdf(self, cr, uid, ids, report_name, html=None, data=None, context=None): - result = super(Report, self).get_pdf(cr, uid, ids, report_name, - html=html, data=data, - context=context) + """ Generate a PDF and returns it. + + If the action configured on the report is server, it prints the + generated document as well. + """ + document = super(Report, self).get_pdf(cr, uid, ids, report_name, + html=html, data=data, + context=context) report = self._get_report_from_name(cr, uid, report_name) - data = report.behaviour()[report.id] - action = data['action'] - printer = data['printer'] - if action != 'client' and result: - printer.print_document(report, result, report.report_type) - return result + behaviour = report.behaviour()[report.id] + if behaviour['action'] == 'server' and document: + printer = behaviour['printer'] + printer.print_document(report, document, report.report_type) + return document diff --git a/base_report_to_printer/static/src/js/qwebactionmanager.js b/base_report_to_printer/static/src/js/qwebactionmanager.js new file mode 100644 index 00000000000..987d252c165 --- /dev/null +++ b/base_report_to_printer/static/src/js/qwebactionmanager.js @@ -0,0 +1,43 @@ +openerp.base_report_to_printer = function(instance) { + + instance.web.ActionManager.include({ + ir_actions_report_xml: function(action, options) { + instance.web.blockUI(); + action = _.clone(action); + var _t = instance.web._t; + var self = this; + var _super = this._super; + + if ('report_type' in action && action.report_type === 'qweb-pdf') { + new instance.web.Model('ir.actions.report.xml') + .call('print_action_for_report_name', [action.report_name]) + .then(function(print_action){ + if (print_action && print_action['action'] === 'server') { + instance.web.unblockUI(); + new instance.web.Model('report') + .call('print_document', + [action.context.active_ids, + action.report_name, + ], + {data: action.data || {}, + context: action.context || {}, + }) + .then(function(result){ + self.do_notify(_t('Report'), + _t('Document sent to the printer ') + print_action.printer_name); + }).fail(function() { + self.do_notify(_t('Report'), + _t('Error when sending the document to the printer ') + print_action.printer_name); + + }); + } else { + return _super.apply(self, [action, options]); + } + }); + } else { + return _super.apply(self, [action, options]); + } + } + }); +}; +