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."""
31
31
_name = 'picking.dispatch.creator'
32
32
_description = 'Picking Dispatch Creator'
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'),
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
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,
43
53
def action_create_dispatch(self, cr, uid, ids, context=None):
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'])],
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 "
68
already_dispatched_ids = {}
70
for move in move_obj.browse(cr, uid, all_move_ids, context=context):
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)
76
ok_move_ids.append(move.id)
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():
84
problems.append(_('Dispatch %s already covers moves %s') % \
86
u', '.join(['%s [%s]' % (mv, pck) for mv, pck in mvs]))
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))
63
94
data = {'date': wiz.get('date'),
95
'name': wiz.get('name'),
96
'notes': wiz.get('notes'),
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)
71
104
return {'type': 'ir.actions.act_window_close'}
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')
80
## filter_id = filter_ids[1]
84
## 'type': 'ir.actions.act_window',
85
## 'name': _('Historical Margins'),
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,