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.

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