Merge lp:~camptocamp/carriers-deliveries/7.0-add-delivery_carrier_label_dispatch-tracking_ids into lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr

Proposed by Guewen Baconnier @ Camptocamp
Status: Merged
Approved by: Yannick Vaucher @ Camptocamp
Approved revision: 21
Merged at revision: 17
Proposed branch: lp:~camptocamp/carriers-deliveries/7.0-add-delivery_carrier_label_dispatch-tracking_ids
Merge into: lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr
Diff against target: 237 lines (+62/-88)
6 files modified
delivery_carrier_label_dispatch/__init__.py (+0/-1)
delivery_carrier_label_dispatch/__openerp__.py (+5/-2)
delivery_carrier_label_dispatch/pdf_utils.py (+2/-0)
delivery_carrier_label_dispatch/stock.py (+0/-62)
delivery_carrier_label_dispatch/wizard/generate_labels.py (+52/-23)
delivery_carrier_label_dispatch/wizard/generate_labels_view.xml (+3/-0)
To merge this branch: bzr merge lp:~camptocamp/carriers-deliveries/7.0-add-delivery_carrier_label_dispatch-tracking_ids
Reviewer Review Type Date Requested Status
Yannick Vaucher @ Camptocamp code review, no tests Approve
Review via email: mp+207205@code.launchpad.net

Description of the change

When using the generate method on a dispatch, instead of generating all the labels for all the packs of the related pickings, it will merely generate the labels of the related packs. It does that using the new parameter defined in https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-base_delivery_carrier_label-tracking_ids/+merge/207199

It also include https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-generate-new-labels-option-yvr

To post a comment you must log in.
20. By Guewen Baconnier @ Camptocamp

use the general method and not the one for the button

21. By Guewen Baconnier @ Camptocamp

avoid utils.PdfReadError('Cannot read an empty file') error

Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

LGTM Thanks

