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['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_action_confirm_without_contract_creation(self):
  109. """ It should create a contract for each contract template used in
  110. order_line """
  111. self.sale.company_id.create_contract_at_sale_order_confirmation = False
  112. self.order_line1.onchange_product()
  113. self.sale.action_confirm()
  114. self.assertEqual(len(self.sale.order_line.mapped('contract_id')), 0)
  115. self.assertTrue(self.sale.need_contract_creation)
  116. self.sale.action_create_contract()
  117. self.assertEqual(len(self.sale.order_line.mapped('contract_id')), 2)
  118. self.assertFalse(self.sale.need_contract_creation)
  119. self.assertEqual(
  120. self.order_line1.contract_id.contract_template_id,
  121. self.contract_template1,
  122. )
  123. contract_line = self.order_line1.contract_id.recurring_invoice_line_ids
  124. self.assertEqual(contract_line.date_start, Date.to_date('2018-01-01'))
  125. self.assertEqual(contract_line.date_end, Date.to_date('2018-12-31'))
  126. self.assertEqual(
  127. contract_line.recurring_next_date, Date.to_date('2018-01-31')
  128. )
  129. def test_sale_contract_count(self):
  130. """It should count contracts as many different contract template used
  131. in order_line"""
  132. self.order_line1.onchange_product()
  133. self.sale.action_confirm()
  134. self.assertEqual(self.sale.contract_count, 2)
  135. def test_onchange_product(self):
  136. """ It should get recurrence invoicing info to the sale line from
  137. its product """
  138. self.order_line1.onchange_product()
  139. self.assertEqual(
  140. self.order_line1.recurring_rule_type,
  141. self.product1.recurring_rule_type,
  142. )
  143. self.assertEqual(
  144. self.order_line1.recurring_invoicing_type,
  145. self.product1.recurring_invoicing_type,
  146. )
  147. self.assertEqual(self.order_line1.date_end, Date.to_date('2018-12-31'))
  148. def test_check_contract_sale_partner(self):
  149. """Can't link order line to a partner contract different then the
  150. order one"""
  151. contract2 = self.env['account.analytic.account'].create(
  152. {
  153. 'name': 'Contract',
  154. 'contract_template_id': self.contract_template2.id,
  155. 'partner_id': self.sale.partner_id.id,
  156. }
  157. )
  158. with self.assertRaises(ValidationError):
  159. self.order_line1.contract_id = contract2
  160. def test_check_contract_sale_contract_template(self):
  161. """Can't link order line to a contract with different contract
  162. template then the product one"""
  163. contract1 = self.env['account.analytic.account'].create(
  164. {
  165. 'name': 'Contract',
  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['account.analytic.invoice.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. )