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.

274 lines
11 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. '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["account.analytic.account"].create(
  59. {
  60. "name": "Test Contract 2",
  61. "partner_id": self.sale.partner_id.id,
  62. "pricelist_id": pricelist,
  63. "recurring_invoices": True,
  64. "contract_type": "sale",
  65. "contract_template_id": self.contract_template1.id,
  66. "recurring_invoice_line_ids": [
  67. (
  68. 0,
  69. 0,
  70. {
  71. "product_id": self.product1.id,
  72. "name": "Services from #START# to #END#",
  73. "quantity": 1,
  74. "uom_id": self.product1.uom_id.id,
  75. "price_unit": 100,
  76. "discount": 50,
  77. "recurring_rule_type": "monthly",
  78. "recurring_interval": 1,
  79. "date_start": "2016-02-15",
  80. "recurring_next_date": "2016-02-29",
  81. },
  82. )
  83. ],
  84. }
  85. )
  86. self.contract_line = self.contract.recurring_invoice_line_ids[0]
  87. def test_compute_is_contract(self):
  88. """Sale Order should have is_contract true if one of its lines is
  89. contract"""
  90. self.assertTrue(self.sale.is_contract)
  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. contract_line = self.order_line1.contract_id.recurring_invoice_line_ids
  103. self.assertEqual(contract_line.date_start, Date.to_date('2018-01-01'))
  104. self.assertEqual(contract_line.date_end, Date.to_date('2018-12-31'))
  105. self.assertEqual(
  106. contract_line.recurring_next_date, Date.to_date('2018-01-31')
  107. )
  108. def test_sale_contract_count(self):
  109. """It should count contracts as many different contract template used
  110. in order_line"""
  111. self.order_line1.onchange_product()
  112. self.sale.action_confirm()
  113. self.assertEqual(self.sale.contract_count, 2)
  114. def test_onchange_product(self):
  115. """ It should get recurrence invoicing info to the sale line from
  116. its product """
  117. self.order_line1.onchange_product()
  118. self.assertEqual(
  119. self.order_line1.recurring_rule_type,
  120. self.product1.recurring_rule_type,
  121. )
  122. self.assertEqual(
  123. self.order_line1.recurring_invoicing_type,
  124. self.product1.recurring_invoicing_type,
  125. )
  126. self.assertEqual(self.order_line1.date_end, Date.to_date('2018-12-31'))
  127. def test_check_contract_sale_partner(self):
  128. """Can't link order line to a partner contract different then the
  129. order one"""
  130. contract2 = self.env['account.analytic.account'].create(
  131. {
  132. 'name': 'Contract',
  133. 'contract_template_id': self.contract_template2.id,
  134. 'partner_id': self.sale.partner_id.id,
  135. }
  136. )
  137. with self.assertRaises(ValidationError):
  138. self.order_line1.contract_id = contract2
  139. def test_check_contract_sale_contract_template(self):
  140. """Can't link order line to a contract with different contract
  141. template then the product one"""
  142. contract1 = self.env['account.analytic.account'].create(
  143. {
  144. 'name': 'Contract',
  145. 'contract_template_id': self.contract_template1.id,
  146. }
  147. )
  148. with self.assertRaises(ValidationError):
  149. self.order_line1.contract_id = contract1
  150. def test_no_contract_proudct(self):
  151. """it should create contract for only product contract"""
  152. self.product1.is_contract = False
  153. self.sale.action_confirm()
  154. self.assertFalse(self.order_line1.contract_id)
  155. def test_sale_order_line_invoice_status(self):
  156. """Sale order line for contract product should have nothing to
  157. invoice as status"""
  158. self.order_line1.onchange_product()
  159. self.sale.action_confirm()
  160. self.assertEqual(self.order_line1.invoice_status, 'no')
  161. def test_sale_order_invoice_status(self):
  162. """Sale order with only contract product should have nothing to
  163. invoice status directtly"""
  164. self.sale.order_line.filtered(
  165. lambda line: not line.product_id.is_contract
  166. ).unlink()
  167. self.order_line1.onchange_product()
  168. self.sale.action_confirm()
  169. self.assertEqual(self.sale.invoice_status, 'no')
  170. def test_sale_order_create_invoice(self):
  171. """Should not invoice contract product on sale order create invoice"""
  172. self.product2.is_contract = False
  173. self.product2.invoice_policy = 'order'
  174. self.order_line1.onchange_product()
  175. self.sale.action_confirm()
  176. self.sale.action_invoice_create()
  177. self.assertEqual(len(self.sale.invoice_ids), 1)
  178. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  179. lambda line: line.product_id.is_contract
  180. )
  181. self.assertEqual(len(invoice_line), 0)
  182. def test_link_contract_invoice_to_sale_order(self):
  183. """It should link contract invoice to sale order"""
  184. self.order_line1.onchange_product()
  185. self.sale.action_confirm()
  186. invoice = self.order_line1.contract_id.recurring_create_invoice()
  187. self.assertTrue(invoice in self.sale.invoice_ids)
  188. def test_contract_upsell(self):
  189. """Should stop contract line at sale order line start date"""
  190. self.order_line1.contract_id = self.contract
  191. self.order_line1.contract_line_id = self.contract_line
  192. self.contract_line.date_end = Date.today() + relativedelta(months=4)
  193. self.contract_line.is_auto_renew = True
  194. self.order_line1.date_start = "2018-06-01"
  195. self.order_line1.onchange_product()
  196. self.sale.action_confirm()
  197. self.assertEqual(
  198. self.contract_line.date_end, Date.to_date("2018-05-31")
  199. )
  200. self.assertFalse(self.contract_line.is_auto_renew)
  201. new_contract_line = self.env['account.analytic.invoice.line'].search(
  202. [('sale_order_line_id', '=', self.order_line1.id)]
  203. )
  204. self.assertEqual(
  205. self.contract_line.successor_contract_line_id, new_contract_line
  206. )
  207. self.assertEqual(
  208. new_contract_line.predecessor_contract_line_id, self.contract_line
  209. )
  210. def test_contract_upsell_2(self):
  211. """Should stop contract line at sale order line start date"""
  212. self.order_line1.contract_id = self.contract
  213. self.order_line1.contract_line_id = self.contract_line
  214. self.contract_line.write(
  215. {
  216. 'date_start': "2018-06-01",
  217. 'recurring_next_date': "2018-06-01",
  218. 'date_end': False,
  219. }
  220. )
  221. self.order_line1.date_start = "2018-06-01"
  222. self.order_line1.onchange_product()
  223. self.sale.action_confirm()
  224. self.assertFalse(self.contract_line.date_end)
  225. self.assertTrue(self.contract_line.is_canceled)
  226. def test_onchange_product_id_recurring_info(self):
  227. self.product2.write(
  228. {
  229. 'recurring_rule_type': 'monthly',
  230. 'recurring_invoicing_type': 'pre-paid',
  231. 'is_auto_renew': True,
  232. 'default_qty': 12,
  233. 'termination_notice_interval': '6',
  234. 'termination_notice_rule_type': 'weekly',
  235. }
  236. )
  237. self.contract_line.write(
  238. {
  239. 'date_start': Date.today(),
  240. 'date_end': Date.today() + relativedelta(years=1),
  241. 'recurring_next_date': Date.today(),
  242. 'product_id': self.product2.id,
  243. }
  244. )
  245. self.contract_line._onchange_product_id_recurring_info()
  246. self.assertEqual(self.contract_line.recurring_rule_type, 'monthly')
  247. self.assertEqual(
  248. self.contract_line.recurring_invoicing_type, 'pre-paid'
  249. )
  250. self.assertEqual(self.contract_line.recurring_interval, 1)
  251. self.assertEqual(self.contract_line.is_auto_renew, True)
  252. self.assertEqual(self.contract_line.auto_renew_interval, 12)
  253. self.assertEqual(self.contract_line.auto_renew_rule_type, 'monthly')
  254. self.assertEqual(self.contract_line.termination_notice_interval, 6)
  255. self.assertEqual(
  256. self.contract_line.termination_notice_rule_type, 'weekly'
  257. )