|
2 | 2 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
3 | 3 |
|
4 | 4 | from odoo.addons.base.tests.common import BaseCommon |
| 5 | +from odoo.exceptions import UserError |
| 6 | +from odoo.tests import Form |
5 | 7 |
|
6 | 8 |
|
7 | 9 | class TestSaleProductPack(BaseCommon): |
@@ -221,6 +223,106 @@ def qty_in_order(): |
221 | 223 | total_qty_confirmed = qty_in_order() |
222 | 224 | self.assertAlmostEqual(total_qty_updated * 2, total_qty_confirmed) |
223 | 225 |
|
| 226 | + |
| 227 | + def test_update_qty_do_not_expand(self): |
| 228 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 229 | + main_sol = self.env["sale.order.line"].create( |
| 230 | + { |
| 231 | + "order_id": self.sale_order.id, |
| 232 | + "name": product_cp.name, |
| 233 | + "product_id": product_cp.id, |
| 234 | + "product_uom_qty": 1, |
| 235 | + } |
| 236 | + ) |
| 237 | + main_sol.with_context(update_prices=True).product_uom_qty = 2 |
| 238 | + self.assertTrue( |
| 239 | + all( |
| 240 | + self.sale_order.order_line.filtered( |
| 241 | + lambda sol: sol.pack_parent_line_id == main_sol |
| 242 | + ).mapped(lambda sol: sol.product_uom_qty == 1) |
| 243 | + ), |
| 244 | + ) |
| 245 | + |
| 246 | + def test_update_pack_qty_with_new_component(self): |
| 247 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 248 | + main_sol = self.env["sale.order.line"].create( |
| 249 | + { |
| 250 | + "order_id": self.sale_order.id, |
| 251 | + "name": product_cp.name, |
| 252 | + "product_id": product_cp.id, |
| 253 | + "product_uom_qty": 1, |
| 254 | + } |
| 255 | + ) |
| 256 | + |
| 257 | + self.assertEqual( |
| 258 | + sum( |
| 259 | + self.sale_order.order_line.filtered( |
| 260 | + lambda sol: sol.pack_parent_line_id == main_sol |
| 261 | + ).mapped("product_uom_qty") |
| 262 | + ), |
| 263 | + 3, |
| 264 | + "Expected 3 lines with quantity 1 while setup this test", |
| 265 | + ) |
| 266 | + |
| 267 | + product_cp.pack_line_ids |= self.env["product.pack.line"].create( |
| 268 | + { |
| 269 | + "parent_product_id": product_cp.id, |
| 270 | + "product_id": self.env.ref("product.product_product_12").id, |
| 271 | + "quantity": 2, |
| 272 | + } |
| 273 | + ) |
| 274 | + |
| 275 | + main_sol.product_uom_qty = 2 |
| 276 | + self.assertEqual( |
| 277 | + sum( |
| 278 | + self.sale_order.order_line.filtered( |
| 279 | + lambda sol: sol.pack_parent_line_id == main_sol |
| 280 | + ).mapped("product_uom_qty") |
| 281 | + ), |
| 282 | + 10, |
| 283 | + "Expected 3 lines with quantity 2 and new component line with quantity 4", |
| 284 | + ) |
| 285 | + |
| 286 | + def test_update_pack_qty_with_new_component_do_not_expand(self): |
| 287 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 288 | + main_sol = self.env["sale.order.line"].create( |
| 289 | + { |
| 290 | + "order_id": self.sale_order.id, |
| 291 | + "name": product_cp.name, |
| 292 | + "product_id": product_cp.id, |
| 293 | + "product_uom_qty": 1, |
| 294 | + } |
| 295 | + ) |
| 296 | + |
| 297 | + self.assertEqual( |
| 298 | + sum( |
| 299 | + self.sale_order.order_line.filtered( |
| 300 | + lambda sol: sol.pack_parent_line_id == main_sol |
| 301 | + ).mapped("product_uom_qty") |
| 302 | + ), |
| 303 | + 3, |
| 304 | + "Expected 3 lines with quantity 1 while setup this test", |
| 305 | + ) |
| 306 | + |
| 307 | + product_cp.pack_line_ids |= self.env["product.pack.line"].create( |
| 308 | + { |
| 309 | + "parent_product_id": product_cp.id, |
| 310 | + "product_id": self.env.ref("product.product_product_12").id, |
| 311 | + "quantity": 2, |
| 312 | + } |
| 313 | + ) |
| 314 | + |
| 315 | + main_sol.with_context(update_prices=True).product_uom_qty = 2 |
| 316 | + self.assertEqual( |
| 317 | + sum( |
| 318 | + self.sale_order.order_line.filtered( |
| 319 | + lambda sol: sol.pack_parent_line_id == main_sol |
| 320 | + ).mapped("product_uom_qty") |
| 321 | + ), |
| 322 | + 3, |
| 323 | + "Expected 3 lines with quantity 2 and no new component line", |
| 324 | + ) |
| 325 | + |
224 | 326 | def test_do_not_expand(self): |
225 | 327 | product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
226 | 328 | pack_line = self.env["sale.order.line"].create( |
@@ -370,3 +472,92 @@ def test_compute_discount_for_detailed_packs(self): |
370 | 472 | [19, 19, 10], |
371 | 473 | "Discounts for the pack lines are not calculated correctly.", |
372 | 474 | ) |
| 475 | + |
| 476 | + def test_copy_sale_order_with_detailed_product_pack(self): |
| 477 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 478 | + self.env["sale.order.line"].create( |
| 479 | + { |
| 480 | + "order_id": self.sale_order.id, |
| 481 | + "name": product_cp.name, |
| 482 | + "product_id": product_cp.id, |
| 483 | + "product_uom_qty": 1, |
| 484 | + } |
| 485 | + ) |
| 486 | + copied_order = self.sale_order.copy() |
| 487 | + copied_order_component_lines_pack_line = copied_order.order_line.filtered( |
| 488 | + lambda line: line.product_id.pack_ok |
| 489 | + ) |
| 490 | + copied_order_component_lines = copied_order.order_line.filtered( |
| 491 | + lambda line: line.pack_parent_line_id |
| 492 | + ) |
| 493 | + self.assertEqual( |
| 494 | + copied_order_component_lines.pack_parent_line_id, |
| 495 | + copied_order_component_lines_pack_line, |
| 496 | + ) |
| 497 | + |
| 498 | + def test_check_pack_line_unlink(self): |
| 499 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 500 | + self.env["sale.order.line"].create( |
| 501 | + { |
| 502 | + "order_id": self.sale_order.id, |
| 503 | + "name": product_cp.name, |
| 504 | + "product_id": product_cp.id, |
| 505 | + "product_uom_qty": 1, |
| 506 | + } |
| 507 | + ) |
| 508 | + with Form(self.sale_order) as so_form: |
| 509 | + with self.assertRaisesRegex( |
| 510 | + UserError, |
| 511 | + "You cannot delete this line because is part of a pack in this " |
| 512 | + "sale order. In order to delete this line you need to delete the " |
| 513 | + "pack itself", |
| 514 | + ): |
| 515 | + so_form.order_line.remove(len(self.sale_order.order_line) - 1) |
| 516 | + |
| 517 | + def test_unlink_pack_form_proxy(self): |
| 518 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 519 | + self.env["sale.order.line"].create( |
| 520 | + { |
| 521 | + "order_id": self.sale_order.id, |
| 522 | + "name": product_cp.name, |
| 523 | + "product_id": product_cp.id, |
| 524 | + "product_uom_qty": 1, |
| 525 | + } |
| 526 | + ) |
| 527 | + with Form(self.sale_order) as so_form: |
| 528 | + so_form.order_line.remove(0) |
| 529 | + so_form.save() |
| 530 | + self.assertEqual(len(self.sale_order.order_line), 0) |
| 531 | + |
| 532 | + def test_unlink_pack_record_unlink(self): |
| 533 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 534 | + self.env["sale.order.line"].create( |
| 535 | + { |
| 536 | + "order_id": self.sale_order.id, |
| 537 | + "name": product_cp.name, |
| 538 | + "product_id": product_cp.id, |
| 539 | + "product_uom_qty": 1, |
| 540 | + } |
| 541 | + ) |
| 542 | + pack_line = self.sale_order.order_line.filtered( |
| 543 | + lambda line: line.product_id.pack_ok |
| 544 | + ) |
| 545 | + pack_line.unlink() |
| 546 | + self.assertEqual(len(self.sale_order.order_line), 0) |
| 547 | + |
| 548 | + def test_unlink_pack_old_style_like_ui(self): |
| 549 | + product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") |
| 550 | + self.env["sale.order.line"].create( |
| 551 | + { |
| 552 | + "order_id": self.sale_order.id, |
| 553 | + "name": product_cp.name, |
| 554 | + "product_id": product_cp.id, |
| 555 | + "product_uom_qty": 1, |
| 556 | + } |
| 557 | + ) |
| 558 | + pack_line = self.sale_order.order_line.filtered( |
| 559 | + lambda line: line.product_id.pack_ok |
| 560 | + ) |
| 561 | + self.sale_order.write({"order_line": [(2, pack_line.id)]}) |
| 562 | + self.assertEqual(len(self.sale_order.order_line), 0) |
| 563 | + |
0 commit comments