~domsense/stock-logistic-flows/adding_7.0_stock_move_backdating

« back to all changes in this revision

Viewing changes to picking_dispatch/wizard/create_dispatch.py

[MRG] picking dispatch improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
class picking_dispatch_creator(TransientModel):
28
28
    """Create a picking dispatch from stock.picking. This will take all related
29
29
    stock move from the selected picking and put them in the dispatch order."""
30
 
    
 
30
 
31
31
    _name = 'picking.dispatch.creator'
32
32
    _description = 'Picking Dispatch Creator'
33
33
    _columns = {
 
34
        'name': fields.char('Name', size=96, required=True,
 
35
                           help='Name of the picking dispatch'),
34
36
        'date': fields.date('Date', required=True, select=True,
35
37
                            help='Date on which the picking dispatched is to be processed'),
36
 
        'picker_id': fields.many2one('res.users', 'Picker',
 
38
        'picker_id': fields.many2one('res.users', 'Picker', required=True,
37
39
                                     help='The user to which the pickings are assigned'),
 
40
        'notes': fields.text('Notes', help='free form remarks'),
38
41
        }
39
42
 
 
43
    def _default_picker(self, cr, uid, context=None):
 
44
        user_obj = self.pool.get('res.users')
 
45
        company = user_obj.browse(cr, uid, uid).company_id
 
46
        return company.default_picker_id.id if company.default_picker_id else False
 
47
    
40
48
    _defaults = {
 
49
        'name': lambda obj, cr, uid, ctxt: obj.pool.get('ir.sequence').get(cr, uid, 'picking.dispatch'),
41
50
        'date': fields.date.context_today,
 
51
        'picker_id': _default_picker,
42
52
        }
43
53
    def action_create_dispatch(self, cr, uid, ids, context=None):
44
54
        """
51
61
            context = {}
52
62
        wiz = self.read(cr, uid, ids, [], context=context)[0]
53
63
        ctx = context.copy()
54
 
        move_ids = move_obj.search(cr, uid,
55
 
                               [('picking_id', 'in', context['active_ids']),
56
 
                                ('dispatch_id', '=', False),
57
 
                                ('state', 'in', ('confirmed', 'waiting', 'assigned'))],
 
64
        all_move_ids = move_obj.search(cr, uid,
 
65
                               [('picking_id', 'in', context['active_ids'])],
58
66
                               context=context)
59
 
        if not move_ids:
60
 
            raise osv.except_osv(_('Warning !'), _("No valide stock moves found to create the dispatch ! "
61
 
                "(Only move that are not part of a disptach order and in confirm, waiting or assigned state can be "
62
 
                "used)"))
 
67
        ok_move_ids = []
 
68
        already_dispatched_ids = {}
 
69
        wrong_state_ids = {}
 
70
        for move in move_obj.browse(cr, uid, all_move_ids, context=context):
 
71
            if move.dispatch_id:
 
72
                already_dispatched_ids.setdefault(move.dispatch_id.name, []).append((move.id, move.picking_id.name))
 
73
            elif move.state not in ('confirmed', 'waiting', 'assigned'):
 
74
                wrong_state_ids.setdefault(move.picking_id.name, {}).setdefault(move.state, []).append(move.id)
 
75
            else:
 
76
                ok_move_ids.append(move.id)
 
77
        if not ok_move_ids:
 
78
            problems = [_("No valid stock moves found to create the dispatch!"),
 
79
                        _("(Only move that are not part of a dispatch order and in "
 
80
                          "confirm, waiting or assigned state can be used)")]
 
81
            for dispatch_name, mvs in already_dispatched_ids.iteritems():
 
82
                mvs.sort()
 
83
                
 
84
                problems.append(_('Dispatch %s already covers moves %s') % \
 
85
                                (dispatch_name,
 
86
                                 u', '.join(['%s [%s]' % (mv, pck) for mv, pck in mvs]))
 
87
                                )
 
88
            for pck, states in wrong_state_ids.iteritems():
 
89
                for state, mvs in states.iteritems():
 
90
                    problems.append(_('Moves %s from picking %s are in state %s') % \
 
91
                                    (tuple(mvs), pck, state))
 
92
            raise osv.except_osv(_('Warning !'), u'\n'.join(problems))
 
93
 
63
94
        data = {'date': wiz.get('date'),
 
95
                'name': wiz.get('name'),
 
96
                'notes': wiz.get('notes'),
64
97
                }
65
98
        if wiz.get('picker_id'):
66
99
            data['picker_id'] = wiz.get('picker_id')[0]
67
100
        dispatch_id = dispatch_obj.create(cr, uid, data, context=context)
68
101
        # for move_id in move_ids:
69
 
        move_obj.write(cr, uid, move_ids, {'dispatch_id': dispatch_id}, context=context)
 
102
        move_obj.write(cr, uid, ok_move_ids, {'dispatch_id': dispatch_id}, context=context)
70
103
 
71
104
        return {'type': 'ir.actions.act_window_close'}
72
 
        # XXX display moves
73
 
        ## data_pool = self.pool.get('ir.model.data')
74
 
        ## filter_ids = data_pool.get_object_reference(cr, uid, 'product',
75
 
        ##                                             'product_search_form_view')
76
 
        ## product_view_id = data_pool.get_object_reference(cr, uid,
77
 
        ##                                                  'product_historical_margin',
78
 
        ##                                                  'view_product_historical_margin')
79
 
        ## if filter_ids:
80
 
        ##     filter_id = filter_ids[1]
81
 
        ## else:
82
 
        ##     filter_id = 0
83
 
        ## return {
84
 
        ##     'type': 'ir.actions.act_window',
85
 
        ##     'name': _('Historical Margins'),
86
 
        ##     'context': ctx,
87
 
        ##     'view_type': 'form',
88
 
        ##     'view_mode': 'tree',
89
 
        ##     'res_model': 'product.product',
90
 
        ##     'view_id': product_view_id[1],
91
 
        ##     'search_view_id': filter_id,
92
 
        ##     }