| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from datetime import datetime, timedelta |
| 5 |
from dateutil.relativedelta import relativedelta |
| 6 |
|
| 7 |
from odoo import fields, Command |
| 8 |
from odoo.addons.mail.tests.common import mail_new_test_user |
| 9 |
from odoo.addons.stock.tests.common import TestStockCommon |
| 10 |
from odoo.tests import Form |
| 11 |
|
| 12 |
|
| 13 |
class TestStockLot(TestStockCommon): |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
super().setUpClass() |
| 18 |
# Creates a tracked product with expiration dates. |
| 19 |
cls.apple_product = cls.ProductObj.create({ |
| 20 |
'name': 'Apple', |
| 21 |
'is_storable': True, |
| 22 |
'tracking': 'lot', |
| 23 |
'use_expiration_date': True, |
| 24 |
'expiration_time': 10, |
| 25 |
'use_time': 5, |
| 26 |
'removal_time': 2, |
| 27 |
'alert_time': 6, |
| 28 |
}) |
| 29 |
|
| 30 |
def test_00_stock_production_lot(self): |
| 31 |
""" Test Scheduled Task on lot with an alert_date in the past creates an activity """ |
| 32 |
|
| 33 |
# create product |
| 34 |
self.productAAA = self.ProductObj.create({ |
| 35 |
'name': 'Product AAA', |
| 36 |
'is_storable': True, |
| 37 |
'tracking':'lot', |
| 38 |
'company_id': self.env.company.id, |
| 39 |
}) |
| 40 |
|
| 41 |
# create a new lot with with alert date in the past |
| 42 |
self.lot1_productAAA = self.LotObj.create({ |
| 43 |
'name': 'Lot 1 ProductAAA', |
| 44 |
'product_id': self.productAAA.id, |
| 45 |
'alert_date': fields.Date.to_string(datetime.today() - relativedelta(days=15)), |
| 46 |
}) |
| 47 |
|
| 48 |
picking_in = self.PickingObj.create({ |
| 49 |
'picking_type_id': self.picking_type_in.id, |
| 50 |
'location_id': self.supplier_location.id, |
| 51 |
'location_dest_id': self.stock_location.id, |
| 52 |
'state': 'draft', |
| 53 |
}) |
| 54 |
|
| 55 |
move_a = self.MoveObj.create({ |
| 56 |
'product_id': self.productAAA.id, |
| 57 |
'product_uom_qty': 33, |
| 58 |
'product_uom': self.productAAA.uom_id.id, |
| 59 |
'picking_id': picking_in.id, |
| 60 |
'location_id': self.supplier_location.id, |
| 61 |
'location_dest_id': self.stock_location.id, |
| 62 |
}) |
| 63 |
|
| 64 |
self.assertEqual(picking_in.move_ids.state, 'draft', 'Wrong state of move line.') |
| 65 |
picking_in.action_confirm() |
| 66 |
self.assertEqual(picking_in.move_ids.state, 'assigned', 'Wrong state of move line.') |
| 67 |
|
| 68 |
# Replace pack operation of incoming shipments. |
| 69 |
picking_in.action_assign() |
| 70 |
move_a.move_line_ids.quantity = 33 |
| 71 |
move_a.move_line_ids.lot_id = self.lot1_productAAA.id |
| 72 |
|
| 73 |
# Transfer Incoming Shipment. |
| 74 |
move_a.picked = True |
| 75 |
picking_in._action_done() |
| 76 |
|
| 77 |
# run scheduled tasks |
| 78 |
self.env['stock.lot']._alert_date_exceeded() |
| 79 |
|
| 80 |
# check a new activity has been created |
| 81 |
activity_id = self.env.ref('mail.mail_activity_data_todo').id |
| 82 |
activity_count = self.env['mail.activity'].search_count([ |
| 83 |
('activity_type_id', '=', activity_id), |
| 84 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 85 |
('res_id', '=', self.lot1_productAAA.id) |
| 86 |
]) |
| 87 |
self.assertEqual(activity_count, 1, 'No activity created while there should be one') |
| 88 |
|
| 89 |
# run the scheduler a second time |
| 90 |
self.env['stock.lot']._alert_date_exceeded() |
| 91 |
|
| 92 |
# check there is still only one activity, no additional activity is created if there is already an existing activity |
| 93 |
activity_count = self.env['mail.activity'].search_count([ |
| 94 |
('activity_type_id', '=', activity_id), |
| 95 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 96 |
('res_id', '=', self.lot1_productAAA.id) |
| 97 |
]) |
| 98 |
self.assertEqual(activity_count, 1, 'There should be one and only one activity') |
| 99 |
|
| 100 |
# mark the activity as done |
| 101 |
mail_activity = self.env['mail.activity'].search([ |
| 102 |
('activity_type_id', '=', activity_id), |
| 103 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 104 |
('res_id', '=', self.lot1_productAAA.id) |
| 105 |
]) |
| 106 |
mail_activity.action_done() |
| 107 |
|
| 108 |
# check there is no more activity (because it is already done) |
| 109 |
activity_count = self.env['mail.activity'].search_count([ |
| 110 |
('activity_type_id', '=', activity_id), |
| 111 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 112 |
('res_id', '=', self.lot1_productAAA.id) |
| 113 |
]) |
| 114 |
self.assertEqual(activity_count, 0,"As activity is done, there shouldn't be any related activity") |
| 115 |
|
| 116 |
# run the scheduler a third time |
| 117 |
self.env['stock.lot']._alert_date_exceeded() |
| 118 |
|
| 119 |
# check there is no activity created |
| 120 |
activity_count = self.env['mail.activity'].search_count([ |
| 121 |
('activity_type_id', '=', activity_id), |
| 122 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 123 |
('res_id', '=',self.lot1_productAAA.id) |
| 124 |
]) |
| 125 |
self.assertEqual(activity_count, 0, "As there is already an activity marked as done, there shouldn't be any related activity created for this lot") |
| 126 |
|
| 127 |
def test_01_stock_production_lot(self): |
| 128 |
""" Test Scheduled Task on lot with an alert_date in future does not create an activity """ |
| 129 |
|
| 130 |
# create product |
| 131 |
self.productBBB = self.ProductObj.create({ |
| 132 |
'name': 'Product BBB', |
| 133 |
'is_storable': True, |
| 134 |
'tracking':'lot' |
| 135 |
}) |
| 136 |
|
| 137 |
# create a new lot with with alert date in the past |
| 138 |
self.lot1_productBBB = self.LotObj.create({ |
| 139 |
'name': 'Lot 1 ProductBBB', |
| 140 |
'product_id': self.productBBB.id, |
| 141 |
'alert_date': fields.Date.to_string(datetime.today() + relativedelta(days=15)), |
| 142 |
}) |
| 143 |
|
| 144 |
picking_in = self.PickingObj.create({ |
| 145 |
'picking_type_id': self.picking_type_in.id, |
| 146 |
'location_id': self.supplier_location.id, |
| 147 |
'state': 'draft', |
| 148 |
'location_dest_id': self.stock_location.id, |
| 149 |
}) |
| 150 |
|
| 151 |
move_b = self.MoveObj.create({ |
| 152 |
'product_id': self.productBBB.id, |
| 153 |
'product_uom_qty': 44, |
| 154 |
'product_uom': self.productBBB.uom_id.id, |
| 155 |
'picking_id': picking_in.id, |
| 156 |
'location_id': self.supplier_location.id, |
| 157 |
'location_dest_id': self.stock_location.id, |
| 158 |
}) |
| 159 |
|
| 160 |
self.assertEqual(picking_in.move_ids.state, 'draft', 'Wrong state of move line.') |
| 161 |
picking_in.action_confirm() |
| 162 |
self.assertEqual(picking_in.move_ids.state, 'assigned', 'Wrong state of move line.') |
| 163 |
|
| 164 |
# Replace pack operation of incoming shipments. |
| 165 |
picking_in.action_assign() |
| 166 |
move_b.move_line_ids.quantity = 44 |
| 167 |
move_b.move_line_ids.lot_id = self.lot1_productBBB.id |
| 168 |
|
| 169 |
# Transfer Incoming Shipment. |
| 170 |
picking_in._action_done() |
| 171 |
|
| 172 |
# run scheduled tasks |
| 173 |
self.env['stock.lot']._alert_date_exceeded() |
| 174 |
|
| 175 |
# check a new activity has not been created |
| 176 |
activity_id = self.env.ref('mail.mail_activity_data_todo').id |
| 177 |
activity_count = self.env['mail.activity'].search_count([ |
| 178 |
('activity_type_id', '=', activity_id), |
| 179 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 180 |
('res_id', '=', self.lot1_productBBB.id) |
| 181 |
]) |
| 182 |
self.assertEqual(activity_count, 0, "An activity has been created while it shouldn't") |
| 183 |
|
| 184 |
def test_02_stock_production_lot(self): |
| 185 |
""" Test Scheduled Task on lot without an alert_date does not create an activity """ |
| 186 |
|
| 187 |
# create product |
| 188 |
self.productCCC = self.ProductObj.create({'name': 'Product CCC', 'is_storable': True, 'tracking': 'lot'}) |
| 189 |
|
| 190 |
# create a new lot with with alert date in the past |
| 191 |
self.lot1_productCCC = self.LotObj.create({'name': 'Lot 1 ProductCCC', 'product_id': self.productCCC.id}) |
| 192 |
|
| 193 |
picking_in = self.PickingObj.create({ |
| 194 |
'picking_type_id': self.picking_type_in.id, |
| 195 |
'location_id': self.supplier_location.id, |
| 196 |
'state': 'draft', |
| 197 |
'location_dest_id': self.stock_location.id, |
| 198 |
}) |
| 199 |
|
| 200 |
move_c = self.MoveObj.create({ |
| 201 |
'product_id': self.productCCC.id, |
| 202 |
'product_uom_qty': 44, |
| 203 |
'product_uom': self.productCCC.uom_id.id, |
| 204 |
'picking_id': picking_in.id, |
| 205 |
'location_id': self.supplier_location.id, |
| 206 |
'location_dest_id': self.stock_location.id, |
| 207 |
}) |
| 208 |
|
| 209 |
self.assertEqual(picking_in.move_ids.state, 'draft', 'Wrong state of move line.') |
| 210 |
picking_in.action_confirm() |
| 211 |
self.assertEqual(picking_in.move_ids.state, 'assigned', 'Wrong state of move line.') |
| 212 |
|
| 213 |
# Replace pack operation of incoming shipments. |
| 214 |
picking_in.action_assign() |
| 215 |
move_c.move_line_ids.quantity = 55 |
| 216 |
move_c.move_line_ids.lot_id = self.lot1_productCCC.id |
| 217 |
|
| 218 |
# Transfer Incoming Shipment. |
| 219 |
picking_in._action_done() |
| 220 |
|
| 221 |
# run scheduled tasks |
| 222 |
self.env['stock.lot']._alert_date_exceeded() |
| 223 |
|
| 224 |
# check a new activity has not been created |
| 225 |
activity_id = self.env.ref('mail.mail_activity_data_todo').id |
| 226 |
activity_count = self.env['mail.activity'].search_count([ |
| 227 |
('activity_type_id', '=', activity_id), |
| 228 |
('res_model_id', '=', self.env.ref('stock.model_stock_lot').id), |
| 229 |
('res_id', '=', self.lot1_productCCC.id) |
| 230 |
]) |
| 231 |
self.assertEqual(activity_count, 0, "An activity has been created while it shouldn't") |
| 232 |
|
| 233 |
def test_03_onchange_expiration_date(self): |
| 234 |
""" Updates the `expiration_date` of the lot production and checks other date |
| 235 |
fields are updated as well. """ |
| 236 |
def check_expiration_dates(product, lot, start_date, delta): |
| 237 |
self.assertAlmostEqual( |
| 238 |
start_date + timedelta(days=product.expiration_time), |
| 239 |
lot.expiration_date, delta=delta) |
| 240 |
self.assertAlmostEqual( |
| 241 |
lot.expiration_date - timedelta(days=product.use_time), |
| 242 |
lot.use_date, delta=delta) |
| 243 |
self.assertAlmostEqual( |
| 244 |
lot.expiration_date - timedelta(days=product.removal_time), |
| 245 |
lot.removal_date, delta=delta) |
| 246 |
self.assertAlmostEqual( |
| 247 |
lot.expiration_date - timedelta(days=product.alert_time), |
| 248 |
lot.alert_date, delta=delta) |
| 249 |
|
| 250 |
# Keeps track of the current datetime and set a delta for the compares. |
| 251 |
today_date = datetime.today() |
| 252 |
time_gap = timedelta(seconds=10) |
| 253 |
# Creates a new lot number and saves it... |
| 254 |
lot_form = Form(self.LotObj) |
| 255 |
lot_form.name = 'Apple Box #1' |
| 256 |
lot_form.product_id = self.apple_product |
| 257 |
apple_lot = lot_form.save() |
| 258 |
# ...then checks date fields have the expected values. |
| 259 |
check_expiration_dates(self.apple_product, apple_lot, today_date, time_gap) |
| 260 |
|
| 261 |
difference = timedelta(days=20) |
| 262 |
new_expiration_date = apple_lot.expiration_date + difference |
| 263 |
new_start_date = new_expiration_date - timedelta(days=self.apple_product.expiration_time) |
| 264 |
random_date = new_expiration_date + difference |
| 265 |
|
| 266 |
# Modifies the lot `expiration_date` several times, without saving... |
| 267 |
lot_form = Form(apple_lot) |
| 268 |
lot_form.expiration_date = new_expiration_date |
| 269 |
lot_form.expiration_date = random_date |
| 270 |
lot_form.expiration_date = new_expiration_date |
| 271 |
apple_lot = lot_form.save() |
| 272 |
|
| 273 |
# ...then checks all other date fields were correctly updated. |
| 274 |
check_expiration_dates(self.apple_product, apple_lot, new_start_date, time_gap) |
| 275 |
|
| 276 |
# Remove all dates, save, update expiration date twice, then save again |
| 277 |
lot_form = Form(apple_lot) |
| 278 |
lot_form.expiration_date = False |
| 279 |
lot_form.use_date = False |
| 280 |
lot_form.removal_date = False |
| 281 |
lot_form.alert_date = False |
| 282 |
lot_form.save() |
| 283 |
lot_form.expiration_date = random_date |
| 284 |
lot_form.expiration_date = new_expiration_date |
| 285 |
apple_lot = lot_form.save() |
| 286 |
|
| 287 |
# ...then check all other date fields were correctly updated. |
| 288 |
check_expiration_dates(self.apple_product, apple_lot, new_start_date, time_gap) |
| 289 |
|
| 290 |
def test_04_expiration_date_on_receipt(self): |
| 291 |
""" Test we can set an expiration date on receipt and all expiration |
| 292 |
date will be correctly set. """ |
| 293 |
partner = self.env['res.partner'].create({ |
| 294 |
'name': 'Apple\'s Joe', |
| 295 |
'company_id': self.env.ref('base.main_company').id, |
| 296 |
}) |
| 297 |
expiration_date = datetime.today() + timedelta(days=30) |
| 298 |
time_gap = timedelta(seconds=10) |
| 299 |
|
| 300 |
# Receives a tracked production using expiration date. |
| 301 |
picking_form = Form(self.env['stock.picking']) |
| 302 |
picking_form.partner_id = partner |
| 303 |
picking_form.picking_type_id = self.picking_type_in |
| 304 |
with picking_form.move_ids.new() as move: |
| 305 |
move.product_id = self.apple_product |
| 306 |
move.product_uom_qty = 4 |
| 307 |
receipt = picking_form.save() |
| 308 |
receipt.action_confirm() |
| 309 |
|
| 310 |
# Defines a date during the receipt. |
| 311 |
move_form = Form(receipt.move_ids, view="stock.view_stock_move_operations") |
| 312 |
with move_form.move_line_ids.edit(0) as line: |
| 313 |
line.lot_name = 'Apple Box #2' |
| 314 |
line.expiration_date = expiration_date |
| 315 |
move = move_form.save() |
| 316 |
|
| 317 |
move.picked = True |
| 318 |
receipt._action_done() |
| 319 |
# Get back the lot created when the picking was done... |
| 320 |
apple_lot = self.env['stock.lot'].search( |
| 321 |
[('product_id', '=', self.apple_product.id)], |
| 322 |
limit=1, |
| 323 |
) |
| 324 |
# ... and checks all date fields are correctly set. |
| 325 |
self.assertAlmostEqual( |
| 326 |
apple_lot.expiration_date, expiration_date, delta=time_gap) |
| 327 |
self.assertAlmostEqual( |
| 328 |
apple_lot.use_date, expiration_date - timedelta(days=self.apple_product.use_time), delta=time_gap) |
| 329 |
self.assertAlmostEqual( |
| 330 |
apple_lot.removal_date, expiration_date - timedelta(days=self.apple_product.removal_time), delta=time_gap) |
| 331 |
self.assertAlmostEqual( |
| 332 |
apple_lot.alert_date, expiration_date - timedelta(days=self.apple_product.alert_time), delta=time_gap) |
| 333 |
|
| 334 |
def test_04_2_expiration_date_on_receipt(self): |
| 335 |
""" Test we can set an expiration date on receipt even if all expiration |
| 336 |
date related fields aren't set on product. """ |
| 337 |
partner = self.env['res.partner'].create({ |
| 338 |
'name': 'Apple\'s Joe', |
| 339 |
'company_id': self.env.ref('base.main_company').id, |
| 340 |
}) |
| 341 |
# Unset some fields. |
| 342 |
self.apple_product.expiration_time = False |
| 343 |
self.apple_product.removal_time = False |
| 344 |
|
| 345 |
expiration_date = datetime.today() + timedelta(days=30) |
| 346 |
time_gap = timedelta(seconds=10) |
| 347 |
|
| 348 |
# Receives a tracked production using expiration date. |
| 349 |
picking_form = Form(self.env['stock.picking']) |
| 350 |
picking_form.partner_id = partner |
| 351 |
picking_form.picking_type_id = self.picking_type_in |
| 352 |
with picking_form.move_ids.new() as move: |
| 353 |
move.product_id = self.apple_product |
| 354 |
move.quantity = 4 |
| 355 |
move.picked = True |
| 356 |
receipt = picking_form.save() |
| 357 |
|
| 358 |
# Defines a date during the receipt. |
| 359 |
move = receipt.move_ids[0] |
| 360 |
line = move.move_line_ids[0] |
| 361 |
self.assertEqual(move.use_expiration_date, True) |
| 362 |
line.lot_name = 'Apple Box #3' |
| 363 |
line.expiration_date = expiration_date |
| 364 |
|
| 365 |
receipt._action_done() |
| 366 |
# Get back the lot created when the picking was done... |
| 367 |
apple_lot = self.env['stock.lot'].search( |
| 368 |
[('product_id', '=', self.apple_product.id)], |
| 369 |
limit=1, |
| 370 |
) |
| 371 |
# ... and checks all date fields are correctly set. |
| 372 |
self.assertAlmostEqual( |
| 373 |
apple_lot.expiration_date, expiration_date, delta=time_gap, |
| 374 |
msg="Must be define even if the product's `expiration_time` isn't set.") |
| 375 |
self.assertAlmostEqual( |
| 376 |
apple_lot.use_date, expiration_date - timedelta(days=self.apple_product.use_time), delta=time_gap) |
| 377 |
self.assertEqual( |
| 378 |
apple_lot.removal_date, expiration_date, |
| 379 |
"Must same as expiration_date as the `removal_time` isn't set on product.") |
| 380 |
self.assertAlmostEqual( |
| 381 |
apple_lot.alert_date, expiration_date - timedelta(days=self.apple_product.alert_time), delta=time_gap) |
| 382 |
|
| 383 |
def test_05_confirmation_on_delivery(self): |
| 384 |
""" Test when user tries to delivery expired lot, he/she gets a |
| 385 |
confirmation wizard. """ |
| 386 |
partner = self.env['res.partner'].create({ |
| 387 |
'name': 'Cider & Son', |
| 388 |
'company_id': self.env.ref('base.main_company').id, |
| 389 |
}) |
| 390 |
# Creates 3 lots (1 non-expired lot, 2 expired lots) |
| 391 |
lot_form = Form(self.LotObj) # Creates the lot. |
| 392 |
lot_form.name = 'good-apple-lot' |
| 393 |
lot_form.product_id = self.apple_product |
| 394 |
good_lot = lot_form.save() |
| 395 |
|
| 396 |
lot_form = Form(self.LotObj) # Creates the lot. |
| 397 |
lot_form.name = 'expired-apple-lot-01' |
| 398 |
lot_form.product_id = self.apple_product |
| 399 |
expired_lot_1 = lot_form.save() |
| 400 |
lot_form = Form(expired_lot_1) # Edits the lot to make it expired. |
| 401 |
lot_form.expiration_date = datetime.today() - timedelta(days=10) |
| 402 |
expired_lot_1 = lot_form.save() |
| 403 |
|
| 404 |
# Case #1: make a delivery with no expired lot. |
| 405 |
picking_form = Form(self.env['stock.picking']) |
| 406 |
picking_form.partner_id = partner |
| 407 |
picking_form.picking_type_id = self.picking_type_out |
| 408 |
with picking_form.move_ids.new() as move: |
| 409 |
move.product_id = self.apple_product |
| 410 |
move.product_uom_qty = 4 |
| 411 |
# Saves and confirms it... |
| 412 |
delivery_1 = picking_form.save() |
| 413 |
delivery_1.action_confirm() |
| 414 |
# ... then create a move line with the non-expired lot and valids the picking. |
| 415 |
delivery_1.move_line_ids = [(5, 0), (0, 0, { |
| 416 |
'company_id': self.env.company.id, |
| 417 |
'location_id': delivery_1.move_ids.location_id.id, |
| 418 |
'location_dest_id': delivery_1.move_ids.location_dest_id.id, |
| 419 |
'lot_id': good_lot.id, |
| 420 |
'product_id': self.apple_product.id, |
| 421 |
'product_uom_id': self.apple_product.uom_id.id, |
| 422 |
'quantity': 4, |
| 423 |
})] |
| 424 |
delivery_1.move_ids.picked = True |
| 425 |
res = delivery_1.button_validate() |
| 426 |
# Validate a delivery for good products must not raise anything. |
| 427 |
self.assertEqual(res, True) |
| 428 |
|
| 429 |
# Case #2: make a delivery with one non-expired lot and one expired lot. |
| 430 |
picking_form = Form(self.env['stock.picking']) |
| 431 |
picking_form.partner_id = partner |
| 432 |
picking_form.picking_type_id = self.picking_type_out |
| 433 |
with picking_form.move_ids.new() as move: |
| 434 |
move.product_id = self.apple_product |
| 435 |
move.product_uom_qty = 8 |
| 436 |
# Saves and confirms it... |
| 437 |
delivery_2 = picking_form.save() |
| 438 |
delivery_2.action_confirm() |
| 439 |
# ... then create a move line for the non-expired lot and for an expired |
| 440 |
# lot and valids the picking. |
| 441 |
delivery_2.move_line_ids = [(5, 0), (0, 0, { |
| 442 |
'company_id': self.env.company.id, |
| 443 |
'location_id': delivery_2.move_ids.location_id.id, |
| 444 |
'location_dest_id': delivery_2.move_ids.location_dest_id.id, |
| 445 |
'lot_id': good_lot.id, |
| 446 |
'product_id': self.apple_product.id, |
| 447 |
'product_uom_id': self.apple_product.uom_id.id, |
| 448 |
'quantity': 4, |
| 449 |
}), (0, 0, { |
| 450 |
'company_id': self.env.company.id, |
| 451 |
'location_id': delivery_2.move_ids.location_id.id, |
| 452 |
'location_dest_id': delivery_2.move_ids.location_dest_id.id, |
| 453 |
'lot_id': expired_lot_1.id, |
| 454 |
'product_id': self.apple_product.id, |
| 455 |
'product_uom_id': self.apple_product.uom_id.id, |
| 456 |
'quantity': 4, |
| 457 |
})] |
| 458 |
delivery_2.move_ids.picked = True |
| 459 |
res = delivery_2.button_validate() |
| 460 |
# Validate a delivery containing expired products must raise a confirmation wizard. |
| 461 |
self.assertNotEqual(res, True) |
| 462 |
self.assertEqual(res['res_model'], 'expiry.picking.confirmation') |
| 463 |
|
| 464 |
# Case #3: make a delivery with only on expired lot. |
| 465 |
picking_form = Form(self.env['stock.picking']) |
| 466 |
picking_form.partner_id = partner |
| 467 |
picking_form.picking_type_id = self.picking_type_out |
| 468 |
with picking_form.move_ids.new() as move: |
| 469 |
move.product_id = self.apple_product |
| 470 |
move.product_uom_qty = 4 |
| 471 |
# Saves and confirms it... |
| 472 |
delivery_3 = picking_form.save() |
| 473 |
delivery_3.action_confirm() |
| 474 |
# ... then create two move lines with expired lot and valids the picking. |
| 475 |
delivery_3.move_line_ids = [(5, 0), (0, 0, { |
| 476 |
'company_id': self.env.company.id, |
| 477 |
'location_id': delivery_3.move_ids.location_id.id, |
| 478 |
'location_dest_id': delivery_3.move_ids.location_dest_id.id, |
| 479 |
'lot_id': expired_lot_1.id, |
| 480 |
'product_id': self.apple_product.id, |
| 481 |
'product_uom_id': self.apple_product.uom_id.id, |
| 482 |
'quantity': 4, |
| 483 |
})] |
| 484 |
delivery_3.move_ids.picked = True |
| 485 |
res = delivery_3.button_validate() |
| 486 |
# Validate a delivery containing expired products must raise a confirmation wizard. |
| 487 |
self.assertNotEqual(res, True) |
| 488 |
self.assertEqual(res['res_model'], 'expiry.picking.confirmation') |
| 489 |
|
| 490 |
def test_edit_removal_date_in_inventory_mode(self): |
| 491 |
""" Try to edit removal_date with the inventory mode. |
| 492 |
""" |
| 493 |
user_group_stock_manager = self.env.ref('stock.group_stock_manager') |
| 494 |
self.demo_user = mail_new_test_user( |
| 495 |
self.env, |
| 496 |
name='Demo user', |
| 497 |
login='userdemo', |
| 498 |
email='[email protected]', |
| 499 |
groups='stock.group_stock_manager', |
| 500 |
) |
| 501 |
lot_form = Form(self.LotObj) |
| 502 |
lot_form.name = 'LOT001' |
| 503 |
lot_form.product_id = self.apple_product |
| 504 |
apple_lot = lot_form.save() |
| 505 |
|
| 506 |
quant = self.StockQuantObj.with_context(inventory_mode=True).create({ |
| 507 |
'product_id': self.apple_product.id, |
| 508 |
'location_id': self.stock_location.id, |
| 509 |
'quantity': 10, |
| 510 |
'lot_id': apple_lot.id, |
| 511 |
}) |
| 512 |
# Try to write on quant with inventory mode |
| 513 |
new_date = datetime.today() + timedelta(days=15) |
| 514 |
quant.with_user(self.demo_user).with_context(inventory_mode=True).write({'removal_date': new_date}) |
| 515 |
self.assertEqual(quant.removal_date, new_date) |
| 516 |
|
| 517 |
def test_apply_lot_date_on_sml(self): |
| 518 |
""" |
| 519 |
When assigning a lot to a SML, if the lot has an expiration date, |
| 520 |
the latter should be applied on the SML |
| 521 |
""" |
| 522 |
exp_date = fields.Datetime.today() + relativedelta(days=15) |
| 523 |
sml_exp_date = fields.Datetime.today() + relativedelta(days=10) |
| 524 |
|
| 525 |
lot = self.env['stock.lot'].create({ |
| 526 |
'name': 'Lot 1', |
| 527 |
'product_id': self.apple_product.id, |
| 528 |
'expiration_date': fields.Datetime.to_string(exp_date), |
| 529 |
}) |
| 530 |
|
| 531 |
move = self.env['stock.move'].create({ |
| 532 |
'location_id': self.supplier_location.id, |
| 533 |
'location_dest_id': self.stock_location.id, |
| 534 |
'product_id': self.apple_product.id, |
| 535 |
'product_uom': self.apple_product.uom_id.id, |
| 536 |
}) |
| 537 |
sml = self.env['stock.move.line'].create({ |
| 538 |
'location_id': self.supplier_location.id, |
| 539 |
'location_dest_id': self.stock_location.id, |
| 540 |
'product_id': self.apple_product.id, |
| 541 |
'quantity': 3, |
| 542 |
'product_uom_id': self.apple_product.uom_id.id, |
| 543 |
'expiration_date': fields.Datetime.to_string(sml_exp_date), |
| 544 |
'company_id': self.env.company.id, |
| 545 |
'move_id': move.id, |
| 546 |
}) |
| 547 |
self.assertEqual(sml.expiration_date, sml_exp_date) |
| 548 |
|
| 549 |
sml.lot_id = lot |
| 550 |
self.assertEqual(sml.expiration_date, exp_date) |
| 551 |
|
| 552 |
def test_apply_same_date_on_expiry_fields(self): |
| 553 |
expiration_time = 10 |
| 554 |
self.apple_product.write({ |
| 555 |
'expiration_time': expiration_time, |
| 556 |
'use_time': 0, |
| 557 |
'removal_time': 0, |
| 558 |
'alert_time': 0, |
| 559 |
}) |
| 560 |
|
| 561 |
lot = self.env['stock.lot'].create({ |
| 562 |
'product_id': self.apple_product.id, |
| 563 |
}) |
| 564 |
|
| 565 |
delta = timedelta(seconds=10) |
| 566 |
expiration_date = datetime.today() + timedelta(days=expiration_time) |
| 567 |
err_msg = "The time on the product is set to 0, it means that the corresponding date should be the same as the expiration one" |
| 568 |
self.assertAlmostEqual(lot.expiration_date, expiration_date, delta=delta) |
| 569 |
self.assertAlmostEqual(lot.use_date, expiration_date, delta=delta, msg=err_msg) |
| 570 |
self.assertAlmostEqual(lot.removal_date, expiration_date, delta=delta, msg=err_msg) |
| 571 |
self.assertAlmostEqual(lot.alert_date, expiration_date, delta=delta, msg=err_msg) |
| 572 |
|
| 573 |
def test_no_expiration_date(self): |
| 574 |
""" |
| 575 |
When use_expiration_date is set to True on the Product, but the lot have an expiration_date set to False, |
| 576 |
the picking should be able to reserve on it because it is considered as 'non-perishable' |
| 577 |
""" |
| 578 |
lot_form = Form(self.LotObj) |
| 579 |
lot_form.name = 'LOT001' |
| 580 |
lot_form.product_id = self.apple_product |
| 581 |
apple_lot = lot_form.save() |
| 582 |
|
| 583 |
lot_form = Form(apple_lot) |
| 584 |
lot_form.expiration_date = False |
| 585 |
lot_form.use_date = False |
| 586 |
lot_form.removal_date = False |
| 587 |
lot_form.alert_date = False |
| 588 |
apple_lot = lot_form.save() |
| 589 |
|
| 590 |
self.StockQuantObj.with_context(inventory_mode=True).create({ |
| 591 |
'product_id': self.apple_product.id, |
| 592 |
'location_id': self.stock_location.id, |
| 593 |
'quantity': 100, |
| 594 |
'lot_id': apple_lot.id, |
| 595 |
}) |
| 596 |
|
| 597 |
self.assertEqual(self.apple_product.qty_available, 100, 'Wrong quantity.') |
| 598 |
|
| 599 |
picking_out = self.PickingObj.create({ |
| 600 |
'picking_type_id': self.picking_type_out.id, |
| 601 |
'location_id': self.stock_location.id, |
| 602 |
'location_dest_id': self.customer_location.id, |
| 603 |
'state': 'draft', |
| 604 |
}) |
| 605 |
|
| 606 |
self.MoveObj.create({ |
| 607 |
'product_id': self.apple_product.id, |
| 608 |
'product_uom_qty': 10, |
| 609 |
'product_uom': self.apple_product.uom_id.id, |
| 610 |
'picking_id': picking_out.id, |
| 611 |
'location_id': self.stock_location.id, |
| 612 |
'location_dest_id': self.customer_location.id, |
| 613 |
}) |
| 614 |
|
| 615 |
self.assertEqual(picking_out.move_ids.state, 'draft', 'Wrong state of move line.') |
| 616 |
picking_out.action_confirm() |
| 617 |
picking_out.action_assign() |
| 618 |
self.assertEqual(picking_out.move_ids.state, 'assigned', 'Wrong state of move line.') |
| 619 |
|
| 620 |
def test_no_lot(self): |
| 621 |
""" |
| 622 |
Try to reserve a move that for an expirable product that has both quants with and without lot attached. |
| 623 |
""" |
| 624 |
# Set the removal strategy to 'First Expiry First Out' |
| 625 |
fefo_strategy = self.env['product.removal'].search( |
| 626 |
[('method', '=', 'fefo')]) |
| 627 |
self.apple_product.categ_id.removal_strategy_id = fefo_strategy.id |
| 628 |
|
| 629 |
apple_lot = self.LotObj.create({ |
| 630 |
'name': 'LOT001', |
| 631 |
'product_id': self.apple_product.id, |
| 632 |
}) |
| 633 |
|
| 634 |
self.StockQuantObj.with_context(inventory_mode=True).create([{ |
| 635 |
'product_id': self.apple_product.id, |
| 636 |
'location_id': self.stock_location.id, |
| 637 |
'quantity': 100, |
| 638 |
}, { |
| 639 |
'product_id': self.apple_product.id, |
| 640 |
'location_id': self.stock_location.id, |
| 641 |
'quantity': 100, |
| 642 |
'lot_id': apple_lot.id, |
| 643 |
}]) |
| 644 |
|
| 645 |
with Form(self.PickingObj) as picking_form: |
| 646 |
picking_form.picking_type_id = self.picking_type_out |
| 647 |
with picking_form.move_ids.new() as move: |
| 648 |
move.product_id = self.apple_product |
| 649 |
move.product_uom_qty = 10 |
| 650 |
picking_out = picking_form.save() |
| 651 |
|
| 652 |
picking_out.action_assign() |
| 653 |
self.assertEqual(picking_out.move_line_ids.lot_id, apple_lot) |
| 654 |
|
| 655 |
def test_compute_expiration_date_from_scheduled_date(self): |
| 656 |
partner = self.env['res.partner'].create({ |
| 657 |
'name': 'Apple\'s Joe', |
| 658 |
'company_id': self.env.ref('base.main_company').id, |
| 659 |
}) |
| 660 |
|
| 661 |
delta = timedelta(seconds=10) |
| 662 |
new_date = datetime.today() + timedelta(days=42) |
| 663 |
expiration_date = new_date + timedelta(days=self.apple_product.expiration_time) |
| 664 |
|
| 665 |
picking_form = Form(self.env['stock.picking']) |
| 666 |
picking_form.partner_id = partner |
| 667 |
picking_form.scheduled_date = new_date |
| 668 |
picking_form.picking_type_id = self.picking_type_in |
| 669 |
|
| 670 |
with picking_form.move_ids.new() as move: |
| 671 |
move.product_id = self.apple_product |
| 672 |
move.product_uom_qty = 4 |
| 673 |
delivery = picking_form.save() |
| 674 |
delivery.action_confirm() |
| 675 |
|
| 676 |
self.assertAlmostEqual(delivery.move_line_ids[0].expiration_date, expiration_date, delta=delta) |
| 677 |
|
| 678 |
def test_compute_display_name(self): |
| 679 |
apple_lot1 = self.LotObj.create({ |
| 680 |
'name': 'LOT-00001', |
| 681 |
'product_id': self.apple_product.id, |
| 682 |
'expiration_date': False, |
| 683 |
'alert_date': False, |
| 684 |
}) |
| 685 |
apple_lot2 = self.LotObj.create({ |
| 686 |
'name': 'LOT-00002', |
| 687 |
'product_id': self.apple_product.id, |
| 688 |
'expiration_date': datetime.today() - timedelta(days=10), |
| 689 |
}) |
| 690 |
apple_lot3 = self.LotObj.create({ |
| 691 |
'name': 'LOT-00003', |
| 692 |
'product_id': self.apple_product.id, |
| 693 |
'alert_date': datetime.today() - timedelta(days=10), |
| 694 |
}) |
| 695 |
self.assertEqual(apple_lot1.with_context(formatted_display_name=True).display_name, "LOT-00001") |
| 696 |
self.assertEqual(apple_lot2.with_context(formatted_display_name=True).display_name, "LOT-00002\t--Expired--") |
| 697 |
self.assertEqual(apple_lot3.with_context(formatted_display_name=True).display_name, "LOT-00003\t--Expire on " + fields.Datetime.to_string(apple_lot3.expiration_date) + "--") |
| 698 |
|
| 699 |
def test_proceed_except_expired_delivery_without_move_removal_date(self): |
| 700 |
lot = self.LotObj.create({ |
| 701 |
'name': 'LOT-001', |
| 702 |
'product_id': self.apple_product.id, |
| 703 |
}) |
| 704 |
lot.removal_date = False |
| 705 |
|
| 706 |
self.StockQuantObj.with_context(inventory_mode=True).create({ |
| 707 |
'product_id': self.apple_product.id, |
| 708 |
'location_id': self.stock_location.id, |
| 709 |
'quantity': 100, |
| 710 |
'lot_id': lot.id, |
| 711 |
}) |
| 712 |
picking = self.PickingObj.create({ |
| 713 |
'partner_id': self.partner_1.id, |
| 714 |
'picking_type_id': self.picking_type_out.id, |
| 715 |
'move_ids': [Command.create({ |
| 716 |
'product_id': self.apple_product.id, |
| 717 |
'product_uom_qty': 2, |
| 718 |
})], |
| 719 |
}) |
| 720 |
picking.button_validate() |
| 721 |
context = { |
| 722 |
'button_validate_picking_ids': [picking.id], |
| 723 |
'default_picking_ids': [picking.id], |
| 724 |
'default_lot_ids': [lot.id], |
| 725 |
} |
| 726 |
wizard = self.env['expiry.picking.confirmation'].with_context(context).create({}) |
| 727 |
self.assertFalse(wizard.picking_ids.move_line_ids.removal_date) |
| 728 |
wizard.process_no_expired() |