-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbom.py
290 lines (241 loc) · 9.61 KB
/
bom.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
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import datetime
from trytond.model import ModelView, Unique, Check, fields
from trytond.wizard import Wizard, StateView, Button, StateAction
from trytond.transaction import Transaction
from trytond.pyson import PYSONEncoder, Bool, Date, Eval, If
from trytond.pool import Pool, PoolMeta
from trytond.i18n import gettext
from trytond.exceptions import UserError, UserWarning
class BOM(metaclass=PoolMeta):
__name__ = 'production.bom'
start_date = fields.Date('Start Date', required=True)
end_date = fields.Date('End Date')
version = fields.Integer('Version', readonly=True)
master_bom = fields.Many2One('production.bom', 'BOM', readonly=True)
reason_change = fields.Text('Reason for Change')
modification_made = fields.Text('Modification Made')
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_constraints += [
('report_code_uniq', Unique(t, t.master_bom, t.version),
'production_bom_versions.msg_bom_report_code_uniq'),
('end_date_check',
Check(t, ((t.end_date == None) | (t.end_date > t.start_date))),
'production_bom_versions.msg_bom_end_date_check'),
]
cls._order.insert(0, ('version', 'DESC NULLS LAST'))
@staticmethod
def default_version():
return 1
@staticmethod
def default_start_date():
pool = Pool()
Date = pool.get('ir.date')
return Date.today()
def get_rec_name(self, name):
rec_name = super().get_rec_name(name)
if self.version:
rec_name += " (%s)" % self.version
return rec_name
@classmethod
def get_last_version(cls, master_bom):
'''
Get latest version for master_bom
'''
with Transaction().set_context(show_versions=True):
boms = cls.search([
('master_bom', '=', master_bom),
], order=[
('version', 'DESC')
], limit=1)
if boms:
return boms[0]
@classmethod
def validate(cls, boms):
super(BOM, cls).validate(boms)
for bom in boms:
bom.check_dates()
def check_dates(self):
if not self.master_bom:
return
with Transaction().set_context(show_versions=True):
domain = [
('master_bom', '=', self.master_bom.id),
('id', '!=', self.id),
]
if not self.end_date:
domain.append(['OR', [
('end_date', '=', None),
], [
('end_date', '>', self.start_date),
]
])
else:
domain.append(('start_date', '<', self.end_date))
domain.append(['OR', [
('end_date', '=', None),
], [
('end_date', '>', self.start_date),
]
])
with Transaction().set_context(show_versions=True):
boms = self.search(domain, limit=1)
if boms:
raise UserError(gettext('production_bom_versions.'
'msg_invalid_dates',
bom=self.rec_name,
version=boms[0].version))
@classmethod
def create(cls, vlist):
boms = super(BOM, cls).create(vlist)
for bom in boms:
if not bom.master_bom:
bom.master_bom = bom
bom.save()
return boms
@classmethod
def copy(cls, boms, default=None):
if default is None:
default = {}
else:
default = default.copy()
if not Transaction().context.get('new_version', False):
default['master_bom'] = None
default['version'] = cls.default_version()
return super(BOM, cls).copy(boms, default=default)
new_boms = []
for bom in boms:
last_bom = cls.get_last_version(bom.master_bom)
default['version'] = last_bom.version + 1
new_boms.extend(super(BOM, cls).copy([bom], default=default))
return new_boms
@classmethod
def new_version(cls, boms, date, reason_change, modification_made):
pool = Pool()
ProductBOM = pool.get('product.product-production.bom')
cls.write(boms, {
'end_date': date - datetime.timedelta(days=1),
})
with Transaction().set_context(new_version=True):
new_boms = cls.copy(boms, {
'end_date': None,
'start_date': date,
'reason_change': reason_change,
'modification_made': modification_made,
})
# relate new version BOMs to the product in case source bom is related to the product
existing_keys = set((pb.product.id, pb.bom.master_bom)
for pb in ProductBOM.search([('bom', 'in', boms)]))
to_save = []
for new_bom in new_boms:
for output in new_bom.outputs:
key = (output.product.id, new_bom.master_bom)
if key in existing_keys:
to_save += [ProductBOM(
product=output.product,
bom=new_bom,
)]
ProductBOM.save(to_save)
return new_boms
class Production(metaclass=PoolMeta):
__name__ = 'production'
bom_valid = fields.Function(fields.Boolean('Bom Valid'), 'on_change_with_bom_valid')
@classmethod
def __setup__(cls):
super(Production, cls).__setup__()
cls.bom.domain += [If((Eval('state').in_(['request', 'draft'])) & ~Bool(Eval('bom', False)),
[
('start_date', '<=', If(Bool(Eval('effective_date')), Eval('effective_date'), Eval('planned_date', Date()))),
['OR',
('end_date', '>=', If(Bool(Eval('effective_date')), Eval('effective_date'), Eval('planned_date', Date()))),
('end_date', '=', None),
]
],
())]
@fields.depends('effective_date', 'planned_date', 'bom')
def on_change_with_bom_valid(self, name=None):
pool = Pool()
Date = pool.get('ir.date')
today = Date.today()
production_date = self.effective_date or self.planned_date or today
bom = self.bom
if not bom:
return True
if (bom.end_date and (bom.start_date <= production_date
and bom.end_date < production_date)):
return False
elif production_date < bom.start_date:
return False
return True
@classmethod
def run(cls, productions):
pool = Pool()
Warning = pool.get('res.user.warning')
for production in productions:
if production.bom_valid:
continue
key = 'bom_expired_date_%s_%s' % (production.id, production.bom.id)
if Warning.check(key):
raise UserWarning(key,
gettext('production_bom_versions.msg_bom_expired_date',
production=production.rec_name,
bom=production.bom.rec_name,
))
return super().run(productions)
class NewVersionStart(ModelView):
'New Version Start'
__name__ = 'production.bom.new.version.start'
date = fields.Date('Date')
reason_change = fields.Text('Reason for Change')
modification_made = fields.Text('Modification Made')
@staticmethod
def default_date():
pool = Pool()
Date = pool.get('ir.date')
return Date.today()
class NewVersion(Wizard):
'New Version'
__name__ = 'production.bom.new.version'
start = StateView('production.bom.new.version.start',
'production_bom_versions.new_version_start_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Create', 'create_', 'tryton-forward', default=True),
])
create_ = StateAction('production.act_bom_list')
def do_create_(self, action):
pool = Pool()
BOM = pool.get('production.bom')
boms = self.records
new_versions = BOM.new_version(boms, self.start.date,
self.start.reason_change, self.start.modification_made)
data = {'res_id': [i.id for i in new_versions]}
if len(new_versions) == 1:
action['views'].reverse()
return action, data
def transition_create_(self):
return 'end'
class OpenVersions(Wizard):
'Open Versions'
__name__ = 'production.bom.open_versions'
start_state = 'open_'
open_ = StateAction('production.act_bom_list')
def do_open_(self, action):
pool = Pool()
Bom = pool.get('production.bom')
bom = self.record
encoder = PYSONEncoder()
action['pyson_domain'] = encoder.encode(
[('master_bom', '=', bom and bom.master_bom and bom.master_bom.id)])
action['pyson_order'] = encoder.encode([('version', 'DESC')])
context = {'show_versions': True}
bom = Bom.get_last_version(bom.master_bom and bom.master_bom.id)
action['pyson_context'] = encoder.encode(context)
action['name'] += ' - %s' % (gettext('production_bom_versions.'
'msg_versions', version=bom.rec_name))
return action, {}
def transition_open_(self):
return 'end'