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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Yannick Vaucher
# Copyright 2013 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from operator import attrgetter
from openerp.osv import orm, fields
from postlogistics.web_service import PostlogisticsWebService
class stock_picking(orm.Model):
_inherit = 'stock.picking'
def _generate_postlogistics_label(self, cr, uid, picking,
webservice_class=None,
tracking_ids=None, context=None):
""" Generate labels and write tracking numbers received """
user_obj = self.pool.get('res.users')
user = user_obj.browse(cr, uid, uid, context=context)
company = user.company_id
if webservice_class is None:
webservice_class = PostlogisticsWebService
if tracking_ids is None:
# get all the trackings of the picking
# no tracking_id wil return a False, meaning that
# we want a label for the picking
trackings = sorted(set(
line.tracking_id for line in picking.move_lines
), key=attrgetter('name'))
else:
# restrict on the provided trackings
tracking_obj = self.pool['stock.tracking']
trackings = tracking_obj.browse(cr, uid, tracking_ids,
context=context)
web_service = webservice_class(company)
res = web_service.generate_label(picking,
trackings,
user_lang=user.lang)
if 'errors' in res:
raise orm.except_orm('Error', '\n'.join(res['errors']))
labels = []
# if there are no pack defined, write tracking_number on picking
# otherwise, write it on serial field of each pack
for track in trackings:
if not track:
# ignore lines without tracking when there is tracking
# in a picking
# Example: if I have 1 move with a tracking and 1
# without, I will have [False, a_tracking] in
# `trackings`. In that case, we are using packs, not the
# picking for the tracking numbers.
if len(trackings) > 1:
continue
label = res['value'][0]
tracking_number = label['tracking_number']
self.write(cr, uid, picking.id,
{'carrier_tracking_ref': tracking_number},
context=context)
else:
label = None
for search_label in res['value']:
if track.name in search_label['item_id'].split('+')[-1]:
label = search_label
tracking_number = label['tracking_number']
track.write({'serial': tracking_number})
break
labels.append({'tracking_id': track.id if track else False,
'file': label['binary'].decode('base64'),
'file_type': label['file_type'],
'name': tracking_number,
})
return labels
def generate_shipping_labels(self, cr, uid, ids, tracking_ids=None,
context=None):
""" Add label generation for Postlogistics """
if isinstance(ids, (long, int)):
ids = [ids]
assert len(ids) == 1
picking = self.browse(cr, uid, ids[0], context=context)
if picking.carrier_id.type == 'postlogistics':
return self._generate_postlogistics_label(
cr, uid, picking,
tracking_ids=tracking_ids,
context=context)
return super(stock_picking, self).\
generate_shipping_labels(cr, uid, ids, tracking_ids=tracking_ids,
context=context)
class ShippingLabel(orm.Model):
""" Child class of ir attachment to identify which are labels """
_inherit = 'shipping.label'
def _get_file_type_selection(self, cr, uid, context=None):
""" Return a sorted list of extensions of label file format
:return: list of tuple (code, name)
"""
file_types = super(ShippingLabel, self
)._get_file_type_selection(cr, uid, context=context)
new_types = [('eps', 'EPS'),
('gif', 'GIF'),
('jpg', 'JPG'),
('png', 'PNG'),
('pdf', 'PDF'),
('spdf', 'sPDF'),
('zpl2', 'ZPL2')]
add_types = [t for t in new_types if not t in file_types]
file_types.extend(add_types)
file_types.sort(key=lambda t: t[0])
return file_types
_columns = {
'file_type': fields.selection(_get_file_type_selection, 'File type')
}
|