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.

410 lines
13 KiB

4 years ago
4 years ago
4 years ago
7 years ago
4 years ago
7 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import logging
  2. from datetime import timedelta
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import UserError, ValidationError
  5. _logger = logging.getLogger(__name__)
  6. def add_days_delta(date_from, days_delta):
  7. if not date_from:
  8. return date_from
  9. next_date = date_from + timedelta(days=days_delta)
  10. return next_date
  11. class ExemptReason(models.Model):
  12. _name = "cooperative.exempt.reason"
  13. _description = "cooperative.exempt.reason"
  14. name = fields.Char(required=True)
  15. class HistoryStatus(models.Model):
  16. _name = "cooperative.status.history"
  17. _description = "cooperative.status.history"
  18. _order = "create_date desc"
  19. status_id = fields.Many2one("cooperative.status")
  20. cooperator_id = fields.Many2one("res.partner")
  21. change = fields.Char()
  22. type = fields.Selection(
  23. [("status", "Status Change"), ("counter", "Counter Change")]
  24. )
  25. user_id = fields.Many2one("res.users", string="User")
  26. class CooperativeStatus(models.Model):
  27. _name = "cooperative.status"
  28. _description = "cooperative.status"
  29. _rec_name = "cooperator_id"
  30. _order = "cooperator_id"
  31. _period = 28
  32. def _get_status(self):
  33. return [
  34. ("ok", "Up to Date"),
  35. ("holiday", "Holidays"),
  36. ("alert", "Alerte"),
  37. ("extension", "Extension"),
  38. ("suspended", "Suspended"),
  39. ("exempted", "Exempted"),
  40. ("unsubscribed", "Unsubscribed"),
  41. ("resigning", "Resigning"),
  42. ]
  43. today = fields.Date(
  44. help="Field that allow to compute field and store them even if they "
  45. "are based on the current date",
  46. default=fields.Date.today,
  47. )
  48. cooperator_id = fields.Many2one("res.partner")
  49. active = fields.Boolean(
  50. related="cooperator_id.active", store=True, index=True
  51. )
  52. info_session = fields.Boolean("Information Session ?")
  53. info_session_date = fields.Date("Information Session Date")
  54. super = fields.Boolean("Super Cooperative")
  55. sr = fields.Integer("Regular shifts counter", default=0)
  56. sc = fields.Integer("Compensation shifts counter", default=0)
  57. time_extension = fields.Integer(
  58. "Extension Days NB",
  59. default=0,
  60. help="Addtional days to the automatic extension, 5 mean that you have "
  61. "a total of 15 extension days of default one is set to 10",
  62. )
  63. holiday_start_time = fields.Date("Holidays Start Day")
  64. holiday_end_time = fields.Date("Holidays End Day")
  65. alert_start_time = fields.Date("Alert Start Day")
  66. extension_start_time = fields.Date("Extension Start Day")
  67. working_mode = fields.Selection(
  68. [
  69. ("regular", "Regular worker"),
  70. ("irregular", "Irregular worker"),
  71. ("exempt", "Exempted"),
  72. ],
  73. string="Working mode",
  74. )
  75. exempt_reason_id = fields.Many2one(
  76. comodel_name="cooperative.exempt.reason", string="Exempt Reason"
  77. )
  78. status = fields.Selection(
  79. selection=_get_status,
  80. compute="_compute_status",
  81. string="Cooperative Status",
  82. store=True,
  83. )
  84. can_shop = fields.Boolean(compute="_compute_can_shop", store=True)
  85. history_ids = fields.One2many(
  86. "cooperative.status.history", "status_id", readonly=True
  87. )
  88. unsubscribed = fields.Boolean(default=False, help="Manually unsubscribed")
  89. resigning = fields.Boolean(
  90. default=False, help="Want to leave the beescoop"
  91. )
  92. # Specific to irregular
  93. irregular_start_date = fields.Date() # TODO migration script
  94. irregular_absence_date = fields.Date()
  95. irregular_absence_counter = (
  96. fields.Integer()
  97. ) # TODO unsubscribe when reach -2
  98. future_alert_date = fields.Date(compute="_compute_future_alert_date")
  99. next_countdown_date = fields.Date(compute="_compute_next_countdown_date")
  100. temporary_exempt_reason_id = fields.Many2one(
  101. comodel_name="cooperative.exempt.reason",
  102. string="Temporary Exempt Reason",
  103. )
  104. temporary_exempt_start_date = fields.Date()
  105. temporary_exempt_end_date = fields.Date()
  106. @api.depends("status")
  107. def _compute_can_shop(self):
  108. for rec in self:
  109. rec.can_shop = rec.status in self._can_shop_status()
  110. @api.depends(
  111. "today",
  112. "sr",
  113. "sc",
  114. "holiday_end_time",
  115. "holiday_start_time",
  116. "time_extension",
  117. "alert_start_time",
  118. "extension_start_time",
  119. "unsubscribed",
  120. "irregular_absence_date",
  121. "irregular_absence_counter",
  122. "temporary_exempt_start_date",
  123. "temporary_exempt_end_date",
  124. "resigning",
  125. "cooperator_id.subscribed_shift_ids",
  126. "working_mode",
  127. )
  128. def _compute_status(self):
  129. update = int(
  130. self.env["ir.config_parameter"]
  131. .sudo()
  132. .get_param("always_update", False)
  133. )
  134. for rec in self:
  135. if update or not rec.today:
  136. rec.status = "ok"
  137. continue
  138. if rec.resigning:
  139. rec.status = "resigning"
  140. continue
  141. if rec.working_mode == "regular":
  142. rec.status = rec._get_regular_status()
  143. elif rec.working_mode == "irregular":
  144. rec.status = rec._get_irregular_status()
  145. elif rec.working_mode == "exempt":
  146. rec.status = "ok"
  147. _sql_constraints = [
  148. (
  149. "cooperator_uniq",
  150. "unique (cooperator_id)",
  151. _("You can only set one cooperator status per cooperator"),
  152. )
  153. ]
  154. @api.constrains("working_mode", "irregular_start_date")
  155. def _constrains_irregular_start_date(self):
  156. if self.working_mode == "irregular" and not self.irregular_start_date:
  157. raise UserError(
  158. _("Irregular workers must have an irregular start date.")
  159. )
  160. @api.multi
  161. def write(self, vals):
  162. """
  163. Overwrite write to historize the change
  164. """
  165. for field in [
  166. "sr",
  167. "sc",
  168. "time_extension",
  169. "extension_start_time",
  170. "alert_start_time",
  171. "unsubscribed",
  172. ]:
  173. if field not in vals:
  174. continue
  175. for rec in self:
  176. data = {
  177. "status_id": rec.id,
  178. "cooperator_id": rec.cooperator_id.id,
  179. "type": "counter",
  180. "user_id": self.env.context.get("real_uid", self.env.uid),
  181. }
  182. if vals.get(field, rec[field]) != rec[field]:
  183. data["change"] = "{}: {} -> {}".format(
  184. field.upper(), rec[field], vals.get(field)
  185. )
  186. self.env["cooperative.status.history"].sudo().create(data)
  187. return super(CooperativeStatus, self).write(vals)
  188. @api.multi
  189. def _write(self, vals):
  190. """
  191. Overwrite write to historize the change of status
  192. and make action on status change
  193. """
  194. if "status" in vals:
  195. self._cr.execute(
  196. 'select id, status, sr, sc from "%s" where id in %%s'
  197. % self._table,
  198. (self._ids,),
  199. )
  200. result = self._cr.dictfetchall()
  201. old_status_per_id = {r["id"]: r for r in result}
  202. for rec in self:
  203. if old_status_per_id[rec.id]["status"] != vals["status"]:
  204. data = {
  205. "status_id": rec.id,
  206. "cooperator_id": rec.cooperator_id.id,
  207. "type": "status",
  208. "change": "STATUS: %s -> %s"
  209. % (
  210. old_status_per_id[rec.id]["status"],
  211. vals["status"],
  212. ),
  213. "user_id": self.env.context.get(
  214. "real_uid", self.env.uid
  215. ),
  216. }
  217. self.env["cooperative.status.history"].sudo().create(data)
  218. rec._state_change(vals["status"])
  219. return super(CooperativeStatus, self)._write(vals)
  220. def get_status_value(self):
  221. """
  222. Workararound to get translated selection value instead of key in mail
  223. template.
  224. """
  225. state_list = (
  226. self.env["cooperative.status"]
  227. ._fields["status"]
  228. ._description_selection(self.env)
  229. )
  230. return dict(state_list)[self.status]
  231. @api.model
  232. def _set_today(self):
  233. """
  234. Method call by the cron to update store value base on the date
  235. """
  236. self.search([]).write({"today": fields.Date.today()})
  237. @api.model
  238. def _cron_compute_counter_irregular(self, today=False):
  239. """
  240. Journal ensure that a irregular worker will be only check
  241. once per day
  242. """
  243. today = today or fields.Date.today()
  244. journal = self.env["beesdoo.shift.journal"].search(
  245. [("date", "=", today)]
  246. )
  247. if not journal:
  248. journal = self.env["beesdoo.shift.journal"].create({"date": today})
  249. domain = self._get_irregular_worker_domain(today=today)
  250. irregular = self.search(domain)
  251. for status in irregular:
  252. delta = (today - status.irregular_start_date).days
  253. if (
  254. delta
  255. and delta % self._period == 0
  256. and status not in journal.line_ids
  257. ):
  258. status._change_irregular_counter()
  259. journal.line_ids |= status
  260. @api.multi
  261. def clear_history(self):
  262. self.ensure_one()
  263. self.history_ids.unlink()
  264. ########################################################
  265. # Method to override #
  266. # To define the behavior of the status #
  267. # #
  268. # By default: everyone is always up to date #
  269. ########################################################
  270. ##############################
  271. # Computed field section #
  272. ##############################
  273. @api.depends("today")
  274. def _compute_future_alert_date(self):
  275. """
  276. Compute date until the worker is up to date
  277. for irregular worker
  278. """
  279. for rec in self:
  280. rec.future_alert_date = False
  281. @api.depends("today")
  282. def _compute_next_countdown_date(self):
  283. """
  284. Compute the following countdown date. This date is the date when
  285. the worker will see his counter changed due to the cron. This
  286. date is like the birthday date of the worker that occurred each
  287. _period.
  288. """
  289. for rec in self:
  290. rec.next_countdown_date = False
  291. def _can_shop_status(self):
  292. """
  293. return the list of status that give access
  294. to active cooperator privilege
  295. """
  296. return ["ok", "alert", "extension", "exempted"]
  297. #####################################
  298. # Status Change implementation #
  299. #####################################
  300. def _get_regular_status(self):
  301. """
  302. Return the value of the status
  303. for the regular worker
  304. """
  305. return "ok"
  306. def _get_irregular_status(self):
  307. """
  308. Return the value of the status
  309. for the irregular worker
  310. """
  311. return "ok"
  312. def _state_change(self, new_state):
  313. """
  314. Hook to watch change in the state
  315. """
  316. pass
  317. def _change_counter(self, data):
  318. """
  319. Call when a shift state is changed
  320. use data generated by _get_counter_date_state_change
  321. """
  322. pass
  323. ###############################################
  324. # Irregular Cron implementation #
  325. ###############################################
  326. def _get_irregular_worker_domain(self):
  327. """
  328. return the domain the give the list
  329. of valid irregular worker that should
  330. get their counter changed by the cron
  331. """
  332. return [(0, "=", 1)]
  333. def _change_irregular_counter(self):
  334. """
  335. Define how the counter will change
  336. for the irregular worker
  337. where today - start_date is a multiple of the period
  338. by default 28 days
  339. """
  340. pass
  341. class ShiftCronJournal(models.Model):
  342. _name = "beesdoo.shift.journal"
  343. _description = "beesdoo.shift.journal"
  344. _order = "date desc"
  345. _rec_name = "date"
  346. date = fields.Date()
  347. line_ids = fields.Many2many("cooperative.status")
  348. _sql_constraints = [
  349. (
  350. "one_entry_per_day",
  351. "unique (date)",
  352. _("You can only create one journal per day"),
  353. )
  354. ]
  355. @api.multi
  356. def run(self):
  357. self.ensure_one()
  358. if not self.user_has_groups("beesdoo_shift.group_cooperative_admin"):
  359. raise ValidationError(
  360. _("You don't have the access to perform this action")
  361. )
  362. self.sudo().env["cooperative.status"]._cron_compute_counter_irregular(
  363. today=self.date
  364. )