1
# -*- coding: utf-8 -*-
2
##############################################################################
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>
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.
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.
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/>.
21
##############################################################################
23
from osv import osv, fields
24
from tools.translate import _
27
class product_stock_type(osv.osv):
28
_name = "product.stock_type"
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')),
37
class product_template(osv.osv):
38
_inherit = "product.template"
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'),
47
class product_product(osv.osv):
48
_inherit = "product.product"
50
def _name_en(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
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]
65
def _name_cn(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
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()
71
if t_name and t_name[0]:
72
value = re.sub("'", "''", t_name[0])
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()
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()
82
result[product.id] = t_ids[0]
86
def _name_sort_en(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
88
for product in self.browse(cursor, uid, ids, context=context):
89
result[product.id] = product.name_en
92
def _name_sort_cn(self, cursor, uid, ids, fields, arg, context=None, maxlength=2048):
94
for product in self.browse(cursor, uid, ids, context=context):
95
result[product.id] = product.name_cn
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
106
value = re.sub("'", "''", str(value.encode('utf-8')))
107
cursor.execute("UPDATE product_template SET name = '%s' WHERE id = %s" % (value, id))
110
def onchange_name_en(self, cursor, uid, ids, 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}
120
def _name_cn_inv(self, cursor, user, id, name, value, arg, context=None):
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()
128
# Do we have a source: 'src'
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)])
134
# It did not work with a source, lets try without a source
136
t_ids = tr_pool.search(cursor, user,
137
[('lang', '=', 'zh_CN'), ('name', '=', 'product.product,name'),
138
('type', '=', 'model'), ('res_id', '=', id)])
140
# Did we find something, with or without a source
142
tr_pool.write(cursor, user, t_ids, {'value': value})
146
'name': 'product.product,name',
152
vals['src'] = t_name[0]
153
tr_pool.create(cursor, user, vals)
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')),
166
'deliver_in': 'normal',
170
class stock_move(osv.osv):
171
_inherit = "stock.move"
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([]):
176
return self.pool.get('stock.move').search(cr, uid, [('product_id', 'in', ids)]) or []
178
def _get_packing_sequence(self, cr, uid, ids, fields, args, context=None):
180
for move in self.browse(cr, uid, ids, context=context):
182
result[move.id] = int(move.product_id.packing_sequence)
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)
193
'packing_sequence': fields.function(_get_packing_sequence, method=True, type='integer', store=_store_packing_sequence, string='Packing Sequence'),
198
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: