2018-01-16 11:34:37 +01:00
flectra . define ( 'report.report' , function ( require ) {
2018-01-16 06:58:15 +01:00
'use strict' ;
var ActionManager = require ( 'web.ActionManager' ) ;
var core = require ( 'web.core' ) ;
var crash _manager = require ( 'web.crash_manager' ) ;
var framework = require ( 'web.framework' ) ;
var _t = core . _t ;
var _lt = core . _lt ;
var wkhtmltopdf _state ;
// Messages that will be shown to the user (if needed).
var WKHTMLTOPDF _MESSAGES = {
'install' : _lt ( 'Unable to find Wkhtmltopdf on this \nsystem. The report will be shown in html.<br><br><a href="http://wkhtmltopdf.org/" target="_blank">\nwkhtmltopdf.org</a>' ) ,
'workers' : _lt ( 'You need to start OpenERP with at least two \nworkers to print a pdf version of the reports.' ) ,
'upgrade' : _lt ( 'You should upgrade your version of\nWkhtmltopdf to at least 0.12.0 in order to get a correct display of headers and footers as well as\nsupport for table-breaking between pages.<br><br><a href="http://wkhtmltopdf.org/" \ntarget="_blank">wkhtmltopdf.org</a>' ) ,
'broken' : _lt ( 'Your installation of Wkhtmltopdf seems to be broken. The report will be shown in html.<br><br><a href="http://wkhtmltopdf.org/" target="_blank">wkhtmltopdf.org</a>' )
} ;
var trigger _download = function ( session , response , c , action , options ) {
return session . get _file ( {
url : '/report/download' ,
data : { data : JSON . stringify ( response ) } ,
complete : framework . unblockUI ,
error : c . rpc _error . bind ( c ) ,
success : function ( ) {
if ( action && options && ! action . dialog ) {
options . on _close ( ) ;
}
} ,
} ) ;
} ;
/ * *
* This helper will generate an object containing the report ' s url ( as value )
* for every qweb - type we support ( as key ) . It ' s convenient because we may want
* to use another report ' s type at some point ( for example , when ` qweb-pdf ` is
* not available ) .
* /
var make _report _url = function ( action ) {
var report _urls = {
'qweb-html' : '/report/html/' + action . report _name ,
'qweb-pdf' : '/report/pdf/' + action . report _name ,
} ;
// We may have to build a query string with `action.data`. It's the place
// were report's using a wizard to customize the output traditionally put
// their options.
if ( _ . isUndefined ( action . data ) || _ . isNull ( action . data ) || ( _ . isObject ( action . data ) && _ . isEmpty ( action . data ) ) ) {
if ( action . context . active _ids ) {
var active _ids _path = '/' + action . context . active _ids . join ( ',' ) ;
// Update the report's type - report's url mapping.
report _urls = _ . mapObject ( report _urls , function ( value , key ) {
return value += active _ids _path ;
} ) ;
}
} else {
var serialized _options _path = '?options=' + encodeURIComponent ( JSON . stringify ( action . data ) ) ;
serialized _options _path += '&context=' + encodeURIComponent ( JSON . stringify ( action . context ) ) ;
// Update the report's type - report's url mapping.
report _urls = _ . mapObject ( report _urls , function ( value , key ) {
return value += serialized _options _path ;
} ) ;
}
return report _urls ;
} ;
ActionManager . include ( {
ir _actions _report : function ( action , options ) {
var self = this ;
action = _ . clone ( action ) ;
var report _urls = make _report _url ( action ) ;
if ( action . report _type === 'qweb-html' ) {
var client _action _options = _ . extend ( { } , options , {
report _url : report _urls [ 'qweb-html' ] ,
report _name : action . report _name ,
report _file : action . report _file ,
data : action . data ,
context : action . context ,
name : action . name ,
display _name : action . display _name ,
} ) ;
return this . do _action ( 'report.client_action' , client _action _options ) ;
} else if ( action . report _type === 'qweb-pdf' ) {
framework . blockUI ( ) ;
// Before doing anything, we check the state of wkhtmltopdf on the server.
( wkhtmltopdf _state = wkhtmltopdf _state || this . _rpc ( { route : '/report/check_wkhtmltopdf' } ) ) . then ( function ( state ) {
// Display a notification to the user according to wkhtmltopdf's state.
if ( WKHTMLTOPDF _MESSAGES [ state ] ) {
self . do _notify ( _t ( 'Report' ) , WKHTMLTOPDF _MESSAGES [ state ] , true ) ;
}
if ( state === 'upgrade' || state === 'ok' ) {
// Trigger the download of the PDF report.
var response ;
var c = crash _manager ;
var treated _actions = [ ] ;
var current _action = action ;
do {
report _urls = make _report _url ( current _action ) ;
response = [
report _urls [ 'qweb-pdf' ] ,
action . report _type , //The 'root' report is considered the maine one, so we use its type for all the others.
] ;
var success = trigger _download ( self . getSession ( ) , response , c , current _action , options ) ;
if ( ! success ) {
self . do _warn ( _t ( 'Warning' ) , _t ( 'A popup window with your report was blocked. You may need to change your browser settings to allow popup windows for this page.' ) , true ) ;
}
treated _actions . push ( current _action ) ;
current _action = current _action . next _report _to _generate ;
} while ( current _action && ! _ . contains ( treated _actions , current _action ) ) ;
//Second part of the condition for security reasons (avoids infinite loop possibilty).
return ;
} else {
// Open the report in the client action if generating the PDF is not possible.
var client _action _options = _ . extend ( { } , options , {
report _url : report _urls [ 'qweb-html' ] ,
report _name : action . report _name ,
report _file : action . report _file ,
data : action . data ,
context : action . context ,
name : action . name ,
display _name : action . display _name ,
} ) ;
framework . unblockUI ( ) ;
return self . do _action ( 'report.client_action' , client _action _options ) ;
}
} ) ;
} else {
2018-04-05 10:25:40 +02:00
self . do _warn ( _t ( 'Error' ) , _t ( 'Non qweb reports are not anymore supported.' ) , true ) ;
return ;
2018-01-16 06:58:15 +01:00
}
}
} ) ;
} ) ;