1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Authors: David BEAL <david.beal@akretion.com>
5
# Sébastien BEAU <sebastien.beau@akretion.com>
6
# Copyright (C) 2012-TODAY Akretion <http://www.akretion.com>.
7
# Author: Yannick Vaucher <yannick.vaucher@camptocamp.com>
8
# Copyright 2013 Camptocamp SA
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Affero General Public License as
12
# published by the Free Software Foundation, either version 3 of the
13
# License, or (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU Affero General Public License for more details.
20
# You should have received a copy of the GNU Affero General Public License
21
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
##############################################################################
24
from openerp.osv import orm, fields
25
from openerp.tools.translate import _
28
class stock_picking(orm.Model):
29
_inherit = 'stock.picking'
31
def _get_carrier_type_selection(self, cr, uid, context=None):
32
carrier_obj = self.pool.get('delivery.carrier')
33
return carrier_obj._get_carrier_type_selection(cr, uid, context=context)
36
'carrier_id': fields.many2one(
37
'delivery.carrier', 'Carrier',
38
states={'done': [('readonly', True)]}),
39
'carrier_type': fields.related(
41
string='Carrier type',
44
selection=_get_carrier_type_selection,
45
help="Carrier type ('group')"),
46
'carrier_code': fields.related(
48
string='Delivery Method Code',
51
help="Delivery Method Code (from carrier)"),
52
'option_ids': fields.many2many('delivery.carrier.option',
56
def generate_default_label(self, cr, uid, ids, context=None):
59
:return: (file_binary, file_type)
64
'No label is configured for selected delivery method.')
66
def generate_shipping_labels(self, cr, uid, ids, context=None):
67
"""Generate a shipping label by default
69
This method can be inherited to create specific shipping labels
70
a list of label must be return as we can have multiple
71
stock.tracking for a single picking representing packs
73
:return: list of dict containing
74
name: name to give to the attachement
76
file_type: string of file type like 'PDF'
78
tracking_id: tracking_id if picking lines have tracking_id and
79
if label generator creates shipping label per
83
return [self.generate_default_label(cr, uid, ids, context=None)]
85
def action_generate_carrier_label(self, cr, uid, ids, context=None):
86
shipping_label_obj = self.pool.get('shipping.label')
88
pickings = self.browse(cr, uid, ids, context=context)
91
shipping_labels = pick.generate_shipping_labels()
92
for label in shipping_labels:
93
# map types with models
94
types = {'in': 'stock.picking.in',
95
'out': 'stock.picking.out',
96
'internal': 'stock.picking',
98
res_model = types[pick.type]
100
'name': label['name'],
102
'res_model': res_model,
103
'datas': label['file'].encode('base64'),
104
'file_type': label['file_type'],
106
if label.get('tracking_id'):
107
data['tracking_id'] = label['tracking_id']
108
context_attachment = context.copy()
109
# remove default_type setted for stock_picking
110
# as it would try to define default value of attachement
111
if 'default_type' in context_attachment:
112
del context_attachment['default_type']
113
shipping_label_obj.create(cr, uid, data, context=context_attachment)
116
def carrier_id_change(self, cr, uid, ids, carrier_id, context=None):
117
""" Inherit this method in your module """
118
carrier_obj = self.pool.get('delivery.carrier')
121
carrier = carrier_obj.browse(cr, uid, carrier_id, context=context)
122
# This can look useless as the field carrier_code and
123
# carrier_type are related field. But it's needed to fill
124
# this field for using this fields in the view. Indeed the
125
# module that depend of delivery base can hide some field
126
# depending of the type or the code
128
default_option_ids = []
129
available_option_ids = []
130
for available_option in carrier.available_option_ids:
131
available_option_ids.append(available_option.id)
132
if available_option.state in ['default_option', 'mandatory']:
133
default_option_ids.append(available_option.id)
135
'value': {'carrier_type': carrier.type,
136
'carrier_code': carrier.code,
137
'option_ids': default_option_ids,
139
'domain': {'option_ids': [('id', 'in', available_option_ids)],
144
def option_ids_change(self, cr, uid, ids, option_ids, carrier_id, context=None):
145
carrier_obj = self.pool.get('delivery.carrier')
149
carrier = carrier_obj.browse(cr, uid, carrier_id, context=context)
150
for available_option in carrier.available_option_ids:
151
if (available_option.state == 'mandatory'
152
and not available_option.id in option_ids[0][2]):
154
'title': _('User Error !'),
155
'message': _("You can not remove a mandatory option."
156
"\nOptions are reset to default.")
158
default_value = self.carrier_id_change(cr, uid, ids,
161
res.update(default_value)
164
def create(self, cr, uid, values, context=None):
165
""" Trigger carrier_id_change on create
167
To ensure options are setted on the basis of carrier_id copied from
168
Sale order or defined by default.
171
carrier_id = values.get('carrier_id')
173
picking_obj = self.pool.get('stock.picking')
174
res = picking_obj.carrier_id_change(cr, uid, [], carrier_id,
176
option_ids = res.get('value', {}).get('option_ids')
178
values.update(option_ids=[(6, 0, option_ids)])
179
picking_id = super(stock_picking, self
180
).create(cr, uid, values, context=context)
184
class stock_picking_in(orm.Model):
185
""" Add what isn't inherited from stock.picking """
186
_inherit = 'stock.picking.in'
188
def _get_carrier_type_selection(self, cr, uid, context=None):
189
carrier_obj = self.pool.get('delivery.carrier')
190
return carrier_obj._get_carrier_type_selection(cr, uid, context=context)
193
'carrier_id': fields.many2one(
194
'delivery.carrier', 'Carrier',
195
states={'done': [('readonly', True)]}),
196
'carrier_type': fields.related(
197
'carrier_id', 'type',
198
string='Carrier type',
201
selection=_get_carrier_type_selection,
202
help="Carrier type ('group')"),
203
'carrier_code': fields.related(
204
'carrier_id', 'code',
205
string='Delivery Method Code',
208
help="Delivery Method Code (from carrier)"),
209
'option_ids': fields.many2many('delivery.carrier.option',
213
def action_generate_carrier_label(self, cr, uid, ids, context=None):
214
picking_obj = self.pool.get('stock.picking')
215
return picking_obj.action_generate_carrier_label(cr, uid, ids,
218
def carrier_id_change(self, cr, uid, ids, carrier_id, context=None):
219
""" Call stock.picking carrier_id_change """
220
picking_obj = self.pool.get('stock.picking')
221
return picking_obj.carrier_id_change(cr, uid, ids,
222
carrier_id, context=context)
224
def option_ids_change(self, cr, uid, ids,
225
option_ids, carrier_id, context=None):
226
""" Call stock.picking option_ids_change """
227
picking_obj = self.pool.get('stock.picking')
228
return picking_obj.option_ids_change(cr, uid, ids,
229
option_ids, carrier_id,
233
class stock_picking_out(orm.Model):
234
""" Add what isn't inherited from stock.picking """
235
_inherit = 'stock.picking.out'
237
def _get_carrier_type_selection(self, cr, uid, context=None):
238
carrier_obj = self.pool.get('delivery.carrier')
239
return carrier_obj._get_carrier_type_selection(cr, uid, context=context)
242
'carrier_id': fields.many2one(
243
'delivery.carrier', 'Carrier',
244
states={'done': [('readonly', True)]}),
245
'carrier_type': fields.related(
246
'carrier_id', 'type',
247
string='Carrier type',
250
selection=_get_carrier_type_selection,
251
help="Carrier type ('group')"),
252
'carrier_code': fields.related(
253
'carrier_id', 'code',
254
string='Delivery Method Code',
257
help="Delivery Method Code (from carrier)"),
258
'option_ids': fields.many2many('delivery.carrier.option',
262
def action_generate_carrier_label(self, cr, uid, ids, context=None):
263
picking_obj = self.pool.get('stock.picking')
264
return picking_obj.action_generate_carrier_label(cr, uid, ids,
267
def carrier_id_change(self, cr, uid, ids, carrier_id, context=None):
268
""" Inherit this method in your module """
269
picking_obj = self.pool.get('stock.picking')
270
return picking_obj.carrier_id_change(cr, uid, ids, carrier_id, context=context)
272
def option_ids_change(self, cr, uid, ids, option_ids, carrier_id, context=None):
273
picking_obj = self.pool.get('stock.picking')
274
return picking_obj.option_ids_change(cr, uid, ids,
275
option_ids, carrier_id,
279
class ShippingLabel(orm.Model):
280
""" Child class of ir attachment to identify which are labels """
281
_inherits = {'ir.attachment': 'attachment_id'}
282
_name = 'shipping.label'
283
_description = "Shipping Label"
285
def _get_file_type_selection(self, cr, uid, context=None):
286
return [('pdf', 'PDF')]
289
'file_type': fields.selection(_get_file_type_selection, 'File type'),
290
'tracking_id': fields.many2one('stock.tracking', 'Pack'),