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.

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