~eoc/openobject-addons/eoc-extra-addons-6.1

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Stock Picking Simple Locations
#    Copyright (C) 2013 Mariano Ruiz <mrsarm@gmail.com>
#    Enterprise Objects Consulting (<http://www.eoconsulting.com.ar>)
#
#    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 openerp.osv import osv, fields
from openerp.tools.translate import _

class stock_picking(osv.osv):
    _inherit = 'stock.picking'

    def onchange_location_id(self, cr, uid, ids, location_id, picking_type, move_lines, context):
        if not move_lines or len(move_lines)==0:
            return {}
        res = {'value':{'move_lines': move_lines}}
        for move in move_lines:
            if not move[2]:
                move[2] = {}
            move[2]['location_id'] = location_id
        return res

    def onchange_location_dest_id(self, cr, uid, ids, location_dest_id, picking_type, move_lines, context):
        if not move_lines or len(move_lines)==0:
            return {}
        res = {'value':{'move_lines': move_lines}}
        for move in move_lines:
            if not move[2]:
                move[2] = {}
            move[2]['location_dest_id'] = location_dest_id
        return res

    def _default_location_source(self, cr, uid, context=None):
        location_id = context.get('location_id', False)
        if location_id:
            return location_id
        else:
            return self.pool.get("stock.move")._default_location_source(cr, uid, context=context)

    def _default_location_destination(self, cr, uid, context=None):
        location_dest_id = context.get('location_dest_id', False)
        if location_dest_id:
            return location_dest_id
        else:
            return self.pool.get("stock.move")._default_location_destination(cr, uid, context=context)

    def onchange_address_id(self, cr, uid, ids, address_id=None, context={}):
        """
        This method replace the bad formed 'onchange_partner_in' method, but
        to prevent compatibility problems, in the first line is invoked
        """
        res = self.onchange_partner_in(cr,uid,context,address_id)
        if 'value' not in res:
            value = {}
            res['value'] = value
        else:
            value = res['value']
        location_id = self._default_location_source(cr, uid, context=context)
        if location_id:
            value['location_id'] = location_id
        location_dest_id = self._default_location_destination(cr, uid, context=context)
        if location_dest_id:
            value['location_dest_id'] = location_dest_id
        return res

    def write(self, cr, uid, ids, vals, context={}):
        if not context or not context.get('no_upd_move_locs', False):
            vals2 = {}
            if 'location_id' in vals:
                vals2['location_id'] = vals['location_id']
            if 'location_dest_id' in vals:
                vals2['location_dest_id'] = vals['location_dest_id']
            if vals2 != {}:
                for pick in self.browse(cr,uid,ids,context):
                    move_ids = [move.id for move in pick.move_lines]
                    self.pool.get('stock.move').write(cr,uid,move_ids,vals2,context)
        return super(stock_picking, self).write(cr, uid, ids, vals, context)

    def copy(self, cr, uid, _id, default=None, context=None):
        if default is None:
            default = {}
        if context is None:
            context = {}
        default = default.copy()
        picking = self.browse(cr, uid, _id, context=context)
        # (src location, dest location) <=> (dest location, src location) in returns
        if '-return' in default.get('name', '') or context.get('return', False):
            if 'location_id' not in default:
                default['location_id'] = picking.location_dest_id.id
            if 'location_dest_id' not in default:
                default['location_dest_id'] = picking.location_id.id
        res = super(stock_picking, self).copy(cr, uid, _id, default, context)
        return res

    _columns = {
        'location_id': fields.many2one('stock.location', 'Source Location',
                                required=True, select=True,
                                states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
                                help="Sets a location if you produce at a fixed location. "
                                     "This can be a partner location if you subcontract the manufacturing operations. "
                                     "This will be the default of the asociated stock moves."),
        'location_dest_id': fields.many2one('stock.location', 'Destination Location',
                                required=True, select=True,
                                states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
                                help="Location where the system will stock the finished products. "
                                     "This will be the default of the asociated stock moves."),
    }

    _defaults = {
        'location_id': _default_location_source,
        'location_dest_id': _default_location_destination,
    }

stock_picking()

class stock_move(osv.osv):
    _inherit = 'stock.move'

    def onchange_product_id(self, cr, uid, ids, prod_id=False, loc_id=False,
                            loc_dest_id=False, address_id=False):
        result = super(stock_move,self).onchange_product_id(cr, uid, ids, prod_id,
                                            loc_id, loc_dest_id, address_id)
        if loc_id == False or loc_dest_id == False:
            raise osv.except_osv(_('No Locations Defined !'),
                    _("You must first select the Location and Destiny Location from the Picking") )
        return result

    def _default_location_source_2(self, cr, uid, context=None):
        location_id = context.get('location_id', False)
        if location_id:
            return location_id
        if 'location_id' in context:
            return False
        return super(stock_move, self)._default_location_source(cr, uid, context=context)

    def _default_location_destination_2(self, cr, uid, context=None):
        location_dest_id = context.get('location_dest_id', False)
        if location_dest_id:
            return location_dest_id
        if 'location_dest_id' in context:
            return False
        return super(stock_move, self)._default_location_destination(cr, uid, context=context)

    _defaults = {
        'location_id': _default_location_source_2,
        'location_dest_id': _default_location_destination_2,
    }

stock_move()