mrp_workorder.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime, timedelta
  4. from dateutil.relativedelta import relativedelta
  5. from collections import defaultdict
  6. import json
  7. from odoo import api, fields, models, _, SUPERUSER_ID
  8. from odoo.exceptions import UserError, ValidationError
  9. from odoo.tools import float_compare, float_round, format_datetime
  10. class MrpWorkorder(models.Model):
  11. _name = 'mrp.workorder'
  12. _description = 'Work Order'
  13. def _read_group_workcenter_id(self, workcenters, domain, order):
  14. workcenter_ids = self.env.context.get('default_workcenter_id')
  15. if not workcenter_ids:
  16. workcenter_ids = workcenters._search([], order=order, access_rights_uid=SUPERUSER_ID)
  17. return workcenters.browse(workcenter_ids)
  18. name = fields.Char(
  19. 'Work Order', required=True,
  20. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]})
  21. workcenter_id = fields.Many2one(
  22. 'mrp.workcenter', 'Work Center', required=True,
  23. states={'done': [('readonly', True)], 'cancel': [('readonly', True)], 'progress': [('readonly', True)]},
  24. group_expand='_read_group_workcenter_id', check_company=True)
  25. working_state = fields.Selection(
  26. string='Workcenter Status', related='workcenter_id.working_state') # technical: used in views only
  27. product_id = fields.Many2one(related='production_id.product_id', readonly=True, store=True, check_company=True)
  28. product_tracking = fields.Selection(related="product_id.tracking")
  29. product_uom_id = fields.Many2one('uom.uom', 'Unit of Measure', required=True, readonly=True)
  30. production_id = fields.Many2one('mrp.production', 'Manufacturing Order', required=True, check_company=True, readonly=True)
  31. production_availability = fields.Selection(
  32. string='Stock Availability', readonly=True,
  33. related='production_id.reservation_state', store=True) # Technical: used in views and domains only
  34. production_state = fields.Selection(
  35. string='Production State', readonly=True,
  36. related='production_id.state') # Technical: used in views only
  37. production_bom_id = fields.Many2one('mrp.bom', related='production_id.bom_id')
  38. qty_production = fields.Float('Original Production Quantity', readonly=True, related='production_id.product_qty')
  39. company_id = fields.Many2one(related='production_id.company_id')
  40. qty_producing = fields.Float(
  41. compute='_compute_qty_producing', inverse='_set_qty_producing',
  42. string='Currently Produced Quantity', digits='Product Unit of Measure')
  43. qty_remaining = fields.Float('Quantity To Be Produced', compute='_compute_qty_remaining', digits='Product Unit of Measure')
  44. qty_produced = fields.Float(
  45. 'Quantity', default=0.0,
  46. readonly=True,
  47. digits='Product Unit of Measure',
  48. copy=False,
  49. help="The number of products already handled by this work order")
  50. is_produced = fields.Boolean(string="Has Been Produced",
  51. compute='_compute_is_produced')
  52. state = fields.Selection([
  53. ('pending', 'Waiting for another WO'),
  54. ('waiting', 'Waiting for components'),
  55. ('ready', 'Ready'),
  56. ('progress', 'In Progress'),
  57. ('done', 'Finished'),
  58. ('cancel', 'Cancelled')], string='Status',
  59. compute='_compute_state', store=True,
  60. default='pending', copy=False, readonly=True, recursive=True, index=True)
  61. leave_id = fields.Many2one(
  62. 'resource.calendar.leaves',
  63. help='Slot into workcenter calendar once planned',
  64. check_company=True, copy=False)
  65. date_planned_start = fields.Datetime(
  66. 'Scheduled Start Date',
  67. compute='_compute_dates_planned',
  68. inverse='_set_dates_planned',
  69. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
  70. store=True, copy=False)
  71. date_planned_finished = fields.Datetime(
  72. 'Scheduled End Date',
  73. compute='_compute_dates_planned',
  74. inverse='_set_dates_planned',
  75. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
  76. store=True, copy=False)
  77. date_start = fields.Datetime(
  78. 'Start Date', copy=False,
  79. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]})
  80. date_finished = fields.Datetime(
  81. 'End Date', copy=False,
  82. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]})
  83. duration_expected = fields.Float(
  84. 'Expected Duration', digits=(16, 2), compute='_compute_duration_expected',
  85. states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
  86. readonly=False, store=True) # in minutes
  87. duration = fields.Float(
  88. 'Real Duration', compute='_compute_duration', inverse='_set_duration',
  89. readonly=False, store=True, copy=False)
  90. duration_unit = fields.Float(
  91. 'Duration Per Unit', compute='_compute_duration',
  92. group_operator="avg", readonly=True, store=True)
  93. duration_percent = fields.Integer(
  94. 'Duration Deviation (%)', compute='_compute_duration',
  95. group_operator="avg", readonly=True, store=True)
  96. progress = fields.Float('Progress Done (%)', digits=(16, 2), compute='_compute_progress')
  97. operation_id = fields.Many2one(
  98. 'mrp.routing.workcenter', 'Operation', check_company=True)
  99. # Should be used differently as BoM can change in the meantime
  100. worksheet = fields.Binary(
  101. 'Worksheet', related='operation_id.worksheet', readonly=True)
  102. worksheet_type = fields.Selection(
  103. string='Worksheet Type', related='operation_id.worksheet_type', readonly=True)
  104. worksheet_google_slide = fields.Char(
  105. 'Worksheet URL', related='operation_id.worksheet_google_slide', readonly=True)
  106. operation_note = fields.Html("Description", related='operation_id.note', readonly=True)
  107. move_raw_ids = fields.One2many(
  108. 'stock.move', 'workorder_id', 'Raw Moves',
  109. domain=[('raw_material_production_id', '!=', False), ('production_id', '=', False)])
  110. move_finished_ids = fields.One2many(
  111. 'stock.move', 'workorder_id', 'Finished Moves',
  112. domain=[('raw_material_production_id', '=', False), ('production_id', '!=', False)])
  113. move_line_ids = fields.One2many(
  114. 'stock.move.line', 'workorder_id', 'Moves to Track',
  115. help="Inventory moves for which you must scan a lot number at this work order")
  116. finished_lot_id = fields.Many2one(
  117. 'stock.lot', string='Lot/Serial Number', compute='_compute_finished_lot_id',
  118. inverse='_set_finished_lot_id', domain="[('product_id', '=', product_id), ('company_id', '=', company_id)]",
  119. check_company=True, search='_search_finished_lot_id')
  120. time_ids = fields.One2many(
  121. 'mrp.workcenter.productivity', 'workorder_id', copy=False)
  122. is_user_working = fields.Boolean(
  123. 'Is the Current User Working', compute='_compute_working_users') # technical: is the current user working
  124. working_user_ids = fields.One2many('res.users', string='Working user on this work order.', compute='_compute_working_users')
  125. last_working_user_id = fields.One2many('res.users', string='Last user that worked on this work order.', compute='_compute_working_users')
  126. costs_hour = fields.Float(
  127. string='Cost per hour',
  128. default=0.0, group_operator="avg")
  129. # Technical field to store the hourly cost of workcenter at time of work order completion (i.e. to keep a consistent cost).',
  130. scrap_ids = fields.One2many('stock.scrap', 'workorder_id')
  131. scrap_count = fields.Integer(compute='_compute_scrap_move_count', string='Scrap Move')
  132. production_date = fields.Datetime('Production Date', related='production_id.date_planned_start', store=True)
  133. json_popover = fields.Char('Popover Data JSON', compute='_compute_json_popover')
  134. show_json_popover = fields.Boolean('Show Popover?', compute='_compute_json_popover')
  135. consumption = fields.Selection(related='production_id.consumption')
  136. qty_reported_from_previous_wo = fields.Float('Carried Quantity', digits='Product Unit of Measure', copy=False,
  137. help="The quantity already produced awaiting allocation in the backorders chain.")
  138. is_planned = fields.Boolean(related='production_id.is_planned')
  139. allow_workorder_dependencies = fields.Boolean(related='production_id.allow_workorder_dependencies')
  140. blocked_by_workorder_ids = fields.Many2many('mrp.workorder', relation="mrp_workorder_dependencies_rel",
  141. column1="workorder_id", column2="blocked_by_id", string="Blocked By",
  142. domain="[('allow_workorder_dependencies', '=', True), ('id', '!=', id), ('production_id', '=', production_id)]",
  143. copy=False)
  144. needed_by_workorder_ids = fields.Many2many('mrp.workorder', relation="mrp_workorder_dependencies_rel",
  145. column1="blocked_by_id", column2="workorder_id", string="Blocks",
  146. domain="[('allow_workorder_dependencies', '=', True), ('id', '!=', id), ('production_id', '=', production_id)]",
  147. copy=False)
  148. @api.depends('production_availability', 'blocked_by_workorder_ids', 'blocked_by_workorder_ids.state')
  149. def _compute_state(self):
  150. # Force the flush of the production_availability, the wo state is modify in the _compute_reservation_state
  151. # It is a trick to force that the state of workorder is computed as the end of the
  152. # cyclic depends with the mo.state, mo.reservation_state and wo.state
  153. for workorder in self:
  154. if workorder.state == 'pending':
  155. if all([wo.state in ('done', 'cancel') for wo in workorder.blocked_by_workorder_ids]):
  156. workorder.state = 'ready' if workorder.production_id.reservation_state == 'assigned' else 'waiting'
  157. continue
  158. if workorder.state not in ('waiting', 'ready'):
  159. continue
  160. if not all([wo.state in ('done', 'cancel') for wo in workorder.blocked_by_workorder_ids]):
  161. workorder.state = 'pending'
  162. continue
  163. if workorder.production_id.reservation_state not in ('waiting', 'confirmed', 'assigned'):
  164. continue
  165. if workorder.production_id.reservation_state == 'assigned' and workorder.state == 'waiting':
  166. workorder.state = 'ready'
  167. elif workorder.production_id.reservation_state != 'assigned' and workorder.state == 'ready':
  168. workorder.state = 'waiting'
  169. @api.depends('production_state', 'date_planned_start', 'date_planned_finished')
  170. def _compute_json_popover(self):
  171. if self.ids:
  172. conflicted_dict = self._get_conflicted_workorder_ids()
  173. for wo in self:
  174. infos = []
  175. if not wo.date_planned_start or not wo.date_planned_finished or not wo.ids:
  176. wo.show_json_popover = False
  177. wo.json_popover = False
  178. continue
  179. if wo.state in ('pending', 'waiting', 'ready'):
  180. previous_wos = wo.blocked_by_workorder_ids
  181. prev_start = min([workorder.date_planned_start for workorder in previous_wos]) if previous_wos else False
  182. prev_finished = max([workorder.date_planned_finished for workorder in previous_wos]) if previous_wos else False
  183. if wo.state == 'pending' and prev_start and not (prev_start > wo.date_planned_start):
  184. infos.append({
  185. 'color': 'text-primary',
  186. 'msg': _("Waiting the previous work order, planned from %(start)s to %(end)s",
  187. start=format_datetime(self.env, prev_start, dt_format=False),
  188. end=format_datetime(self.env, prev_finished, dt_format=False))
  189. })
  190. if wo.date_planned_finished < fields.Datetime.now():
  191. infos.append({
  192. 'color': 'text-warning',
  193. 'msg': _("The work order should have already been processed.")
  194. })
  195. if prev_start and prev_start > wo.date_planned_start:
  196. infos.append({
  197. 'color': 'text-danger',
  198. 'msg': _("Scheduled before the previous work order, planned from %(start)s to %(end)s",
  199. start=format_datetime(self.env, prev_start, dt_format=False),
  200. end=format_datetime(self.env, prev_finished, dt_format=False))
  201. })
  202. if conflicted_dict.get(wo.id):
  203. infos.append({
  204. 'color': 'text-danger',
  205. 'msg': _("Planned at the same time as other workorder(s) at %s", wo.workcenter_id.display_name)
  206. })
  207. color_icon = infos and infos[-1]['color'] or False
  208. wo.show_json_popover = bool(color_icon)
  209. wo.json_popover = json.dumps({
  210. 'popoverTemplate': 'mrp.workorderPopover',
  211. 'infos': infos,
  212. 'color': color_icon,
  213. 'icon': 'fa-exclamation-triangle' if color_icon in ['text-warning', 'text-danger'] else 'fa-info-circle',
  214. 'replan': color_icon not in [False, 'text-primary']
  215. })
  216. @api.depends('production_id.lot_producing_id')
  217. def _compute_finished_lot_id(self):
  218. for workorder in self:
  219. workorder.finished_lot_id = workorder.production_id.lot_producing_id
  220. def _search_finished_lot_id(self, operator, value):
  221. return [('production_id.lot_producing_id', operator, value)]
  222. def _set_finished_lot_id(self):
  223. for workorder in self:
  224. workorder.production_id.lot_producing_id = workorder.finished_lot_id
  225. @api.depends('production_id.qty_producing')
  226. def _compute_qty_producing(self):
  227. for workorder in self:
  228. workorder.qty_producing = workorder.production_id.qty_producing
  229. def _set_qty_producing(self):
  230. for workorder in self:
  231. if workorder.qty_producing != 0 and workorder.production_id.qty_producing != workorder.qty_producing:
  232. workorder.production_id.qty_producing = workorder.qty_producing
  233. workorder.production_id._set_qty_producing()
  234. # Both `date_planned_start` and `date_planned_finished` are related fields on `leave_id`. Let's say
  235. # we slide a workorder on a gantt view, a single call to write is made with both
  236. # fields Changes. As the ORM doesn't batch the write on related fields and instead
  237. # makes multiple call, the constraint check_dates() is raised.
  238. # That's why the compute and set methods are needed. to ensure the dates are updated
  239. # in the same time.
  240. @api.depends('leave_id')
  241. def _compute_dates_planned(self):
  242. for workorder in self:
  243. workorder.date_planned_start = workorder.leave_id.date_from
  244. workorder.date_planned_finished = workorder.leave_id.date_to
  245. def _set_dates_planned(self):
  246. if not self[0].date_planned_start or not self[0].date_planned_finished:
  247. if not self.leave_id:
  248. return
  249. raise UserError(_("It is not possible to unplan one single Work Order. "
  250. "You should unplan the Manufacturing Order instead in order to unplan all the linked operations."))
  251. date_from = self[0].date_planned_start
  252. date_to = self[0].date_planned_finished
  253. to_write = self.env['mrp.workorder']
  254. for wo in self.sudo():
  255. if wo.leave_id:
  256. to_write |= wo
  257. else:
  258. wo.leave_id = wo.env['resource.calendar.leaves'].create({
  259. 'name': wo.display_name,
  260. 'calendar_id': wo.workcenter_id.resource_calendar_id.id,
  261. 'date_from': date_from,
  262. 'date_to': date_to,
  263. 'resource_id': wo.workcenter_id.resource_id.id,
  264. 'time_type': 'other',
  265. })
  266. to_write.leave_id.write({
  267. 'date_from': date_from,
  268. 'date_to': date_to,
  269. })
  270. @api.constrains('blocked_by_workorder_ids')
  271. def _check_no_cyclic_dependencies(self):
  272. if not self._check_m2m_recursion('blocked_by_workorder_ids'):
  273. raise ValidationError(_("You cannot create cyclic dependency."))
  274. def name_get(self):
  275. res = []
  276. for wo in self:
  277. if len(wo.production_id.workorder_ids) == 1:
  278. res.append((wo.id, "%s - %s - %s" % (wo.production_id.name, wo.product_id.name, wo.name)))
  279. else:
  280. res.append((wo.id, "%s - %s - %s - %s" % (wo.production_id.workorder_ids.ids.index(wo._origin.id) + 1, wo.production_id.name, wo.product_id.name, wo.name)))
  281. return res
  282. def unlink(self):
  283. # Removes references to workorder to avoid Validation Error
  284. (self.mapped('move_raw_ids') | self.mapped('move_finished_ids')).write({'workorder_id': False})
  285. self.mapped('leave_id').unlink()
  286. mo_dirty = self.production_id.filtered(lambda mo: mo.state in ("confirmed", "progress", "to_close"))
  287. for workorder in self:
  288. workorder.blocked_by_workorder_ids.needed_by_workorder_ids = workorder.needed_by_workorder_ids
  289. res = super().unlink()
  290. # We need to go through `_action_confirm` for all workorders of the current productions to
  291. # make sure the links between them are correct (`next_work_order_id` could be obsolete now).
  292. mo_dirty.workorder_ids._action_confirm()
  293. return res
  294. @api.depends('production_id.product_qty', 'qty_produced', 'production_id.product_uom_id')
  295. def _compute_is_produced(self):
  296. self.is_produced = False
  297. for order in self.filtered(lambda p: p.production_id and p.production_id.product_uom_id):
  298. rounding = order.production_id.product_uom_id.rounding
  299. order.is_produced = float_compare(order.qty_produced, order.production_id.product_qty, precision_rounding=rounding) >= 0
  300. @api.depends('operation_id', 'workcenter_id', 'qty_production')
  301. def _compute_duration_expected(self):
  302. for workorder in self:
  303. workorder.duration_expected = workorder._get_duration_expected()
  304. @api.depends('time_ids.duration', 'qty_produced')
  305. def _compute_duration(self):
  306. for order in self:
  307. order.duration = sum(order.time_ids.mapped('duration'))
  308. order.duration_unit = round(order.duration / max(order.qty_produced, 1), 2) # rounding 2 because it is a time
  309. if order.duration_expected:
  310. order.duration_percent = max(-2147483648, min(2147483647, 100 * (order.duration_expected - order.duration) / order.duration_expected))
  311. else:
  312. order.duration_percent = 0
  313. def _set_duration(self):
  314. def _float_duration_to_second(duration):
  315. minutes = duration // 1
  316. seconds = (duration % 1) * 60
  317. return minutes * 60 + seconds
  318. for order in self:
  319. old_order_duration = sum(order.time_ids.mapped('duration'))
  320. new_order_duration = order.duration
  321. if new_order_duration == old_order_duration:
  322. continue
  323. delta_duration = new_order_duration - old_order_duration
  324. if delta_duration > 0:
  325. enddate = datetime.now()
  326. date_start = enddate - timedelta(seconds=_float_duration_to_second(delta_duration))
  327. if order.duration_expected >= new_order_duration or old_order_duration >= order.duration_expected:
  328. # either only productive or only performance (i.e. reduced speed) time respectively
  329. self.env['mrp.workcenter.productivity'].create(
  330. order._prepare_timeline_vals(new_order_duration, date_start, enddate)
  331. )
  332. else:
  333. # split between productive and performance (i.e. reduced speed) times
  334. maxdate = fields.Datetime.from_string(enddate) - relativedelta(minutes=new_order_duration - order.duration_expected)
  335. self.env['mrp.workcenter.productivity'].create([
  336. order._prepare_timeline_vals(order.duration_expected, date_start, maxdate),
  337. order._prepare_timeline_vals(new_order_duration, maxdate, enddate)
  338. ])
  339. else:
  340. duration_to_remove = abs(delta_duration)
  341. timelines_to_unlink = self.env['mrp.workcenter.productivity']
  342. for timeline in order.time_ids.sorted():
  343. if duration_to_remove <= 0.0:
  344. break
  345. if timeline.duration <= duration_to_remove:
  346. duration_to_remove -= timeline.duration
  347. timelines_to_unlink |= timeline
  348. else:
  349. new_time_line_duration = timeline.duration - duration_to_remove
  350. timeline.date_start = timeline.date_end - timedelta(seconds=_float_duration_to_second(new_time_line_duration))
  351. break
  352. timelines_to_unlink.unlink()
  353. @api.depends('duration', 'duration_expected', 'state')
  354. def _compute_progress(self):
  355. for order in self:
  356. if order.state == 'done':
  357. order.progress = 100
  358. elif order.duration_expected:
  359. order.progress = order.duration * 100 / order.duration_expected
  360. else:
  361. order.progress = 0
  362. def _compute_working_users(self):
  363. """ Checks whether the current user is working, all the users currently working and the last user that worked. """
  364. for order in self:
  365. order.working_user_ids = [(4, order.id) for order in order.time_ids.filtered(lambda time: not time.date_end).sorted('date_start').mapped('user_id')]
  366. if order.working_user_ids:
  367. order.last_working_user_id = order.working_user_ids[-1]
  368. elif order.time_ids:
  369. order.last_working_user_id = order.time_ids.filtered('date_end').sorted('date_end')[-1].user_id if order.time_ids.filtered('date_end') else order.time_ids[-1].user_id
  370. else:
  371. order.last_working_user_id = False
  372. if order.time_ids.filtered(lambda x: (x.user_id.id == self.env.user.id) and (not x.date_end) and (x.loss_type in ('productive', 'performance'))):
  373. order.is_user_working = True
  374. else:
  375. order.is_user_working = False
  376. def _compute_scrap_move_count(self):
  377. data = self.env['stock.scrap']._read_group([('workorder_id', 'in', self.ids)], ['workorder_id'], ['workorder_id'])
  378. count_data = dict((item['workorder_id'][0], item['workorder_id_count']) for item in data)
  379. for workorder in self:
  380. workorder.scrap_count = count_data.get(workorder.id, 0)
  381. @api.onchange('operation_id')
  382. def _onchange_operation_id(self):
  383. if self.operation_id:
  384. self.name = self.operation_id.name
  385. self.workcenter_id = self.operation_id.workcenter_id.id
  386. @api.onchange('date_planned_start', 'duration_expected', 'workcenter_id')
  387. def _onchange_date_planned_start(self):
  388. if self.date_planned_start and self.workcenter_id:
  389. self.date_planned_finished = self._calculate_date_planned_finished()
  390. def _calculate_date_planned_finished(self, date_planned_start=False):
  391. return self.workcenter_id.resource_calendar_id.plan_hours(
  392. self.duration_expected / 60.0, date_planned_start or self.date_planned_start,
  393. compute_leaves=True, domain=[('time_type', 'in', ['leave', 'other'])]
  394. )
  395. @api.onchange('date_planned_finished')
  396. def _onchange_date_planned_finished(self):
  397. if self.date_planned_start and self.date_planned_finished and self.workcenter_id:
  398. self.duration_expected = self._calculate_duration_expected()
  399. def _calculate_duration_expected(self, date_planned_start=False, date_planned_finished=False):
  400. interval = self.workcenter_id.resource_calendar_id.get_work_duration_data(
  401. date_planned_start or self.date_planned_start, date_planned_finished or self.date_planned_finished,
  402. domain=[('time_type', 'in', ['leave', 'other'])]
  403. )
  404. return interval['hours'] * 60
  405. @api.onchange('finished_lot_id')
  406. def _onchange_finished_lot_id(self):
  407. if self.production_id:
  408. res = self.production_id._can_produce_serial_number(sn=self.finished_lot_id)
  409. if res is not True:
  410. return res
  411. def write(self, values):
  412. if 'production_id' in values and any(values['production_id'] != w.production_id.id for w in self):
  413. raise UserError(_('You cannot link this work order to another manufacturing order.'))
  414. if 'workcenter_id' in values:
  415. for workorder in self:
  416. if workorder.workcenter_id.id != values['workcenter_id']:
  417. if workorder.state in ('progress', 'done', 'cancel'):
  418. raise UserError(_('You cannot change the workcenter of a work order that is in progress or done.'))
  419. workorder.leave_id.resource_id = self.env['mrp.workcenter'].browse(values['workcenter_id']).resource_id
  420. if 'date_planned_start' in values or 'date_planned_finished' in values:
  421. for workorder in self:
  422. start_date = fields.Datetime.to_datetime(values.get('date_planned_start', workorder.date_planned_start))
  423. end_date = fields.Datetime.to_datetime(values.get('date_planned_finished', workorder.date_planned_finished))
  424. if start_date and end_date and start_date > end_date:
  425. raise UserError(_('The planned end date of the work order cannot be prior to the planned start date, please correct this to save the work order.'))
  426. if 'duration_expected' not in values and not self.env.context.get('bypass_duration_calculation'):
  427. if values.get('date_planned_start') and values.get('date_planned_finished'):
  428. computed_finished_time = workorder._calculate_date_planned_finished(start_date)
  429. values['date_planned_finished'] = computed_finished_time
  430. elif start_date and end_date:
  431. computed_duration = workorder._calculate_duration_expected(date_planned_start=start_date, date_planned_finished=end_date)
  432. values['duration_expected'] = computed_duration
  433. # Update MO dates if the start date of the first WO or the
  434. # finished date of the last WO is update.
  435. if workorder == workorder.production_id.workorder_ids[0] and 'date_planned_start' in values:
  436. if values['date_planned_start']:
  437. workorder.production_id.with_context(force_date=True).write({
  438. 'date_planned_start': fields.Datetime.to_datetime(values['date_planned_start'])
  439. })
  440. if workorder == workorder.production_id.workorder_ids[-1] and 'date_planned_finished' in values:
  441. if values['date_planned_finished']:
  442. workorder.production_id.with_context(force_date=True).write({
  443. 'date_planned_finished': fields.Datetime.to_datetime(values['date_planned_finished'])
  444. })
  445. return super(MrpWorkorder, self).write(values)
  446. @api.model_create_multi
  447. def create(self, values):
  448. res = super().create(values)
  449. # Auto-confirm manually added workorders.
  450. # We need to go through `_action_confirm` for all workorders of the current productions to
  451. # make sure the links between them are correct.
  452. if self.env.context.get('skip_confirm'):
  453. return res
  454. to_confirm = res.filtered(lambda wo: wo.production_id.state in ("confirmed", "progress", "to_close"))
  455. to_confirm = to_confirm.production_id.workorder_ids
  456. to_confirm._action_confirm()
  457. return res
  458. def _action_confirm(self):
  459. for production in self.mapped("production_id"):
  460. production._link_workorders_and_moves()
  461. def _get_byproduct_move_to_update(self):
  462. return self.production_id.move_finished_ids.filtered(lambda x: (x.product_id.id != self.production_id.product_id.id) and (x.state not in ('done', 'cancel')))
  463. def _plan_workorder(self, replan=False):
  464. self.ensure_one()
  465. # Plan workorder after its predecessors
  466. start_date = max(self.production_id.date_planned_start, datetime.now())
  467. for workorder in self.blocked_by_workorder_ids:
  468. if workorder.state in ['done', 'cancel']:
  469. continue
  470. workorder._plan_workorder(replan)
  471. start_date = max(start_date, workorder.date_planned_finished)
  472. # Plan only suitable workorders
  473. if self.state not in ['pending', 'waiting', 'ready']:
  474. return
  475. if self.leave_id:
  476. if replan:
  477. self.leave_id.unlink()
  478. else:
  479. return
  480. # Consider workcenter and alternatives
  481. workcenters = self.workcenter_id | self.workcenter_id.alternative_workcenter_ids
  482. best_finished_date = datetime.max
  483. vals = {}
  484. for workcenter in workcenters:
  485. # Compute theoretical duration
  486. if self.workcenter_id == workcenter:
  487. duration_expected = self.duration_expected
  488. else:
  489. duration_expected = self._get_duration_expected(alternative_workcenter=workcenter)
  490. from_date, to_date = workcenter._get_first_available_slot(start_date, duration_expected)
  491. # If the workcenter is unavailable, try planning on the next one
  492. if not from_date:
  493. continue
  494. # Check if this workcenter is better than the previous ones
  495. if to_date and to_date < best_finished_date:
  496. best_start_date = from_date
  497. best_finished_date = to_date
  498. best_workcenter = workcenter
  499. vals = {
  500. 'workcenter_id': workcenter.id,
  501. 'duration_expected': duration_expected,
  502. }
  503. # If none of the workcenter are available, raise
  504. if best_finished_date == datetime.max:
  505. raise UserError(_('Impossible to plan the workorder. Please check the workcenter availabilities.'))
  506. # Create leave on chosen workcenter calendar
  507. leave = self.env['resource.calendar.leaves'].create({
  508. 'name': self.display_name,
  509. 'calendar_id': best_workcenter.resource_calendar_id.id,
  510. 'date_from': best_start_date,
  511. 'date_to': best_finished_date,
  512. 'resource_id': best_workcenter.resource_id.id,
  513. 'time_type': 'other'
  514. })
  515. vals['leave_id'] = leave.id
  516. self.write(vals)
  517. def _cal_cost(self, times=None):
  518. self.ensure_one()
  519. times = times or self.time_ids
  520. duration = sum(times.mapped('duration'))
  521. return (duration / 60.0) * self.workcenter_id.costs_hour
  522. @api.model
  523. def gantt_unavailability(self, start_date, end_date, scale, group_bys=None, rows=None):
  524. """Get unavailabilities data to display in the Gantt view."""
  525. workcenter_ids = set()
  526. def traverse_inplace(func, row, **kargs):
  527. res = func(row, **kargs)
  528. if res:
  529. kargs.update(res)
  530. for row in row.get('rows'):
  531. traverse_inplace(func, row, **kargs)
  532. def search_workcenter_ids(row):
  533. if row.get('groupedBy') and row.get('groupedBy')[0] == 'workcenter_id' and row.get('resId'):
  534. workcenter_ids.add(row.get('resId'))
  535. for row in rows:
  536. traverse_inplace(search_workcenter_ids, row)
  537. start_datetime = fields.Datetime.to_datetime(start_date)
  538. end_datetime = fields.Datetime.to_datetime(end_date)
  539. workcenters = self.env['mrp.workcenter'].browse(workcenter_ids)
  540. unavailability_mapping = workcenters._get_unavailability_intervals(start_datetime, end_datetime)
  541. # Only notable interval (more than one case) is send to the front-end (avoid sending useless information)
  542. cell_dt = (scale in ['day', 'week'] and timedelta(hours=1)) or (scale == 'month' and timedelta(days=1)) or timedelta(days=28)
  543. def add_unavailability(row, workcenter_id=None):
  544. if row.get('groupedBy') and row.get('groupedBy')[0] == 'workcenter_id' and row.get('resId'):
  545. workcenter_id = row.get('resId')
  546. if workcenter_id:
  547. notable_intervals = filter(lambda interval: interval[1] - interval[0] >= cell_dt, unavailability_mapping[workcenter_id])
  548. row['unavailabilities'] = [{'start': interval[0], 'stop': interval[1]} for interval in notable_intervals]
  549. return {'workcenter_id': workcenter_id}
  550. for row in rows:
  551. traverse_inplace(add_unavailability, row)
  552. return rows
  553. def button_start(self):
  554. self.ensure_one()
  555. if any(not time.date_end for time in self.time_ids.filtered(lambda t: t.user_id.id == self.env.user.id)):
  556. return True
  557. # As button_start is automatically called in the new view
  558. if self.state in ('done', 'cancel'):
  559. return True
  560. if self.product_tracking == 'serial' and self.qty_producing == 0:
  561. self.qty_producing = 1.0
  562. elif self.qty_producing == 0:
  563. self.qty_producing = self.qty_remaining
  564. if self._should_start_timer():
  565. self.env['mrp.workcenter.productivity'].create(
  566. self._prepare_timeline_vals(self.duration, datetime.now())
  567. )
  568. if self.production_id.state != 'progress':
  569. self.production_id.write({
  570. 'date_start': datetime.now(),
  571. })
  572. if self.state == 'progress':
  573. return True
  574. start_date = datetime.now()
  575. vals = {
  576. 'state': 'progress',
  577. 'date_start': start_date,
  578. }
  579. if not self.leave_id:
  580. leave = self.env['resource.calendar.leaves'].create({
  581. 'name': self.display_name,
  582. 'calendar_id': self.workcenter_id.resource_calendar_id.id,
  583. 'date_from': start_date,
  584. 'date_to': start_date + relativedelta(minutes=self.duration_expected),
  585. 'resource_id': self.workcenter_id.resource_id.id,
  586. 'time_type': 'other'
  587. })
  588. vals['leave_id'] = leave.id
  589. return self.write(vals)
  590. else:
  591. if not self.date_planned_start or self.date_planned_start > start_date:
  592. vals['date_planned_start'] = start_date
  593. vals['date_planned_finished'] = self._calculate_date_planned_finished(start_date)
  594. if self.date_planned_finished and self.date_planned_finished < start_date:
  595. vals['date_planned_finished'] = start_date
  596. return self.with_context(bypass_duration_calculation=True).write(vals)
  597. def button_finish(self):
  598. end_date = fields.Datetime.now()
  599. for workorder in self:
  600. if workorder.state in ('done', 'cancel'):
  601. continue
  602. workorder.end_all()
  603. vals = {
  604. 'qty_produced': workorder.qty_produced or workorder.qty_producing or workorder.qty_production,
  605. 'state': 'done',
  606. 'date_finished': end_date,
  607. 'date_planned_finished': end_date,
  608. 'costs_hour': workorder.workcenter_id.costs_hour
  609. }
  610. if not workorder.date_start:
  611. vals['date_start'] = end_date
  612. if not workorder.date_planned_start or end_date < workorder.date_planned_start:
  613. vals['date_planned_start'] = end_date
  614. workorder.with_context(bypass_duration_calculation=True).write(vals)
  615. return True
  616. def end_previous(self, doall=False):
  617. """
  618. @param: doall: This will close all open time lines on the open work orders when doall = True, otherwise
  619. only the one of the current user
  620. """
  621. # TDE CLEANME
  622. domain = [('workorder_id', 'in', self.ids), ('date_end', '=', False)]
  623. if not doall:
  624. domain.append(('user_id', '=', self.env.user.id))
  625. self.env['mrp.workcenter.productivity'].search(domain, limit=None if doall else 1)._close()
  626. return True
  627. def end_all(self):
  628. return self.end_previous(doall=True)
  629. def button_pending(self):
  630. self.end_previous()
  631. return True
  632. def button_unblock(self):
  633. for order in self:
  634. order.workcenter_id.unblock()
  635. return True
  636. def action_cancel(self):
  637. self.leave_id.unlink()
  638. self.end_all()
  639. return self.write({'state': 'cancel'})
  640. def action_replan(self):
  641. """Replan a work order.
  642. It actually replans every "ready" or "pending"
  643. work orders of the linked manufacturing orders.
  644. """
  645. for production in self.production_id:
  646. production._plan_workorders(replan=True)
  647. return True
  648. def button_done(self):
  649. if any(x.state in ('done', 'cancel') for x in self):
  650. raise UserError(_('A Manufacturing Order is already done or cancelled.'))
  651. self.end_all()
  652. end_date = datetime.now()
  653. return self.write({
  654. 'state': 'done',
  655. 'date_finished': end_date,
  656. 'date_planned_finished': end_date,
  657. 'costs_hour': self.workcenter_id.costs_hour
  658. })
  659. def button_scrap(self):
  660. self.ensure_one()
  661. return {
  662. 'name': _('Scrap'),
  663. 'view_mode': 'form',
  664. 'res_model': 'stock.scrap',
  665. 'views': [(self.env.ref('stock.stock_scrap_form_view2').id, 'form')],
  666. 'type': 'ir.actions.act_window',
  667. 'context': {'default_company_id': self.production_id.company_id.id,
  668. 'default_workorder_id': self.id,
  669. 'default_production_id': self.production_id.id,
  670. 'product_ids': (self.production_id.move_raw_ids.filtered(lambda x: x.state not in ('done', 'cancel')) | self.production_id.move_finished_ids.filtered(lambda x: x.state == 'done')).mapped('product_id').ids},
  671. 'target': 'new',
  672. }
  673. def action_see_move_scrap(self):
  674. self.ensure_one()
  675. action = self.env["ir.actions.actions"]._for_xml_id("stock.action_stock_scrap")
  676. action['domain'] = [('workorder_id', '=', self.id)]
  677. return action
  678. def action_open_wizard(self):
  679. self.ensure_one()
  680. action = self.env["ir.actions.actions"]._for_xml_id("mrp.mrp_workorder_mrp_production_form")
  681. action['res_id'] = self.id
  682. return action
  683. @api.depends('qty_production', 'qty_reported_from_previous_wo', 'qty_produced', 'production_id.product_uom_id')
  684. def _compute_qty_remaining(self):
  685. for wo in self:
  686. if wo.production_id.product_uom_id:
  687. wo.qty_remaining = max(float_round(wo.qty_production - wo.qty_reported_from_previous_wo - wo.qty_produced, precision_rounding=wo.production_id.product_uom_id.rounding), 0)
  688. else:
  689. wo.qty_remaining = 0
  690. def _get_duration_expected(self, alternative_workcenter=False, ratio=1):
  691. self.ensure_one()
  692. if not self.workcenter_id:
  693. return self.duration_expected
  694. if not self.operation_id:
  695. duration_expected_working = (self.duration_expected - self.workcenter_id.time_start - self.workcenter_id.time_stop) * self.workcenter_id.time_efficiency / 100.0
  696. if duration_expected_working < 0:
  697. duration_expected_working = 0
  698. return self.workcenter_id._get_expected_duration(self.product_id) + duration_expected_working * ratio * 100.0 / self.workcenter_id.time_efficiency
  699. qty_production = self.production_id.product_uom_id._compute_quantity(self.qty_production, self.production_id.product_id.uom_id)
  700. capacity = self.workcenter_id._get_capacity(self.product_id)
  701. cycle_number = float_round(qty_production / capacity, precision_digits=0, rounding_method='UP')
  702. if alternative_workcenter:
  703. # TODO : find a better alternative : the settings of workcenter can change
  704. duration_expected_working = (self.duration_expected - self.workcenter_id.time_start - self.workcenter_id.time_stop) * self.workcenter_id.time_efficiency / (100.0 * cycle_number)
  705. if duration_expected_working < 0:
  706. duration_expected_working = 0
  707. capacity = alternative_workcenter._get_capacity(self.product_id)
  708. alternative_wc_cycle_nb = float_round(qty_production / capacity, precision_digits=0, rounding_method='UP')
  709. return alternative_workcenter._get_expected_duration(self.product_id) + alternative_wc_cycle_nb * duration_expected_working * 100.0 / alternative_workcenter.time_efficiency
  710. time_cycle = self.operation_id.time_cycle
  711. return self.workcenter_id._get_expected_duration(self.product_id) + cycle_number * time_cycle * 100.0 / self.workcenter_id.time_efficiency
  712. def _get_conflicted_workorder_ids(self):
  713. """Get conlicted workorder(s) with self.
  714. Conflict means having two workorders in the same time in the same workcenter.
  715. :return: defaultdict with key as workorder id of self and value as related conflicted workorder
  716. """
  717. self.flush_model(['state', 'date_planned_start', 'date_planned_finished', 'workcenter_id'])
  718. sql = """
  719. SELECT wo1.id, wo2.id
  720. FROM mrp_workorder wo1, mrp_workorder wo2
  721. WHERE
  722. wo1.id IN %s
  723. AND wo1.state IN ('pending', 'waiting', 'ready')
  724. AND wo2.state IN ('pending', 'waiting', 'ready')
  725. AND wo1.id != wo2.id
  726. AND wo1.workcenter_id = wo2.workcenter_id
  727. AND (DATE_TRUNC('second', wo2.date_planned_start), DATE_TRUNC('second', wo2.date_planned_finished))
  728. OVERLAPS (DATE_TRUNC('second', wo1.date_planned_start), DATE_TRUNC('second', wo1.date_planned_finished))
  729. """
  730. self.env.cr.execute(sql, [tuple(self.ids)])
  731. res = defaultdict(list)
  732. for wo1, wo2 in self.env.cr.fetchall():
  733. res[wo1].append(wo2)
  734. return res
  735. def _prepare_timeline_vals(self, duration, date_start, date_end=False):
  736. # Need a loss in case of the real time exceeding the expected
  737. if not self.duration_expected or duration <= self.duration_expected:
  738. loss_id = self.env['mrp.workcenter.productivity.loss'].search([('loss_type', '=', 'productive')], limit=1)
  739. if not len(loss_id):
  740. raise UserError(_("You need to define at least one productivity loss in the category 'Productivity'. Create one from the Manufacturing app, menu: Configuration / Productivity Losses."))
  741. else:
  742. loss_id = self.env['mrp.workcenter.productivity.loss'].search([('loss_type', '=', 'performance')], limit=1)
  743. if not len(loss_id):
  744. raise UserError(_("You need to define at least one productivity loss in the category 'Performance'. Create one from the Manufacturing app, menu: Configuration / Productivity Losses."))
  745. return {
  746. 'workorder_id': self.id,
  747. 'workcenter_id': self.workcenter_id.id,
  748. 'description': _('Time Tracking: %(user)s', user=self.env.user.name),
  749. 'loss_id': loss_id[0].id,
  750. 'date_start': date_start.replace(microsecond=0),
  751. 'date_end': date_end.replace(microsecond=0) if date_end else date_end,
  752. 'user_id': self.env.user.id, # FIXME sle: can be inconsistent with company_id
  753. 'company_id': self.company_id.id,
  754. }
  755. def _update_finished_move(self):
  756. """ Update the finished move & move lines in order to set the finished
  757. product lot on it as well as the produced quantity. This method get the
  758. information either from the last workorder or from the Produce wizard."""
  759. production_move = self.production_id.move_finished_ids.filtered(
  760. lambda move: move.product_id == self.product_id and
  761. move.state not in ('done', 'cancel')
  762. )
  763. if not production_move:
  764. return
  765. if production_move.product_id.tracking != 'none':
  766. if not self.finished_lot_id:
  767. raise UserError(_('You need to provide a lot for the finished product.'))
  768. move_line = production_move.move_line_ids.filtered(
  769. lambda line: line.lot_id.id == self.finished_lot_id.id
  770. )
  771. if move_line:
  772. if self.product_id.tracking == 'serial':
  773. raise UserError(_('You cannot produce the same serial number twice.'))
  774. move_line.reserved_uom_qty += self.qty_producing
  775. move_line.qty_done += self.qty_producing
  776. else:
  777. quantity = self.product_uom_id._compute_quantity(self.qty_producing, self.product_id.uom_id, rounding_method='HALF-UP')
  778. putaway_location = production_move.location_dest_id._get_putaway_strategy(self.product_id, quantity)
  779. move_line.create({
  780. 'move_id': production_move.id,
  781. 'product_id': production_move.product_id.id,
  782. 'lot_id': self.finished_lot_id.id,
  783. 'reserved_uom_qty': self.qty_producing,
  784. 'product_uom_id': self.product_uom_id.id,
  785. 'qty_done': self.qty_producing,
  786. 'location_id': production_move.location_id.id,
  787. 'location_dest_id': putaway_location.id,
  788. })
  789. else:
  790. rounding = production_move.product_uom.rounding
  791. production_move._set_quantity_done(
  792. float_round(self.qty_producing, precision_rounding=rounding)
  793. )
  794. def _check_sn_uniqueness(self):
  795. # todo master: remove
  796. pass
  797. def _should_start_timer(self):
  798. return True
  799. def _update_qty_producing(self, quantity):
  800. self.ensure_one()
  801. if self.qty_producing:
  802. self.qty_producing = quantity
  803. def get_working_duration(self):
  804. """Get the additional duration for 'open times' i.e. productivity lines with no date_end."""
  805. self.ensure_one()
  806. duration = 0
  807. for time in self.time_ids.filtered(lambda time: not time.date_end):
  808. duration += (datetime.now() - time.date_start).total_seconds() / 60
  809. return duration