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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import Form, SavepointCase
class StockGenerate(SavepointCase):
@classmethod
def setUpClass(cls):
super(StockGenerate, cls).setUpClass()
Product = cls.env['product.product']
cls.product_serial = Product.create({
'name': 'Tracked by SN',
'type': 'product',
'tracking': 'serial',
})
cls.uom_unit = cls.env.ref('uom.product_uom_unit')
cls.warehouse = cls.env['stock.warehouse'].create({
'name': 'Base Warehouse',
'reception_steps': 'one_step',
'delivery_steps': 'ship_only',
'code': 'BWH'
})
cls.location = cls.env['stock.location'].create({
'name': 'Room A',
'location_id': cls.warehouse.lot_stock_id.id,
})
cls.location_dest = cls.env['stock.location'].create({
'name': 'Room B',
'location_id': cls.warehouse.lot_stock_id.id,
})
cls.Wizard = cls.env['stock.assign.serial']
def get_new_move(self, nbre_of_lines):
move_lines_val = []
for i in range(nbre_of_lines):
move_lines_val.append({
'product_id': self.product_serial.id,
'product_uom_id': self.uom_unit.id,
'product_uom_qty': 1,
'location_id': self.location.id,
'location_dest_id': self.location_dest.id
})
return self.env['stock.move'].create({
'name': 'Move Test',
'product_id': self.product_serial.id,
'product_uom': self.uom_unit.id,
'location_id': self.location.id,
'location_dest_id': self.location_dest.id,
'move_line_ids': [(0, 0, line_vals) for line_vals in move_lines_val]
})
def test_generate_01_sn(self):
""" Creates a move with 5 move lines, then asks for generates 5 Serial
Numbers. Checks move has 5 new move lines with each a SN, and the 5
original move lines are still unchanged.
"""
nbre_of_lines = 5
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='001',
default_next_serial_count=nbre_of_lines,
))
wiz = form_wizard.save()
self.assertEqual(len(move.move_line_ids), nbre_of_lines)
wiz.generate_serial_numbers()
# Checks new move lines have the right SN
generated_numbers = ['001', '002', '003', '004', '005']
self.assertEqual(len(move.move_line_ids), nbre_of_lines + len(generated_numbers))
for move_line in move.move_line_nosuggest_ids:
# For a product tracked by SN, the `qty_done` is set on 1 when
# `lot_name` is set.
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(move_line.lot_name, generated_numbers.pop(0))
# Checks pre-generated move lines didn't change
for move_line in (move.move_line_ids - move.move_line_nosuggest_ids):
self.assertEqual(move_line.qty_done, 0)
self.assertEqual(move_line.lot_name, False)
def test_generate_02_prefix_suffix(self):
""" Generates some Serial Numbers and checks the prefix and/or suffix
are correctly used.
"""
nbre_of_lines = 10
# Case #1: Prefix, no suffix
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='bilou-87',
default_next_serial_count=nbre_of_lines,
))
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Checks all move lines have the right SN
generated_numbers = [
'bilou-87', 'bilou-88', 'bilou-89', 'bilou-90', 'bilou-91',
'bilou-92', 'bilou-93', 'bilou-94', 'bilou-95', 'bilou-96'
]
for move_line in move.move_line_nosuggest_ids:
# For a product tracked by SN, the `qty_done` is set on 1 when
# `lot_name` is set.
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(
move_line.lot_name,
generated_numbers.pop(0)
)
# Case #2: No prefix, suffix
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='005-ccc',
default_next_serial_count=nbre_of_lines,
))
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Checks all move lines have the right SN
generated_numbers = [
'005-ccc', '006-ccc', '007-ccc', '008-ccc', '009-ccc',
'010-ccc', '011-ccc', '012-ccc', '013-ccc', '014-ccc'
]
for move_line in move.move_line_nosuggest_ids:
# For a product tracked by SN, the `qty_done` is set on 1 when
# `lot_name` is set.
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(
move_line.lot_name,
generated_numbers.pop(0)
)
# Case #3: Prefix + suffix
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='alpha-012-345-beta',
default_next_serial_count=nbre_of_lines,
))
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Checks all move lines have the right SN
generated_numbers = [
'alpha-012-345-beta', 'alpha-012-346-beta', 'alpha-012-347-beta',
'alpha-012-348-beta', 'alpha-012-349-beta', 'alpha-012-350-beta',
'alpha-012-351-beta', 'alpha-012-352-beta', 'alpha-012-353-beta',
'alpha-012-354-beta'
]
for move_line in move.move_line_nosuggest_ids:
# For a product tracked by SN, the `qty_done` is set on 1 when
# `lot_name` is set.
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(
move_line.lot_name,
generated_numbers.pop(0)
)
# Case #4: Prefix + suffix, identical number pattern
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='BAV023B00001S00001',
default_next_serial_count=nbre_of_lines,
))
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Checks all move lines have the right SN
generated_numbers = [
'BAV023B00001S00001', 'BAV023B00001S00002', 'BAV023B00001S00003',
'BAV023B00001S00004', 'BAV023B00001S00005', 'BAV023B00001S00006',
'BAV023B00001S00007', 'BAV023B00001S00008', 'BAV023B00001S00009',
'BAV023B00001S00010'
]
for move_line in move.move_line_nosuggest_ids:
# For a product tracked by SN, the `qty_done` is set on 1 when
# `lot_name` is set.
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(
move_line.lot_name,
generated_numbers.pop(0)
)
def test_generate_03_raise_exception(self):
""" Tries to generate some SN but with invalid initial number.
"""
move = self.get_new_move(3)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
default_next_serial_number='code-xxx',
))
wiz = form_wizard.save()
with self.assertRaises(UserError):
wiz.generate_serial_numbers()
form_wizard.next_serial_count = 0
# Must raise an exception because `next_serial_count` must be greater than 0.
with self.assertRaises(ValidationError):
form_wizard.save()
def test_generate_04_generate_in_multiple_time(self):
""" Generates a Serial Number for each move lines (except the last one)
but with multiple assignments, and checks the generated Serial Numbers
are what we expect.
"""
nbre_of_lines = 10
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
))
# First assignment
form_wizard.next_serial_count = 3
form_wizard.next_serial_number = '001'
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Second assignment
form_wizard.next_serial_count = 2
form_wizard.next_serial_number = 'bilou-64'
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Third assignment
form_wizard.next_serial_count = 4
form_wizard.next_serial_number = 'ro-1337-bot'
wiz = form_wizard.save()
wiz.generate_serial_numbers()
# Checks all move lines have the right SN
generated_numbers = [
# Correspond to the first assignment
'001', '002', '003',
# Correspond to the second assignment
'bilou-64', 'bilou-65',
# Correspond to the third assignment
'ro-1337-bot', 'ro-1338-bot', 'ro-1339-bot', 'ro-1340-bot',
]
self.assertEqual(len(move.move_line_ids), nbre_of_lines + len(generated_numbers))
self.assertEqual(len(move.move_line_nosuggest_ids), len(generated_numbers))
for move_line in move.move_line_nosuggest_ids:
self.assertEqual(move_line.qty_done, 1)
self.assertEqual(move_line.lot_name, generated_numbers.pop(0))
for move_line in (move.move_line_ids - move.move_line_nosuggest_ids):
self.assertEqual(move_line.qty_done, 0)
self.assertEqual(move_line.lot_name, False)
def test_generate_with_putaway(self):
""" Checks the `location_dest_id` of generated move lines is correclty
set in fonction of defined putaway rules.
"""
nbre_of_lines = 4
shelf_location = self.env['stock.location'].create({
'name': 'shelf1',
'usage': 'internal',
'location_id': self.location_dest.id,
})
# Checks a first time without putaway...
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
))
form_wizard.next_serial_count = nbre_of_lines
form_wizard.next_serial_number = '001'
wiz = form_wizard.save()
wiz.generate_serial_numbers()
for move_line in move.move_line_nosuggest_ids:
self.assertEqual(move_line.qty_done, 1)
# The location dest must be the default one.
self.assertEqual(move_line.location_dest_id.id, self.location_dest.id)
# We need to activate multi-locations to use putaway rules.
grp_multi_loc = self.env.ref('stock.group_stock_multi_locations')
self.env.user.write({'groups_id': [(4, grp_multi_loc.id)]})
# Creates a putaway rule
putaway_product = self.env['stock.putaway.rule'].create({
'product_id': self.product_serial.id,
'location_in_id': self.location_dest.id,
'location_out_id': shelf_location.id,
})
# Checks now with putaway...
move = self.get_new_move(nbre_of_lines)
form_wizard = Form(self.env['stock.assign.serial'].with_context(
default_move_id=move.id,
))
form_wizard.next_serial_count = nbre_of_lines
form_wizard.next_serial_number = '001'
wiz = form_wizard.save()
wiz.generate_serial_numbers()
for move_line in move.move_line_nosuggest_ids:
self.assertEqual(move_line.qty_done, 1)
# The location dest must be now the one from the putaway.
self.assertEqual(move_line.location_dest_id.id, shelf_location.id)
def test_set_multiple_lot_name_01(self):
""" Sets five SN in one time in stock move view form, then checks move
has five new move lines with the right `lot_name`.
"""
nbre_of_lines = 10
picking_type = self.env['stock.picking.type'].search([
('use_create_lots', '=', True),
('warehouse_id', '=', self.warehouse.id)
])
move = self.get_new_move(nbre_of_lines)
move.picking_type_id = picking_type
# We must begin with a move with 10 move lines.
self.assertEqual(len(move.move_line_ids), nbre_of_lines)
value_list = [
'abc-235',
'abc-237',
'abc-238',
'abc-282',
'abc-301',
]
values = '\n'.join(value_list)
move_form = Form(move, view='stock.view_stock_move_nosuggest_operations')
with move_form.move_line_nosuggest_ids.new() as line:
line.lot_name = values
move = move_form.save()
# After we set multiple SN, we must have now 15 move lines.
self.assertEqual(len(move.move_line_ids), nbre_of_lines + len(value_list))
# Then we look each SN name is correct.
for move_line in move.move_line_nosuggest_ids:
self.assertEqual(move_line.lot_name, value_list.pop(0))
for move_line in (move.move_line_ids - move.move_line_nosuggest_ids):
self.assertEqual(move_line.lot_name, False)
def test_set_multiple_lot_name_02_empty_values(self):
""" Sets multiple values with some empty lines in one time, then checks
we haven't create useless move line and all move line's `lot_name` have
been correctly set.
"""
nbre_of_lines = 5
picking_type = self.env['stock.picking.type'].search([
('use_create_lots', '=', True),
('warehouse_id', '=', self.warehouse.id)
])
move = self.get_new_move(nbre_of_lines)
move.picking_type_id = picking_type
# We must begin with a move with five move lines.
self.assertEqual(len(move.move_line_ids), nbre_of_lines)
value_list = [
'',
'abc-235',
'',
'abc-237',
'',
'',
'abc-238',
'abc-282',
'abc-301',
'',
]
values = '\n'.join(value_list)
# Checks we have more values than move lines.
self.assertTrue(len(move.move_line_ids) < len(value_list))
move_form = Form(move, view='stock.view_stock_move_nosuggest_operations')
with move_form.move_line_nosuggest_ids.new() as line:
line.lot_name = values
move = move_form.save()
filtered_value_list = list(filter(lambda line: len(line), value_list))
# After we set multiple SN, we must have a line for each value.
self.assertEqual(len(move.move_line_ids), nbre_of_lines + len(filtered_value_list))
# Then we look each SN name is correct.
for move_line in move.move_line_nosuggest_ids:
self.assertEqual(move_line.lot_name, filtered_value_list.pop(0))
for move_line in (move.move_line_ids - move.move_line_nosuggest_ids):
self.assertEqual(move_line.lot_name, False)
|