test_purchase_order.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import re
  4. from datetime import datetime, timedelta
  5. from freezegun import freeze_time
  6. from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
  7. from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon
  8. from odoo.tests import Form, tagged
  9. @freeze_time("2021-01-14 09:12:15")
  10. @tagged('post_install', '-at_install')
  11. class TestPurchaseOrder(ValuationReconciliationTestCommon):
  12. @classmethod
  13. def setUpClass(cls, chart_template_ref=None):
  14. super().setUpClass(chart_template_ref=chart_template_ref)
  15. cls.product_id_1 = cls.env['product.product'].create({'name': 'Large Desk', 'purchase_method': 'purchase'})
  16. cls.product_id_2 = cls.env['product.product'].create({'name': 'Conference Chair', 'purchase_method': 'purchase'})
  17. cls.po_vals = {
  18. 'partner_id': cls.partner_a.id,
  19. 'order_line': [
  20. (0, 0, {
  21. 'name': cls.product_id_1.name,
  22. 'product_id': cls.product_id_1.id,
  23. 'product_qty': 5.0,
  24. 'product_uom': cls.product_id_1.uom_po_id.id,
  25. 'price_unit': 500.0,
  26. 'date_planned': datetime.today().replace(hour=9).strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  27. }),
  28. (0, 0, {
  29. 'name': cls.product_id_2.name,
  30. 'product_id': cls.product_id_2.id,
  31. 'product_qty': 5.0,
  32. 'product_uom': cls.product_id_2.uom_po_id.id,
  33. 'price_unit': 250.0,
  34. 'date_planned': datetime.today().replace(hour=9).strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  35. })],
  36. }
  37. def test_00_purchase_order_flow(self):
  38. # Ensure product_id_2 doesn't have res_partner_1 as supplier
  39. if self.partner_a in self.product_id_2.seller_ids.partner_id:
  40. id_to_remove = self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a).ids[0] if self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a) else False
  41. if id_to_remove:
  42. self.product_id_2.write({
  43. 'seller_ids': [(2, id_to_remove, False)],
  44. })
  45. self.assertFalse(self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a), 'Purchase: the partner should not be in the list of the product suppliers')
  46. self.po = self.env['purchase.order'].create(self.po_vals)
  47. self.assertTrue(self.po, 'Purchase: no purchase order created')
  48. self.assertEqual(self.po.invoice_status, 'no', 'Purchase: PO invoice_status should be "Not purchased"')
  49. self.assertEqual(self.po.order_line.mapped('qty_received'), [0.0, 0.0], 'Purchase: no product should be received"')
  50. self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [0.0, 0.0], 'Purchase: no product should be invoiced"')
  51. self.po.button_confirm()
  52. self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase"')
  53. self.assertEqual(self.po.invoice_status, 'to invoice', 'Purchase: PO invoice_status should be "Waiting Invoices"')
  54. self.assertTrue(self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a), 'Purchase: the partner should be in the list of the product suppliers')
  55. seller = self.product_id_2._select_seller(partner_id=self.partner_a, quantity=2.0, date=self.po.date_planned, uom_id=self.product_id_2.uom_po_id)
  56. price_unit = seller.price if seller else 0.0
  57. if price_unit and seller and self.po.currency_id and seller.currency_id != self.po.currency_id:
  58. price_unit = seller.currency_id._convert(price_unit, self.po.currency_id, self.po.company_id, self.po.date_order)
  59. self.assertEqual(price_unit, 250.0, 'Purchase: the price of the product for the supplier should be 250.0.')
  60. self.assertEqual(self.po.incoming_picking_count, 1, 'Purchase: one picking should be created"')
  61. self.picking = self.po.picking_ids[0]
  62. self.picking.move_line_ids.write({'qty_done': 5.0})
  63. self.picking.button_validate()
  64. self.assertEqual(self.po.order_line.mapped('qty_received'), [5.0, 5.0], 'Purchase: all products should be received"')
  65. move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
  66. move_form.partner_id = self.partner_a
  67. move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-self.po.id)
  68. self.invoice = move_form.save()
  69. self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [5.0, 5.0], 'Purchase: all products should be invoiced"')
  70. def test_02_po_return(self):
  71. """
  72. Test a PO with a product on Incoming shipment. Validate the PO, then do a return
  73. of the picking with Refund.
  74. """
  75. # Draft purchase order created
  76. self.po = self.env['purchase.order'].create(self.po_vals)
  77. self.assertTrue(self.po, 'Purchase: no purchase order created')
  78. self.assertEqual(self.po.order_line.mapped('qty_received'), [0.0, 0.0], 'Purchase: no product should be received"')
  79. self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [0.0, 0.0], 'Purchase: no product should be invoiced"')
  80. self.po.button_confirm()
  81. self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase"')
  82. self.assertEqual(self.po.invoice_status, 'to invoice', 'Purchase: PO invoice_status should be "Waiting Invoices"')
  83. # Confirm the purchase order
  84. self.po.button_confirm()
  85. self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase')
  86. self.assertEqual(self.po.incoming_picking_count, 1, 'Purchase: one picking should be created"')
  87. self.picking = self.po.picking_ids[0]
  88. self.picking.move_line_ids.write({'qty_done': 5.0})
  89. self.picking.button_validate()
  90. self.assertEqual(self.po.order_line.mapped('qty_received'), [5.0, 5.0], 'Purchase: all products should be received"')
  91. #After Receiving all products create vendor bill.
  92. move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
  93. move_form.invoice_date = move_form.date
  94. move_form.partner_id = self.partner_a
  95. move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-self.po.id)
  96. self.invoice = move_form.save()
  97. self.invoice.action_post()
  98. self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [5.0, 5.0], 'Purchase: all products should be invoiced"')
  99. # Check quantity received
  100. received_qty = sum(pol.qty_received for pol in self.po.order_line)
  101. self.assertEqual(received_qty, 10.0, 'Purchase: Received quantity should be 10.0 instead of %s after validating incoming shipment' % received_qty)
  102. # Create return picking
  103. pick = self.po.picking_ids
  104. stock_return_picking_form = Form(self.env['stock.return.picking']
  105. .with_context(active_ids=pick.ids, active_id=pick.ids[0],
  106. active_model='stock.picking'))
  107. return_wiz = stock_return_picking_form.save()
  108. return_wiz.product_return_moves.write({'quantity': 2.0, 'to_refund': True}) # Return only 2
  109. res = return_wiz.create_returns()
  110. return_pick = self.env['stock.picking'].browse(res['res_id'])
  111. # Validate picking
  112. return_pick.move_line_ids.write({'qty_done': 2})
  113. return_pick.button_validate()
  114. # Check Received quantity
  115. self.assertEqual(self.po.order_line[0].qty_received, 3.0, 'Purchase: delivered quantity should be 3.0 instead of "%s" after picking return' % self.po.order_line[0].qty_received)
  116. #Create vendor bill for refund qty
  117. move_form = Form(self.env['account.move'].with_context(default_move_type='in_refund'))
  118. move_form.invoice_date = move_form.date
  119. move_form.partner_id = self.partner_a
  120. # Not supposed to see/change the purchase order of a refund invoice by default
  121. # <field name="purchase_id" invisible="1"/>
  122. # <label for="purchase_vendor_bill_id" string="Auto-Complete" class="oe_edit_only"
  123. # attrs="{'invisible': ['|', ('state','!=','draft'), ('move_type', '!=', 'in_invoice')]}" />
  124. # <field name="purchase_vendor_bill_id" nolabel="1"
  125. # attrs="{'invisible': ['|', ('state','!=','draft'), ('move_type', '!=', 'in_invoice')]}"
  126. move_form._view['modifiers']['purchase_id']['invisible'] = False
  127. move_form.purchase_id = self.po
  128. self.invoice = move_form.save()
  129. move_form = Form(self.invoice)
  130. with move_form.invoice_line_ids.edit(0) as line_form:
  131. line_form.quantity = 2.0
  132. with move_form.invoice_line_ids.edit(1) as line_form:
  133. line_form.quantity = 2.0
  134. self.invoice = move_form.save()
  135. self.invoice.action_post()
  136. self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [3.0, 3.0], 'Purchase: Billed quantity should be 3.0')
  137. def test_03_po_return_and_modify(self):
  138. """Change the picking code of the delivery to internal. Make a PO for 10 units, go to the
  139. picking and return 5, edit the PO line to 15 units.
  140. The purpose of the test is to check the consistencies across the received quantities and the
  141. procurement quantities.
  142. """
  143. # Change the code of the picking type delivery
  144. self.env['stock.picking.type'].search([('code', '=', 'outgoing')]).write({'code': 'internal'})
  145. # Sell and deliver 10 units
  146. item1 = self.product_id_1
  147. uom_unit = self.env.ref('uom.product_uom_unit')
  148. po1 = self.env['purchase.order'].create({
  149. 'partner_id': self.partner_a.id,
  150. 'order_line': [
  151. (0, 0, {
  152. 'name': item1.name,
  153. 'product_id': item1.id,
  154. 'product_qty': 10,
  155. 'product_uom': uom_unit.id,
  156. 'price_unit': 123.0,
  157. 'date_planned': datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  158. }),
  159. ],
  160. })
  161. po1.button_confirm()
  162. picking = po1.picking_ids
  163. wiz_act = picking.button_validate()
  164. wiz = Form(self.env[wiz_act['res_model']].with_context(wiz_act['context'])).save()
  165. wiz.process()
  166. # Return 5 units
  167. stock_return_picking_form = Form(self.env['stock.return.picking'].with_context(
  168. active_ids=picking.ids,
  169. active_id=picking.ids[0],
  170. active_model='stock.picking'
  171. ))
  172. return_wiz = stock_return_picking_form.save()
  173. for return_move in return_wiz.product_return_moves:
  174. return_move.write({
  175. 'quantity': 5,
  176. 'to_refund': True
  177. })
  178. res = return_wiz.create_returns()
  179. return_pick = self.env['stock.picking'].browse(res['res_id'])
  180. wiz_act = return_pick.button_validate()
  181. wiz = Form(self.env[wiz_act['res_model']].with_context(wiz_act['context'])).save()
  182. wiz.process()
  183. self.assertEqual(po1.order_line.qty_received, 5)
  184. # Deliver 15 instead of 10.
  185. po1.write({
  186. 'order_line': [
  187. (1, po1.order_line[0].id, {'product_qty': 15}),
  188. ]
  189. })
  190. # A new move of 10 unit (15 - 5 units)
  191. self.assertEqual(po1.order_line.qty_received, 5)
  192. self.assertEqual(po1.picking_ids[-1].move_ids.product_qty, 10)
  193. def test_04_update_date_planned(self):
  194. today = datetime.today().replace(hour=9, microsecond=0)
  195. tomorrow = datetime.today().replace(hour=9, microsecond=0) + timedelta(days=1)
  196. po = self.env['purchase.order'].create(self.po_vals)
  197. po.button_confirm()
  198. # update first line
  199. po._update_date_planned_for_lines([(po.order_line[0], tomorrow)])
  200. self.assertEqual(po.order_line[0].date_planned, tomorrow)
  201. activity = self.env['mail.activity'].search([
  202. ('summary', '=', 'Date Updated'),
  203. ('res_model_id', '=', 'purchase.order'),
  204. ('res_id', '=', po.id),
  205. ])
  206. self.assertTrue(activity)
  207. self.assertEqual(
  208. '<p>partner_a modified receipt dates for the following products:</p>\n'
  209. '<p> - Large Desk from %s to %s</p>\n'
  210. '<p>Those dates have been updated accordingly on the receipt %s.</p>' % (today.date(), tomorrow.date(), po.picking_ids.name),
  211. activity.note,
  212. )
  213. # receive products
  214. wiz_act = po.picking_ids.button_validate()
  215. wiz = Form(self.env[wiz_act['res_model']].with_context(wiz_act['context'])).save()
  216. wiz.process()
  217. # update second line
  218. old_date = po.order_line[1].date_planned
  219. po._update_date_planned_for_lines([(po.order_line[1], tomorrow)])
  220. self.assertEqual(po.order_line[1].date_planned, old_date)
  221. self.assertEqual(
  222. '<p>partner_a modified receipt dates for the following products:</p>\n'
  223. '<p> - Large Desk from %s to %s</p>\n'
  224. '<p> - Conference Chair from %s to %s</p>\n'
  225. '<p>Those dates couldn’t be modified accordingly on the receipt %s which had already been validated.</p>' % (
  226. today.date(), tomorrow.date(), today.date(), tomorrow.date(), po.picking_ids.name),
  227. activity.note,
  228. )
  229. def test_05_multi_company(self):
  230. company_a = self.env.user.company_id
  231. company_b = self.env['res.company'].create({
  232. "name": "Test Company",
  233. "currency_id": self.env['res.currency'].with_context(active_test=False).search([
  234. ('id', '!=', company_a.currency_id.id),
  235. ], limit=1).id
  236. })
  237. self.env.user.write({
  238. 'company_id': company_b.id,
  239. 'company_ids': [(4, company_b.id), (4, company_a.id)],
  240. })
  241. po = self.env['purchase.order'].create(dict(company_id=company_a.id, partner_id=self.partner_a.id))
  242. self.assertEqual(po.company_id, company_a)
  243. self.assertEqual(po.picking_type_id.warehouse_id.company_id, company_a)
  244. self.assertEqual(po.currency_id, po.company_id.currency_id)
  245. def test_06_on_time_rate(self):
  246. company_a = self.env.user.company_id
  247. company_b = self.env['res.company'].create({
  248. "name": "Test Company",
  249. "currency_id": self.env['res.currency'].with_context(active_test=False).search([
  250. ('id', '!=', company_a.currency_id.id),
  251. ], limit=1).id
  252. })
  253. # Create a purchase order with 90% qty received for company A
  254. self.env.user.write({
  255. 'company_id': company_a.id,
  256. 'company_ids': [(6, 0, [company_a.id])],
  257. })
  258. po = self.env['purchase.order'].create(self.po_vals)
  259. po.order_line.write({'product_qty': 10})
  260. po.button_confirm()
  261. picking = po.picking_ids[0]
  262. # Process 9.0 out of the 10.0 ordered qty
  263. picking.move_line_ids.write({'qty_done': 9.0})
  264. res_dict = picking.button_validate()
  265. # No backorder
  266. self.env['stock.backorder.confirmation'].with_context(res_dict['context']).process_cancel_backorder()
  267. # `on_time_rate` should be equals to the ratio of quantity received against quantity ordered
  268. expected_rate = sum(picking.move_line_ids.mapped("qty_done")) / sum(po.order_line.mapped("product_qty")) * 100
  269. self.assertEqual(expected_rate, po.on_time_rate)
  270. # Create a purchase order with 80% qty received for company B
  271. # The On-Time Delivery Rate shouldn't be shared across multiple companies
  272. self.env.user.write({
  273. 'company_id': company_b.id,
  274. 'company_ids': [(6, 0, [company_b.id])],
  275. })
  276. po = self.env['purchase.order'].create(self.po_vals)
  277. po.order_line.write({'product_qty': 10})
  278. po.button_confirm()
  279. picking = po.picking_ids[0]
  280. # Process 8.0 out of the 10.0 ordered qty
  281. picking.move_line_ids.write({'qty_done': 8.0})
  282. res_dict = picking.button_validate()
  283. # No backorder
  284. self.env['stock.backorder.confirmation'].with_context(res_dict['context']).process_cancel_backorder()
  285. # `on_time_rate` should be equal to the ratio of quantity received against quantity ordered
  286. expected_rate = sum(picking.move_line_ids.mapped("qty_done")) / sum(po.order_line.mapped("product_qty")) * 100
  287. self.assertEqual(expected_rate, po.on_time_rate)
  288. # Tricky corner case
  289. # As `purchase.order.on_time_rate` is a related to `partner_id.on_time_rate`
  290. # `on_time_rate` on the PO should equals `on_time_rate` on the partner.
  291. # Related fields are by default computed as sudo
  292. # while non-stored computed fields are not computed as sudo by default
  293. # If the computation of the related field (`purchase.order.on_time_rate`) was asked
  294. # and `res.partner.on_time_rate` was not yet in the cache
  295. # the `sudo` requested for the computation of the related `purchase.order.on_time_rate`
  296. # was propagated to the computation of `res.partner.on_time_rate`
  297. # and therefore the multi-company record rules were ignored.
  298. # 1. Compute `res.partner.on_time_rate` regular non-stored comptued field
  299. partner_on_time_rate = po.partner_id.on_time_rate
  300. # 2. Invalidate the cache for that record and field, so it's not reused in the next step.
  301. po.partner_id.invalidate_recordset(["on_time_rate"])
  302. # 3. Compute the related field `purchase.order.on_time_rate`
  303. po_on_time_rate = po.on_time_rate
  304. # 4. Check both are equals.
  305. self.assertEqual(partner_on_time_rate, po_on_time_rate)
  306. def test_04_multi_uom(self):
  307. yards_uom = self.env['uom.uom'].create({
  308. 'category_id': self.env.ref('uom.uom_categ_length').id,
  309. 'name': 'Yards',
  310. 'factor_inv': 0.9144,
  311. 'uom_type': 'bigger',
  312. })
  313. self.product_id_2.write({
  314. 'uom_id': self.env.ref('uom.product_uom_meter').id,
  315. 'uom_po_id': yards_uom.id,
  316. })
  317. po = self.env['purchase.order'].create({
  318. 'partner_id': self.partner_a.id,
  319. 'order_line': [
  320. (0, 0, {
  321. 'name': self.product_id_2.name,
  322. 'product_id': self.product_id_2.id,
  323. 'product_qty': 4.0,
  324. 'product_uom': self.product_id_2.uom_po_id.id,
  325. 'price_unit': 1.0,
  326. 'date_planned': datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  327. })
  328. ],
  329. })
  330. po.button_confirm()
  331. picking = po.picking_ids[0]
  332. picking.move_line_ids.write({'qty_done': 3.66})
  333. picking.button_validate()
  334. self.assertEqual(po.order_line.mapped('qty_received'), [4.0], 'Purchase: no conversion error on receipt in different uom"')
  335. def test_05_po_update_qty_stock_move_merge(self):
  336. """ This test ensures that changing product quantity when unit price has high decimal precision
  337. merged with the original instead of creating a new return
  338. """
  339. unit_price_precision = self.env['decimal.precision'].search([('name', '=', 'Product Price')])
  340. unit_price_precision.digits = 6
  341. tax = self.env["account.tax"].create({
  342. "name": "Dummy Tax",
  343. "amount": "5.00",
  344. "type_tax_use": "purchase",
  345. })
  346. super_product = self.env['product.product'].create({
  347. 'name': 'Super Product',
  348. 'type': 'product',
  349. 'categ_id': self.stock_account_product_categ.id,
  350. 'standard_price': 9.876543,
  351. })
  352. purchase_order = self.env['purchase.order'].create({
  353. 'partner_id': self.env.ref('base.res_partner_3').id,
  354. 'order_line': [(0, 0, {
  355. 'name': super_product.name,
  356. 'product_id': super_product.id,
  357. 'product_qty': 7,
  358. 'product_uom': super_product.uom_id.id,
  359. 'price_unit': super_product.standard_price,
  360. 'taxes_id': [(4, tax.id)],
  361. })],
  362. })
  363. purchase_order.button_confirm()
  364. self.assertEqual(purchase_order.state, 'purchase')
  365. self.assertEqual(len(purchase_order.picking_ids), 1)
  366. self.assertEqual(len(purchase_order.picking_ids.move_line_ids), 1)
  367. self.assertEqual(purchase_order.picking_ids.move_line_ids.reserved_qty, 7)
  368. purchase_order.order_line.product_qty = 4
  369. # updating quantity shouldn't create a seperate stock move
  370. # the new stock move (-3) should be merged with the previous
  371. purchase_order.button_confirm()
  372. self.assertEqual(len(purchase_order.picking_ids), 1)
  373. self.assertEqual(len(purchase_order.picking_ids.move_line_ids), 1)
  374. self.assertEqual(purchase_order.picking_ids.move_line_ids.reserved_qty, 4)
  375. def test_message_qty_already_received(self):
  376. self.env.user.write({'company_id': self.company_data['company'].id})
  377. _purchase_order = self.env['purchase.order'].create({
  378. 'partner_id': self.partner_a.id,
  379. 'order_line': [
  380. (0, 0, {
  381. 'name': self.product_id_2.name,
  382. 'product_id': self.product_id_2.id,
  383. 'product_qty': 25.0,
  384. 'price_unit': 250.0,
  385. })],
  386. })
  387. _purchase_order.button_confirm()
  388. first_picking = _purchase_order.picking_ids[0]
  389. first_picking.move_ids.quantity_done = 5
  390. backorder_wizard_dict = first_picking.button_validate()
  391. backorder_wizard = Form(self.env[backorder_wizard_dict['res_model']].with_context(backorder_wizard_dict['context'])).save()
  392. backorder_wizard.process()
  393. second_picking = _purchase_order.picking_ids[1]
  394. second_picking.move_ids.quantity_done = 5
  395. backorder_wizard_dict = second_picking.button_validate()
  396. backorder_wizard = Form(self.env[backorder_wizard_dict['res_model']].with_context(backorder_wizard_dict['context'])).save()
  397. backorder_wizard.process()
  398. third_picking = _purchase_order.picking_ids[2]
  399. third_picking.move_ids.quantity_done = 5
  400. backorder_wizard_dict = third_picking.button_validate()
  401. backorder_wizard = Form(self.env[backorder_wizard_dict['res_model']].with_context(backorder_wizard_dict['context'])).save()
  402. backorder_wizard.process()
  403. _message_content = _purchase_order.message_ids.mapped("body")[0]
  404. self.assertIsNotNone(re.search(r"Received Quantity: 5.0 -&gt; 10.0", _message_content), "Already received quantity isn't correctly taken into consideration")
  405. def test_pol_description(self):
  406. """
  407. Suppose a product with several sellers, all with the same partner. On the purchase order, the product
  408. description should be based on the correct seller
  409. """
  410. self.env.user.write({'company_id': self.company_data['company'].id})
  411. product = self.env['product.product'].create({
  412. 'name': 'Super Product',
  413. 'seller_ids': [(0, 0, {
  414. 'partner_id': self.partner_a.id,
  415. 'min_qty': 1,
  416. 'price': 10,
  417. 'product_code': 'C01',
  418. 'product_name': 'Name01',
  419. 'sequence': 1,
  420. }), (0, 0, {
  421. 'partner_id': self.partner_a.id,
  422. 'min_qty': 20,
  423. 'price': 2,
  424. 'product_code': 'C02',
  425. 'product_name': 'Name02',
  426. 'sequence': 2,
  427. })]
  428. })
  429. orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
  430. orderpoint_form.product_id = product
  431. orderpoint_form.product_min_qty = 1
  432. orderpoint_form.product_max_qty = 0.000
  433. orderpoint_form.save()
  434. self.env['procurement.group'].run_scheduler()
  435. pol = self.env['purchase.order.line'].search([('product_id', '=', product.id)])
  436. self.assertEqual(pol.name, "[C01] Name01")
  437. with Form(pol.order_id) as po_form:
  438. with po_form.order_line.edit(0) as pol_form:
  439. pol_form.product_qty = 25
  440. self.assertEqual(pol.name, "[C02] Name02")
  441. def test_packaging_and_qty_decrease(self):
  442. packaging = self.env['product.packaging'].create({
  443. 'name': "Super Packaging",
  444. 'product_id': self.product_a.id,
  445. 'qty': 10.0,
  446. })
  447. po_form = Form(self.env['purchase.order'])
  448. po_form.partner_id = self.partner_a
  449. with po_form.order_line.new() as line:
  450. line.product_id = self.product_a
  451. line.product_qty = 10
  452. po = po_form.save()
  453. po.button_confirm()
  454. self.assertEqual(po.order_line.product_packaging_id, packaging)
  455. with Form(po) as po_form:
  456. with po_form.order_line.edit(0) as line:
  457. line.product_qty = 8
  458. self.assertEqual(po.picking_ids.move_ids.product_uom_qty, 8)
  459. def test_packaging_propagation(self):
  460. """
  461. Editing the packaging on an purchase.order.line
  462. should propagate to the delivery order, so that
  463. when we are editing the packaging, the lines can be merged
  464. with the new packaging and quantity.
  465. """
  466. # set the 3 step route
  467. warehouse = self.company_data['default_warehouse']
  468. warehouse.reception_steps = 'three_steps'
  469. packOf10 = self.env['product.packaging'].create({
  470. 'name': 'PackOf10',
  471. 'product_id': self.product_a.id,
  472. 'qty': 10
  473. })
  474. packOf20 = self.env['product.packaging'].create({
  475. 'name': 'PackOf20',
  476. 'product_id': self.product_a.id,
  477. 'qty': 20
  478. })
  479. po = self.env['purchase.order'].create({
  480. 'partner_id': self.partner_a.id,
  481. 'order_line': [
  482. (0, 0, {
  483. 'product_id': self.product_a.id,
  484. 'product_uom_qty': 10.0,
  485. 'product_uom': self.product_a.uom_id.id,
  486. 'product_packaging_id': packOf10.id,
  487. })],
  488. })
  489. po.button_confirm()
  490. # the 3 moves for the 3 steps
  491. step_1 = po.order_line.move_ids
  492. step_2 = step_1.move_dest_ids
  493. step_3 = step_2.move_dest_ids
  494. self.assertEqual(step_1.product_packaging_id, packOf10)
  495. self.assertEqual(step_2.product_packaging_id, packOf10)
  496. self.assertEqual(step_3.product_packaging_id, packOf10)
  497. po.order_line[0].write({
  498. 'product_packaging_id': packOf20.id,
  499. 'product_uom_qty': 20
  500. })
  501. self.assertEqual(step_1.product_packaging_id, packOf20)
  502. self.assertEqual(step_2.product_packaging_id, packOf20)
  503. self.assertEqual(step_3.product_packaging_id, packOf20)
  504. po.order_line[0].write({'product_packaging_id': False})
  505. self.assertFalse(step_1.product_packaging_id)
  506. self.assertFalse(step_2.product_packaging_id)
  507. self.assertFalse(step_3.product_packaging_id)
  508. def test_putaway_strategy_in_backorder(self):
  509. stock_location = self.company_data['default_warehouse'].lot_stock_id
  510. sub_loc_01 = self.env['stock.location'].create([{
  511. 'name': 'Sub Location 1',
  512. 'usage': 'internal',
  513. 'location_id': stock_location.id,
  514. }])
  515. self.env["stock.putaway.rule"].create({
  516. "location_in_id": stock_location.id,
  517. "location_out_id": sub_loc_01.id,
  518. "product_id": self.product_a.id,
  519. })
  520. po = self.env['purchase.order'].create({
  521. 'partner_id': self.partner_a.id,
  522. 'order_line': [
  523. (0, 0, {
  524. 'product_id': self.product_a.id,
  525. 'product_qty': 2.0,
  526. })],
  527. })
  528. po.button_confirm()
  529. picking = po.picking_ids
  530. self.assertEqual(po.state, "purchase")
  531. self.assertEqual(picking.move_line_ids_without_package.location_dest_id.id, sub_loc_01.id)
  532. picking.move_line_ids_without_package.write({'qty_done': 1})
  533. res_dict = picking.button_validate()
  534. self.env[res_dict['res_model']].with_context(res_dict['context']).process()
  535. backorder = picking.backorder_ids
  536. self.assertEqual(backorder.move_line_ids_without_package.location_dest_id.id, sub_loc_01.id)
  537. def test_inventory_adjustments_with_po(self):
  538. """ check that the quant created by a PO can be applied in an inventory adjustment correctly """
  539. product = self.env['product.product'].create({
  540. 'name': 'Product A',
  541. 'type': 'product',
  542. })
  543. po_form = Form(self.env['purchase.order'])
  544. po_form.partner_id = self.partner_a
  545. with po_form.order_line.new() as line:
  546. line.product_id = product
  547. line.product_qty = 5
  548. po = po_form.save()
  549. po.button_confirm()
  550. po.picking_ids.move_ids.quantity_done = 5
  551. po.picking_ids.button_validate()
  552. self.assertEqual(po.picking_ids.state, 'done')
  553. quant = self.env['stock.quant'].search([('product_id', '=', product.id), ('location_id.usage', '=', 'internal')])
  554. wizard = self.env['stock.inventory.adjustment.name'].create({'quant_ids': quant})
  555. wizard.action_apply()
  556. self.assertEqual(quant.quantity, 5)
  557. def test_po_edit_after_receive(self):
  558. self.po = self.env['purchase.order'].create(self.po_vals)
  559. self.po.button_confirm()
  560. self.po.picking_ids.move_ids.quantity_done = 5
  561. self.po.picking_ids.button_validate()
  562. self.assertEqual(self.po.picking_ids.move_ids.mapped('product_uom_qty'), [5.0, 5.0])
  563. self.po.with_context(import_file=True).order_line[0].product_qty = 10
  564. self.assertEqual(self.po.picking_ids.move_ids.mapped('product_uom_qty'), [5.0, 5.0, 5.0])