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.

315 lines
13 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['contract.template'].create(
  15. {'name': 'Template 1'}
  16. )
  17. self.contract_template2 = self.env['contract.template'].create(
  18. {
  19. 'name': 'Template 2',
  20. 'contract_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. 'recurring_rule_type': "monthlylastday",
  43. 'contract_template_id': self.contract_template1.id,
  44. }
  45. )
  46. self.product2.write(
  47. {
  48. 'is_contract': True,
  49. 'contract_template_id': self.contract_template2.id,
  50. }
  51. )
  52. self.order_line1 = self.sale.order_line.filtered(
  53. lambda l: l.product_id == self.product1
  54. )
  55. self.order_line1.date_start = '2018-01-01'
  56. self.order_line1.product_uom_qty = 12
  57. pricelist = self.sale.partner_id.property_product_pricelist.id
  58. self.contract = self.env["contract.contract"].create(
  59. {
  60. "name": "Test Contract 2",
  61. "partner_id": self.sale.partner_id.id,
  62. "pricelist_id": pricelist,
  63. "contract_type": "sale",
  64. "contract_template_id": self.contract_template1.id,
  65. "contract_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.contract_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. contract_line = self.order_line1.contract_id.contract_line_ids
  102. self.assertEqual(contract_line.date_start, Date.to_date('2018-01-01'))
  103. self.assertEqual(contract_line.date_end, Date.to_date('2018-12-31'))
  104. self.assertEqual(
  105. contract_line.recurring_next_date, Date.to_date('2018-01-31')
  106. )
  107. def test_sale_order_invoice_status(self):
  108. """
  109. sale line linked to contracts must not be invoiced from sale order
  110. """
  111. self.sale.action_confirm()
  112. self.assertEqual(self.order_line1.invoice_status, 'no')
  113. invoice = self.order_line1.contract_id.recurring_create_invoice()
  114. self.assertTrue(invoice)
  115. self.assertEqual(self.order_line1.invoice_qty, 1)
  116. self.assertEqual(self.order_line1.qty_to_invoice, 0)
  117. def test_action_confirm_without_contract_creation(self):
  118. """ It should create a contract for each contract template used in
  119. order_line """
  120. self.sale.company_id.create_contract_at_sale_order_confirmation = False
  121. self.order_line1.onchange_product()
  122. self.sale.action_confirm()
  123. self.assertEqual(len(self.sale.order_line.mapped('contract_id')), 0)
  124. self.assertTrue(self.sale.need_contract_creation)
  125. self.sale.action_create_contract()
  126. self.assertEqual(len(self.sale.order_line.mapped('contract_id')), 2)
  127. self.assertFalse(self.sale.need_contract_creation)
  128. self.assertEqual(
  129. self.order_line1.contract_id.contract_template_id,
  130. self.contract_template1,
  131. )
  132. contract_line = self.order_line1.contract_id.contract_line_ids
  133. self.assertEqual(contract_line.date_start, Date.to_date('2018-01-01'))
  134. self.assertEqual(contract_line.date_end, Date.to_date('2018-12-31'))
  135. self.assertEqual(
  136. contract_line.recurring_next_date, Date.to_date('2018-01-31')
  137. )
  138. def test_sale_contract_count(self):
  139. """It should count contracts as many different contract template used
  140. in order_line"""
  141. self.order_line1.onchange_product()
  142. self.sale.action_confirm()
  143. self.assertEqual(self.sale.contract_count, 2)
  144. def test_onchange_product(self):
  145. """ It should get recurrence invoicing info to the sale line from
  146. its product """
  147. self.order_line1.onchange_product()
  148. self.assertEqual(
  149. self.order_line1.recurring_rule_type,
  150. self.product1.recurring_rule_type,
  151. )
  152. self.assertEqual(
  153. self.order_line1.recurring_invoicing_type,
  154. self.product1.recurring_invoicing_type,
  155. )
  156. self.assertEqual(self.order_line1.date_end, Date.to_date('2018-12-31'))
  157. def test_check_contract_sale_partner(self):
  158. """Can't link order line to a partner contract different then the
  159. order one"""
  160. contract2 = self.env['contract.contract'].create(
  161. {
  162. 'name': 'Contract',
  163. 'contract_template_id': self.contract_template2.id,
  164. 'partner_id': self.sale.partner_id.id,
  165. }
  166. )
  167. with self.assertRaises(ValidationError):
  168. self.order_line1.contract_id = contract2
  169. def test_check_contract_sale_contract_template(self):
  170. """Can't link order line to a contract with different contract
  171. template then the product one"""
  172. contract1 = self.env['contract.contract'].create(
  173. {
  174. 'name': 'Contract',
  175. 'partner_id': self.env.user.partner_id.id,
  176. 'contract_template_id': self.contract_template1.id,
  177. }
  178. )
  179. with self.assertRaises(ValidationError):
  180. self.order_line1.contract_id = contract1
  181. def test_no_contract_proudct(self):
  182. """it should create contract for only product contract"""
  183. self.product1.is_contract = False
  184. self.sale.action_confirm()
  185. self.assertFalse(self.order_line1.contract_id)
  186. def test_sale_order_line_invoice_status(self):
  187. """Sale order line for contract product should have nothing to
  188. invoice as status"""
  189. self.order_line1.onchange_product()
  190. self.sale.action_confirm()
  191. self.assertEqual(self.order_line1.invoice_status, 'no')
  192. def test_sale_order_invoice_status(self):
  193. """Sale order with only contract product should have nothing to
  194. invoice status directtly"""
  195. self.sale.order_line.filtered(
  196. lambda line: not line.product_id.is_contract
  197. ).unlink()
  198. self.order_line1.onchange_product()
  199. self.sale.action_confirm()
  200. self.assertEqual(self.sale.invoice_status, 'no')
  201. def test_sale_order_create_invoice(self):
  202. """Should not invoice contract product on sale order create invoice"""
  203. self.product2.is_contract = False
  204. self.product2.invoice_policy = 'order'
  205. self.order_line1.onchange_product()
  206. self.sale.action_confirm()
  207. self.sale.action_invoice_create()
  208. self.assertEqual(len(self.sale.invoice_ids), 1)
  209. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  210. lambda line: line.product_id.is_contract
  211. )
  212. self.assertEqual(len(invoice_line), 0)
  213. def test_link_contract_invoice_to_sale_order(self):
  214. """It should link contract invoice to sale order"""
  215. self.order_line1.onchange_product()
  216. self.sale.action_confirm()
  217. invoice = self.order_line1.contract_id.recurring_create_invoice()
  218. self.assertTrue(invoice in self.sale.invoice_ids)
  219. def test_contract_upsell(self):
  220. """Should stop contract line at sale order line start date"""
  221. self.order_line1.contract_id = self.contract
  222. self.order_line1.contract_line_id = self.contract_line
  223. self.contract_line.date_end = Date.today() + relativedelta(months=4)
  224. self.contract_line.is_auto_renew = True
  225. self.order_line1.date_start = "2018-06-01"
  226. self.order_line1.onchange_product()
  227. self.sale.action_confirm()
  228. self.assertEqual(
  229. self.contract_line.date_end, Date.to_date("2018-05-31")
  230. )
  231. self.assertFalse(self.contract_line.is_auto_renew)
  232. new_contract_line = self.env['contract.line'].search(
  233. [('sale_order_line_id', '=', self.order_line1.id)]
  234. )
  235. self.assertEqual(
  236. self.contract_line.successor_contract_line_id, new_contract_line
  237. )
  238. self.assertEqual(
  239. new_contract_line.predecessor_contract_line_id, self.contract_line
  240. )
  241. def test_contract_upsell_2(self):
  242. """Should stop contract line at sale order line start date"""
  243. self.order_line1.contract_id = self.contract
  244. self.order_line1.contract_line_id = self.contract_line
  245. self.contract_line.write(
  246. {
  247. 'date_start': "2018-06-01",
  248. 'recurring_next_date': "2018-06-01",
  249. 'date_end': False,
  250. }
  251. )
  252. self.order_line1.date_start = "2018-06-01"
  253. self.order_line1.onchange_product()
  254. self.sale.action_confirm()
  255. self.assertFalse(self.contract_line.date_end)
  256. self.assertTrue(self.contract_line.is_canceled)
  257. def test_onchange_product_id_recurring_info(self):
  258. self.product2.write(
  259. {
  260. 'recurring_rule_type': 'monthly',
  261. 'recurring_invoicing_type': 'pre-paid',
  262. 'is_auto_renew': True,
  263. 'default_qty': 12,
  264. 'termination_notice_interval': '6',
  265. 'termination_notice_rule_type': 'weekly',
  266. }
  267. )
  268. self.contract_line.write(
  269. {
  270. 'date_start': Date.today(),
  271. 'date_end': Date.today() + relativedelta(years=1),
  272. 'recurring_next_date': Date.today(),
  273. 'product_id': self.product2.id,
  274. }
  275. )
  276. self.contract_line._onchange_product_id_recurring_info()
  277. self.assertEqual(self.contract_line.recurring_rule_type, 'monthly')
  278. self.assertEqual(
  279. self.contract_line.recurring_invoicing_type, 'pre-paid'
  280. )
  281. self.assertEqual(self.contract_line.recurring_interval, 1)
  282. self.assertEqual(self.contract_line.is_auto_renew, True)
  283. self.assertEqual(self.contract_line.auto_renew_interval, 12)
  284. self.assertEqual(self.contract_line.auto_renew_rule_type, 'monthly')
  285. self.assertEqual(self.contract_line.termination_notice_interval, 6)
  286. self.assertEqual(
  287. self.contract_line.termination_notice_rule_type, 'weekly'
  288. )
  289. def test_action_show_contracts(self):
  290. self.sale.action_confirm()
  291. action = self.sale.action_show_contracts()
  292. self.assertEqual(
  293. self.env['contract.contract'].search(action['domain']),
  294. self.sale.order_line.mapped('contract_id'),
  295. )