-
-
Notifications
You must be signed in to change notification settings - Fork 690
/
product_product.py
86 lines (72 loc) · 2.76 KB
/
product_product.py
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
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# 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 import models, fields, api
from openerp.tools.translate import _
def update_null_and_slash_codes(cr): # pragma: no cover
"""
Updates existing codes matching the default '/' or
empty. Primarily this ensures installation does not
fail for demo data.
:param cr: database cursor
:return: void
"""
cr.execute("UPDATE product_product "
"SET default_code = '!!mig!!' || id "
"WHERE default_code IS NULL OR default_code = '/';")
class ProductTemplate(models.Model):
_inherit = 'product.template'
default_code = fields.Char(required=True)
class ProductProduct(models.Model):
_inherit = 'product.product'
default_code = fields.Char(
string='Reference',
size=64,
select=True,
required=True,
default='/')
_sql_constraints = [
('uniq_default_code',
'unique(default_code)',
'The reference must be unique'),
]
@api.model
def create(self, vals):
if 'default_code' not in vals or vals['default_code'] == '/':
vals['default_code'] = self.env['ir.sequence'].get(
'product.product')
return super(ProductProduct, self).create(vals)
@api.multi
def write(self, vals):
for product in self:
if product.default_code in [False, '/']:
vals['default_code'] = self.env['ir.sequence'].get(
'product.product')
super(ProductProduct, product).write(vals)
return True
@api.multi
def copy(self, default=None):
if default is None:
default = {}
if self.default_code:
default.update({
'default_code': self.default_code + _('-copy'),
})
return super(ProductProduct, self).copy(default)