~stock-logistic-core-editors/carriers-deliveries/github-7.0

« back to all changes in this revision

Viewing changes to base_delivery_carrier_label/stock.py

  • Committer: Joel Grand-Guillaume
  • Author(s): Yannick Vaucher
  • Date: 2014-01-17 15:32:57 UTC
  • mfrom: (7.1.22)
  • Revision ID: git-v1:0748467ab0e1873d434e8b30dc0e871af2d4e659
[MRG][MIGR] Port of delivery_base and deliver_shipping_label removing direct print logic

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
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
 
9
#
 
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.
 
14
#
 
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.
 
19
#
 
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/>.
 
22
#
 
23
##############################################################################
 
24
from openerp.osv import orm, fields
 
25
from openerp.tools.translate import _
 
26
 
 
27
 
 
28
class stock_picking(orm.Model):
 
29
    _inherit = 'stock.picking'
 
30
 
 
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)
 
34
 
 
35
    _columns = {
 
36
        'carrier_id': fields.many2one(
 
37
            'delivery.carrier', 'Carrier',
 
38
            states={'done': [('readonly', True)]}),
 
39
        'carrier_type': fields.related(
 
40
            'carrier_id', 'type',
 
41
            string='Carrier type',
 
42
            readonly=True,
 
43
            type='selection',
 
44
            selection=_get_carrier_type_selection,
 
45
            help="Carrier type ('group')"),
 
46
        'carrier_code': fields.related(
 
47
            'carrier_id', 'code',
 
48
            string='Delivery Method Code',
 
49
            readonly=True,
 
50
            type='char',
 
51
            help="Delivery Method Code (from carrier)"),
 
52
        'option_ids': fields.many2many('delivery.carrier.option',
 
53
                                       string='Options'),
 
54
    }
 
55
 
 
56
    def generate_default_label(self, cr, uid, ids, context=None):
 
57
        """ Abstract method
 
58
 
 
59
        :return: (file_binary, file_type)
 
60
 
 
61
        """
 
62
        raise orm.except_orm(
 
63
                'Error',
 
64
                'No label is configured for selected delivery method.')
 
65
 
 
66
    def generate_shipping_labels(self, cr, uid, ids, context=None):
 
67
        """Generate a shipping label by default
 
68
 
 
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
 
72
 
 
73
        :return: list of dict containing
 
74
           name: name to give to the attachement
 
75
           file: file as string
 
76
           file_type: string of file type like 'PDF'
 
77
           (optional)
 
78
           tracking_id: tracking_id if picking lines have tracking_id and
 
79
                        if label generator creates shipping label per
 
80
                        pack
 
81
 
 
82
        """
 
83
        return [self.generate_default_label(cr, uid, ids, context=None)]
 
84
 
 
85
    def action_generate_carrier_label(self, cr, uid, ids, context=None):
 
86
        shipping_label_obj = self.pool.get('shipping.label')
 
87
 
 
88
        pickings = self.browse(cr, uid, ids, context=context)
 
89
 
 
90
        for pick in pickings:
 
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',
 
97
                         }
 
98
                res_model = types[pick.type]
 
99
                data = {
 
100
                    'name': label['name'],
 
101
                    'res_id': pick.id,
 
102
                    'res_model': res_model,
 
103
                    'datas': label['file'].encode('base64'),
 
104
                    'file_type': label['file_type'],
 
105
                }
 
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)
 
114
        return True
 
115
 
 
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')
 
119
        res = {}
 
120
        if carrier_id:
 
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
 
127
 
 
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)
 
134
            res = {
 
135
                'value': {'carrier_type': carrier.type,
 
136
                          'carrier_code': carrier.code,
 
137
                          'option_ids': default_option_ids,
 
138
                          },
 
139
                'domain': {'option_ids': [('id', 'in', available_option_ids)],
 
140
                           },
 
141
            }
 
142
        return res
 
143
 
 
144
    def option_ids_change(self, cr, uid, ids, option_ids, carrier_id, context=None):
 
145
        carrier_obj = self.pool.get('delivery.carrier')
 
146
        res = {}
 
147
        if not carrier_id:
 
148
            return res
 
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]):
 
153
                res['warning'] = {
 
154
                    'title': _('User Error !'),
 
155
                    'message':  _("You can not remove a mandatory option."
 
156
                                  "\nOptions are reset to default.")
 
157
                }
 
158
                default_value = self.carrier_id_change(cr, uid, ids,
 
159
                                                       carrier_id,
 
160
                                                       context=context)
 
161
                res.update(default_value)
 
162
        return res
 
163
 
 
164
    def create(self, cr, uid, values, context=None):
 
165
        """ Trigger carrier_id_change on create
 
166
 
 
167
        To ensure options are setted on the basis of carrier_id copied from
 
168
        Sale order or defined by default.
 
169
 
 
170
        """
 
171
        carrier_id = values.get('carrier_id')
 
172
        if carrier_id:
 
173
            picking_obj = self.pool.get('stock.picking')
 
174
            res = picking_obj.carrier_id_change(cr, uid, [], carrier_id,
 
175
                                                context=context)
 
176
            option_ids = res.get('value', {}).get('option_ids')
 
177
            if 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)
 
181
        return picking_id
 
182
 
 
183
 
 
184
class stock_picking_in(orm.Model):
 
185
    """ Add what isn't inherited from stock.picking """
 
186
    _inherit = 'stock.picking.in'
 
187
 
 
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)
 
191
 
 
192
    _columns = {
 
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',
 
199
            readonly=True,
 
200
            type='selection',
 
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',
 
206
            readonly=True,
 
207
            type='char',
 
208
            help="Delivery Method Code (from carrier)"),
 
209
        'option_ids': fields.many2many('delivery.carrier.option',
 
210
                                       string='Options'),
 
211
    }
 
212
 
 
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,
 
216
                                                         context=context)
 
217
 
 
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)
 
223
 
 
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,
 
230
                                             context=context)
 
231
 
 
232
 
 
233
class stock_picking_out(orm.Model):
 
234
    """ Add what isn't inherited from stock.picking """
 
235
    _inherit = 'stock.picking.out'
 
236
 
 
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)
 
240
 
 
241
    _columns = {
 
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',
 
248
            readonly=True,
 
249
            type='selection',
 
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',
 
255
            readonly=True,
 
256
            type='char',
 
257
            help="Delivery Method Code (from carrier)"),
 
258
        'option_ids': fields.many2many('delivery.carrier.option',
 
259
                                       string='Options'),
 
260
    }
 
261
 
 
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,
 
265
                                                         context=context)
 
266
 
 
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)
 
271
 
 
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,
 
276
                                             context=context)
 
277
 
 
278
 
 
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"
 
284
 
 
285
    def _get_file_type_selection(self, cr, uid, context=None):
 
286
        return [('pdf', 'PDF')]
 
287
 
 
288
    _columns = {
 
289
        'file_type': fields.selection(_get_file_type_selection, 'File type'),
 
290
        'tracking_id': fields.many2one('stock.tracking', 'Pack'),
 
291
    }
 
292
 
 
293
    _defaults = {
 
294
        'file_type': 'pdf'
 
295
    }