~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_postlogistics-tracking_ids

« back to all changes in this revision

Viewing changes to delivery_carrier_label_postlogistics/stock.py

  • Committer: Guewen Baconnier
  • Date: 2014-02-19 13:15:37 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20140219131537-ocaqtu1bhmveizik
[ADD] use the new parameter 'tracking_ids' to generate only the labels of the trackings given in tracking_ids if used.

The implementation by default still generate them all at once from a button, but for instance the module allowing to print labels of a dispatch use it

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
19
#
20
20
##############################################################################
 
21
from operator import attrgetter
 
22
 
21
23
from openerp.osv import orm, fields
22
24
 
23
25
from postlogistics.web_service import PostlogisticsWebService
27
29
    _inherit = 'stock.picking'
28
30
 
29
31
    def _generate_postlogistics_label(self, cr, uid, picking,
30
 
                                      webservice_class=None, context=None):
 
32
                                      webservice_class=None,
 
33
                                      tracking_ids=None, context=None):
31
34
        """ Generate labels and write tracking numbers received """
32
35
        user_obj = self.pool.get('res.users')
33
36
        user = user_obj.browse(cr, uid, uid, context=context)
35
38
        if webservice_class is None:
36
39
            webservice_class = PostlogisticsWebService
37
40
 
 
41
        if tracking_ids is None:
 
42
            # get all the trackings of the picking
 
43
            # no tracking_id wil return a False, meaning that
 
44
            # we want a label for the picking
 
45
            trackings = sorted(set(
 
46
                line.tracking_id for line in picking.move_lines
 
47
            ), key=attrgetter('name'))
 
48
        else:
 
49
            # restrict on the provided trackings
 
50
            tracking_obj = self.pool['stock.tracking']
 
51
            trackings = tracking_obj.browse(cr, uid, tracking_ids,
 
52
                                            context=context)
 
53
 
38
54
        web_service = webservice_class(company)
39
 
        res = web_service.generate_label(picking, user.lang)
 
55
        res = web_service.generate_label(picking,
 
56
                                         trackings,
 
57
                                         user_lang=user.lang)
40
58
 
41
59
        if 'errors' in res:
42
60
            raise orm.except_orm('Error', '\n'.join(res['errors']))
43
61
 
44
 
        trackings = set([line.tracking_id for line in picking.move_lines])
45
 
 
46
62
        labels = []
47
63
        # if there are no pack defined, write tracking_number on picking
48
64
        # otherwise, write it on serial field of each pack
50
66
            if not track:
51
67
                # ignore lines without tracking when there is tracking
52
68
                # in a picking
 
69
                # Example: if I have 1 move with a tracking and 1
 
70
                # without, I will have [False, a_tracking] in
 
71
                # `trackings`. In that case, we are using packs, not the
 
72
                # picking for the tracking numbers.
53
73
                if len(trackings) > 1:
54
74
                    continue
55
75
                label = res['value'][0]
65
85
                        tracking_number = label['tracking_number']
66
86
                        track.write({'serial': tracking_number})
67
87
                        break
68
 
            labels.append({'tracking_id': track and track.id or False,
 
88
            labels.append({'tracking_id': track.id if track else False,
69
89
                           'file': label['binary'].decode('base64'),
70
90
                           'file_type': label['file_type'],
71
91
                           'name': tracking_number,
73
93
 
74
94
        return labels
75
95
 
76
 
    def generate_shipping_labels(self, cr, uid, ids, context=None):
 
96
    def generate_shipping_labels(self, cr, uid, ids, tracking_ids=None,
 
97
                                 context=None):
77
98
        """ Add label generation for Postlogistics """
78
99
        if isinstance(ids, (long, int)):
79
100
            ids = [ids]
80
101
        assert len(ids) == 1
81
102
        picking = self.browse(cr, uid, ids[0], context=context)
82
103
        if picking.carrier_id.type == 'postlogistics':
83
 
            return self._generate_postlogistics_label(cr, uid, picking,
84
 
                                                      context=context)
85
 
        return super(stock_picking, self
86
 
                     ).generate_shipping_labels(cr, uid, ids, context=context)
 
104
            return self._generate_postlogistics_label(
 
105
                cr, uid, picking,
 
106
                tracking_ids=tracking_ids,
 
107
                context=context)
 
108
        return super(stock_picking, self).\
 
109
            generate_shipping_labels(cr, uid, ids, tracking_ids=tracking_ids,
 
110
                                     context=context)
87
111
 
88
112
 
89
113
class ShippingLabel(orm.Model):