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.

306 lines
12 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. from datetime import date, datetime, timedelta
  5. from openerp import exceptions, fields
  6. from openerp.exceptions import UserError, ValidationError
  7. from openerp.tests.common import TransactionCase
  8. class TestAttendanceSheet(TransactionCase):
  9. def setUp(self):
  10. super(TestAttendanceSheet, 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_shift.beesdoo_shift_user_1_demo"
  26. )
  27. self.user_permanent = self.env.ref(
  28. "beesdoo_shift.beesdoo_shift_user_2_demo"
  29. )
  30. self.worker_regular_1 = self.env.ref(
  31. "beesdoo_base.res_partner_cooperator_6_demo"
  32. )
  33. self.worker_regular_2 = self.env.ref(
  34. "beesdoo_base.res_partner_cooperator_5_demo"
  35. )
  36. self.worker_regular_3 = self.env.ref(
  37. "beesdoo_base.res_partner_cooperator_3_demo"
  38. )
  39. self.worker_regular_super_1 = self.env.ref(
  40. "beesdoo_base.res_partner_cooperator_1_demo"
  41. )
  42. self.worker_irregular_1 = self.env.ref(
  43. "beesdoo_base.res_partner_cooperator_2_demo"
  44. )
  45. self.worker_irregular_2 = self.env.ref(
  46. "beesdoo_base.res_partner_cooperator_4_demo"
  47. )
  48. self.task_type_1 = self.env.ref(
  49. "beesdoo_shift.beesdoo_shift_task_type_1_demo"
  50. )
  51. self.task_type_2 = self.env.ref(
  52. "beesdoo_shift.beesdoo_shift_task_type_2_demo"
  53. )
  54. self.task_type_3 = self.env.ref(
  55. "beesdoo_shift.beesdoo_shift_task_type_3_demo"
  56. )
  57. self.task_template_1 = self.env.ref(
  58. "beesdoo_shift.beesdoo_shift_task_template_1_demo"
  59. )
  60. self.task_template_2 = self.env.ref(
  61. "beesdoo_shift.beesdoo_shift_task_template_2_demo"
  62. )
  63. self.shift_regular_regular_1 = self.shift_model.create(
  64. {
  65. "task_template_id": self.task_template_1.id,
  66. "task_type_id": self.task_type_1.id,
  67. "worker_id": self.worker_regular_1.id,
  68. "start_time": self.current_time + timedelta(minutes=5),
  69. "end_time": self.current_time + timedelta(minutes=10),
  70. "is_regular": True,
  71. "is_compensation": False,
  72. }
  73. )
  74. self.shift_regular_regular_2 = self.shift_model.create(
  75. {
  76. "task_template_id": self.task_template_2.id,
  77. "task_type_id": self.task_type_2.id,
  78. "worker_id": self.worker_regular_2.id,
  79. "start_time": self.current_time - timedelta(minutes=50),
  80. "end_time": self.current_time - timedelta(minutes=20),
  81. "is_regular": True,
  82. "is_compensation": False,
  83. }
  84. )
  85. self.shift_regular_regular_replaced_1 = self.shift_model.create(
  86. {
  87. "task_template_id": self.task_template_1.id,
  88. "task_type_id": self.task_type_3.id,
  89. "worker_id": self.worker_regular_3.id,
  90. "start_time": self.current_time + timedelta(minutes=5),
  91. "end_time": self.current_time + timedelta(minutes=10),
  92. "is_regular": True,
  93. "is_compensation": False,
  94. "replaced_id": self.worker_regular_2.id,
  95. }
  96. )
  97. self.shift_regular_compensation_1 = self.shift_model.create(
  98. {
  99. "task_template_id": self.task_template_2.id,
  100. "task_type_id": self.task_type_1.id,
  101. "worker_id": self.worker_regular_super_1.id,
  102. "start_time": self.current_time + timedelta(minutes=9),
  103. "end_time": self.current_time + timedelta(minutes=21),
  104. "is_regular": False,
  105. "is_compensation": True,
  106. }
  107. )
  108. self.shift_irregular_1 = self.shift_model.create(
  109. {
  110. "task_template_id": self.task_template_2.id,
  111. "task_type_id": self.task_type_2.id,
  112. "worker_id": self.worker_irregular_1.id,
  113. "start_time": self.current_time + timedelta(minutes=5),
  114. "end_time": self.current_time + timedelta(minutes=10),
  115. }
  116. )
  117. self.shift_irregular_2 = self.shift_model.create(
  118. {
  119. "task_template_id": self.task_template_1.id,
  120. "task_type_id": self.task_type_3.id,
  121. "worker_id": self.worker_irregular_2.id,
  122. "start_time": self.current_time + timedelta(minutes=40),
  123. "end_time": self.current_time + timedelta(minutes=50),
  124. }
  125. )
  126. self.shift_empty_1 = self.shift_model.create(
  127. {
  128. "task_template_id": self.task_template_2.id,
  129. "task_type_id": self.task_type_1.id,
  130. "start_time": self.current_time + timedelta(minutes=5),
  131. "end_time": self.current_time + timedelta(minutes=10),
  132. }
  133. )
  134. def _search_sheets(self, start_time, end_time):
  135. return self.attendance_sheet_model.search(
  136. [("start_time", "=", start_time), ("end_time", "=", end_time)]
  137. )
  138. def test_default_task_type_setting(self):
  139. "Test default task type setting"
  140. setting_wizard = self.env["beesdoo.shift.config.settings"].sudo(
  141. self.user_admin
  142. )
  143. for task_type in (self.task_type_1, self.task_type_2):
  144. # setting default value
  145. setting_wizard_1 = setting_wizard.create(
  146. {"default_task_type_id": task_type.id}
  147. )
  148. setting_wizard_1.execute()
  149. param_id = self.env["ir.config_parameter"].get_param(
  150. "beesdoo_shift.default_task_type_id"
  151. )
  152. self.assertEquals(int(param_id), task_type.id)
  153. # check propagation on attendance sheet shifts
  154. self.assertEquals(
  155. self.attendance_sheet_shift_model.default_task_type_id(),
  156. task_type,
  157. )
  158. def test_attendance_sheet_creation(self):
  159. "Test creation of an attendance sheet with all its expected shifts"
  160. start_in_1 = self.shift_regular_regular_1.start_time
  161. end_in_1 = self.shift_regular_regular_1.end_time
  162. start_in_2 = self.shift_regular_compensation_1.start_time
  163. end_in_2 = self.shift_regular_compensation_1.end_time
  164. start_out_1 = self.shift_regular_regular_2.start_time
  165. end_out_1 = self.shift_regular_regular_2.end_time
  166. start_out_2 = self.shift_irregular_2.start_time
  167. end_out_2 = self.shift_irregular_2.end_time
  168. # test attendance sheets creation
  169. self.attendance_sheet_model._generate_attendance_sheet()
  170. self.assertEquals(len(self._search_sheets(start_in_1, end_in_1)), 1)
  171. self.assertEquals(len(self._search_sheets(start_in_2, end_in_2)), 1)
  172. self.assertEquals(len(self._search_sheets(start_out_1, end_out_1)), 0)
  173. self.assertEquals(len(self._search_sheets(start_out_2, end_out_2)), 0)
  174. # test expected shifts creation
  175. sheet_1 = self._search_sheets(start_in_1, end_in_1)
  176. sheet_2 = self._search_sheets(start_in_2, end_in_2)
  177. self.assertTrue(sheet_1.start_time)
  178. self.assertTrue(sheet_1.end_time)
  179. # empty shift should not be added
  180. self.assertEquals(len(sheet_1.expected_shift_ids), 3)
  181. self.assertEquals(len(sheet_1.added_shift_ids), 0)
  182. self.assertEquals(len(sheet_2.expected_shift_ids), 1)
  183. # test consistency with actual shift
  184. for shift in sheet_1.expected_shift_ids:
  185. self.assertEquals(shift.worker_id, shift.task_id.worker_id)
  186. self.assertEquals(
  187. shift.replacement_worker_id, shift.task_id.replaced_id
  188. )
  189. self.assertEquals(shift.task_type_id, shift.task_id.task_type_id)
  190. self.assertEquals(shift.super_coop_id, shift.task_id.super_coop_id)
  191. self.assertEquals(shift.working_mode, shift.task_id.working_mode)
  192. if shift.working_mode == "regular":
  193. self.assertEquals(shift.state, "absent_2")
  194. if shift.working_mode == "irregular":
  195. self.assertEquals(shift.state, "absent_1")
  196. # test maximum number of workers calculation from task_templates
  197. self.assertEquals(sheet_1.max_worker_no, 21)
  198. # test default values creation
  199. self.assertTrue(sheet_1.time_slot)
  200. self.assertTrue(sheet_1.name)
  201. day = fields.Datetime.from_string(sheet_1.start_time)
  202. self.assertEquals(sheet_1.day, fields.Date.to_string(day))
  203. self.assertFalse(sheet_1.annotation)
  204. self.assertFalse(sheet_1.is_annotated)
  205. # test default super-cooperator setting
  206. self.assertTrue(self.shift_expected_model.default_task_type_id())
  207. def test_attendance_sheet_barcode_scanner(self):
  208. "Test edition of an attendance sheet with barcode scanner"
  209. # attendance sheet generation
  210. self.attendance_sheet_model.sudo(self.user_generic)._generate_attendance_sheet()
  211. sheet_1 = self._search_sheets(
  212. self.shift_regular_regular_1.start_time,
  213. self.shift_regular_regular_1.end_time,
  214. )
  215. """
  216. Expected workers :
  217. worker_regular_1 (barcode : 421457731745)
  218. worker_regular_3 replaced by worker_regular_2 (barcode : 421457731744))
  219. worker_irregular_1 (barcode : 429919251493)
  220. """
  221. # scan barcode for expected workers
  222. for barcode in [421457731745, 421457731744, 429919251493]:
  223. sheet_1.on_barcode_scanned(barcode)
  224. # check expected shifts update
  225. for id in sheet_1.expected_shift_ids.ids:
  226. shift = sheet_1.expected_shift_ids.browse(id)
  227. self.assertEquals(shift.state, "present")
  228. """
  229. Added workers :
  230. worker_regular_super_1 (barcode : 421457731741)
  231. worker_irregular_2 (barcode : 421457731743)
  232. """
  233. # _onchange method not applying on temporary object in tests
  234. sheet_1._origin = sheet_1
  235. # scan barcode for added workers
  236. sheet_1.on_barcode_scanned(421457731741)
  237. self.assertEquals(len(sheet_1.added_shift_ids), 1)
  238. sheet_1.on_barcode_scanned(421457731743)
  239. # scan an already added worker should not change anything
  240. sheet_1.on_barcode_scanned(421457731743)
  241. self.assertEquals(len(sheet_1.added_shift_ids), 2)
  242. # check added shifts fields
  243. for id in sheet_1.added_shift_ids.ids:
  244. shift = sheet_1.added_shift_ids.browse(id)
  245. self.assertEquals(sheet_1, shift.attendance_sheet_id)
  246. self.assertEquals(shift.state, "present")
  247. self.assertEquals(
  248. shift.task_type_id,
  249. self.attendance_sheet_shift_model.default_task_type_id(),
  250. )
  251. if shift.working_mode == "regular":
  252. self.assertTrue(shift.is_compensation)
  253. else:
  254. self.assertFalse(shift.is_compensation)
  255. # add a worker that should be replaced
  256. with self.assertRaises(UserError) as e:
  257. sheet_1.on_barcode_scanned(421457731742)
  258. # wrong barcode
  259. with self.assertRaises(UserError) as e:
  260. sheet_1.on_barcode_scanned(101010)
  261. # add an already expected worker
  262. with self.assertRaises(ValidationError) as e:
  263. sheet_1.added_shift_ids |= sheet_1.added_shift_ids.new(
  264. {
  265. "task_type_id": sheet_1.added_shift_ids.default_task_type_id(),
  266. "state": "present",
  267. "attendance_sheet_id": sheet_1.id,
  268. "worker_id": self.worker_regular_1.id,
  269. "regular_task_type": "normal",
  270. }
  271. )