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.

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