~credativ/credativ-openerp/addons-7.0-purchase-arrival-auto-invoice

« back to all changes in this revision

Viewing changes to order_edit/order_edit.py

  • Committer: Craig Gowing (credativ)
  • Date: 2014-04-07 15:10:31 UTC
  • mfrom: (16.1.3 credativ-addons)
  • Revision ID: craig.gowing@credativ.co.uk-20140407151031-knw7m1uwdrpujh0w
[MERGE] Order edit module ported to 7 with improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
            return line_id
111
111
 
112
112
        line_moves = defaultdict(list)
 
113
        other_moves = defaultdict(list)
113
114
        for m in moves:
114
115
            if m.state in ['done', 'assigned']:
115
116
                line_id = add_product_order_line(m.product_id.id, m.product_qty)
116
117
                line_moves[line_id].append(m)
 
118
            else:
 
119
                other_moves[m.product_id.id].append(m)
117
120
 
 
121
        remain_moves = {}
118
122
        for product, edit_total in edit_totals.iteritems():
119
123
            remainder = edit_total - done_totals.get(product, 0)
120
124
            if remainder > 0:
121
 
                add_product_order_line(product.id, remainder)
 
125
                line_id = add_product_order_line(product.id, remainder)
 
126
                remain_moves[line_id] = other_moves.get(product.id)
122
127
 
123
 
        return line_moves
 
128
        return line_moves, remain_moves
124
129
 
125
130
    def check_consolidation(self, cr, uid, ids, context=None):
126
131
        # TODO: edit with available moves
128
133
        # need to change confirm button to run an action that warns user of the available move
129
134
        # and then trigger the workflow to continue the current behaviour
130
135
        line_moves = None
 
136
        remain_moves = None
131
137
        for order in self.browse(cr, uid, ids, context=context):
132
138
            original_ids = None
133
139
            if order.origin:
134
140
                original_ids = self.search(cr, uid, [('name', '=', order.origin)], context=context)
135
141
            if original_ids:
136
 
                line_moves = self._consolidate_edit_lines(cr, uid, original_ids,
 
142
                line_moves, remain_moves = self._consolidate_edit_lines(cr, uid, original_ids,
137
143
                                                          order, context)
138
 
        return line_moves
 
144
        return line_moves, remain_moves
139
145
 
140
146
    def copy_for_edit(self, cr, uid, id_, context=None):
141
147
        if context is None:
456
462
            wkf_service = netsvc.LocalService('workflow')
457
463
 
458
464
        #8 Cancel MO,create new MO and run procurement scheduler
459
 
        self._cancel_mo(cr,uid,original,context=context)
 
465
        if self.pool.get('mrp.production'):
 
466
            self._cancel_mo(cr,uid,original,context=context)
460
467
 
461
468
 
462
469
    def _unreconcile_refund_and_cancel(self, cr, uid, original_id, order, context=None):
480
487
        else:
481
488
            return False
482
489
 
483
 
    def _fixup_created_picking(self, cr, uid, line_moves, context):
 
490
    def _fixup_created_picking(self, cr, uid, line_moves, remain_moves, context):
484
491
        # This is a post confirm hook
485
492
        # - post-action hook: replace new stuff generated in the action with old stuff
486
493
        # identified in the pre-action hook
487
494
        move_pool = self.pool.get('stock.move')
 
495
        pick_pool = self.pool.get('stock.picking')
 
496
        wf_service = netsvc.LocalService("workflow")
488
497
 
489
498
        if line_moves is not None:
490
499
            for line_id, old_moves in line_moves.iteritems():
495
504
                        created_move = created_moves.pop()
496
505
                    except IndexError:
497
506
                        raise osv.except_osv(_('Error!'), _('The edited order must include any done or assigned moves'))
 
507
                    # Move old stock_move and stock_picking to new order
498
508
                    picking = created_move.picking_id
499
 
                    move_pool.write(cr, uid, [old_move.id], {'sale_line_id': line_id, 'picking_id': picking.id})
 
509
                    move_pool.write(cr, uid, [old_move.id], {'sale_line_id': line_id})
 
510
                    pick_pool.write(cr, uid, old_move.picking_id.id, {'sale_id':line.order_id.id})
 
511
                    # Cancel and remove new replaced stock_move and stock_picking
500
512
                    move_pool.write(cr, uid, created_move.id, {'sale_line_id': False, 'picking_id': False})
501
513
                    created_move.action_cancel()
502
 
                    picking.action_done()
 
514
                    picking.refresh()
 
515
                    if not picking.move_lines:
 
516
                        pick_pool.write(cr, uid, picking.id, {'sale_id': False})
 
517
                        wf_service.trg_validate(uid, 'stock.picking', picking.id, 'button_cancel', cr)
 
518
                        wf_service.trg_validate(uid, 'stock.picking', picking.id, 'button_cancel', cr)
 
519
                        pick_pool.action_cancel(cr, uid, [picking.id])
503
520
                assert(len(created_moves) == 0)
504
521
 
 
522
        if remain_moves is not None:
 
523
            picking = None
 
524
            old_picking_copy = None
 
525
            for line_id, old_moves in remain_moves.iteritems():
 
526
                line = self.pool.get('sale.order.line').browse(cr, uid, line_id)
 
527
                created_moves = [x for x in line.move_ids]
 
528
                if not picking and not old_picking_copy:
 
529
                    picking = old_moves and old_moves[0].picking_id or None
 
530
                    if picking:
 
531
                        old_picking_copy = pick_pool.copy(cr, uid, picking.id, {'move_lines': [], 'sale_id': line.order_id.id, 'name': '/'})
 
532
                if not old_picking_copy or not created_moves:
 
533
                    continue
 
534
                for created_move in created_moves:
 
535
                    new_picking = created_move.picking_id
 
536
                    move_pool.write(cr, uid, created_move.id, {'sale_line_id': line_id, 'picking_id': old_picking_copy})
 
537
                    new_picking.refresh()
 
538
                    if not new_picking.move_lines:
 
539
                        pick_pool.write(cr, uid, new_picking.id, {'sale_id': False})
 
540
                        wf_service.trg_validate(uid, 'stock.picking', new_picking.id, 'button_cancel', cr)
 
541
                        wf_service.trg_validate(uid, 'stock.picking', new_picking.id, 'button_cancel', cr)
 
542
                        pick_pool.action_cancel(cr, uid, [new_picking.id])
 
543
            if old_picking_copy:
 
544
                wf_service.trg_validate(uid, 'stock.picking', old_picking_copy, 'button_confirm', cr)
 
545
            # Old confirmed moves get canceled during refund
 
546
 
505
547
    def action_ship_create(self, cr, uid, ids, context=None):
506
548
        # run on order confirm, after action_wait
507
549
 
508
550
#        # Sale order edit
509
551
#        # -  pre-action hook: find what has been edited
510
 
        line_moves = self.check_consolidation(cr, uid, ids, context)
 
552
        line_moves, remain_moves = self.check_consolidation(cr, uid, ids, context)
511
553
 
512
554
        # -    action: run original action
513
555
        res = super(sale_order, self).action_ship_create(cr, uid, ids, context=context)
514
556
 
515
557
#        # - post-action hook: replace new stuff generated in the action with old stuff
516
 
        self._fixup_created_picking(cr, uid, line_moves, context)
 
558
        self._fixup_created_picking(cr, uid, line_moves, remain_moves, context)
517
559
 
518
560
        for order in self.browse(cr, uid, ids, context=context):
519
561
            original_id = self.get_edit_original(cr, uid, order, context=context)