112
112
def action_done(self, cr, uid, ids, context=None):
113
115
move_obj = self.pool.get('stock.move')
114
116
move_ids = move_obj.search(cr, uid, [('dispatch_id', 'in', ids)], context=context)
115
117
return move_obj.action_partial_move(cr, uid, move_ids, context)
119
def check_finished(self, cr, uid, ids, context):
120
"""set the dispatch to finished if all the moves are finished"""
124
for dispatch in self.browse(cr, uid, ids, context=context):
125
if all(move.state in ('cancel', 'done') for move in dispatch.move_ids):
126
finished.append(dispatch.id)
127
_logger.debug('set state to done for picking.dispatch %s', finished)
128
self.write(cr, uid, finished, {'state': 'done'}, context)
117
131
def action_cancel(self, cr, uid, ids, context=None):
118
132
move_obj = self.pool.get('stock.move')
119
133
move_ids = move_obj.search(cr, uid, [('dispatch_id', 'in', ids)], context=context)
128
142
now = datetime.now().date()
129
143
for obj in self.browse(cr, uid, ids, context):
130
144
date = datetime.strptime(obj.date, '%Y-%m-%d').date()
132
146
raise except_osv(_('Error'),
133
147
_('This dispatch cannot be processed until %s') % obj.date)
158
172
# so the difference between the original set of moves and the complete_moves is the
159
173
# set of unprocessed moves
160
174
unprocessed_move_ids = set(ids) - set(complete_move_ids)
175
_logger.debug('partial stock.moves: complete_move_ids %s, unprocessed_move_ids %s', complete_move_ids, unprocessed_move_ids)
161
176
# unprocessed moves are still linked to the dispatch : this dispatch must not be marked as Done
162
177
unfinished_dispatch_ids = {}
163
178
for move in move_obj.browse(cr, uid, list(unprocessed_move_ids), context=context):
164
179
if not move.dispatch_id:
166
181
unfinished_dispatch_ids[move.dispatch_id.id] = None # value will be set later to a new dispatch
167
finished_dispatches = {}
182
maybe_finished_dispatches = {}
168
183
for move in move_obj.browse(cr, uid, complete_move_ids, context=context):
169
184
if not move.dispatch_id:
181
196
unfinished_dispatch_ids[dispatch_id] = new_dispatch_id
182
197
dispatch_id = unfinished_dispatch_ids[dispatch_id]
183
finished_dispatches.setdefault(dispatch_id, []).append(move.id)
184
for dispatch_id, move_ids in finished_dispatches.iteritems():
198
maybe_finished_dispatches.setdefault(dispatch_id, []).append(move.id)
199
for dispatch_id, move_ids in maybe_finished_dispatches.iteritems():
185
200
move_obj.write(cr, uid, move_ids, {'dispatch_id': dispatch_id})
186
if finished_dispatches:
187
_logger.debug('set state to done for picking.dispatch %s', list(finished_dispatches))
188
dispatch_obj.write(cr, uid, list(finished_dispatches), {'state': 'done'}, context)
201
dispatch_obj.check_finished(cr, uid, list(maybe_finished_dispatches), context)
189
202
return complete_move_ids
226
239
for move in self.browse(cr, uid, ids, context=context):
227
240
if move.dispatch_id:
228
241
dispatches.add(move.dispatch_id.id)
229
for dispatch in dispatch_obj.browse(cr, uid, list(dispatches), context=context):
230
if any(move.state not in ('cancel', 'done') for move in dispatch.move_ids):
231
dispatches.remove(dispatch.id)
233
_logger.debug('set state to done for picking.dispatch %s', list(dispatches))
234
dispatch_obj.write(cr, uid, list(dispatches), {'state': 'done'}, context)
242
dispatch_obj.check_finished(cr, uid, list(dispatches), context)