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.

504 lines
19 KiB

  1. # Copyright 2019 - Today Coop IT Easy SCRLfs (<http://www.coopiteasy.be>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. import time
  4. from datetime import datetime, timedelta
  5. from odoo import exceptions, fields
  6. from odoo.exceptions import UserError, ValidationError
  7. from odoo.tests.common import TransactionCase
  8. class TestBeesdooShift(TransactionCase):
  9. def setUp(self):
  10. super(TestBeesdooShift, self).setUp()
  11. self.shift_model = self.env["beesdoo.shift.shift"]
  12. self.shift_template_model = self.env["beesdoo.shift.template"]
  13. self.attendance_sheet_model = self.env["beesdoo.shift.sheet"]
  14. self.attendance_sheet_shift_model = self.env[
  15. "beesdoo.shift.sheet.shift"
  16. ]
  17. self.shift_expected_model = self.env["beesdoo.shift.sheet.expected"]
  18. self.shift_added_model = self.env["beesdoo.shift.sheet.added"]
  19. self.default_task_type_id = self.env["ir.config_parameter"].get_param(
  20. "beesdoo_shift.default_task_type_id"
  21. )
  22. self.current_time = datetime.now()
  23. self.user_admin = self.env.ref("base.user_root")
  24. self.user_generic = self.env.ref(
  25. "beesdoo_base.beesdoo_shift_user_1_demo"
  26. )
  27. self.user_permanent = self.env.ref(
  28. "beesdoo_base.beesdoo_shift_user_2_demo"
  29. )
  30. self.setting_wizard = self.env["beesdoo.shift.config.settings"].sudo(
  31. self.user_admin
  32. )
  33. self.worker_regular_1 = self.env.ref(
  34. "beesdoo_base.res_partner_cooperator_6_demo"
  35. )
  36. self.worker_regular_2 = self.env.ref(
  37. "beesdoo_base.res_partner_cooperator_5_demo"
  38. )
  39. self.worker_regular_3 = self.env.ref(
  40. "beesdoo_base.res_partner_cooperator_3_demo"
  41. )
  42. self.worker_regular_super_1 = self.env.ref(
  43. "beesdoo_base.res_partner_cooperator_1_demo"
  44. )
  45. self.worker_irregular_1 = self.env.ref(
  46. "beesdoo_base.res_partner_cooperator_2_demo"
  47. )
  48. self.worker_irregular_2 = self.env.ref(
  49. "beesdoo_base.res_partner_cooperator_4_demo"
  50. )
  51. self.task_type_1 = self.env.ref(
  52. "beesdoo_shift.beesdoo_shift_task_type_1_demo"
  53. )
  54. self.task_type_2 = self.env.ref(
  55. "beesdoo_shift.beesdoo_shift_task_type_2_demo"
  56. )
  57. self.task_type_3 = self.env.ref(
  58. "beesdoo_shift.beesdoo_shift_task_type_3_demo"
  59. )
  60. self.task_template_1 = self.env.ref(
  61. "beesdoo_shift.beesdoo_shift_task_template_1_demo"
  62. )
  63. self.task_template_2 = self.env.ref(
  64. "beesdoo_shift.beesdoo_shift_task_template_2_demo"
  65. )
  66. # Set time in and out of generation interval parameter
  67. self.start_in_1 = self.current_time + timedelta(seconds=2)
  68. self.end_in_1 = self.current_time + timedelta(minutes=10)
  69. self.start_in_2 = self.current_time + timedelta(minutes=9)
  70. self.end_in_2 = self.current_time + timedelta(minutes=21)
  71. self.start_out_1 = self.current_time - timedelta(minutes=50)
  72. self.end_out_1 = self.current_time - timedelta(minutes=20)
  73. self.start_out_2 = self.current_time + timedelta(minutes=40)
  74. self.end_out_2 = self.current_time + timedelta(minutes=50)
  75. self.shift_regular_regular_1 = self.shift_model.create(
  76. {
  77. "task_template_id": self.task_template_1.id,
  78. "task_type_id": self.task_type_1.id,
  79. "worker_id": self.worker_regular_1.id,
  80. "start_time": self.start_in_1,
  81. "end_time": self.end_in_1,
  82. "is_regular": True,
  83. "is_compensation": False,
  84. }
  85. )
  86. self.shift_regular_regular_2 = self.shift_model.create(
  87. {
  88. "task_type_id": self.task_type_2.id,
  89. "worker_id": self.worker_regular_2.id,
  90. "start_time": self.start_out_1,
  91. "end_time": self.end_out_1,
  92. "is_regular": True,
  93. "is_compensation": False,
  94. }
  95. )
  96. self.shift_regular_regular_replaced_1 = self.shift_model.create(
  97. {
  98. "task_template_id": self.task_template_1.id,
  99. "task_type_id": self.task_type_3.id,
  100. "worker_id": self.worker_regular_3.id,
  101. "start_time": self.start_in_1,
  102. "end_time": self.end_in_1,
  103. "is_regular": True,
  104. "is_compensation": False,
  105. "replaced_id": self.worker_regular_2.id,
  106. }
  107. )
  108. future_shift_regular = self.shift_model.create(
  109. {
  110. "task_template_id": self.task_template_2.id,
  111. "task_type_id": self.task_type_1.id,
  112. "worker_id": self.worker_regular_super_1.id,
  113. "start_time": self.start_in_2,
  114. "end_time": self.end_in_2,
  115. "is_regular": False,
  116. "is_compensation": True,
  117. }
  118. )
  119. self.shift_irregular_1 = self.shift_model.create(
  120. {
  121. "task_template_id": self.task_template_1.id,
  122. "task_type_id": self.task_type_2.id,
  123. "worker_id": self.worker_irregular_1.id,
  124. "start_time": self.start_in_1,
  125. "end_time": self.end_in_1,
  126. }
  127. )
  128. self.shift_irregular_2 = self.shift_model.create(
  129. {
  130. "task_type_id": self.task_type_3.id,
  131. "worker_id": self.worker_irregular_2.id,
  132. "start_time": self.start_out_2,
  133. "end_time": self.end_out_2,
  134. }
  135. )
  136. self.shift_empty_1 = self.shift_model.create(
  137. {
  138. "task_template_id": self.task_template_1.id,
  139. "task_type_id": self.task_type_1.id,
  140. "start_time": self.start_in_1,
  141. "end_time": self.end_in_1,
  142. }
  143. )
  144. def search_sheets(self, start_time, end_time):
  145. if (type(start_time) and type(end_time)) == datetime:
  146. return self.attendance_sheet_model.search(
  147. [("start_time", "=", start_time), ("end_time", "=", end_time)]
  148. )
  149. def test_default_task_type_setting(self):
  150. "Test default task type setting"
  151. for task_type in (self.task_type_1, self.task_type_2):
  152. # Setting default value
  153. setting_wizard_1 = self.setting_wizard.create(
  154. {"default_task_type_id": task_type.id}
  155. )
  156. setting_wizard_1.execute()
  157. param_id = self.env["ir.config_parameter"].get_param(
  158. "beesdoo_shift.default_task_type_id"
  159. )
  160. self.assertEqual(int(param_id), task_type.id)
  161. # Check propagation on attendance sheet shifts
  162. self.assertEqual(
  163. self.attendance_sheet_shift_model.default_task_type_id(),
  164. task_type,
  165. )
  166. def test_attendance_sheet_creation(self):
  167. "Test creation of an attendance sheet with all its expected shifts"
  168. # Set generation interval setting
  169. setting_wizard_1 = self.setting_wizard.create(
  170. {
  171. "default_task_type_id": self.task_type_1.id,
  172. "attendance_sheet_generation_interval": 15,
  173. }
  174. )
  175. setting_wizard_1.execute()
  176. # Test attendance sheets creation
  177. self.attendance_sheet_model._generate_attendance_sheet()
  178. self.assertEqual(
  179. len(self.search_sheets(self.start_in_1, self.end_in_1)), 1
  180. )
  181. self.assertEqual(
  182. len(self.search_sheets(self.start_in_2, self.end_in_2)), 1
  183. )
  184. self.assertEqual(
  185. len(self.search_sheets(self.start_out_1, self.end_out_1)), 0
  186. )
  187. self.assertEqual(
  188. len(self.search_sheets(self.start_out_2, self.end_out_2)), 0
  189. )
  190. # Test expected shifts creation
  191. # Sheet 1 starts at current time + 2 secs, ends at current time + 10 min
  192. # Sheet 2 starts at current time + 9 min, ends at current time + 21 min
  193. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1)
  194. sheet_2 = self.search_sheets(self.start_in_2, self.end_in_2)
  195. self.assertTrue(sheet_1.start_time)
  196. self.assertTrue(sheet_1.end_time)
  197. # Empty shift should not be added
  198. self.assertEqual(len(sheet_1.expected_shift_ids), 3)
  199. self.assertEqual(len(sheet_1.added_shift_ids), 0)
  200. self.assertEqual(len(sheet_2.expected_shift_ids), 1)
  201. # Test consistency with actual shift for sheet 1
  202. for shift in sheet_1.expected_shift_ids:
  203. self.assertEqual(shift.worker_id, shift.task_id.worker_id)
  204. self.assertEqual(
  205. shift.replacement_worker_id, shift.task_id.replaced_id
  206. )
  207. self.assertEqual(shift.task_type_id, shift.task_id.task_type_id)
  208. self.assertEqual(shift.super_coop_id, shift.task_id.super_coop_id)
  209. self.assertEqual(shift.working_mode, shift.task_id.working_mode)
  210. # Status should be "absent" for all shifts
  211. self.assertEqual(shift.state, "absent")
  212. self.assertEqual(shift.compensation_no, "2")
  213. # Empty shift should be considered in max worker number calculation
  214. self.assertEqual(sheet_1.max_worker_no, 4)
  215. # Test default values creation
  216. self.assertTrue(sheet_1.time_slot)
  217. self.assertEqual(sheet_1.day, fields.Date.to_string(self.start_in_1))
  218. self.assertEqual(sheet_1.day_abbrevation, "Lundi")
  219. self.assertEqual(sheet_1.week, "Semaine A")
  220. self.assertTrue(sheet_1.name)
  221. self.assertFalse(sheet_1.notes)
  222. self.assertFalse(sheet_1.is_annotated)
  223. def test_attendance_sheet_barcode_scan(self):
  224. """
  225. Edition of an attendance sheet
  226. with barcode scanner, as a generic user"
  227. """
  228. # Attendance sheet generation
  229. self.attendance_sheet_model.sudo(
  230. self.user_generic
  231. )._generate_attendance_sheet()
  232. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1,)
  233. """
  234. Expected workers are :
  235. worker_regular_1 (barcode : 421457731745)
  236. worker_regular_3 replaced by worker_regular_2 (barcode : 421457731744))
  237. worker_irregular_1 (barcode : 429919251493)
  238. """
  239. # Scan barcode for expected workers
  240. for barcode in [421457731745, 421457731744, 429919251493]:
  241. sheet_1.on_barcode_scanned(barcode)
  242. # Check expected shifts update
  243. for id in sheet_1.expected_shift_ids.ids:
  244. shift = sheet_1.expected_shift_ids.browse(id)
  245. self.assertEqual(shift.state, "done")
  246. """
  247. Added workers are :
  248. worker_regular_super_1 (barcode : 421457731741)
  249. worker_irregular_2 (barcode : 421457731743)
  250. """
  251. # Workararound for _onchange method
  252. # (not applying on temporary object in tests)
  253. sheet_1._origin = sheet_1
  254. # Scan barcode for added workers
  255. sheet_1.on_barcode_scanned(421457731741)
  256. self.assertEqual(len(sheet_1.added_shift_ids), 1)
  257. sheet_1.on_barcode_scanned(421457731743)
  258. # Scan an already added worker should not change anything
  259. sheet_1.on_barcode_scanned(421457731743)
  260. self.assertEqual(len(sheet_1.added_shift_ids), 2)
  261. # Check added shifts fields
  262. for id in sheet_1.added_shift_ids.ids:
  263. shift = sheet_1.added_shift_ids.browse(id)
  264. self.assertEqual(sheet_1, shift.attendance_sheet_id)
  265. self.assertEqual(shift.state, "done")
  266. self.assertEqual(
  267. shift.task_type_id,
  268. self.attendance_sheet_shift_model.default_task_type_id(),
  269. )
  270. if shift.working_mode == "regular":
  271. self.assertTrue(shift.is_compensation)
  272. else:
  273. self.assertFalse(shift.is_compensation)
  274. # Add a worker that should be replaced
  275. with self.assertRaises(UserError) as e:
  276. sheet_1.on_barcode_scanned(421457731742)
  277. # Wrong barcode
  278. with self.assertRaises(UserError) as e:
  279. sheet_1.on_barcode_scanned(101010)
  280. # Add an unsubscribed worker
  281. self.worker_regular_1.cooperative_status_ids.sr = -2
  282. self.worker_regular_1.cooperative_status_ids.sc = -2
  283. with self.assertRaises(UserError) as e:
  284. sheet_1.on_barcode_scanned(421457731745)
  285. def test_attendance_sheet_edition(self):
  286. # Attendance sheet generation
  287. self.attendance_sheet_model.sudo(
  288. self.user_generic
  289. )._generate_attendance_sheet()
  290. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1)
  291. # Expected shifts edition
  292. sheet_1.expected_shift_ids[1].state = "done"
  293. sheet_1.expected_shift_ids[1].compensation_no = False
  294. sheet_1.expected_shift_ids[2].compensation_no = "1"
  295. # Added shits addition
  296. sheet_1.added_shift_ids |= sheet_1.added_shift_ids.new(
  297. {
  298. "task_type_id": self.task_type_2.id,
  299. "state": "done",
  300. "attendance_sheet_id": sheet_1.id,
  301. "worker_id": self.worker_irregular_2.id,
  302. "is_compensation": False,
  303. "is_regular": False,
  304. }
  305. )
  306. # Same task type as empty shift (should edit it on validation)
  307. sheet_1.added_shift_ids |= sheet_1.added_shift_ids.new(
  308. {
  309. "task_type_id": self.task_type_1.id,
  310. "state": "done",
  311. "attendance_sheet_id": sheet_1.id,
  312. "worker_id": self.worker_regular_super_1.id,
  313. "is_compensation": True,
  314. "is_regular": False,
  315. }
  316. )
  317. # Wait necessary time for shifts to begin
  318. waiting_time = (self.start_in_1 - datetime.now()).total_seconds()
  319. if waiting_time > 0:
  320. with self.assertRaises(UserError) as e:
  321. sheet_1.validate_with_checks()
  322. self.assertIn("wait", str(e.exception))
  323. time.sleep(waiting_time)
  324. # TODO: test validation with wizard (as generic user)
  325. # class odoo.tests.common.Form(recordp, view=None)
  326. # is only available from version 12
  327. # sheet_1 = sheet_1.sudo(self.user_generic)
  328. # Validation without wizard (as admin user)
  329. sheet_1 = sheet_1.sudo(self.user_admin)
  330. sheet_1.worker_nb_feedback = "enough"
  331. sheet_1.feedback = "Great session."
  332. sheet_1.notes = "Important information."
  333. sheet_1.validate_with_checks()
  334. with self.assertRaises(UserError) as e:
  335. sheet_1.validate_with_checks()
  336. self.assertIn("already been validated", str(e.exception))
  337. self.assertEqual(sheet_1.state, "validated")
  338. self.assertEqual(sheet_1.validated_by, self.user_admin.partner_id)
  339. self.assertTrue(sheet_1.is_annotated)
  340. self.assertFalse(sheet_1.is_read)
  341. # Check actual shifts update
  342. workers = sheet_1.expected_shift_ids.mapped(
  343. "worker_id"
  344. ) | sheet_1.added_shift_ids.mapped("worker_id")
  345. self.assertEqual(len(workers), 5)
  346. self.assertEqual(
  347. sheet_1.expected_shift_ids[0].task_id.state, "absent_2"
  348. )
  349. self.assertEqual(sheet_1.expected_shift_ids[1].task_id.state, "done")
  350. self.assertEqual(
  351. sheet_1.expected_shift_ids[2].task_id.state, "absent_1"
  352. )
  353. self.assertEqual(sheet_1.added_shift_ids[0].task_id.state, "done")
  354. self.assertEqual(sheet_1.added_shift_ids[1].task_id.state, "done")
  355. # Empty shift should have been updated
  356. self.assertEqual(
  357. sheet_1.added_shift_ids[0].task_id, self.shift_empty_1
  358. )
  359. # sheet_1.expected_shift_ids[0].worker_id
  360. # sheet_1.expected_shift_ids[2].replacement_worker_id
  361. def test_shift_counters(self):
  362. "Test shift counters calculation and cooperative status update"
  363. status_1 = self.worker_regular_1.cooperative_status_ids
  364. status_2 = self.worker_regular_3.cooperative_status_ids
  365. status_3 = self.worker_irregular_1.cooperative_status_ids
  366. shift_regular = self.shift_model.create(
  367. {
  368. "task_template_id": self.task_template_1.id,
  369. "task_type_id": self.task_type_1.id,
  370. "worker_id": self.worker_regular_1.id,
  371. "start_time": datetime.now() - timedelta(minutes=50),
  372. "end_time": datetime.now() - timedelta(minutes=40),
  373. "is_regular": True,
  374. "is_compensation": False,
  375. }
  376. )
  377. future_shift_regular = self.shift_model.create(
  378. {
  379. "task_template_id": self.task_template_2.id,
  380. "task_type_id": self.task_type_2.id,
  381. "worker_id": self.worker_regular_1.id,
  382. "start_time": datetime.now() + timedelta(minutes=20),
  383. "end_time": datetime.now() + timedelta(minutes=30),
  384. "is_regular": True,
  385. "is_compensation": False,
  386. }
  387. )
  388. shift_irregular = self.shift_model.create(
  389. {
  390. "task_template_id": self.task_template_2.id,
  391. "task_type_id": self.task_type_3.id,
  392. "worker_id": self.worker_irregular_1.id,
  393. "start_time": datetime.now() - timedelta(minutes=15),
  394. "end_time": datetime.now() - timedelta(minutes=10),
  395. }
  396. )
  397. # For a regular worker
  398. status_1.sr = 0
  399. status_1.sc = 0
  400. self.assertEqual(status_1.status, "ok")
  401. shift_regular.state = "absent_1"
  402. self.assertEqual(status_1.sr, -1)
  403. self.assertEqual(status_1.status, "alert")
  404. shift_regular.state = "done"
  405. self.assertEquals(status_1.sr, 0)
  406. self.assertEquals(status_1.sc, 0)
  407. shift_regular.state = "open"
  408. shift_regular.write({"is_regular": False, "is_compensation": True})
  409. shift_regular.state = "done"
  410. self.assertEquals(status_1.sr, 1)
  411. self.assertEquals(status_1.sc, 0)
  412. # Check unsubscribed status
  413. status_1.sr = -1
  414. status_1.sc = -1
  415. # Subscribe him to another future shift
  416. future_shift_regular.worker_id = self.worker_regular_1
  417. with self.assertRaises(ValidationError) as e:
  418. future_shift_regular.state = "absent_2"
  419. self.assertIn("future", str(e.exception))
  420. status_1.sr = -2
  421. status_1.sc = -2
  422. self.assertEquals(status_1.status, "unsubscribed")
  423. # Should be unsubscribed from future shift
  424. self.assertFalse(future_shift_regular.worker_id)
  425. # With replacement worker (self.worker_regular_3)
  426. shift_regular.state = "open"
  427. status_1.sr = 0
  428. status_1.sc = 0
  429. status_2.sr = 0
  430. status_2.sc = 0
  431. shift_regular.replaced_id = self.worker_regular_3
  432. shift_regular.state = "absent_2"
  433. self.assertEqual(status_1.sr, 0)
  434. self.assertEqual(status_1.sc, 0)
  435. self.assertEqual(status_2.sr, -1)
  436. self.assertEqual(status_2.sc, -1)
  437. # For an irregular worker
  438. status_3.sr = 0
  439. status_3.sc = 0
  440. self.assertEqual(status_3.status, "ok")
  441. shift_irregular.state = "done"
  442. self.assertEqual(status_3.sr, 1)
  443. shift_irregular.state = "absent_2"
  444. self.assertEqual(status_3.sr, -1)