You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
8.7 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # Copyright 2018 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. from odoo.exceptions import ValidationError
  6. from odoo.fields import Date
  7. class TestSaleOrder(TransactionCase):
  8. def setUp(self):
  9. super(TestSaleOrder, self).setUp()
  10. self.product1 = self.env.ref('product.product_product_1')
  11. self.product2 = self.env.ref('product.product_product_2')
  12. self.sale = self.env.ref('sale.sale_order_2')
  13. self.contract_template1 = self.env['account.analytic.contract'].create(
  14. {'name': 'Template 1'}
  15. )
  16. self.contract_template2 = self.env['account.analytic.contract'].create(
  17. {
  18. 'name': 'Template 2',
  19. 'recurring_invoice_line_ids': [
  20. (
  21. 0,
  22. 0,
  23. {
  24. 'product_id': self.product2.id,
  25. 'name': 'Services from #START# to #END#',
  26. 'quantity': 1,
  27. 'uom_id': self.product2.uom_id.id,
  28. 'price_unit': 100,
  29. 'discount': 50,
  30. 'recurring_rule_type': 'yearly',
  31. 'recurring_interval': 1,
  32. },
  33. )
  34. ],
  35. }
  36. )
  37. self.product1.write(
  38. {
  39. 'is_contract': True,
  40. 'is_auto_renew': True,
  41. 'contract_template_id': self.contract_template1.id,
  42. }
  43. )
  44. self.product2.write(
  45. {
  46. 'is_contract': True,
  47. 'contract_template_id': self.contract_template2.id,
  48. }
  49. )
  50. self.order_line1 = self.sale.order_line.filtered(
  51. lambda l: l.product_id == self.product1
  52. )
  53. self.order_line1.date_start = '2018-01-01'
  54. pricelist = self.sale.partner_id.property_product_pricelist.id
  55. self.contract = self.env["account.analytic.account"].create(
  56. {
  57. "name": "Test Contract 2",
  58. "partner_id": self.sale.partner_id.id,
  59. "pricelist_id": pricelist,
  60. "recurring_invoices": True,
  61. "contract_type": "purchase",
  62. "contract_template_id": self.contract_template1.id,
  63. "recurring_invoice_line_ids": [
  64. (
  65. 0,
  66. 0,
  67. {
  68. "product_id": self.product1.id,
  69. "name": "Services from #START# to #END#",
  70. "quantity": 1,
  71. "uom_id": self.product1.uom_id.id,
  72. "price_unit": 100,
  73. "discount": 50,
  74. "recurring_rule_type": "monthly",
  75. "recurring_interval": 1,
  76. "date_start": "2016-02-15",
  77. "recurring_next_date": "2016-02-29",
  78. },
  79. )
  80. ],
  81. }
  82. )
  83. self.contract_line = self.contract.recurring_invoice_line_ids[0]
  84. def test_compute_is_contract(self):
  85. """Sale Order should have is_contract true if one of its lines is
  86. contract"""
  87. self.assertTrue(self.sale.is_contract)
  88. def test_action_confirm_auto_renew_without_date_end(self):
  89. with self.assertRaises(ValidationError):
  90. self.sale.action_confirm()
  91. def test_action_confirm(self):
  92. """ It should create a contract for each contract template used in
  93. order_line """
  94. self.order_line1.onchange_product()
  95. self.sale.action_confirm()
  96. contracts = self.sale.order_line.mapped('contract_id')
  97. self.assertEqual(len(contracts), 2)
  98. self.assertEqual(
  99. self.order_line1.contract_id.contract_template_id,
  100. self.contract_template1,
  101. )
  102. def test_sale_contract_count(self):
  103. """It should count contracts as many different contract template used
  104. in order_line"""
  105. self.order_line1.onchange_product()
  106. self.sale.action_confirm()
  107. self.assertEqual(self.sale.contract_count, 2)
  108. def test_onchange_product(self):
  109. """ It should get recurrence invoicing info to the sale line from
  110. its product """
  111. self.order_line1.onchange_product()
  112. self.assertEqual(
  113. self.order_line1.recurring_rule_type,
  114. self.product1.recurring_rule_type,
  115. )
  116. self.assertEqual(
  117. self.order_line1.recurring_interval,
  118. self.product1.recurring_interval,
  119. )
  120. self.assertEqual(
  121. self.order_line1.recurring_invoicing_type,
  122. self.product1.recurring_invoicing_type,
  123. )
  124. self.assertEqual(self.order_line1.date_end, Date.to_date('2018-12-31'))
  125. def test_check_contract_sale_partner(self):
  126. """Can't link order line to a partner contract different then the
  127. order one"""
  128. contract2 = self.env['account.analytic.account'].create(
  129. {
  130. 'name': 'Contract',
  131. 'contract_template_id': self.contract_template2.id,
  132. 'partner_id': self.sale.partner_id.id,
  133. }
  134. )
  135. with self.assertRaises(ValidationError):
  136. self.order_line1.contract_id = contract2
  137. def test_check_contract_sale_contract_template(self):
  138. """Can't link order line to a contract with different contract
  139. template then the product one"""
  140. contract1 = self.env['account.analytic.account'].create(
  141. {
  142. 'name': 'Contract',
  143. 'contract_template_id': self.contract_template1.id,
  144. }
  145. )
  146. with self.assertRaises(ValidationError):
  147. self.order_line1.contract_id = contract1
  148. def test_no_contract_proudct(self):
  149. """it should create contract for only product contract"""
  150. self.product1.is_contract = False
  151. self.sale.action_confirm()
  152. self.assertFalse(self.order_line1.contract_id)
  153. def test_sale_order_line_invoice_status(self):
  154. """Sale order line for contract product should have nothing to
  155. invoice as status"""
  156. self.order_line1.onchange_product()
  157. self.sale.action_confirm()
  158. self.assertEqual(self.order_line1.invoice_status, 'no')
  159. def test_sale_order_invoice_status(self):
  160. """Sale order with only contract product should have nothing to
  161. invoice status directtly"""
  162. self.sale.order_line.filtered(
  163. lambda line: not line.product_id.is_contract
  164. ).unlink()
  165. self.order_line1.onchange_product()
  166. self.sale.action_confirm()
  167. self.assertEqual(self.sale.invoice_status, 'no')
  168. def test_sale_order_create_invoice(self):
  169. """Should not invoice contract product on sale order create invoice"""
  170. self.product2.is_contract = False
  171. self.product2.invoice_policy = 'order'
  172. self.order_line1.onchange_product()
  173. self.sale.action_confirm()
  174. self.sale.action_invoice_create()
  175. self.assertEqual(len(self.sale.invoice_ids), 1)
  176. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  177. lambda line: line.product_id.is_contract
  178. )
  179. self.assertEqual(len(invoice_line), 0)
  180. def test_link_contract_invoice_to_sale_order(self):
  181. """It should link contract invoice to sale order"""
  182. self.order_line1.onchange_product()
  183. self.sale.action_confirm()
  184. invoice = self.order_line1.contract_id.recurring_create_invoice()
  185. self.assertTrue(invoice in self.sale.invoice_ids)
  186. def test_contract_upsell(self):
  187. """Should stop contract line at sale order line start date"""
  188. self.order_line1.contract_id = self.contract
  189. self.order_line1.contract_line_id = self.contract_line
  190. self.contract_line.date_end = "2019-01-01"
  191. self.contract_line.is_auto_renew = "2019-01-01"
  192. self.order_line1.date_start = "2018-06-01"
  193. self.order_line1.onchange_product()
  194. self.sale.action_confirm()
  195. self.assertEqual(
  196. self.contract_line.date_end, Date.to_date("2018-05-31")
  197. )
  198. self.assertFalse(self.contract_line.is_auto_renew)
  199. new_contract_line = self.env['account.analytic.invoice.line'].search(
  200. [('sale_order_line_id', '=', self.order_line1.id)]
  201. )
  202. self.assertEqual(
  203. self.contract_line.successor_contract_line_id, new_contract_line
  204. )
  205. self.assertEqual(
  206. new_contract_line.predecessor_contract_line_id, self.contract_line
  207. )