~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
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
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 TeMPO Consulting, MSF 
#
#    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 osv, fields
from tools.translate import _


class stock_move_tracking(osv.osv_memory):
    _name = 'stock.move.tracking'
    _description = 'Stock Move Tracking'
    
    _columns= {
        'product_id': fields.many2one('product.product', string='Product'),
#        'prodlot_id': fields.char(size=64, string='Batch number'),
        'prodlot_id': fields.many2one('stock.production.lot', string='Batch number'),
        'expired_date': fields.date('Expired date'),
    }
    
    def get_ids(self, cr, uid, ids, context={}):
        '''
        Returns all stock moves according to parameters
        '''
        move_obj = self.pool.get('stock.move')
        
        res = []
        lot_ids = False
        for track in self.browse(cr, uid, ids):
            if not track.product_id and not track.prodlot_id and not track.expired_date:
                raise osv.except_osv(_('Error'), _('You should at least enter one information'))
            
            domain = []
            if track.expired_date:
                # Add two lines in domain because we cannot compare equality between date and datetime
                domain.append(('expired_date', '>=', track.expired_date))
                domain.append(('expired_date', '<=', track.expired_date))
            if track.product_id:
                domain.append(('product_id', '=', track.product_id.id))
            if track.prodlot_id:
                domain.append(('prodlot_id', '=', track.prodlot_id.id))
#                # Search all batch begining with the string
#                lot_ids = self.pool.get('stock.production.lot').search(cr, uid, [('name', '=like', '%s%%' %track.prodlot_id)]) 
#                domain.append(('prodlot_id', 'in', lot_ids))
            
            res.extend(move_obj.search(cr, uid, domain, order='date'))
            
        return res, lot_ids
    
    def print_report(self, cr, uid, ids, context={}):
        '''
        Print the report as PDF file
        '''
        product_name = False
        product_code = False
        prodlot_id = False
        expired_date = False
        
        for track in self.browse(cr, uid, ids):
            if track.product_id:
                product_name = track.product_id and track.product_id.name or False
                product_code = track.product_id and track.product_id.default_code or False
            prodlot_id = track.prodlot_id
            expired_date = track.expired_date
        
        data = {
                'product_id': product_name,
                'product_code': product_code,
                'prodlot_id': prodlot_id,
                'expired_date': expired_date}
       
        move_ids = self.get_ids(cr, uid, ids, context=context)
        if not move_ids:
            raise osv.except_osv(_('Warning !'), _('Your search did not match with any moves'))
        datas = {'ids': move_ids[0],
                 'model': 'stock.move',
                 'form': data}

        return {'type': 'ir.actions.report.xml',
                'report_name': 'tracking.move.report',
                'datas': datas}
    
    def print_view(self, cr, uid, ids, context={}):
        '''
        Print the report on Web client (search view)
        '''
        mod_obj = self.pool.get('ir.model.data')
        act_obj = self.pool.get('ir.actions.act_window')

        result = mod_obj._get_id(cr, uid, 'stock_move_tracking', 'action_stock_move_tracking')
        id = mod_obj.read(cr, uid, [result], ['res_id'], context=context)[0]['res_id']
        
        result = act_obj.read(cr, uid, [id], context=context)[0]
        
        res_ids = self.get_ids(cr, uid, ids, context=context)
        result['domain'] = [('id', 'in', res_ids[0])]
        
        if res_ids[1]:
            result['context'] = {'search_default_group_product': 1}
            if type(res_ids[1]) == type([]) and len(res_ids[1]) > 0:
                result['context'].update({'search_default_groupby_prodlot_id': 1,})
        
        return result
    
stock_move_tracking()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: