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.

395 lines
12 KiB

  1. # Copyright 2018 ACSONE SA/NV.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import fields
  4. from odoo.tests.common import TransactionCase
  5. def to_date(date):
  6. return fields.Date.to_date(date)
  7. class TestProductTemplate(TransactionCase):
  8. def setUp(self):
  9. super(TestProductTemplate, self).setUp()
  10. self.partner = self.env.ref("base.res_partner_2")
  11. self.product = self.env.ref("product.product_product_1")
  12. self.contract = self.env["contract.contract"].create(
  13. {
  14. "name": "Test Contract 2",
  15. "partner_id": self.partner.id,
  16. "pricelist_id": self.partner.property_product_pricelist.id,
  17. "contract_type": "purchase",
  18. "contract_line_ids": [
  19. (
  20. 0,
  21. 0,
  22. {
  23. "product_id": self.product.id,
  24. "name": "Services from #START# to #END#",
  25. "quantity": 1,
  26. "uom_id": self.product.uom_id.id,
  27. "price_unit": 100,
  28. "discount": 50,
  29. "recurring_rule_type": "monthly",
  30. "recurring_interval": 1,
  31. "date_start": "2016-02-15",
  32. "recurring_next_date": "2016-02-29",
  33. },
  34. )
  35. ],
  36. }
  37. )
  38. self.contract_line = self.contract.contract_line_ids[0]
  39. def test_compute_prorated(self):
  40. def update_contract_line(
  41. case,
  42. recurring_rule_type,
  43. recurring_interval,
  44. recurring_invoicing_type,
  45. date_start,
  46. recurring_next_date,
  47. date_end,
  48. last_date_invoiced=False,
  49. ):
  50. self.contract_line.write(
  51. {
  52. "recurring_rule_type": recurring_rule_type,
  53. "recurring_invoicing_type": recurring_invoicing_type,
  54. "recurring_interval": recurring_interval,
  55. "date_start": date_start,
  56. "recurring_next_date": recurring_next_date,
  57. "date_end": date_end,
  58. "last_date_invoiced": last_date_invoiced,
  59. }
  60. )
  61. def error_message(
  62. case,
  63. recurring_rule_type,
  64. recurring_interval,
  65. recurring_invoicing_type,
  66. date_start,
  67. recurring_next_date,
  68. date_end,
  69. last_date_invoiced=False,
  70. ):
  71. return (
  72. "%s : Error in %s every %d %s case, start %s, next %s, end %s,"
  73. " last %s"
  74. % (
  75. case,
  76. recurring_invoicing_type,
  77. recurring_interval,
  78. recurring_rule_type,
  79. date_start,
  80. recurring_next_date,
  81. date_end,
  82. last_date_invoiced or "",
  83. )
  84. )
  85. combinations = [
  86. (
  87. 1.00,
  88. (
  89. "Case 1",
  90. "monthly",
  91. 1,
  92. "pre-paid",
  93. to_date("2018-01-05"),
  94. to_date("2018-01-05"),
  95. False,
  96. ),
  97. ),
  98. (
  99. 1.00,
  100. (
  101. "Case 2",
  102. "monthly",
  103. 1,
  104. "pre-paid",
  105. to_date("2018-01-05"),
  106. to_date("2018-02-01"),
  107. False,
  108. to_date("2018-01-31"),
  109. ),
  110. ),
  111. (
  112. 1.00,
  113. (
  114. "Case 3",
  115. "monthly",
  116. 1,
  117. "pre-paid",
  118. to_date("2017-01-05"),
  119. to_date("2018-02-01"),
  120. to_date("2018-03-01"),
  121. to_date("2018-01-31"),
  122. ),
  123. ),
  124. (
  125. 0.892,
  126. (
  127. "Case 4",
  128. "monthly",
  129. 1,
  130. "pre-paid",
  131. to_date("2017-01-05"),
  132. to_date("2018-02-01"),
  133. to_date("2018-02-25"),
  134. to_date("2018-01-31"),
  135. ),
  136. ),
  137. (
  138. 1.00,
  139. (
  140. "Case 5",
  141. "monthly",
  142. 1,
  143. "post-paid",
  144. to_date("2018-01-05"),
  145. to_date("2018-02-05"),
  146. False,
  147. ),
  148. ),
  149. (
  150. 0.87,
  151. (
  152. "Case 6",
  153. "monthly",
  154. 1,
  155. "post-paid",
  156. to_date("2018-01-05"),
  157. to_date("2018-02-01"),
  158. False,
  159. ),
  160. ),
  161. (
  162. 1.00,
  163. (
  164. "Case 7",
  165. "monthly",
  166. 1,
  167. "post-paid",
  168. to_date("2017-01-05"),
  169. to_date("2018-02-01"),
  170. to_date("2018-03-01"),
  171. to_date("2017-12-31"),
  172. ),
  173. ),
  174. (
  175. 0.892,
  176. (
  177. "Case 8",
  178. "monthly",
  179. 1,
  180. "post-paid",
  181. to_date("2017-01-05"),
  182. to_date("2018-03-01"),
  183. to_date("2018-02-25"),
  184. to_date("2018-01-31"),
  185. ),
  186. ),
  187. (
  188. 1.00,
  189. (
  190. "Case 9",
  191. "monthlylastday",
  192. 1,
  193. "post-paid",
  194. to_date("2018-01-01"),
  195. to_date("2018-01-31"),
  196. to_date("2018-02-25"),
  197. ),
  198. ),
  199. (
  200. 0.87,
  201. (
  202. "Case 10",
  203. "monthlylastday",
  204. 1,
  205. "post-paid",
  206. to_date("2018-01-05"),
  207. to_date("2018-01-31"),
  208. to_date("2018-02-25"),
  209. ),
  210. ),
  211. (
  212. 0.892,
  213. (
  214. "Case 11",
  215. "monthlylastday",
  216. 1,
  217. "post-paid",
  218. to_date("2018-01-05"),
  219. to_date("2018-02-28"),
  220. to_date("2018-02-25"),
  221. to_date("2018-01-31"),
  222. ),
  223. ),
  224. (
  225. 0.5,
  226. (
  227. "Case 12",
  228. "monthlylastday",
  229. 1,
  230. "post-paid",
  231. to_date("2018-02-01"),
  232. to_date("2018-02-28"),
  233. to_date("2018-02-14"),
  234. ),
  235. ),
  236. (
  237. 0.5,
  238. (
  239. "Case 13",
  240. "monthlylastday",
  241. 1,
  242. "post-paid",
  243. to_date("2018-02-15"),
  244. to_date("2018-02-28"),
  245. False,
  246. ),
  247. ),
  248. (
  249. 0.032,
  250. (
  251. "Case 14",
  252. "monthlylastday",
  253. 1,
  254. "post-paid",
  255. to_date("2017-02-15"),
  256. to_date("2018-01-31"),
  257. False,
  258. to_date("2018-01-30"),
  259. ),
  260. ),
  261. (
  262. 1.035,
  263. (
  264. "Case 15",
  265. "monthlylastday",
  266. 1,
  267. "post-paid",
  268. to_date("2017-02-15"),
  269. to_date("2018-02-28"),
  270. False,
  271. to_date("2018-01-30"),
  272. ),
  273. ),
  274. (
  275. 0.032,
  276. (
  277. "Case 16",
  278. "monthly",
  279. 1,
  280. "post-paid",
  281. to_date("2017-02-15"),
  282. to_date("2018-02-01"),
  283. False,
  284. to_date("2018-01-30"),
  285. ),
  286. ),
  287. (
  288. 1.035,
  289. (
  290. "Case 17",
  291. "monthly",
  292. 1,
  293. "post-paid",
  294. to_date("2017-02-15"),
  295. to_date("2018-03-01"),
  296. False,
  297. to_date("2018-01-30"),
  298. ),
  299. ),
  300. (
  301. 0.032,
  302. (
  303. "Case 18",
  304. "monthly",
  305. 1,
  306. "pre-paid",
  307. to_date("2017-02-15"),
  308. to_date("2018-01-01"),
  309. False,
  310. to_date("2018-01-30"),
  311. ),
  312. ),
  313. (
  314. 1.035,
  315. (
  316. "Case 19",
  317. "monthly",
  318. 1,
  319. "pre-paid",
  320. to_date("2017-02-15"),
  321. to_date("2018-02-01"),
  322. False,
  323. to_date("2018-01-30"),
  324. ),
  325. ),
  326. (
  327. 1.0,
  328. (
  329. "Case 18",
  330. "monthlylastday",
  331. 1,
  332. "pre-paid",
  333. to_date("2017-02-15"),
  334. to_date("2018-01-31"),
  335. False,
  336. to_date("2017-12-31"),
  337. ),
  338. ),
  339. (
  340. 1.566,
  341. (
  342. "Case 19",
  343. "monthlylastday",
  344. 1,
  345. "pre-paid",
  346. to_date("2018-03-15"),
  347. to_date("2018-04-30"),
  348. False,
  349. ),
  350. ),
  351. (
  352. 1.48,
  353. (
  354. "Case 20",
  355. "monthly",
  356. 1,
  357. "post-paid",
  358. to_date("2018-03-15"),
  359. to_date("2018-04-30"),
  360. False,
  361. ),
  362. ),
  363. (
  364. 2.53,
  365. (
  366. "Case 21",
  367. "monthly",
  368. 1,
  369. "pre-paid",
  370. to_date("2018-03-15"),
  371. to_date("2018-04-30"),
  372. False,
  373. ),
  374. ),
  375. ]
  376. for result, combination in combinations:
  377. update_contract_line(*combination)
  378. dates = self.contract_line._get_period_to_invoice(
  379. self.contract_line.last_date_invoiced,
  380. self.contract_line.recurring_next_date,
  381. )
  382. self.assertAlmostEqual(
  383. result,
  384. self.contract_line.compute_prorated(*dates),
  385. places=2,
  386. msg=error_message(*combination),
  387. )