test_delivery_stock_move.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # -*- coding: utf-8 -*-
  2. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  3. from odoo.tests import tagged, Form
  4. @tagged('post_install', '-at_install')
  5. class StockMoveInvoice(AccountTestInvoicingCommon):
  6. @classmethod
  7. def setUpClass(cls, chart_template_ref=None):
  8. super().setUpClass(chart_template_ref=chart_template_ref)
  9. cls.ProductProduct = cls.env['product.product']
  10. cls.SaleOrder = cls.env['sale.order']
  11. cls.AccountJournal = cls.env['account.journal']
  12. cls.partner_18 = cls.env['res.partner'].create({'name': 'My Test Customer'})
  13. cls.pricelist_id = cls.env.ref('product.list0')
  14. cls.product_11 = cls.env['product.product'].create({'name': 'A product to deliver'})
  15. cls.product_cable_management_box = cls.env['product.product'].create({
  16. 'name': 'Another product to deliver',
  17. 'weight': 1.0,
  18. 'invoice_policy': 'order',
  19. })
  20. cls.product_uom_unit = cls.env.ref('uom.product_uom_unit')
  21. cls.product_delivery_normal = cls.env['product.product'].create({
  22. 'name': 'Normal Delivery Charges',
  23. 'invoice_policy': 'order',
  24. 'type': 'service',
  25. 'list_price': 10.0,
  26. 'categ_id': cls.env.ref('delivery.product_category_deliveries').id,
  27. })
  28. cls.normal_delivery = cls.env['delivery.carrier'].create({
  29. 'name': 'Normal Delivery Charges',
  30. 'fixed_price': 10,
  31. 'delivery_type': 'fixed',
  32. 'product_id': cls.product_delivery_normal.id,
  33. })
  34. def test_01_delivery_stock_move(self):
  35. # Test if the stored fields of stock moves are computed with invoice before delivery flow
  36. self.sale_prepaid = self.SaleOrder.create({
  37. 'partner_id': self.partner_18.id,
  38. 'partner_invoice_id': self.partner_18.id,
  39. 'partner_shipping_id': self.partner_18.id,
  40. 'pricelist_id': self.pricelist_id.id,
  41. 'order_line': [(0, 0, {
  42. 'name': 'Cable Management Box',
  43. 'product_id': self.product_cable_management_box.id,
  44. 'product_uom_qty': 2,
  45. 'product_uom': self.product_uom_unit.id,
  46. 'price_unit': 750.00,
  47. })],
  48. })
  49. # I add delivery cost in Sales order
  50. delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
  51. 'default_order_id': self.sale_prepaid.id,
  52. 'default_carrier_id': self.normal_delivery.id,
  53. }))
  54. choose_delivery_carrier = delivery_wizard.save()
  55. choose_delivery_carrier.button_confirm()
  56. # I confirm the SO.
  57. self.sale_prepaid.action_confirm()
  58. self.sale_prepaid._create_invoices()
  59. # I check that the invoice was created
  60. self.assertEqual(len(self.sale_prepaid.invoice_ids), 1, "Invoice not created.")
  61. # I confirm the invoice
  62. self.invoice = self.sale_prepaid.invoice_ids
  63. self.invoice.action_post()
  64. # I pay the invoice.
  65. self.journal = self.AccountJournal.search([('type', '=', 'cash'), ('company_id', '=', self.sale_prepaid.company_id.id)], limit=1)
  66. register_payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=self.invoice.ids).create({
  67. 'journal_id': self.journal.id,
  68. })
  69. register_payments._create_payments()
  70. # Check the SO after paying the invoice
  71. self.assertNotEqual(self.sale_prepaid.invoice_count, 0, 'order not invoiced')
  72. self.assertTrue(self.sale_prepaid.invoice_status == 'invoiced', 'order is not invoiced')
  73. self.assertEqual(len(self.sale_prepaid.picking_ids), 1, 'pickings not generated')
  74. # Check the stock moves
  75. moves = self.sale_prepaid.picking_ids.move_ids
  76. self.assertEqual(moves[0].product_qty, 2, 'wrong product_qty')
  77. self.assertEqual(moves[0].weight, 2.0, 'wrong move weight')
  78. # Ship
  79. moves.move_line_ids.write({'qty_done': 2})
  80. self.picking = self.sale_prepaid.picking_ids._action_done()
  81. self.assertEqual(moves[0].move_line_ids.sale_price, 1725.0, 'wrong shipping value')
  82. def test_02_delivery_stock_move(self):
  83. # Test if SN product shipment line has the correct amount
  84. self.product_cable_management_box.write({
  85. 'tracking': 'serial'
  86. })
  87. serial_numbers = self.env['stock.lot'].create([{
  88. 'name': str(x),
  89. 'product_id': self.product_cable_management_box.id,
  90. 'company_id': self.env.company.id,
  91. } for x in range(5)])
  92. self.sale_prepaid = self.SaleOrder.create({
  93. 'partner_id': self.partner_18.id,
  94. 'partner_invoice_id': self.partner_18.id,
  95. 'partner_shipping_id': self.partner_18.id,
  96. 'pricelist_id': self.pricelist_id.id,
  97. 'order_line': [(0, 0, {
  98. 'name': 'Cable Management Box',
  99. 'product_id': self.product_cable_management_box.id,
  100. 'product_uom_qty': 2,
  101. 'product_uom': self.product_uom_unit.id,
  102. 'price_unit': 750.00,
  103. })],
  104. })
  105. # I add delivery cost in Sales order
  106. delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
  107. 'default_order_id': self.sale_prepaid.id,
  108. 'default_carrier_id': self.normal_delivery.id,
  109. }))
  110. choose_delivery_carrier = delivery_wizard.save()
  111. choose_delivery_carrier.button_confirm()
  112. # I confirm the SO.
  113. self.sale_prepaid.action_confirm()
  114. moves = self.sale_prepaid.picking_ids.move_ids
  115. # Ship
  116. for ml, lot in zip(moves.move_line_ids, serial_numbers):
  117. ml.write({'qty_done': 1, 'lot_id': lot.id})
  118. self.picking = self.sale_prepaid.picking_ids._action_done()
  119. self.assertEqual(moves[0].move_line_ids[0].sale_price, 862.5, 'wrong shipping value')
  120. def test_03_invoiced_status(self):
  121. super_product = self.env['product.product'].create({
  122. 'name': 'Super Product',
  123. 'invoice_policy': 'delivery',
  124. })
  125. great_product = self.env['product.product'].create({
  126. 'name': 'Great Product',
  127. 'invoice_policy': 'delivery',
  128. })
  129. so = self.env['sale.order'].create({
  130. 'name': 'Sale order',
  131. 'partner_id': self.partner_a.id,
  132. 'partner_invoice_id': self.partner_a.id,
  133. 'order_line': [
  134. (0, 0, {'name': super_product.name, 'product_id': super_product.id, 'product_uom_qty': 1, 'price_unit': 1,}),
  135. (0, 0, {'name': great_product.name, 'product_id': great_product.id, 'product_uom_qty': 1, 'price_unit': 1,}),
  136. ]
  137. })
  138. # Confirm the SO
  139. so.action_confirm()
  140. # Deliver one product and create a backorder
  141. self.assertEqual(sum([line.quantity_done for line in so.picking_ids.move_ids]), 0)
  142. so.picking_ids.move_ids[0].quantity_done = 1
  143. backorder_wizard_dict = so.picking_ids.button_validate()
  144. backorder_wizard = Form(self.env[backorder_wizard_dict['res_model']].with_context(backorder_wizard_dict['context'])).save()
  145. backorder_wizard.process()
  146. self.assertEqual(sum([line.quantity_done for line in so.picking_ids.move_ids]), 1)
  147. # Invoice the delivered product
  148. invoice = so._create_invoices()
  149. invoice.action_post()
  150. self.assertEqual(so.invoice_status, 'no')
  151. # Add delivery fee
  152. delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
  153. 'default_order_id': so.id,
  154. 'default_carrier_id': self.normal_delivery.id
  155. }))
  156. choose_delivery_carrier = delivery_wizard.save()
  157. choose_delivery_carrier.button_confirm()
  158. self.assertEqual(so.invoice_status, 'no', 'The status should still be "Nothing To Invoice"')
  159. def test_delivery_carrier_from_confirmed_so(self):
  160. """Test if adding shipping method in sale order after confirmation
  161. will add it in pickings too"""
  162. sale_order = self.SaleOrder.create({
  163. "partner_id": self.partner_18.id,
  164. "partner_invoice_id": self.partner_18.id,
  165. "partner_shipping_id": self.partner_18.id,
  166. "order_line": [(0, 0, {
  167. "name": "Cable Management Box",
  168. "product_id": self.product_cable_management_box.id,
  169. "product_uom_qty": 2,
  170. "product_uom": self.product_uom_unit.id,
  171. "price_unit": 750.00,
  172. })],
  173. })
  174. sale_order.action_confirm()
  175. sale_order.picking_ids.move_ids.quantity_done = 2
  176. sale_order.picking_ids.button_validate()
  177. # Return picking
  178. return_form = Form(self.env["stock.return.picking"].with_context(active_id=sale_order.picking_ids.id, active_model="stock.picking"))
  179. return_wizard = return_form.save()
  180. action = return_wizard.create_returns()
  181. return_picking = self.env["stock.picking"].browse(action["res_id"])
  182. # add new product so new picking is created
  183. sale_order.write({
  184. "order_line": [(0, 0, {
  185. "name": "Another product to deliver",
  186. "product_id": self.product_11.id,
  187. "product_uom_qty": 2,
  188. "product_uom": self.product_uom_unit.id,
  189. "price_unit": 750.00,
  190. })],
  191. })
  192. # Add delivery cost in Sales order
  193. delivery_wizard = Form(self.env["choose.delivery.carrier"].with_context({
  194. "default_order_id": sale_order.id,
  195. "default_carrier_id": self.normal_delivery.id,
  196. }))
  197. choose_delivery_carrier = delivery_wizard.save()
  198. choose_delivery_carrier.button_confirm()
  199. # Check the carrier in picking after confirm sale order
  200. delivery_for_product_11 = sale_order.picking_ids.filtered(lambda p: self.product_11 in p.move_ids.product_id)
  201. self.assertEqual(delivery_for_product_11.carrier_id, self.normal_delivery, "The shipping method should be set in pending deliveries.")
  202. done_delivery = sale_order.picking_ids.filtered(lambda p: p.state == "done")
  203. self.assertFalse(done_delivery.carrier_id.id, "The shipping method should not be set in done deliveries.")
  204. self.assertFalse(return_picking.carrier_id.id, "The shipping method should not set in return pickings")