File: test_auto_waving.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (369 lines) | stat: -rw-r--r-- 17,003 bytes parent folder | download
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
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import Command
from odoo.tests.common import TransactionCase


class TestAutoWaving(TransactionCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.stock_location = cls.env.ref('stock.stock_location_stock')
        cls.child_location_1 = cls.env['stock.location'].create({
            'name': 'Sub Location 1',
            'location_id': cls.stock_location.id,
        })
        cls.child_location_2 = cls.env['stock.location'].create({
            'name': 'Sub Location 2',
            'location_id': cls.stock_location.id,
        })
        cls.grandchild_location = cls.env['stock.location'].create({
            'name': 'Grandchild Location',
            'location_id': cls.child_location_1.id,
        })
        cls.sibling_location = cls.env['stock.location'].create({
            'name': 'Sibling Location',
            'location_id': cls.stock_location.location_id.id,
        })

        cls.product_1 = cls.env['product.product'].create({
            'name': 'Product 1',
            'is_storable': True,
        })
        cls.product_2 = cls.env['product.product'].create({
            'name': 'Product 2',
            'is_storable': True,
        })
        cls.product_3 = cls.env['product.product'].create({
            'name': 'Product 3',
            'is_storable': True,
        })
        cls.product_4 = cls.env['product.product'].create({
            'name': 'Product 4',
            'is_storable': True,
        })

        Quant = cls.env['stock.quant']

        Quant._update_available_quantity(cls.product_1, cls.stock_location, 2)
        Quant._update_available_quantity(cls.product_1, cls.child_location_1, 6)
        Quant._update_available_quantity(cls.product_1, cls.child_location_2, 6)
        Quant._update_available_quantity(cls.product_1, cls.grandchild_location, 3)

        Quant._update_available_quantity(cls.product_2, cls.child_location_1, 4)
        Quant._update_available_quantity(cls.product_2, cls.child_location_2, 2)
        Quant._update_available_quantity(cls.product_2, cls.sibling_location, 2)

        Quant._update_available_quantity(cls.product_3, cls.grandchild_location, 3)

        Quant._update_available_quantity(cls.product_4, cls.child_location_1, 3)

        cls.picking_type_out = cls.env.ref('stock.picking_type_out')

        cls.us_client = cls.env['res.partner'].create({
            'name': 'US Client',
            'country_id': cls.env.ref('base.us').id,
        })
        cls.be_client = cls.env['res.partner'].create({
            'name': 'BE Client',
            'country_id': cls.env.ref('base.be').id,
        })
        cls.fr_client = cls.env['res.partner'].create({
            'name': 'FR Client',
            'country_id': cls.env.ref('base.fr').id,
        })
        cls.demo_partners = cls.us_client | cls.be_client | cls.fr_client

        cls.picking_1 = cls.env['stock.picking'].create({
            'location_id': cls.stock_location.id,
            'picking_type_id': cls.picking_type_out.id,
            'partner_id': cls.us_client.id,
            'move_ids': [
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 3,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.child_location_1.id,
                }),
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.child_location_2.id,
                }),
                Command.create({
                    'name': cls.product_2.name,
                    'product_id': cls.product_2.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_2.uom_id.id,
                    'location_id': cls.child_location_1.id,
                })
            ]
        })
        cls.picking_2 = cls.env['stock.picking'].create({
            'location_id': cls.stock_location.id,
            'picking_type_id': cls.picking_type_out.id,
            'partner_id': cls.be_client.id,
            'move_ids': [
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 3,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.grandchild_location.id,
                }),
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.child_location_2.id,
                }),
                Command.create({
                    'name': cls.product_2.name,
                    'product_id': cls.product_2.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_2.uom_id.id,
                    'location_id': cls.child_location_1.id,
                })
            ]
        })
        cls.picking_3 = cls.env['stock.picking'].create({
            'location_id': cls.stock_location.id,
            'picking_type_id': cls.picking_type_out.id,
            'partner_id': cls.us_client.id,
            'move_ids': [
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.stock_location.id,
                }),
                Command.create({
                    'name': cls.product_2.name,
                    'product_id': cls.product_2.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_2.uom_id.id,
                    'location_id': cls.child_location_2.id,
                }),
                Command.create({
                    'name': cls.product_3.name,
                    'product_id': cls.product_3.id,
                    'product_uom_qty': 3,
                    'product_uom': cls.product_3.uom_id.id,
                    'location_id': cls.grandchild_location.id,
                })
            ]
        })
        cls.picking_4 = cls.env['stock.picking'].create({
            'location_id': cls.stock_location.id,
            'picking_type_id': cls.picking_type_out.id,
            'partner_id': cls.be_client.id,
            'move_ids': [
                Command.create({
                    'name': cls.product_4.name,
                    'product_id': cls.product_4.id,
                    'product_uom_qty': 3,
                    'product_uom': cls.product_4.uom_id.id,
                    'location_id': cls.child_location_1.id,
                })
            ]
        })
        cls.picking_5 = cls.env['stock.picking'].create({
            'location_id': cls.stock_location.id,
            'picking_type_id': cls.picking_type_out.id,
            'partner_id': cls.fr_client.id,
            'move_ids': [
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 3,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.child_location_1.id,
                }),
                Command.create({
                    'name': cls.product_1.name,
                    'product_id': cls.product_1.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_1.uom_id.id,
                    'location_id': cls.child_location_2.id,
                }),
                Command.create({
                    'name': cls.product_2.name,
                    'product_id': cls.product_2.id,
                    'product_uom_qty': 2,
                    'product_uom': cls.product_2.uom_id.id,
                    'location_id': cls.sibling_location.id,
                })
            ]
        })
        cls.all_pickings = cls.picking_1 | cls.picking_2 | cls.picking_3 | cls.picking_4 | cls.picking_5

    def test_group_by_partner_and_location(self):
        self.picking_type_out.write({
            'auto_batch': True,
            'batch_group_by_partner': True,
            'wave_group_by_location': True,
            'wave_location_ids': self.child_location_2,
            'batch_group_by_destination': False,
            'batch_group_by_src_loc': False,
            'batch_group_by_dest_loc': False,
            'wave_group_by_product': False,
            'wave_group_by_category': False,
        })

        self.all_pickings.action_assign()

        all_batches = self.env['stock.picking.batch'].search([('picking_ids.partner_id', 'in', self.demo_partners.ids)])
        waves = all_batches.filtered(lambda b: b.is_wave)

        wave_1 = waves.filtered(lambda w: w.description == f'{self.us_client.name}, {self.child_location_2.complete_name}')
        self.assertEqual(len(wave_1), 1)
        self.assertEqual(len(wave_1.picking_ids), 2)
        self.assertEqual(len(wave_1.move_line_ids), 2)
        self.assertEqual(wave_1.picking_ids.partner_id, self.us_client)
        self.assertEqual(wave_1.move_line_ids.location_id, self.child_location_2)

        wave_2 = waves.filtered(lambda w: w.description == f'{self.be_client.name}, {self.child_location_2.complete_name}')
        self.assertEqual(len(wave_2), 1)
        self.assertEqual(len(wave_2.picking_ids), 1)
        self.assertEqual(len(wave_2.move_line_ids), 1)
        self.assertEqual(wave_2.picking_ids.partner_id, self.be_client)
        self.assertEqual(wave_2.move_line_ids.location_id, self.child_location_2)

        wave_3 = waves.filtered(lambda w: w.description == f'{self.fr_client.name}, {self.child_location_2.complete_name}')
        self.assertEqual(len(wave_3), 1)
        self.assertEqual(len(wave_3.picking_ids), 1)
        self.assertEqual(len(wave_3.move_line_ids), 1)
        self.assertEqual(wave_3.picking_ids.partner_id, self.fr_client)
        self.assertEqual(wave_3.move_line_ids.location_id, self.child_location_2)

        batches = all_batches - waves
        batch_1 = batches.filtered(lambda b: b.description == self.us_client.name)
        self.assertEqual(len(batch_1), 1)
        self.assertEqual(len(batch_1.picking_ids), 2)
        self.assertEqual(len(batch_1.move_line_ids), 4)
        self.assertEqual(batch_1.picking_ids.partner_id, self.us_client)
        self.assertEqual(len(batch_1.move_line_ids.location_id), 3)

        batch_2 = batches.filtered(lambda b: b.description == self.be_client.name)
        self.assertEqual(len(batch_2), 1)
        self.assertEqual(len(batch_2.picking_ids), 2)
        self.assertEqual(len(batch_2.move_line_ids), 3)
        self.assertEqual(batch_2.picking_ids.partner_id, self.be_client)
        self.assertEqual(len(batch_2.move_line_ids.location_id), 2)

        batch_3 = batches.filtered(lambda b: b.description == self.fr_client.name)
        self.assertEqual(len(batch_3), 1)
        self.assertEqual(len(batch_3.picking_ids), 1)
        self.assertEqual(len(batch_3.move_line_ids), 2)
        self.assertEqual(batch_3.picking_ids.partner_id, self.fr_client)
        self.assertEqual(len(batch_3.move_line_ids.location_id), 2)

    def test_group_by_locations(self):
        self.picking_type_out.write({
            'auto_batch': True,
            'wave_group_by_location': True,
            'wave_location_ids': (self.stock_location | self.child_location_1).ids,
            'batch_group_by_partner': False,
            'batch_group_by_destination': False,
            'batch_group_by_src_loc': False,
            'batch_group_by_dest_loc': False,
            'wave_group_by_product': False,
            'wave_group_by_category': False,
        })

        self.all_pickings.action_assign()

        all_batches = self.env['stock.picking.batch'].search([('picking_ids.partner_id', 'in', self.demo_partners.ids)])
        waves = all_batches.filtered(lambda b: b.is_wave)

        wave_1 = waves.filtered(lambda w: w.description == self.child_location_1.complete_name)
        self.assertEqual(len(wave_1), 1)
        self.assertEqual(len(wave_1.picking_ids), 5)
        self.assertEqual(len(wave_1.move_line_ids), 7)
        self.assertEqual(len(wave_1.picking_ids.partner_id), 3)
        self.assertTrue(all(l._child_of(self.child_location_1) for l in wave_1.move_line_ids.location_id))

        wave_2 = waves.filtered(lambda w: w.description == self.stock_location.complete_name)
        self.assertEqual(len(wave_2), 1)
        self.assertEqual(len(wave_2.picking_ids), 4)
        self.assertEqual(len(wave_2.move_line_ids), 5)
        self.assertEqual(len(wave_2.picking_ids.partner_id), 3)
        self.assertTrue(all(l._child_of(self.stock_location) for l in wave_1.move_line_ids.location_id))

    def test_group_by_country_and_product(self):
        self.picking_type_out.write({
            'auto_batch': True,
            'batch_group_by_destination': True,
            'wave_group_by_product': True,
            'batch_group_by_partner': False,
            'batch_group_by_src_loc': False,
            'batch_group_by_dest_loc': False,
            'wave_group_by_category': False,
            'wave_group_by_location': False,
        })

        self.all_pickings.action_assign()

        all_batches = self.env['stock.picking.batch'].search([('picking_ids.partner_id', 'in', self.demo_partners.ids)])
        waves = all_batches.filtered(lambda b: b.is_wave)

        wave_1 = waves.filtered(lambda w: w.description == f'United States, {self.product_1.name}')
        self.assertEqual(len(wave_1), 1)
        self.assertEqual(len(wave_1.picking_ids), 2)
        self.assertEqual(len(wave_1.move_line_ids), 3)
        self.assertEqual(wave_1.picking_ids.partner_id, self.us_client)
        self.assertEqual(wave_1.move_line_ids.product_id, self.product_1)

        wave_2 = waves.filtered(lambda w: w.description == f'United States, {self.product_2.name}')
        self.assertEqual(len(wave_2), 1)
        self.assertEqual(len(wave_2.picking_ids), 2)
        self.assertEqual(len(wave_2.move_line_ids), 2)
        self.assertEqual(wave_2.picking_ids.partner_id, self.us_client)
        self.assertEqual(wave_2.move_line_ids.product_id, self.product_2)

        wave_3 = waves.filtered(lambda w: w.description == f'United States, {self.product_3.name}')
        self.assertEqual(len(wave_3), 1)
        self.assertEqual(len(wave_3.picking_ids), 1)
        self.assertEqual(len(wave_3.move_line_ids), 1)
        self.assertEqual(wave_3.picking_ids.partner_id, self.us_client)
        self.assertEqual(wave_3.move_line_ids.product_id, self.product_3)

        wave_4 = waves.filtered(lambda w: w.description == f'Belgium, {self.product_1.name}')
        self.assertEqual(len(wave_4), 1)
        self.assertEqual(len(wave_4.picking_ids), 1)
        self.assertEqual(len(wave_4.move_line_ids), 2)
        self.assertEqual(wave_4.picking_ids.partner_id, self.be_client)
        self.assertEqual(wave_4.move_line_ids.product_id, self.product_1)

        wave_5 = waves.filtered(lambda w: w.description == f'Belgium, {self.product_2.name}')
        self.assertEqual(len(wave_5), 1)
        self.assertEqual(len(wave_5.picking_ids), 1)
        self.assertEqual(len(wave_5.move_line_ids), 1)
        self.assertEqual(wave_5.picking_ids.partner_id, self.be_client)
        self.assertEqual(wave_5.move_line_ids.product_id, self.product_2)

        wave_6 = waves.filtered(lambda w: w.description == f'Belgium, {self.product_4.name}')
        self.assertEqual(len(wave_6), 1)
        self.assertEqual(len(wave_6.picking_ids), 1)
        self.assertEqual(len(wave_6.move_line_ids), 1)
        self.assertEqual(wave_6.picking_ids.partner_id, self.be_client)
        self.assertEqual(wave_6.move_line_ids.product_id, self.product_4)

        wave_7 = waves.filtered(lambda w: w.description == f'France, {self.product_1.name}')
        self.assertEqual(len(wave_7), 1)
        self.assertEqual(len(wave_7.picking_ids), 1)
        self.assertEqual(len(wave_7.move_line_ids), 2)
        self.assertEqual(wave_7.picking_ids.partner_id, self.fr_client)
        self.assertEqual(wave_7.move_line_ids.product_id, self.product_1)

        wave_8 = waves.filtered(lambda w: w.description == f'France, {self.product_2.name}')
        self.assertEqual(len(wave_8), 1)
        self.assertEqual(len(wave_8.picking_ids), 1)
        self.assertEqual(len(wave_8.move_line_ids), 1)
        self.assertEqual(wave_8.picking_ids.partner_id, self.fr_client)
        self.assertEqual(wave_8.move_line_ids.product_id, self.product_2)