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.

507 lines
19 KiB

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