~opencrea/+junk/aprobio

« back to all changes in this revision

Viewing changes to base_report_to_printer/models/report.py

  • Committer: joannes
  • Date: 2017-05-17 09:40:42 UTC
  • Revision ID: joannes@debian-20170517094042-47q3j6on72w2h1il
community module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (c) 2014 Camptocamp SA
 
3
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
 
4
 
 
5
from odoo import models, exceptions, _, api
 
6
 
 
7
 
 
8
class Report(models.Model):
 
9
    _inherit = 'report'
 
10
 
 
11
    @api.model
 
12
    def print_document(self, record_ids, report_name, html=None, data=None):
 
13
        """ Print a document, do not return the document file """
 
14
        document = self.with_context(must_skip_send_to_printer=True).get_pdf(
 
15
            record_ids, report_name, html=html, data=data)
 
16
        report = self._get_report_from_name(report_name)
 
17
        behaviour = report.behaviour()[report.id]
 
18
        printer = behaviour['printer']
 
19
        if not printer:
 
20
            raise exceptions.Warning(
 
21
                _('No printer configured to print this report.')
 
22
            )
 
23
        return printer.print_document(report, document, report.report_type)
 
24
 
 
25
    @api.multi
 
26
    def _can_print_report(self, behaviour, printer, document):
 
27
        """Predicate that decide if report can be sent to printer
 
28
 
 
29
        If you want to prevent `get_pdf` to send report you can set
 
30
        the `must_skip_send_to_printer` key to True in the context
 
31
        """
 
32
        if self.env.context.get('must_skip_send_to_printer'):
 
33
            return False
 
34
        if behaviour['action'] == 'server' and printer and document:
 
35
            return True
 
36
        return False
 
37
 
 
38
    @api.model
 
39
    def get_pdf(self, docids, report_name, html=None, data=None):
 
40
        """ Generate a PDF and returns it.
 
41
 
 
42
        If the action configured on the report is server, it prints the
 
43
        generated document as well.
 
44
        """
 
45
        document = super(Report, self).get_pdf(
 
46
            docids, report_name, html=html, data=data)
 
47
 
 
48
        report = self._get_report_from_name(report_name)
 
49
        behaviour = report.behaviour()[report.id]
 
50
        printer = behaviour['printer']
 
51
        can_print_report = self._can_print_report(behaviour, printer, document)
 
52
 
 
53
        if can_print_report:
 
54
            printer.print_document(report, document, report.report_type)
 
55
 
 
56
        return document