-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.py
78 lines (64 loc) · 2.83 KB
/
invoice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- encoding: utf-8 -*-
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
from trytond.model import dualmethod
from trytond.modules.jasper_reports.jasper import JasperReport
from trytond.exceptions import UserError
from trytond.rpc import RPC
__all__ = ['Invoice', 'InvoiceReport']
class Invoice(metaclass=PoolMeta):
__name__ = 'account.invoice'
@dualmethod
def print_invoice(cls, invoices):
# replace and render invoice jasper report
InvoiceReport = Pool().get('account.invoice.jreport', type='report')
for invoice in invoices:
if invoice.invoice_report_cache:
return
InvoiceReport.execute([invoice.id], {})
class InvoiceReport(JasperReport):
__name__ = 'account.invoice.jreport'
@classmethod
def __setup__(cls):
super(InvoiceReport, cls).__setup__()
cls.__rpc__['execute'] = RPC(False)
@classmethod
def render(cls, report, data, model, ids):
pool = Pool()
Invoice = pool.get('account.invoice')
new_invoices = []
invoice_reports_cache = []
invoice_reports_format = []
for invoice in Invoice.browse(ids):
if invoice.invoice_report_cache:
invoice_reports_cache.append(invoice.invoice_report_cache)
invoice_reports_format.append(invoice.invoice_report_format)
else:
new_invoices.append(invoice.id)
pages = len(invoice_reports_cache) or 1
invoice_reports_format = list(set(invoice_reports_format))
if invoice_reports_format and len(invoice_reports_format) != 1:
raise UserError('Warning', 'When try to generate multiple '
'reports at same time all them need to be the same format.'
' E.g.: "pdf".')
invoice_reports_format = (invoice_reports_format and
invoice_reports_format[0] or '')
if invoice_reports_cache and invoice_reports_format == 'pdf':
ndata = None
if new_invoices:
ntype, ndata, npages = super(InvoiceReport, cls).render(
report, data, model, new_invoices)
if ndata:
invoice_reports_cache.append(ndata)
pages = pages + npages
file_data = JasperReport.merge_pdfs(invoice_reports_cache)
return (invoice_reports_format, file_data, pages)
res = super(InvoiceReport, cls).render(report, data, model, ids)
if len(ids) == 1:
invoice = Invoice(ids[0])
if invoice.state in ('posted', 'paid') and invoice.type == 'out':
invoice.invoice_report_format = res[0]
invoice.invoice_report_cache = res[1]
invoice.save()
return res