Skip to content

Commit

Permalink
Do no longer returns a PDF when a report is printed
Browse files Browse the repository at this point in the history
Instead, a notification is displayed to the user.
When report.get_pdf() is called on a report that must be printer,
it will print the report *and* returns the pdf, thus code that
calls directly report.get_pdf() will print the pdf on the printer
as expected.

Fixes OCA#16
  • Loading branch information
guewen authored and gdgellatly committed May 12, 2019
1 parent 9f7329c commit 477ba60
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
1 change: 1 addition & 0 deletions base_report_to_printer/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'security/security.xml',
'printing_data.xml',
'printing_view.xml',
'base_report_to_printer.xml',
'wizard/update_printers.xml',
],
'installable': True,
Expand Down
11 changes: 11 additions & 0 deletions base_report_to_printer/base_report_to_printer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="assets_backend" name="base_report_to_printer assets" inherit_id="report.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/base_report_to_printer/static/src/js/qwebactionmanager.js"></script>
</xpath>
</template>
</data>
</openerp>

17 changes: 17 additions & 0 deletions base_report_to_printer/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
33 changes: 24 additions & 9 deletions base_report_to_printer/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions base_report_to_printer/static/src/js/qwebactionmanager.js
Original file line number Diff line number Diff line change
@@ -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]);
}
}
});
};

0 comments on commit 477ba60

Please sign in to comment.