1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Author: Yannick Vaucher
5
# Copyright 2013 Camptocamp SA
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
21
from StringIO import StringIO
22
from PyPDF2 import PdfFileReader, PdfFileWriter
24
from openerp.osv import orm, fields
25
from tools.translate import _
28
def assemble_pdf(pdf_list):
30
Assemble a list of pdf
32
# Even though we are using PyPDF2 we can't use PdfFileMerger
33
# as this issue still exists in mostly used wkhtmltohpdf reports version
34
# http://code.google.com/p/wkhtmltopdf/issues/detail?id=635
35
#merger = PdfFileMerger()
36
#merger.append(fileobj=StringIO(invoice_pdf))
37
#merger.append(fileobj=StringIO(bvr_pdf))
39
#with tempfile.TemporaryFile() as merged_pdf:
40
#merger.write(merged_pdf)
41
#return merged_pdf.read(), 'pdf'
43
output = PdfFileWriter()
45
reader = PdfFileReader(StringIO(pdf))
46
for page in range(reader.getNumPages()):
47
output.addPage(reader.getPage(page))
53
class DeliveryCarrierLabelGenerate(orm.TransientModel):
55
_name = 'delivery.carrier.label.generate'
57
def _get_dispatch_ids(self, cr, uid, context=None):
61
if (context.get('active_model') == 'picking.dispatch'
62
and context.get('active_ids')):
63
res = context['active_ids']
67
'dispatch_ids': fields.many2many('picking.dispatch',
68
string='Picking Dispatch'),
69
'label_pdf_file': fields.binary('Labels file'),
73
'dispatch_ids': _get_dispatch_ids,
76
def action_generate_labels(self, cr, uid, ids, context=None):
78
Call the creation of the delivery carrier label
79
and merge them in a single PDF
81
context = context or {}
82
this = self.browse(cr, uid, ids, context=context)[0]
83
if not this.dispatch_ids:
84
raise orm.except_orm(_('Error'), _('No picking dispatch selected'))
86
picking_out_obj = self.pool.get('stock.picking.out')
89
picking_ids = [picking.id for dispatch in this.dispatch_ids
90
for picking in dispatch.related_picking_ids
91
if not picking.get_pdf_label()[picking.id]]
92
# generate missing picking labels
93
picking_out_obj.action_generate_carrier_label(cr, uid,
98
data_list = [picking.get_pdf_label()[picking.id]
99
for dispatch in this.dispatch_ids
100
for picking in dispatch.related_picking_ids]
101
pdf_list = [data.decode('base64') for data in data_list if data]
102
pdf_file = assemble_pdf(pdf_list)
103
this.write({'label_pdf_file': pdf_file.encode('base64')})
105
'type': 'ir.actions.act_window',
106
'res_model': 'delivery.carrier.label.generate',
110
'views': [(False, 'form')],