-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.py
294 lines (242 loc) · 10.8 KB
/
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from datetime import datetime
from sql.operators import (Less, Greater, LessEqual,
GreaterEqual, Equal, NotEqual)
from sql import Null
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.modules.agronomics.wine import WineMixin
from trytond.transaction import Transaction
class Certification(ModelSQL, ModelView):
"Certification"
__name__ = 'agronomics.certification'
_rec_name = 'number'
number = fields.Char('Number')
date = fields.Date('Date')
def get_rec_name(self, name):
return self.number
class Container(ModelSQL, ModelView):
"Container"
__name__ = 'agronomics.container'
name = fields.Char('Name')
capacity = fields.Numeric('Capacity', digits=(16, 2))
class ProductConfiguration(metaclass=PoolMeta):
__name__ = 'product.configuration'
variant_deactivation_time = fields.TimeDelta("Variant Deactivation Time")
class Template(metaclass=PoolMeta):
__name__ = 'product.template'
needs_sample = fields.Boolean('Needs Samples')
agronomic_type = fields.Selection([
(None, ''),
('grape', "Grape"),
('do-wort', "DO Wort"),
('not-do-wort', "Not DO Wort"),
('unfiltered-wine', 'Unfiltered Wine'),
('filtered-wine', 'Filtered Wine'),
('clarified-wine', 'Clarified Wine'),
('wine', "Wine"),
('bottled-wine', "Bottled Wine"),
], "Agronomic Type")
container = fields.Many2One('agronomics.container', 'Container',
states={
'invisible': Eval('agronomic_type') != 'bottled-wine',
})
capacity = fields.Function(fields.Numeric('Capacity', digits=(16, 2),
states={
'invisible': Eval('agronomic_type') != 'bottled-wine',
}), 'get_capacity',
searcher='search_capacity')
quality_weighing = fields.Many2One('quality.template', 'Quality Weighing')
variant_deactivate_stock_zero = fields.Boolean("Variant Deactivate Stock 0")
def get_capacity(self, name):
if self.container:
return self.container.capacity
@classmethod
def search_capacity(cls, name, clause):
return [('container.capacity',) + tuple(clause[1:])]
class ProductVariety(ModelSQL, ModelView):
'Product Variety'
__name__ = 'product.variety'
variety = fields.Many2One('product.taxon', 'Variety', required=True)
percent = fields.Float('Percent', digits=(16, 4), required=True)
product = fields.Many2One('product.product', 'Product', required=True)
def get_rec_name(self, name):
return f'{self.variety.rec_name} ({self.percent:.0f}%)'
class Product(WineMixin, metaclass=PoolMeta):
__name__ = 'product.product'
vintages = fields.Many2Many('product.product-agronomics.crop', 'product',
'crop', 'Vintages')
varieties = fields.One2Many('product.variety', 'product', 'Varieties')
denominations_of_origin = fields.Many2Many(
'product.product-agronomics.denomination_of_origin', 'product',
'do', 'DOs',
states={
'invisible': Eval('agronomic_type').in_(
['not-do-wort']
)
})
ecologicals = fields.Many2Many('product.product-agronomics.ecological',
'product', 'ecological', 'Ecologicals')
certification = fields.Many2One('agronomics.certification',
'Certification', states={
'invisible': ~ Eval('agronomic_type').in_(
['wine', 'unfiltered-wine', 'filtered-wine', 'clarified-wine',
'bottled-wine']
)
})
alcohol_volume = fields.Function(fields.Numeric('Alcohol Volume',
digits=(16, 2), states={
'invisible': ~ Eval('agronomic_type').in_(
['wine', 'unfiltered-wine', 'filtered-wine', 'clarified-wine',
'bottled-wine']
)}), 'get_alcohol_volume')
quality_tests = fields.One2Many('quality.test', 'document', 'Quality Tests')
quality_samples = fields.Many2Many('product.product-quality.sample',
'product', 'sample', 'Quality Samples')
wine_aging = fields.One2Many('product.wine.wine_aging.history', 'product',
"Wine Aging", readonly=True,
context={
'product': Eval('id', -1),
}, depends=['id'])
wine_history_material = fields.Function(fields.Text("History Material"),
'get_wine_history', searcher='search_wine_history')
wine_history_duration = fields.Function(fields.Text("History Duration"),
'get_wine_history', searcher='search_wine_history')
vintages_str = fields.Function(fields.Char("Vintage"), 'get_vintages_str')
def get_vintages_str(self, name):
return ', '.join([v.name for v in self.vintages])
@classmethod
def deactivate_no_stock_variants_cron(cls):
pool = Pool()
Location = pool.get('stock.location')
ProductConfiguration = pool.get('product.configuration')
WineAgingHistory = pool.get('wine.wine_aging.history')
Date = pool.get('ir.date')
today = Date.today()
config = ProductConfiguration(1)
locations = Location.search(['type', '=', 'warehouse'])
locations = [location.id for location in locations]
with Transaction().set_context(locations=locations, with_childs=True):
if config.variant_deactivation_time is not None:
products = cls.search(
[
('quantity', '=', 0),
('template.variant_deactivate_stock_zero', '=', True),
('create_date', '<',
(datetime.now() - config.variant_deactivation_time))
])
if products:
cls.write(products, {'active': False})
histories = WineAgingHistory.search([
('product', 'in', products),
('date_end', '=', None),
])
if histories:
WineAgingHistory.write(histories, {'date_end': today})
@classmethod
def validate(cls, products):
for product in products:
if (product.agronomic_type in
['grape', 'do-wort', 'not-do-wort', 'bottled-wine']):
if len(product.vintages) > 1:
raise UserError(gettext('agronomics.msg_vintage_limit',
product=product.rec_name))
if product.agronomic_type == 'grape':
if len(product.varieties) > 1:
raise UserError(gettext('agronomics.msg_variety_limit',
product=product.rec_name))
def get_alcohol_volume(self, name):
if self.template.capacity and self.wine_alcohol_content:
return Decimal(
(float(self.template.capacity) * float(self.wine_alcohol_content))
/ 100).quantize(
Decimal(str(10 ** -self.__class__.alcohol_volume.digits[1])))
def get_wine_history(self, name):
# not implemented
pass
@classmethod
def search_wine_history(cls, name, clause):
pool = Pool()
WineAgingHistory = pool.get('wine.wine_aging.history')
Material = pool.get('stock.location.material')
wineaginghistory = WineAgingHistory.__table__()
material = Material.__table__()
Operator = fields.SQL_OPERATORS[clause[1]]
value = clause[2]
join1 = wineaginghistory.join(material,
condition=wineaginghistory.material == material.id)
query = join1.select(wineaginghistory.product)
if name == 'wine_history_material':
query.where = (Operator(material.name, clause[2])
& (wineaginghistory.material != Null)
& (wineaginghistory.duration != Null))
elif name == 'wine_history_duration':
operator = clause[1]
try:
value = int(value)
except (ValueError, TypeError):
value = None
if value:
if operator == '=':
query.where = Equal(wineaginghistory.duration, value)
elif operator == '!=':
query.where = NotEqual(wineaginghistory.duration, value)
elif operator == '>':
query.where = Greater(wineaginghistory.duration, value)
elif operator == '>=':
query.where = GreaterEqual(wineaginghistory.duration, value)
elif operator == '<':
query.where = Less(wineaginghistory.duration, value)
elif operator == '<=':
query.where = LessEqual(wineaginghistory.duration, value)
return [('id', 'in', query)]
def get_rec_name(self, name):
rec_name = super().get_rec_name(name)
if not self.vintages:
return rec_name
aging = ",".join(x.name for x in self.vintages)
rec_name = rec_name + "-" + aging
return rec_name
class Cron(metaclass=PoolMeta):
__name__ = 'ir.cron'
@classmethod
def __setup__(cls):
super(Cron, cls).__setup__()
cls.method.selection.append(
('product.product|deactivate_no_stock_variants_cron',
"Deactivate Variants"),
)
class ProductCrop(ModelSQL):
"Product - Crop"
__name__ = 'product.product-agronomics.crop'
product = fields.Many2One('product.product', 'Product',
ondelete='CASCADE', required=True)
crop = fields.Many2One('agronomics.crop', 'Crop',
ondelete='CASCADE', required=True)
class ProductDO(ModelSQL):
"Product - DO"
__name__ = 'product.product-agronomics.denomination_of_origin'
product = fields.Many2One('product.product', 'Product',
ondelete='CASCADE', required=True)
do = fields.Many2One('agronomics.denomination_of_origin', 'DO',
ondelete='CASCADE', required=True)
class ProductEcological(ModelSQL):
"Product - Ecological"
__name__ = 'product.product-agronomics.ecological'
product = fields.Many2One('product.product', 'Product',
ondelete='CASCADE', required=True)
ecological = fields.Many2One('agronomics.ecological', 'Ecological',
ondelete='CASCADE', required=True)
class ProductPriceListType(ModelSQL, ModelView):
"Product Price List Type"
__name__ = 'product.price_list.type'
name = fields.Char("Name", required=True)
class PriceList(metaclass=PoolMeta):
__name__ = 'product.price_list'
product_price_list_type = fields.Many2One('product.price_list.type',
"Product Price List Type")