review: Approve (code review, no tests)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'delivery_carrier_label_dispatch/__init__.py'
2--- delivery_carrier_label_dispatch/__init__.py 2013-12-06 12:51:58 +0000
3+++ delivery_carrier_label_dispatch/__init__.py 2014-02-20 08:59:04 +0000
4@@ -18,5 +18,4 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6 #
7 ##############################################################################
8-from . import stock
9 from . import wizard
10
11=== modified file 'delivery_carrier_label_dispatch/__openerp__.py'
12--- delivery_carrier_label_dispatch/__openerp__.py 2013-12-06 12:51:58 +0000
13+++ delivery_carrier_label_dispatch/__openerp__.py 2014-02-20 08:59:04 +0000
14@@ -29,8 +29,11 @@
15 [Link module] Carrier labels - Picking dispatch
16 ==============================
17
18-This module adds a wizard on picking dispatch to generate related picking
19-labels
20+This module adds a wizard on picking dispatch to generate the labels
21+of the packs. The labels are merged in one PDF file.
22+
23+All the moves should have been put in a pack before the labels can be
24+printed.
25
26 Contributors
27 ------------
28
29=== modified file 'delivery_carrier_label_dispatch/pdf_utils.py'
30--- delivery_carrier_label_dispatch/pdf_utils.py 2013-12-17 07:55:56 +0000
31+++ delivery_carrier_label_dispatch/pdf_utils.py 2014-02-20 08:59:04 +0000
32@@ -39,6 +39,8 @@
33
34 output = PdfFileWriter()
35 for pdf in pdf_list:
36+ if not pdf:
37+ continue
38 reader = PdfFileReader(StringIO(pdf))
39
40 for page in range(reader.getNumPages()):
41
42=== removed file 'delivery_carrier_label_dispatch/stock.py'
43--- delivery_carrier_label_dispatch/stock.py 2013-12-17 07:42:04 +0000
44+++ delivery_carrier_label_dispatch/stock.py 1970-01-01 00:00:00 +0000
45@@ -1,62 +0,0 @@
46-# -*- coding: utf-8 -*-
47-##############################################################################
48-#
49-# Author: Yannick Vaucher
50-# Copyright 2013 Camptocamp SA
51-#
52-# This program is free software: you can redistribute it and/or modify
53-# it under the terms of the GNU Affero General Public License as
54-# published by the Free Software Foundation, either version 3 of the
55-# License, or (at your option) any later version.
56-#
57-# This program is distributed in the hope that it will be useful,
58-# but WITHOUT ANY WARRANTY; without even the implied warranty of
59-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60-# GNU Affero General Public License for more details.
61-#
62-# You should have received a copy of the GNU Affero General Public License
63-# along with this program. If not, see <http://www.gnu.org/licenses/>.
64-#
65-##############################################################################
66-from pdf_utils import assemble_pdf
67-from openerp.osv import orm
68-
69-
70-class stock_picking(orm.Model):
71- _inherit = 'stock.picking'
72-
73- def get_pdf_label(self, cr, uid, ids, context=None):
74- """ Return a single pdf of labels for a stock picking
75-
76- If more than one label is found for a picking we merge one label per
77- tracking in a single pdf
78-
79- :return: a list of pdf file data
80- """
81- res = dict.fromkeys(ids, False)
82- label_obj = self.pool.get('shipping.label')
83- for picking_id in ids:
84- label_ids = label_obj.search(cr, uid,
85- [('file_type', '=', 'pdf'),
86- ('res_id', '=', picking_id)],
87- order='create_date',
88- context=context)
89- if label_ids:
90- all_picking_labels = label_obj.browse(cr, uid,
91- label_ids,
92- context=context)
93-
94- tracking_ids = [l.tracking_id for l in all_picking_labels]
95-
96- # filter for newest created label for each trackings
97- label_datas = []
98- tracking_ids = set(tracking_ids)
99- while tracking_ids:
100- tracking_id = tracking_ids.pop()
101- for label in all_picking_labels:
102- if label.tracking_id.id == tracking_id.id:
103- label_datas.append(label.datas.decode('base64'))
104-
105- label_pdf = assemble_pdf(label_datas)
106- res[picking_id] = label_pdf.encode('base64')
107- return res
108
109=== modified file 'delivery_carrier_label_dispatch/wizard/generate_labels.py'
110--- delivery_carrier_label_dispatch/wizard/generate_labels.py 2014-01-21 12:26:56 +0000
111+++ delivery_carrier_label_dispatch/wizard/generate_labels.py 2014-02-20 08:59:04 +0000
112@@ -18,11 +18,14 @@
113 # along with this program. If not, see <http://www.gnu.org/licenses/>.
114 #
115 ##############################################################################
116+from operator import attrgetter
117+from itertools import groupby
118+
119+from openerp.osv import orm, fields
120+from openerp.tools.translate import _
121+
122 from ..pdf_utils import assemble_pdf
123
124-from openerp.osv import orm, fields
125-from tools.translate import _
126-
127
128 class DeliveryCarrierLabelGenerate(orm.TransientModel):
129
130@@ -40,12 +43,53 @@
131 _columns = {
132 'dispatch_ids': fields.many2many('picking.dispatch',
133 string='Picking Dispatch'),
134+ 'generate_new_labels': fields.boolean(
135+ 'Generate new labels',
136+ help="If this option is used, new labels will be "
137+ "generated for the packs even if they already have one.\n"
138+ "The default is to use the existing label."),
139 }
140
141 _defaults = {
142 'dispatch_ids': _get_dispatch_ids,
143+ 'generate_new_labels': False,
144 }
145
146+ def _get_packs(self, cr, uid, wizard, dispatch, context=None):
147+ moves = sorted(dispatch.move_ids, key=attrgetter('tracking_id.name'))
148+ for pack, moves in groupby(moves, key=attrgetter('tracking_id')):
149+ pack_label = self._find_pack_label(cr, uid, wizard, pack,
150+ context=context)
151+ yield pack, list(moves), pack_label
152+
153+ def _find_pack_label(self, cr, uid, wizard, pack, context=None):
154+ label_obj = self.pool['shipping.label']
155+ domain = [('file_type', '=', 'pdf'),
156+ ('tracking_id', '=', pack.id),
157+ ]
158+ label_id = label_obj.search(cr, uid, domain, order='create_date DESC',
159+ limit=1, context=context)
160+ if not label_id:
161+ return None
162+ return label_obj.browse(cr, uid, label_id[0], context=context)
163+
164+ def _get_all_pdf(self, cr, uid, wizard, dispatch, context=None):
165+ for pack, moves, label in self._get_packs(cr, uid, wizard, dispatch,
166+ context=context):
167+ if not label or wizard.generate_new_labels:
168+ picking_out_obj = self.pool['stock.picking.out']
169+ picking_id = moves[0].picking_id.id
170+ # generate the label of the pack
171+ picking_out_obj.generate_labels(
172+ cr, uid, [picking_id],
173+ tracking_ids=[pack.id],
174+ context=context)
175+ label = self._find_pack_label(cr, uid, wizard, pack,
176+ context=context)
177+ if not label:
178+ continue # no label could be generated
179+ yield label
180+
181 def action_generate_labels(self, cr, uid, ids, context=None):
182 """
183 Call the creation of the delivery carrier label
184@@ -57,33 +101,18 @@
185 if not this.dispatch_ids:
186 raise orm.except_orm(_('Error'), _('No picking dispatch selected'))
187
188- picking_out_obj = self.pool.get('stock.picking.out')
189 attachment_obj = self.pool.get('ir.attachment')
190
191 for dispatch in this.dispatch_ids:
192- # flatten all picking in one list to keep the order in case
193- # if pickings have been ordered to ease packaging
194- pickings = [(pick, pick.get_pdf_label()[pick.id])
195- for pick in dispatch.related_picking_ids]
196- # get picking ids for which we want to generate pdf label
197- picking_ids = [pick.id for pick, pdf in pickings
198- if not pdf]
199- # generate missing picking labels
200- picking_out_obj.action_generate_carrier_label(cr, uid,
201- picking_ids,
202- #file_type='pdf',
203- context=context)
204- # Get all pdf files adding the newly generated ones
205- data_list = [pdf or pick.get_pdf_label()[pick.id]
206- for pick, pdf in pickings]
207- pdf_list = [data.decode('base64') for data in data_list if data]
208-
209- pdf_file = assemble_pdf(pdf_list)
210+ labels = self._get_all_pdf(cr, uid, this, dispatch,
211+ context=context)
212+ labels = (label.datas for label in labels)
213+ labels = (label.decode('base64') for label in labels if labels)
214 data = {
215 'name': dispatch.name,
216 'res_id': dispatch.id,
217 'res_model': 'picking.dispatch',
218- 'datas': pdf_file.encode('base64'),
219+ 'datas': assemble_pdf(labels).encode('base64'),
220 }
221 attachment_obj.create(cr, uid, data, context=context)
222
223
224=== modified file 'delivery_carrier_label_dispatch/wizard/generate_labels_view.xml'
225--- delivery_carrier_label_dispatch/wizard/generate_labels_view.xml 2014-01-21 12:26:56 +0000
226+++ delivery_carrier_label_dispatch/wizard/generate_labels_view.xml 2014-02-20 08:59:04 +0000
227@@ -9,7 +9,10 @@
228 <form string="Generate Carriers Labels" version="7.0">
229 <group>
230 <label string="This wizard creates an attachement on each selected dispatch containing picking labels"/>
231+ </group>
232+ <group>
233 <field name="dispatch_ids"/>
234+ <field name="generate_new_labels"/>
235 </group>
236 <footer>
237 <button name="action_generate_labels" string="Generate Labels" type="object" icon="gtk-execute" class="oe_highlight"/>

Subscribers

People subscribed via source and target branches

to all changes: