~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr

« back to all changes in this revision

Viewing changes to delivery_carrier_label_dispatch/wizard/generate_labels.py

  • Committer: Yannick Vaucher
  • Date: 2013-12-06 12:51:58 UTC
  • Revision ID: yannick.vaucher@camptocamp.com-20131206125158-ai0mqzju06bkbjd5
[ADD] module delivery_carrier_label_dispatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Yannick Vaucher
 
5
#    Copyright 2013 Camptocamp SA
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
##############################################################################
 
21
from StringIO import StringIO
 
22
from PyPDF2 import PdfFileReader, PdfFileWriter
 
23
 
 
24
from openerp.osv import orm, fields
 
25
from tools.translate import _
 
26
 
 
27
 
 
28
def assemble_pdf(pdf_list):
 
29
    """
 
30
    Assemble a list of pdf
 
31
    """
 
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))
 
38
 
 
39
    #with tempfile.TemporaryFile() as merged_pdf:
 
40
        #merger.write(merged_pdf)
 
41
        #return merged_pdf.read(), 'pdf'
 
42
 
 
43
    output = PdfFileWriter()
 
44
    for pdf in pdf_list:
 
45
        reader = PdfFileReader(StringIO(pdf))
 
46
        for page in range(reader.getNumPages()):
 
47
            output.addPage(reader.getPage(page))
 
48
    s = StringIO()
 
49
    output.write(s)
 
50
    return s.getvalue()
 
51
 
 
52
 
 
53
class DeliveryCarrierLabelGenerate(orm.TransientModel):
 
54
 
 
55
    _name = 'delivery.carrier.label.generate'
 
56
 
 
57
    def _get_dispatch_ids(self, cr, uid, context=None):
 
58
        if context is None:
 
59
            context = {}
 
60
        res = False
 
61
        if (context.get('active_model') == 'picking.dispatch'
 
62
                and context.get('active_ids')):
 
63
            res = context['active_ids']
 
64
        return res
 
65
 
 
66
    _columns = {
 
67
        'dispatch_ids': fields.many2many('picking.dispatch',
 
68
                                         string='Picking Dispatch'),
 
69
        'label_pdf_file': fields.binary('Labels file'),
 
70
    }
 
71
 
 
72
    _defaults = {
 
73
        'dispatch_ids': _get_dispatch_ids,
 
74
    }
 
75
 
 
76
    def action_generate_labels(self, cr, uid, ids, context=None):
 
77
        """
 
78
        Call the creation of the delivery carrier label
 
79
        and merge them in a single PDF
 
80
        """
 
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'))
 
85
 
 
86
        picking_out_obj = self.pool.get('stock.picking.out')
 
87
 
 
88
        # flatten all ids
 
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,
 
94
                                                      picking_ids,
 
95
                                                      #file_type='pdf',
 
96
                                                      context=context)
 
97
 
 
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')})
 
104
        return {
 
105
            'type': 'ir.actions.act_window',
 
106
            'res_model': 'delivery.carrier.label.generate',
 
107
            'view_mode': 'form',
 
108
            'view_type': 'form',
 
109
            'res_id': this.id,
 
110
            'views': [(False, 'form')],
 
111
            'target': 'new',
 
112
        }