# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com>
#    Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
#    Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
#    Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
#    Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import openerp

from openerp.service.report import self_reports

original_exp_report = openerp.service.report.exp_report


def exp_report(db, uid, object, ids, datas=None, context=None):
    """ Export Report """
    # We can't use the named args because a monkey patch in 'calendar'
    # doesn't use them and use a different name for 'datas'
    res = original_exp_report(db, uid, object, ids, datas, context)
    self_reports[res]['report_name'] = object
    return res


openerp.service.report.exp_report = exp_report


original_exp_report_get = openerp.service.report.exp_report_get


def exp_report_get(db, uid, report_id):
    registry = openerp.registry(db)
    cr = registry.cursor()
    try:
        # First of all load report defaults: name, action and printer
        report_obj = registry['ir.actions.report.xml']
        report_name = self_reports[report_id]['report_name']
        report = report_obj.search(cr, uid,
                                   [('report_name', '=', report_name)])
        if report:
            report = report_obj.browse(cr, uid, report[0])
            data = report.behaviour()[report.id]
            action = data['action']
            printer = data['printer']
            if action != 'client':
                if (self_reports and self_reports.get(report_id)
                        and self_reports[report_id].get('result')
                        and self_reports[report_id].get('format')):
                    printer.print_document(report,
                                           self_reports[report_id]['result'],
                                           self_reports[report_id]['format'])
                    # FIXME "Warning" removed as it breaks the workflow
                    # it would be interesting to have a dialog box to
                    # confirm if we really want to print in this case it
                    # must be with a by pass parameter to allow massive
                    # prints
                    # raise osv.except_osv(
                    #     _('Printing...'),
                    #     _('Document sent to printer %s') % (printer,))

    except:
        cr.rollback()
        raise
    finally:
        cr.close()

    return original_exp_report_get(db, uid, report_id)


openerp.service.report.exp_report_get = exp_report_get

@@ -1844,40 +1844,140 @@
     
-registry = openerp.registry(db)%0A
+# First we need to know if the module is installed%0A    registry = openerp.registry(db)%0A    if registry.get('printing.printer'):%0A    
     
@@ -2007,13 +2007,21 @@
     
+ 
+   
 try:%0A
+    
     
@@ -2090,24 +2090,28 @@
 ter%0A        
+   
+ 
 report_obj =
@@ -2145,16 +2145,20 @@
 t.xml'%5D%0A
+    
         
@@ -2210,32 +2210,36 @@
 _name'%5D%0A        
+    
 report = report_
@@ -2293,16 +2293,20 @@
         
+ 
+   
 %5B('repor
@@ -2338,24 +2338,28 @@
 )%5D)%0A        
+    
 if report:%0A 
@@ -2361,32 +2361,36 @@
 rt:%0A            
+   
+ 
 report = report_
@@ -2420,16 +2420,20 @@
 ort%5B0%5D)%0A
+    
         
@@ -2485,16 +2485,20 @@
         
+    
 action =
@@ -2521,24 +2521,28 @@
             
+ 
+   
 printer = da
@@ -2567,16 +2567,20 @@
         
+    
 if actio
@@ -2610,16 +2610,20 @@
         
+   
+ 
 if (self
@@ -2663,16 +2663,20 @@
 ort_id)%0A
+    
         
@@ -2749,32 +2749,36 @@
                 
+    
 and self_reports
@@ -2821,24 +2821,28 @@
             
+ 
+   
 printer.prin
@@ -2895,32 +2895,36 @@
                 
+    
 self_reports%5Brep
@@ -2911,32 +2911,80 @@
     self_reports
+%0A                                               
 %5Breport_id%5D%5B'res
@@ -3025,32 +3025,36 @@
                 
+    
 self_reports%5Brep
@@ -3041,32 +3041,80 @@
     self_reports
+%0A                                               
 %5Breport_id%5D%5B'for
@@ -3140,16 +3140,20 @@
         
+   
+ 
 # FIXME 
@@ -3196,16 +3196,20 @@
 orkflow%0A
+    
         
@@ -3290,16 +3290,20 @@
         
+    
 # confir
@@ -3367,16 +3367,20 @@
         
+ 
+   
 # must b
@@ -3443,16 +3443,20 @@
         
+    
 # prints
@@ -3476,16 +3476,20 @@
         
+   
+ 
 # raise 
@@ -3504,16 +3504,20 @@
 pt_osv(%0A
+    
         
@@ -3564,32 +3564,36 @@
                 
+    
 #     _('Documen
@@ -3638,16 +3638,20 @@
     
+ 
+   
 except:%0A
     
@@ -3646,16 +3646,20 @@
 except:%0A
+    
         
@@ -3684,14 +3684,22 @@
     
+   
+ 
 raise%0A
+    
     
@@ -3715,16 +3715,20 @@
         
+    
 cr.close
@@ -3730,17 +3730,16 @@
 close()%0A
-%0A
     retu

test if the module is installed before monkey patching