~unifield-team/unifield-wm/us-826

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) Copyright (C) 2011 MSF, TeMPO Consulting.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv
from tools.translate import _
import decimal_precision as dp

import netsvc


class enter_reason(osv.osv_memory):
    '''
    wizard called to split a memory stock move from create picking wizard
    '''
    _name = "enter.reason"
    _columns = {'picking_id': fields.many2one('stock.picking', string='Incoming Shipment', readonly=True),
                'change_reason': fields.char(string='Change Reason', size=1024),
                }
    _defaults = {'picking_id': lambda obj, cr, uid, c: c and c.get('picking_id', False),
                 }

    def do_cancel(self, cr, uid, ids, context=None):
        # quick integrity check
        assert context, 'No context defined, problem on method call'
        if isinstance(ids, (int, long)):
            ids = [ids]
        # objects
        picking_obj = self.pool.get('stock.picking')
        purchase_obj = self.pool.get('purchase.order')
        pol_obj = self.pool.get('purchase.order.line')
        # workflow
        wf_service = netsvc.LocalService("workflow")
        # depending on the button clicked the behavior is different
        cancel_type = context.get('cancel_type', False)
        # picking ids
        picking_ids = context['active_ids']
        # integrity check
        for obj in self.browse(cr, uid, ids, context=context):
            if not obj.change_reason:
                raise osv.except_osv(_('Error !'), _('You must specify a reason.'))
        
        # change reason
        data = self.read(cr, uid, ids, ['change_reason'], context=context)[0]
        change_reason = data['change_reason']
        values = {'change_reason': change_reason}
        # update the object
        for obj in picking_obj.browse(cr, uid, picking_ids, context=context):
            # purchase order line to re-source
            pol_ids = []
            pol_qty = {}
            # set the reason
            obj.write({'change_reason': change_reason}, context=context)

            for move in obj.move_lines:
                if move.state != 'cancel':
                    pol_ids.append(move.purchase_line_id.id)
                    pol_qty.setdefault(move.purchase_line_id.id, 0.00)
                    pol_qty[move.purchase_line_id.id] += move.product_qty

            # if full cancel (no resource), we updated corresponding out and correct po state
            if cancel_type == 'update_out':
                picking_obj.cancel_and_update_out(cr, uid, [obj.id], context=context)
            else:
                context['pol_qty'] = pol_qty
                pol_obj.write(cr, uid, pol_ids, {'has_to_be_resourced': True}, context=context)
                pol_obj.cancel_sol(cr, uid, pol_ids, context=context)
            
            # cancel the IN
            wf_service.trg_validate(uid, 'stock.picking', obj.id, 'button_cancel', cr)

            # correct the corresponding po manually if exists - should be in shipping exception
            if obj.purchase_id:
                wf_service.trg_validate(uid, 'purchase.order', obj.purchase_id.id, 'picking_ok', cr)
                purchase_obj.log(cr, uid, obj.purchase_id.id, _('The Purchase Order %s is %s%% received')%(obj.purchase_id.name, round(obj.purchase_id.shipped_rate,2)))

        return {'type': 'ir.actions.act_window_close'}
    
enter_reason()