test_product.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. # Author: Leonardo Pistone
  4. # Copyright 2015 Camptocamp SA
  5. from odoo.addons.stock.tests.common2 import TestStockCommon
  6. from odoo.exceptions import UserError
  7. from odoo.tests.common import Form
  8. class TestVirtualAvailable(TestStockCommon):
  9. @classmethod
  10. def setUpClass(cls):
  11. super().setUpClass()
  12. # Make `product3` a storable product for this test. Indeed, creating quants
  13. # and playing with owners is not possible for consumables.
  14. cls.product_3.type = 'product'
  15. cls.env['stock.picking.type'].browse(cls.env.ref('stock.picking_type_out').id).reservation_method = 'manual'
  16. cls.env['stock.quant'].create({
  17. 'product_id': cls.product_3.id,
  18. 'location_id': cls.env.ref('stock.stock_location_stock').id,
  19. 'quantity': 30.0})
  20. cls.env['stock.quant'].create({
  21. 'product_id': cls.product_3.id,
  22. 'location_id': cls.env.ref('stock.stock_location_stock').id,
  23. 'quantity': 10.0,
  24. 'owner_id': cls.user_stock_user.partner_id.id})
  25. cls.picking_out = cls.env['stock.picking'].create({'picking_type_id': cls.env.ref('stock.picking_type_out').id})
  26. cls.env['stock.move'].create({
  27. 'name': 'a move',
  28. 'product_id': cls.product_3.id,
  29. 'product_uom_qty': 3.0,
  30. 'product_uom': cls.product_3.uom_id.id,
  31. 'picking_id': cls.picking_out.id,
  32. 'location_id': cls.env.ref('stock.stock_location_stock').id,
  33. 'location_dest_id': cls.env.ref('stock.stock_location_customers').id})
  34. cls.picking_out_2 = cls.env['stock.picking'].create({
  35. 'picking_type_id': cls.env.ref('stock.picking_type_out').id})
  36. cls.env['stock.move'].create({
  37. 'restrict_partner_id': cls.user_stock_user.partner_id.id,
  38. 'name': 'another move',
  39. 'product_id': cls.product_3.id,
  40. 'product_uom_qty': 5.0,
  41. 'product_uom': cls.product_3.uom_id.id,
  42. 'picking_id': cls.picking_out_2.id,
  43. 'location_id': cls.env.ref('stock.stock_location_stock').id,
  44. 'location_dest_id': cls.env.ref('stock.stock_location_customers').id})
  45. def test_without_owner(self):
  46. self.assertAlmostEqual(40.0, self.product_3.virtual_available)
  47. self.picking_out.action_assign()
  48. self.picking_out_2.action_assign()
  49. self.assertAlmostEqual(32.0, self.product_3.virtual_available)
  50. def test_with_owner(self):
  51. prod_context = self.product_3.with_context(owner_id=self.user_stock_user.partner_id.id)
  52. self.assertAlmostEqual(10.0, prod_context.virtual_available)
  53. self.picking_out.action_assign()
  54. self.picking_out_2.action_assign()
  55. self.assertAlmostEqual(5.0, prod_context.virtual_available)
  56. def test_free_quantity(self):
  57. """ Test the value of product.free_qty. Free_qty = qty_on_hand - qty_reserved"""
  58. self.assertAlmostEqual(40.0, self.product_3.free_qty)
  59. self.picking_out.action_confirm()
  60. self.picking_out_2.action_confirm()
  61. # No reservation so free_qty is unchanged
  62. self.assertAlmostEqual(40.0, self.product_3.free_qty)
  63. self.picking_out.action_assign()
  64. self.picking_out_2.action_assign()
  65. # 8 units are now reserved
  66. self.assertAlmostEqual(32.0, self.product_3.free_qty)
  67. self.picking_out.do_unreserve()
  68. self.picking_out_2.do_unreserve()
  69. # 8 units are available again
  70. self.assertAlmostEqual(40.0, self.product_3.free_qty)
  71. def test_archive_product_1(self):
  72. """`qty_available` and `virtual_available` are computed on archived products"""
  73. self.assertTrue(self.product_3.active)
  74. self.assertAlmostEqual(40.0, self.product_3.qty_available)
  75. self.assertAlmostEqual(40.0, self.product_3.virtual_available)
  76. self.product_3.active = False
  77. self.assertAlmostEqual(40.0, self.product_3.qty_available)
  78. self.assertAlmostEqual(40.0, self.product_3.virtual_available)
  79. def test_archive_product_2(self):
  80. """Archiving a product should archive its reordering rules"""
  81. self.assertTrue(self.product_3.active)
  82. orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
  83. orderpoint_form.product_id = self.product_3
  84. orderpoint_form.location_id = self.env.ref('stock.stock_location_stock')
  85. orderpoint_form.product_min_qty = 0.0
  86. orderpoint_form.product_max_qty = 5.0
  87. orderpoint = orderpoint_form.save()
  88. self.assertTrue(orderpoint.active)
  89. self.product_3.active = False
  90. self.assertFalse(orderpoint.active)
  91. def test_change_product_company(self):
  92. """ Checks we can't change the product's company if this product has
  93. quant in another company. """
  94. company1 = self.env.ref('base.main_company')
  95. company2 = self.env['res.company'].create({'name': 'Second Company'})
  96. product = self.env['product.product'].create({
  97. 'name': 'Product [TEST - Change Company]',
  98. 'type': 'product',
  99. })
  100. # Creates a quant for productA in the first company.
  101. self.env['stock.quant'].create({
  102. 'product_id': product.id,
  103. 'product_uom_id': self.uom_unit.id,
  104. 'location_id': self.location_1.id,
  105. 'quantity': 7,
  106. 'reserved_quantity': 0,
  107. })
  108. # Assigns a company: should be OK for company1 but should raise an error for company2.
  109. product.company_id = company1.id
  110. with self.assertRaises(UserError):
  111. product.company_id = company2.id
  112. # Checks we can assing company2 for the product once there is no more quant for it.
  113. quant = self.env['stock.quant'].search([('product_id', '=', product.id)])
  114. quant.quantity = 0
  115. self.env['stock.quant']._unlink_zero_quants()
  116. product.company_id = company2.id # Should work this time.
  117. def test_change_product_company_02(self):
  118. """ Checks we can't change the product's company if this product has
  119. stock move line in another company. """
  120. company1 = self.env.ref('base.main_company')
  121. company2 = self.env['res.company'].create({'name': 'Second Company'})
  122. product = self.env['product.product'].create({
  123. 'name': 'Product [TEST - Change Company]',
  124. 'type': 'consu',
  125. })
  126. picking = self.env['stock.picking'].create({
  127. 'location_id': self.env.ref('stock.stock_location_customers').id,
  128. 'location_dest_id': self.env.ref('stock.stock_location_stock').id,
  129. 'picking_type_id': self.ref('stock.picking_type_in'),
  130. })
  131. self.env['stock.move'].create({
  132. 'name': 'test',
  133. 'location_id': self.env.ref('stock.stock_location_customers').id,
  134. 'location_dest_id': self.env.ref('stock.stock_location_stock').id,
  135. 'product_id': product.id,
  136. 'product_uom': product.uom_id.id,
  137. 'product_uom_qty': 1,
  138. 'picking_id': picking.id,
  139. })
  140. picking.action_confirm()
  141. wizard_data = picking.button_validate()
  142. wizard = Form(self.env[wizard_data['res_model']].with_context(wizard_data['context'])).save()
  143. wizard.process()
  144. product.company_id = company1.id
  145. with self.assertRaises(UserError):
  146. product.company_id = company2.id
  147. def test_change_product_company_exclude_vendor_and_customer_location(self):
  148. """ Checks we can change product company where only exist single company
  149. and exist quant in vendor/customer location"""
  150. company1 = self.env.ref('base.main_company')
  151. customer_location = self.env.ref('stock.stock_location_customers')
  152. supplier_location = self.env.ref('stock.stock_location_suppliers')
  153. product = self.env['product.product'].create({
  154. 'name': 'Product Single Company',
  155. 'type': 'product',
  156. })
  157. # Creates a quant for company 1.
  158. self.env['stock.quant'].create({
  159. 'product_id': product.id,
  160. 'product_uom_id': self.uom_unit.id,
  161. 'location_id': self.location_1.id,
  162. 'quantity': 5,
  163. })
  164. # Creates a quant for vendor location.
  165. self.env['stock.quant'].create({
  166. 'product_id': product.id,
  167. 'product_uom_id': self.uom_unit.id,
  168. 'location_id': supplier_location.id,
  169. 'quantity': -15,
  170. })
  171. # Creates a quant for customer location.
  172. self.env['stock.quant'].create({
  173. 'product_id': product.id,
  174. 'product_uom_id': self.uom_unit.id,
  175. 'location_id': customer_location.id,
  176. 'quantity': 10,
  177. })
  178. # Assigns a company: should be ok because only exist one company (exclude vendor and customer location)
  179. product.company_id = company1.id
  180. # Reset product company to empty
  181. product.company_id = False
  182. company2 = self.env['res.company'].create({'name': 'Second Company'})
  183. # Assigns to another company: should be not okay because exist quants in defferent company (exclude vendor and customer location)
  184. with self.assertRaises(UserError):
  185. product.company_id = company2.id
  186. def test_search_qty_available(self):
  187. product = self.env['product.product'].create({
  188. 'name': 'Brand new product',
  189. 'type': 'product',
  190. })
  191. result = self.env['product.product'].search([
  192. ('qty_available', '=', 0),
  193. ('id', 'in', product.ids),
  194. ])
  195. self.assertEqual(product, result)
  196. def test_search_product_template(self):
  197. """
  198. Suppose a variant V01 that can not be deleted because it is used by a
  199. lot [1]. Then, the variant's template T is changed: we add a dynamic
  200. attribute. Because of [1], V01 is archived. This test ensures that
  201. `name_search` still finds T.
  202. Then, we create a new variant V02 of T. This test also ensures that
  203. calling `name_search` with a negative operator will exclude T from the
  204. result.
  205. """
  206. template = self.env['product.template'].create({
  207. 'name': 'Super Product',
  208. })
  209. product01 = template.product_variant_id
  210. self.env['stock.lot'].create({
  211. 'name': 'lot1',
  212. 'product_id': product01.id,
  213. 'company_id': self.env.company.id,
  214. })
  215. product_attribute = self.env['product.attribute'].create({
  216. 'name': 'PA',
  217. 'create_variant': 'dynamic'
  218. })
  219. self.env['product.attribute.value'].create([{
  220. 'name': 'PAV' + str(i),
  221. 'attribute_id': product_attribute.id
  222. } for i in range(2)])
  223. tmpl_attr_lines = self.env['product.template.attribute.line'].create({
  224. 'attribute_id': product_attribute.id,
  225. 'product_tmpl_id': product01.product_tmpl_id.id,
  226. 'value_ids': [(6, 0, product_attribute.value_ids.ids)],
  227. })
  228. self.assertFalse(product01.active)
  229. self.assertTrue(template.active)
  230. self.assertFalse(template.product_variant_ids)
  231. res = self.env['product.template'].name_search(name='super', operator='ilike')
  232. res_ids = [r[0] for r in res]
  233. self.assertIn(template.id, res_ids)
  234. product02 = self.env['product.product'].create({
  235. 'default_code': '123',
  236. 'product_tmpl_id': template.id,
  237. 'product_template_attribute_value_ids': [(6, 0, tmpl_attr_lines.product_template_value_ids[0].ids)]
  238. })
  239. self.assertFalse(product01.active)
  240. self.assertTrue(product02.active)
  241. self.assertTrue(template)
  242. self.assertEqual(template.product_variant_ids, product02)
  243. res = self.env['product.template'].name_search(name='123', operator='not ilike')
  244. res_ids = [r[0] for r in res]
  245. self.assertNotIn(template.id, res_ids)
  246. def test_product_qty_field_and_context(self):
  247. main_warehouse = self.warehouse_1
  248. other_warehouse = self.env['stock.warehouse'].search([('id', '!=', main_warehouse.id)], limit=1)
  249. warehouses = main_warehouse | other_warehouse
  250. main_loc = main_warehouse.lot_stock_id
  251. other_loc = other_warehouse.lot_stock_id
  252. self.assertTrue(other_warehouse, 'The test needs another warehouse')
  253. (main_loc | other_loc).name = 'Stock'
  254. sub_loc01, sub_loc02, sub_loc03 = self.env['stock.location'].create([{
  255. 'name': 'Sub0%s' % (i + 1),
  256. 'location_id': main_loc.id,
  257. } for i in range(3)])
  258. self.env['stock.quant'].search([('product_id', '=', self.product_3.id)]).unlink()
  259. self.env['stock.quant']._update_available_quantity(self.product_3, other_loc, 1000)
  260. self.env['stock.quant']._update_available_quantity(self.product_3, main_loc, 100)
  261. self.env['stock.quant']._update_available_quantity(self.product_3, sub_loc01, 10)
  262. self.env['stock.quant']._update_available_quantity(self.product_3, sub_loc02, 1)
  263. for wh, loc, expected in [
  264. (False, False, 1111.0),
  265. (False, other_loc.id, 1000.0),
  266. (False, main_loc.id, 111.0),
  267. (False, sub_loc01.id, 10.0),
  268. (False, sub_loc01.name, 10.0),
  269. (False, 'sub', 11.0),
  270. (False, main_loc.name, 1111.0),
  271. (False, (sub_loc01 | sub_loc02 | sub_loc03).ids, 11.0),
  272. (main_warehouse.id, main_loc.name, 111.0),
  273. (main_warehouse.id, main_loc.id, 111.0),
  274. (main_warehouse.id, (main_loc | other_loc).ids, 111.0),
  275. (main_warehouse.id, sub_loc01.id, 10.0),
  276. (main_warehouse.id, (sub_loc01 | sub_loc02).ids, 11.0),
  277. (other_warehouse.id, main_loc.name, 1000.0),
  278. (other_warehouse.id, main_loc.id, 0.0),
  279. (main_warehouse.name, False, 111.0),
  280. (main_warehouse.id, False, 111.0),
  281. (warehouses.ids, False, 1111.0),
  282. (warehouses.ids, (other_loc | sub_loc02).ids, 1001),
  283. ]:
  284. product_qty = self.product_3.with_context(warehouse=wh, location=loc).qty_available
  285. self.assertEqual(product_qty, expected)
  286. def test_change_type_tracked_product(self):
  287. product = self.env['product.template'].create({
  288. 'name': 'Brand new product',
  289. 'type': 'product',
  290. 'tracking': 'serial',
  291. })
  292. product_form = Form(product)
  293. product_form.detailed_type = 'service'
  294. product = product_form.save()
  295. self.assertEqual(product.tracking, 'none')
  296. product.detailed_type = 'product'
  297. product.tracking = 'serial'
  298. self.assertEqual(product.tracking, 'serial')
  299. # change the type from "product.product" form
  300. product_form = Form(product.product_variant_id)
  301. product_form.detailed_type = 'service'
  302. product = product_form.save()
  303. self.assertEqual(product.tracking, 'none')