~openerp-community/openobject-addons/trunk-addons-community

277 by omar
[ADD] Many Pexego modules related to stock traceability and stock management
1
# -*- encoding: utf-8 -*-
2
##############################################################################
3
#
4
#    OpenERP, Open Source Management Solution
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
#    $Id$
7
#
8
#    This program is free software: you can redistribute it and/or modify
9
#    it under the terms of the GNU General Public License as published by
10
#    the Free Software Foundation, either version 3 of the License, or
11
#    (at your option) any later version.
12
#
13
#    This program is distributed in the hope that it will be useful,
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
#    GNU General Public License for more details.
17
#
18
#    You should have received a copy of the GNU General Public License
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
#
21
##############################################################################
22
23
"""inherits fromk stock_move for checks blocked prodlots"""
24
25
from osv import osv, fields
26
from tools.translate import _
27
28
class stock_move(osv.osv):
29
    """inherits fromk stock_move for checks blocked prodlots"""
30
    _inherit = "stock.move"
31
32
    def get_move_in_location(self, cr, uid, location_id, prodlot_id, qty = 0):
33
        """returns the move in location"""
34
        moves = super(stock_move, self).get_move_in_location(cr, uid, location_id, prodlot_id, qty=qty)
35
36
        if moves:
37
            ids_to_remove = []
38
            for move in self.browse(cr, uid, moves):
39
                if move.location_dest_id.usage == 'elimination':
40
                    ids_to_remove.append(move.id)
41
            ids = list(set(moves) - set(ids_to_remove))
42
            return ids
43
        else:
44
            return moves
45
46
    def _check_prodlot_on_alert(self, cr, uid, ids):
47
        """checks if prodlot is on_alert and is trying move it to internal or customer location"""
48
        for move in self.browse(cr, uid, ids):
49
            if move.prodlot_id and move.location_dest_id:
50
                if move.prodlot_id.in_alert and move.location_dest_id.usage in ['internal', 'customer'] and move.state in ['assigned', 'done']:
51
                    return False
52
        return True
53
54
    def _check_prodlot_blocked(self, cr, uid, ids):
55
        """checks if prodlot is blocked and is trying move it to internal or customer location"""
56
        for move in self.browse(cr, uid, ids):
57
            if move.prodlot_id and move.location_dest_id:
58
                if move.prodlot_id.blocked and move.location_dest_id.usage in ['internal', 'customer'] and move.state in ['assigned', 'done'] and move.location_id.usage != 'customer':
59
                    return False
60
        return True
61
62
    def _default_location_destination(self, cr, uid, context={}):
63
        """obtiene la ubicación de destino por defecto, si es de tipo devolcuión obtendrá devolución"""
64
        if context.get('move_line', []):
65
            if context['move_line'][0]:
66
                if isinstance(context['move_line'][0], (tuple, list)):
67
                    return context['move_line'][0][2] and context['move_line'][0][2]['location_dest_id'] or False
68
                else:
69
                    move_list = self.pool.get('stock.move').read(cr, uid, context['move_line'][0], ['location_dest_id'])
70
                    return move_list and move_list['location_dest_id'][0] or False
71
        if context.get('address_out_id', False):
72
            if 'type' in context:
73
                if context['type'] == 'return':
74
                    return self.pool.get('res.partner.address').browse(cr, uid, context['address_out_id']).partner_id.property_stock_returns.id
75
            if self.pool.get('res.partner.address').browse(cr, uid, context['address_out_id'], context).partner_id.property_stock_customer:
76
                return self.pool.get('res.partner.address').browse(cr, uid, context['address_out_id'], context).partner_id.property_stock_customer.id
77
        return False
78
79
    def _default_location_source(self, cr, uid, context={}):
80
        """obtiene la ubicación de origen por defecto, si es de tipo devolucion cogerá la del cliente"""
81
        if context.get('move_line', []):
82
            try:
83
                return context['move_line'][0][2]['location_id']
84
            except:
85
                pass
86
        if 'type' in context and 'address_out_id' in context:
87
            if context['type'] == 'return':
88
                return self.pool.get('res.partner.address').browse(cr, uid, context['address_out_id']).partner_id.property_stock_customer.id
89
90
        if context.get('address_in_id', False):
91
            return self.pool.get('res.partner.address').browse(cr, uid, context['address_in_id'], context).partner_id.property_stock_supplier.id
92
        return False
93
94
    _columns = {
95
                'towaste': fields.boolean('Eliminated', readonly=True),
96
                }
97
98
    _defaults = {
99
        'location_id': _default_location_source,
100
        'location_dest_id': _default_location_destination
101
    }
102
103
    _constraints = [(_check_prodlot_on_alert, 'Cannot move a production lot in alert to internal or customer location', ['in_alert']),
104
    (_check_prodlot_blocked, 'Cannot move a blocked production lot to internal or customer location', ['blocked'])
105
    ]
106
107
    def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False, loc_id=False, context={}):
108
        """overwrites this event for shows a warning if the production lot selected is on alert"""
109
        if not prodlot_id or not loc_id:
110
            return {}
111
        res = super(stock_move, self).onchange_lot_id(cr, uid, ids, prodlot_id = prodlot_id, product_qty = product_qty, loc_id = loc_id, context = context)
112
        if 'warning' in res and len(res['warning']) > 0:
113
            return {'warning': res['warning'], 'value': {'prodlot_id': False}}
114
        else:
115
            obj_prodlot_id = self.pool.get('stock.production.lot').browse(cr, uid, prodlot_id)
116
            if obj_prodlot_id.in_alert:
117
                res['warning'] = {
118
                    'title': _('Production Lot in Alert!'),
119
                    'message': _('This production lot is on alert because any of compounds are in poor condition'),
120
                        }
121
                return {'warning': res['warning']}
122
            elif obj_prodlot_id.blocked:
123
                res['warning'] = {
124
                    'title': _('Production Lot Blocked!'),
125
                    'message': _('This production lot is blocked because it is poor condition'),
126
                        }
127
                return {'warning': res['warning'], 'value': {'prodlot_id': False}}
128
        return res
129
130
    def action_done(self, cr, uid, ids, context=None):
131
        """overwrites this method for that to waste product be marked that eliminated"""
132
        for move in self.browse(cr, uid, ids):
133
            if move.location_dest_id.id == move.product_id.product_tmpl_id.property_waste.id:
134
                self.write(cr, uid, move.id, {'towaste': True})
135
        return super(stock_move, self).action_done(cr, uid, ids, context)
136
137
stock_move()