Debian Codegraph @

addons/product_expiry/tests/test_generate_serial_numbers.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from datetime import datetime
4 from freezegun import freeze_time
5
6 from odoo import Command
7 from odoo.addons.stock.tests.test_generate_serial_numbers import StockGenerateCommon
8 from odoo.addons.stock.tests.test_picking_tours import TestStockPickingTour
9 from odoo.tools.misc import get_lang
10
11
12 class TestStockLot(StockGenerateCommon):
13 @classmethod
14 def setUpClass(cls):
15 super().setUpClass()
16 cls.product_lot = cls.env['product.product'].create({
17 'name': 'Tracked by Lot Numbers',
18 'tracking': 'lot',
19 'is_storable': True,
20 'use_expiration_date': True,
21 })
22
23 def _import_lots(self, lots, move):
24 location_id = move.location_id
25 move_lines_vals = move.split_lots(lots)
26 move_lines_commands = move._generate_serial_move_line_commands(move_lines_vals, location_dest_id=location_id)
27 move.update({'move_line_ids': move_lines_commands})
28
29 def test_set_multiple_lot_name_with_expiration_date_01(self):
30 """ In a move line's `lot_name` field, pastes a list of lots and expiration dates.
31 Checks the values are correctly interpreted and the expiration dates are correctly created
32 depending of the user lang's date format.
33 """
34 user_lang = self.env['res.lang'].browse([get_lang(self.env).id])
35 # Try first with the "day/month/year" date format.
36 user_lang.date_format = "%d/%m/%y"
37 list_lot_and_qty = [
38 {'lot_name': "ln01", "date": "03/05/25", "datetime": datetime.strptime('2025-05-03', "%Y-%m-%d")},
39 {'lot_name': "ln02", "date": "06/05/25", "datetime": datetime.strptime('2025-05-06', "%Y-%m-%d")},
40 {'lot_name': "ln03", "date": "03/06/25", "datetime": datetime.strptime('2025-06-03', "%Y-%m-%d")},
41 {'lot_name': "ln04", "date": "06/06/25", "datetime": datetime.strptime('2025-06-06', "%Y-%m-%d")},
42 {'lot_name': "ln05", "date": "03/07/25", "datetime": datetime.strptime('2025-07-03', "%Y-%m-%d")},
43 ]
44 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
45 move = self.get_new_move(product=self.product_lot)
46 self._import_lots(list_as_string, move)
47 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
48 for i, move_line in enumerate(move.move_line_ids):
49 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
50 self.assertEqual(move_line.quantity, 1)
51 self.assertEqual(move_line.expiration_date, list_lot_and_qty[i]["datetime"])
52
53 # Same test but with with the "month/day/year" date format this time.
54 user_lang.date_format = "%m/%d/%y"
55 list_lot_and_qty = [
56 {'lot_name': "ln01", "date": "03/05/25", "datetime": datetime.strptime('2025-03-05', "%Y-%m-%d")},
57 {'lot_name': "ln02", "date": "06/05/25", "datetime": datetime.strptime('2025-06-05', "%Y-%m-%d")},
58 {'lot_name': "ln03", "date": "03/06/25", "datetime": datetime.strptime('2025-03-06', "%Y-%m-%d")},
59 {'lot_name': "ln04", "date": "06/06/25", "datetime": datetime.strptime('2025-06-06', "%Y-%m-%d")},
60 {'lot_name': "ln05", "date": "03/07/25", "datetime": datetime.strptime('2025-03-07', "%Y-%m-%d")},
61 ]
62 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
63 move = self.get_new_move(product=self.product_lot)
64 self._import_lots(list_as_string, move)
65 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
66 for i, move_line in enumerate(move.move_line_ids):
67 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
68 self.assertEqual(move_line.quantity, 1)
69 self.assertEqual(move_line.expiration_date, list_lot_and_qty[i]["datetime"])
70
71 def test_set_multiple_lot_name_with_expiration_date_02_product_dont_use_expiration_date(self):
72 """ In a move line's `lot_name` field, pastes a list of lots and expiration dates.
73 Checks the values are correctly interpreted and since the product doesn't use expiration
74 date, the expiration dates should be ignored.
75 """
76 self.product_lot.use_expiration_date = False
77 user_lang = self.env['res.lang'].browse([get_lang(self.env).id])
78 # Try first with the "day/month/year" date format.
79 user_lang.date_format = "%d/%m/%y"
80 list_lot_and_qty = [
81 {'lot_name': "ln01", "date": "03/05/25", "datetime": datetime.strptime('2025-05-03', "%Y-%m-%d")},
82 {'lot_name': "ln02", "date": "06/05/25", "datetime": datetime.strptime('2025-05-06', "%Y-%m-%d")},
83 {'lot_name': "ln03", "date": "03/06/25", "datetime": datetime.strptime('2025-06-03', "%Y-%m-%d")},
84 {'lot_name': "ln04", "date": "06/06/25", "datetime": datetime.strptime('2025-06-06', "%Y-%m-%d")},
85 {'lot_name': "ln05", "date": "03/07/25", "datetime": datetime.strptime('2025-07-03', "%Y-%m-%d")},
86 ]
87 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
88 move = self.get_new_move(product=self.product_lot)
89 self._import_lots(list_as_string, move)
90 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
91 for i, move_line in enumerate(move.move_line_ids):
92 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
93 self.assertEqual(move_line.quantity, 1)
94 self.assertEqual(move_line.expiration_date, False)
95
96 def test_set_multiple_lot_name_with_expiration_date_03_adaptive_date_format(self):
97 """ Checks if the given dates don't follow the user lang's date format, the created expiration
98 date will follow the first given date's format (at least in the scope of the limitations).
99 """
100 user_lang = self.env['res.lang'].browse([get_lang(self.env).id])
101 # Month first in the system but day in the first place in the given dates.
102 user_lang.date_format = "%m/%d/%y"
103 list_lot_and_qty = [
104 {'lot_name': "ln01", "date": "30/05/25", "datetime": datetime.strptime('2025-05-30', "%Y-%m-%d")},
105 {'lot_name': "ln02", "date": "06/05/25", "datetime": datetime.strptime('2025-05-06', "%Y-%m-%d")},
106 {'lot_name': "ln03", "date": "01/06/25", "datetime": datetime.strptime('2025-06-01', "%Y-%m-%d")},
107 {'lot_name': "ln04", "date": "06/06/25", "datetime": datetime.strptime('2025-06-06', "%Y-%m-%d")},
108 {'lot_name': "ln05", "date": "01/07/25", "datetime": datetime.strptime('2025-07-01', "%Y-%m-%d")},
109 ]
110 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
111 move = self.get_new_move(product=self.product_lot)
112 self._import_lots(list_as_string, move)
113 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
114 for i, move_line in enumerate(move.move_line_ids):
115 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
116 self.assertEqual(move_line.quantity, 1)
117 self.assertEqual(move_line.expiration_date, list_lot_and_qty[i]["datetime"])
118
119 # Now, tries with day first but the year is at the first place in the given dates.
120 user_lang.date_format = "%d/%m/%y"
121 list_lot_and_qty = [
122 {'lot_name': "ln01", "date": "89/05/04", "datetime": datetime.strptime('1989-05-04', "%Y-%m-%d")},
123 {'lot_name': "ln02", "date": "10/05/06", "datetime": datetime.strptime('2010-05-06', "%Y-%m-%d")},
124 {'lot_name': "ln03", "date": "12/06/15", "datetime": datetime.strptime('2012-06-15', "%Y-%m-%d")},
125 {'lot_name': "ln04", "date": "30/06/06", "datetime": datetime.strptime('2030-06-06', "%Y-%m-%d")},
126 {'lot_name': "ln05", "date": "04/07/08", "datetime": datetime.strptime('2004-07-08', "%Y-%m-%d")},
127 ]
128 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
129 move = self.get_new_move(product=self.product_lot)
130 self._import_lots(list_as_string, move)
131 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
132 for i, move_line in enumerate(move.move_line_ids):
133 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
134 self.assertEqual(move_line.quantity, 1)
135 self.assertEqual(move_line.expiration_date, list_lot_and_qty[i]["datetime"])
136
137 def test_set_multiple_lot_name_with_expiration_date_04_written_months(self):
138 """ Checks the expiration date is correctly created when the month is written in letters.
139 """
140 list_lot_and_qty = [
141 {'lot_name': "ln01", "date": "01 march 2077", "datetime": datetime.strptime('2077-03-01', "%Y-%m-%d")},
142 {'lot_name': "ln02", "date": "11 april 2077", "datetime": datetime.strptime('2077-04-11', "%Y-%m-%d")},
143 {'lot_name': "ln03", "date": "10 december 2077", "datetime": datetime.strptime('2077-12-10', "%Y-%m-%d")},
144 ]
145 list_as_string = '\n'.join([f'{line["lot_name"]};{line["date"]}' for line in list_lot_and_qty])
146 move = self.get_new_move(product=self.product_lot)
147 self._import_lots(list_as_string, move)
148 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
149 for i, move_line in enumerate(move.move_line_ids):
150 self.assertEqual(move_line.lot_name, list_lot_and_qty[i]['lot_name'])
151 self.assertEqual(move_line.quantity, 1)
152 self.assertEqual(move_line.expiration_date, list_lot_and_qty[i]["datetime"])
153
154 @freeze_time('2025-9-13')
155 def test_set_multiple_lot_name_with_expiration_date_05_import_dates(self):
156 """ When importing lot names, quantities and expiration dates, make sure the date is not
157 replaced by the computed date and if there's no date, it takes the computed date. """
158 self.product_lot.expiration_time = 10
159 receipt_picking = self.env['stock.picking'].create({
160 'picking_type_id': self.warehouse.in_type_id.id,
161 'location_id': self.env.ref('stock.stock_location_suppliers').id,
162 'location_dest_id': self.warehouse.lot_stock_id.id,
163 'state': 'draft',
164 'move_ids': [Command.create({
165 'product_id': self.product_lot.id,
166 'product_uom_qty': 20,
167 'location_id': self.env.ref('stock.stock_location_suppliers').id,
168 'location_dest_id': self.warehouse.lot_stock_id.id,
169 })]
170 })
171 action_context = {
172 'default_company_id': self.env.company.id,
173 'default_picking_id': receipt_picking.id,
174 'default_picking_type_id': self.warehouse.in_type_id.id,
175 'default_location_id': receipt_picking.location_id.id,
176 'default_location_dest_id': receipt_picking.location_dest_id.id,
177 'default_product_id': self.product_lot.id,
178 'default_tracking': 'lot',
179 }
180 move_line_vals = self.env['stock.move'].action_generate_lot_line_vals(
181 action_context, 'import', None, 0, 'lot1;10;2025-12-31\nlot2;7\nlot3;3;1970-1-1'
182 )
183 self.assert_move_line_vals_values(move_line_vals, [
184 {'quantity': 10, 'lot_name': 'lot1', 'expiration_date': datetime.strptime('2025-12-31', "%Y-%m-%d")},
185 {'quantity': 7, 'lot_name': 'lot2', 'expiration_date': datetime.strptime('2025-9-23', "%Y-%m-%d")},
186 {'quantity': 3, 'lot_name': 'lot3', 'expiration_date': datetime.strptime('1970-1-1', "%Y-%m-%d")},
187 ])
188
189 @freeze_time('2023-04-17')
190 def test_set_multiple_lot_name_with_expiration_date_05_wrong_given_date(self):
191 """ This test ensure when the given dates aren't correctly written, the
192 full string is used as the lot's name.
193 """
194 today = datetime(day=17, month=4, year=2023)
195 list_lot_and_qty = [
196 "ln01\t31/12", # Day is missing but the date is valid.
197 "ln02\t1989-04", # Day is missing but the date is valid.
198 "ln03\t01", # Single number: will be used as the quantity.
199 "ln04\t1989", # Single number: will be used as the quantity.
200 "ln05\t1989.04", # Signle number (with decimal): will be used as the quantity.
201 "ln06\t1989+04", # Wrong, all the string will be used as the lot name.
202 "ln07\tdacember", # Typo, all the string will be used as the lot name.
203 "ln08\tdecember", # Day and year are missing but the date is valid.
204 ]
205 list_as_string = '\n'.join(list_lot_and_qty)
206 move = self.get_new_move(product=self.product_lot)
207 self._import_lots(list_as_string, move)
208 self.assertEqual(len(move.move_line_ids), len(list_lot_and_qty))
209
210 self.assertEqual(move.move_line_ids[0].lot_name, "ln01")
211 self.assertEqual(move.move_line_ids[0].quantity, 1)
212 self.assertEqual(move.move_line_ids[0].expiration_date, datetime(day=31, month=12, year=2023))
213
214 self.assertEqual(move.move_line_ids[1].lot_name, "ln02")
215 self.assertEqual(move.move_line_ids[1].quantity, 1)
216 self.assertEqual(move.move_line_ids[1].expiration_date, datetime(day=17, month=4, year=1989))
217
218 self.assertEqual(move.move_line_ids[2].lot_name, "ln03")
219 self.assertEqual(move.move_line_ids[2].quantity, 1)
220 self.assertEqual(move.move_line_ids[2].expiration_date, today)
221
222 self.assertEqual(move.move_line_ids[3].lot_name, "ln04")
223 self.assertEqual(move.move_line_ids[3].quantity, 1989)
224 self.assertEqual(move.move_line_ids[3].expiration_date, today)
225
226 self.assertEqual(move.move_line_ids[4].lot_name, "ln05")
227 self.assertEqual(move.move_line_ids[4].quantity, 1989.04)
228 self.assertEqual(move.move_line_ids[4].expiration_date, today)
229
230 self.assertEqual(move.move_line_ids[5].lot_name, "ln06\t1989+04")
231 self.assertEqual(move.move_line_ids[5].quantity, 1)
232 self.assertEqual(move.move_line_ids[5].expiration_date, today)
233
234 self.assertEqual(move.move_line_ids[6].lot_name, "ln07\tdacember")
235 self.assertEqual(move.move_line_ids[6].quantity, 1)
236 self.assertEqual(move.move_line_ids[6].expiration_date, today)
237
238 self.assertEqual(move.move_line_ids[7].lot_name, "ln08")
239 self.assertEqual(move.move_line_ids[7].quantity, 1)
240 self.assertEqual(move.move_line_ids[7].expiration_date, datetime(day=17, month=12, year=2023))
241
242 def test_set_multiple_lot_name_with_expiration_date_06_one_line(self):
243 """ Checks the pasted data are correctly parsed even if the user pastes only one line.
244 """
245 user_lang = self.env['res.lang'].browse([get_lang(self.env).id])
246 user_lang.date_format = "%d/%m/%y"
247 for lot_name in ["lot-001;20;4 Aug 2048", "lot-001\t04/08/2048\t20"]:
248 move = self.get_new_move(product=self.product_lot)
249 self._import_lots(lot_name, move)
250 self.assertEqual(move.move_line_ids.lot_name, "lot-001")
251 self.assertEqual(move.move_line_ids.quantity, 20)
252 self.assertEqual(move.move_line_ids.expiration_date, datetime(day=4, month=8, year=2048))
253
254
255 class TestProductExpiryTour(TestStockPickingTour):
256
257 @freeze_time("2020-06-01")
258 def test_generate_serial_with_expiration(self):
259 """
260 Ensure that serial/lot numbers generated using the 'Generate Serials/Lots' button in Detailed
261 Operations have expiration dates set.
262 """
263 product_exp = self.env['product.product'].create({
264 'name': 'Product Exp',
265 'is_storable': True,
266 'tracking': 'serial',
267 'use_expiration_date': True,
268 'expiration_time': 2,
269 })
270
271 self.env['stock.move'].create({
272 'product_id': product_exp.id,
273 'product_uom_qty': 2,
274 'product_uom': product_exp.uom_id.id,
275 'picking_id': self.receipt.id,
276 })
277
278 self.receipt.action_confirm()
279 url = self._get_picking_url(self.receipt.id)
280
281 self.start_tour(url, 'test_generate_serial_with_expiration', login='admin')