~credativ/openobject-addons/elico-7.0-fixes

« back to all changes in this revision

Viewing changes to product_stock_type/product.py

  • Committer:
  • Date: 2013-09-09 09:06:00 UTC
  • mfrom: (3.2.1 elico-7.0)
  • Revision ID: elicoidal@hotmail.com-20130909090600-ca5wvn9l8npx01w5
[ADD] several new modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (c) 2010-2013 Elico Corp. All Rights Reserved.
 
6
#    Author: Yannick Gouin <yannick.gouin@elico-corp.com>
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (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 Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv, fields
 
24
from tools.translate import _
 
25
import re
 
26
 
 
27
class product_stock_type(osv.osv):
 
28
    _name = "product.stock_type"
 
29
    _columns = {
 
30
        'name': fields.char('Name', size=32, translate=True),
 
31
        'code': fields.char('Code', size=12),
 
32
        #'location_ids': fields.one2many('stock.location', 'stock_type_id', _('Location for this stock type')),
 
33
    }
 
34
product_stock_type()
 
35
 
 
36
 
 
37
class product_template(osv.osv):
 
38
    _inherit = "product.template"
 
39
    _columns = {
 
40
        'stock_type_id': fields.many2one('product.stock_type', 'Stock Type', change_default=True),
 
41
        'name': fields.char('Name', size=128, translate=True, select=True),
 
42
        #'ul_id': fields.many2one('product.ul','Packaging'),
 
43
    }
 
44
product_template()
 
45
 
 
46
 
 
47
class product_product(osv.osv):
 
48
    _inherit = "product.product"
 
49
 
 
50
    def _name_en(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
 
51
        result = {}
 
52
        for product in self.browse(cursor, uid, ids, context=context):
 
53
            cursor.execute("SELECT name_template FROM product_product WHERE id = %s" % (product.id))
 
54
            name = cursor.fetchone()
 
55
            result[product.id] = name[0]
 
56
            # cursor.execute("SELECT name, name_template FROM product_product WHERE id = %s" % (product.id))
 
57
            # name = cursor.fetchone()
 
58
            # if name and name[0]:
 
59
            #     result[product.id] = name[0]
 
60
            # elif name and name[1]:
 
61
            #     result[product.id] = name[1]
 
62
        return result
 
63
 
 
64
 
 
65
    def _name_cn(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
 
66
        result = {}
 
67
        for product in self.browse(cursor, uid, ids, context=context):
 
68
            cursor.execute("SELECT name_template FROM product_product WHERE id = %s" % product.id)
 
69
            t_name = cursor.fetchone()
 
70
            t_ids = False
 
71
            if t_name and t_name[0]:
 
72
                value = re.sub("'", "''", t_name[0])
 
73
 
 
74
                cursor.execute("SELECT value FROM ir_translation WHERE lang = 'zh_CN' " + \
 
75
                 " AND name ='product.product,name' AND src = '%s' AND type = 'model' AND res_id = %s" % (value, product.id))
 
76
                t_ids = cursor.fetchone()
 
77
            if not t_ids:
 
78
                cursor.execute("SELECT value FROM ir_translation WHERE lang = 'zh_CN' " + \
 
79
                 " AND name ='product.product,name' AND type = 'model' AND res_id = %s" % (product.id))
 
80
                t_ids = cursor.fetchone()
 
81
            if t_ids:
 
82
                result[product.id] = t_ids[0]
 
83
        return result
 
84
 
 
85
 
 
86
    def _name_sort_en(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
 
87
        result = {}
 
88
        for product in self.browse(cursor, uid, ids, context=context):
 
89
            result[product.id] = product.name_en
 
90
        return result
 
91
    
 
92
    def _name_sort_cn(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
 
93
        result = {}
 
94
        for product in self.browse(cursor, uid, ids, context=context):
 
95
            result[product.id] = product.name_cn
 
96
        return result
 
97
    
 
98
 
 
99
    def _name_en_inv(self, cursor, user, id, name, value, arg, context=None):
 
100
        """ All the logic is in the onchange_name_en()
 
101
            Need to update the Translation table directly
 
102
            If default language is English, then only need to save the name (useless)
 
103
            If default language is Chinese, then we need to update the propduct_product table directly
 
104
        """
 
105
        if value:
 
106
            value = re.sub("'", "''", str(value.encode('utf-8')))
 
107
            cursor.execute("UPDATE product_template SET name = '%s' WHERE id = %s" % (value, id))
 
108
 
 
109
 
 
110
    def onchange_name_en(self, cursor, uid, ids, name_en):
 
111
        result = {}
 
112
        if name_en:
 
113
            # only do that in English
 
114
            obj = self.pool.get('res.users').browse(cursor, uid, uid)
 
115
            if obj.lang == 'en_US':
 
116
                result['name'] = name_en
 
117
        return result and {'value': result}
 
118
 
 
119
 
 
120
    def _name_cn_inv(self, cursor, user, id, name, value, arg, context=None):
 
121
        if value:
 
122
            tr_pool = self.pool.get('ir.translation')
 
123
            for product in self.browse(cursor, user, [id], context=context):
 
124
                cursor.execute("SELECT name_template FROM product_product WHERE id = %s" % id)
 
125
                t_name = cursor.fetchone()
 
126
                t_ids = False
 
127
                
 
128
                # Do we have a source: 'src'
 
129
                if t_name[0]:
 
130
                    t_ids = tr_pool.search(cursor, user,
 
131
                                     [('lang', '=', 'zh_CN'), ('name', '=', 'product.product,name'),
 
132
                                      ('type', '=', 'model'), ('src', '=', t_name[0]), ('res_id', '=', id)])
 
133
                
 
134
                # It did not work with a source, lets try without a source
 
135
                if not t_ids:
 
136
                    t_ids = tr_pool.search(cursor, user,
 
137
                                     [('lang', '=', 'zh_CN'), ('name', '=', 'product.product,name'),
 
138
                                      ('type', '=', 'model'), ('res_id', '=', id)])
 
139
 
 
140
                # Did we find something, with or without a source
 
141
                if t_ids:
 
142
                    tr_pool.write(cursor, user, t_ids, {'value': value})
 
143
                else:
 
144
                    vals = {
 
145
                       'lang': 'zh_CN',
 
146
                       'name': 'product.product,name',
 
147
                       'type': 'model',
 
148
                       'value': value,
 
149
                       'res_id':id,
 
150
                    }
 
151
                    if t_name[0]:
 
152
                        vals['src'] = t_name[0]
 
153
                    tr_pool.create(cursor, user, vals)
 
154
 
 
155
 
 
156
    _columns = {
 
157
        'packing_sequence': fields.integer('Packing Sequence'),
 
158
        'deliver_in':fields.selection([('normal',_('normal')),('iced',_('Iced')),('warm',_('Warm')),('cold',_('Cold')),('iced_n_warm',_('Warm & Cold'))],_('Website Product Deliver In'),help="Website Product Deliver In", size=128, required=True),
 
159
        'name_en':          fields.function(_name_en, method=True, type='char', size=128, string=_('Name EN'), fnct_inv=_name_en_inv),
 
160
        'name_cn':          fields.function(_name_cn, method=True, type='char', size=128, string=_('Name CN'), fnct_inv=_name_cn_inv),
 
161
        'name_sort_en':     fields.function(_name_sort_en, method=True, type='char', size=128, store={'product.product': (lambda self, cr, uid, ids, c={}: ids, ['name_en'], 10)}, string=_('Name EN')),
 
162
        'name_sort_cn':     fields.function(_name_sort_cn, method=True, type='char', size=128, store={'product.product': (lambda self, cr, uid, ids, c={}: ids, ['name_cn'], 10)}, string=_('Name CN')),
 
163
    }
 
164
 
 
165
    _defaults = {
 
166
       'deliver_in': 'normal',
 
167
    }
 
168
product_product()
 
169
 
 
170
class stock_move(osv.osv):
 
171
    _inherit = "stock.move"
 
172
    
 
173
    def _move_to_update_folowing_product_packing_sequence_change(self, cr, uid, ids, fields=None, arg=None, context=None):
 
174
        if type(ids) != type([]):
 
175
            ids = [ids]
 
176
        return self.pool.get('stock.move').search(cr, uid, [('product_id', 'in', ids)]) or []
 
177
    
 
178
    def _get_packing_sequence(self, cr, uid, ids, fields, args, context=None):
 
179
        result = {}
 
180
        for move in self.browse(cr, uid, ids, context=context):
 
181
           if move.product_id:
 
182
               result[move.id] = int(move.product_id.packing_sequence)
 
183
           else:
 
184
               result[move.id] = 0
 
185
        return result    
 
186
    
 
187
    _store_packing_sequence = {
 
188
        'stock.move': (lambda self, cr, uid, ids, context: ids, ['product_id'], 10),
 
189
        'product.product': (_move_to_update_folowing_product_packing_sequence_change, ['packing_sequence'], 10)
 
190
    }
 
191
    
 
192
    _columns = {
 
193
        'packing_sequence': fields.function(_get_packing_sequence, method=True, type='integer', store=_store_packing_sequence, string='Packing Sequence'),
 
194
    }
 
195
stock_move()
 
196
 
 
197
 
 
198
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: