[MERGE] forward port of branch 8.0 up to 491372e
[odoo/odoo.git] / addons / report / static / src / js / qwebactionmanager.js
1 openerp.report = function(instance) {
2     var wkhtmltopdf_state;
3
4     var trigger_download = function(session, response, c) {
5         session.get_file({
6             url: '/report/download',
7             data: {data: JSON.stringify(response)},
8             complete: openerp.web.unblockUI,
9             error: c.rpc_error.bind(c)
10         });
11     }
12     var show_pdf = function(session, response, c, options, self) {
13         response.push("pdf_viewer")
14         session.show_pdf({
15             url: '/report/download',
16             data: {data: JSON.stringify(response)},
17             complete: function(){
18                 openerp.web.unblockUI();
19                 if (!self.dialog) {
20                     options.on_close();
21                 }
22                 self.dialog_stop();
23                 window.scrollTo(0,0);
24             },
25             error: c.rpc_error.bind(c)
26         });
27     }
28
29     instance.web.ActionManager = instance.web.ActionManager.extend({
30         ir_actions_report_xml: function(action, options) {
31             var self = this;
32             instance.web.blockUI();
33             action = _.clone(action);
34             _t =  instance.web._t;
35
36             // QWeb reports
37             if ('report_type' in action && (action.report_type == 'qweb-html' || action.report_type == 'qweb-pdf' || action.report_type == 'controller')) {
38                 var report_url = '';
39                 switch (action.report_type) {
40                     case 'qweb-html':
41                         report_url = '/report/html/' + action.report_name;
42                         break;
43                     case 'qweb-pdf':
44                         report_url = '/report/pdf/' + action.report_name;
45                         break;
46                     case 'controller':
47                         report_url = action.report_file;
48                         break;
49                     default:
50                         report_url = '/report/html/' + action.report_name;
51                         break;
52                 }
53
54                 // generic report: no query string
55                 // particular: query string of action.data.form and context
56                 if (!('data' in action) || !(action.data)) {
57                     if ('active_ids' in action.context) {
58                         report_url += "/" + action.context.active_ids.join(',');
59                     }
60                 } else {
61                     report_url += "?options=" + encodeURIComponent(JSON.stringify(action.data));
62                     report_url += "&context=" + encodeURIComponent(JSON.stringify(action.context));
63                 }
64
65                 var response = new Array();
66                 response[0] = report_url;
67                 response[1] = action.report_type;
68                 var c = openerp.webclient.crashmanager;
69
70                 if (action.report_type == 'qweb-html') {
71                     window.open(report_url, '_blank', 'scrollbars=1,height=900,width=1280');
72                     instance.web.unblockUI();
73                 } else if (action.report_type === 'qweb-pdf') {
74                     // Trigger the download of the pdf/controller report
75                     (wkhtmltopdf_state = wkhtmltopdf_state || openerp.session.rpc('/report/check_wkhtmltopdf')).then(function (presence) {
76                         // Fallback on html if wkhtmltopdf is not installed or if OpenERP is started with one worker
77                         if (presence === 'install') {
78                             self.do_notify(_t('Report'), _t('Unable to find Wkhtmltopdf on this \
79 system. The report will be shown in html.<br><br><a href="http://wkhtmltopdf.org/" target="_blank">\
80 wkhtmltopdf.org</a>'), true);
81                             report_url = report_url.substring(12)
82                             window.open('/report/html/' + report_url, '_blank', 'height=768,width=1024');
83                             instance.web.unblockUI();
84                             return;
85                         } else if (presence === 'workers') {
86                             self.do_notify(_t('Report'), _t('You need to start OpenERP with at least two \
87 workers to print a pdf version of the reports.'), true);
88                             report_url = report_url.substring(12)
89                             window.open('/report/html/' + report_url, '_blank', 'height=768,width=1024');
90                             instance.web.unblockUI();
91                             return;
92                         } else if (presence === 'upgrade') {
93                             self.do_notify(_t('Report'), _t('You should upgrade your version of\
94  Wkhtmltopdf to at least 0.12.0 in order to get a correct display of headers and footers as well as\
95  support for table-breaking between pages.<br><br><a href="http://wkhtmltopdf.org/" \
96  target="_blank">wkhtmltopdf.org</a>'), true);
97                         }
98                         if(action.hasOwnProperty('pdf_viewer')){
99                             return show_pdf(self.session, response, c, options, self);
100                         }
101                         else {
102                             return trigger_download(self.session, response, c );
103                         }
104                     });
105                 } else if (action.report_type === 'controller') {
106                     return trigger_download(self.session, response, c);
107                 }                     
108             } else {
109                 return self._super(action, options);
110             }
111         }
112     });
113 };