fleet_vehicle.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding:utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class FleetVehicle(models.Model):
  6. _inherit = 'fleet.vehicle'
  7. mobility_card = fields.Char(compute='_compute_mobility_card', store=True)
  8. driver_employee_id = fields.Many2one(
  9. 'hr.employee', 'Driver (Employee)',
  10. compute='_compute_driver_employee_id', store=True,
  11. domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
  12. tracking=True,
  13. )
  14. driver_employee_name = fields.Char(related="driver_employee_id.name")
  15. future_driver_employee_id = fields.Many2one(
  16. 'hr.employee', 'Future Driver (Employee)',
  17. compute='_compute_future_driver_employee_id', store=True,
  18. domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
  19. tracking=True,
  20. )
  21. @api.depends('driver_id')
  22. def _compute_driver_employee_id(self):
  23. for vehicle in self:
  24. if vehicle.driver_id:
  25. vehicle.driver_employee_id = self.env['hr.employee'].search([
  26. ('address_home_id', '=', vehicle.driver_id.id),
  27. ], limit=1)
  28. else:
  29. vehicle.driver_employee_id = False
  30. @api.depends('future_driver_id')
  31. def _compute_future_driver_employee_id(self):
  32. for vehicle in self:
  33. if vehicle.future_driver_id:
  34. vehicle.future_driver_employee_id = self.env['hr.employee'].search([
  35. ('address_home_id', '=', vehicle.future_driver_id.id),
  36. ], limit=1)
  37. else:
  38. vehicle.future_driver_employee_id = False
  39. @api.depends('driver_id')
  40. def _compute_mobility_card(self):
  41. for vehicle in self:
  42. employee = self.env['hr.employee']
  43. if vehicle.driver_id:
  44. employee = employee.search([('address_home_id', '=', vehicle.driver_id.id)], limit=1)
  45. if not employee:
  46. employee = employee.search([('user_id.partner_id', '=', vehicle.driver_id.id)], limit=1)
  47. vehicle.mobility_card = employee.mobility_card
  48. def _update_create_write_vals(self, vals):
  49. if 'driver_employee_id' in vals:
  50. partner = False
  51. if vals['driver_employee_id']:
  52. employee = self.env['hr.employee'].sudo().browse(vals['driver_employee_id'])
  53. partner = employee.address_home_id.id
  54. vals['driver_id'] = partner
  55. elif 'driver_id' in vals:
  56. # Reverse the process if we can find a single employee
  57. employee = False
  58. if vals['driver_id']:
  59. # Limit to 2, we only care about the first one if he is the only one
  60. employee_ids = self.env['hr.employee'].sudo().search([
  61. ('address_home_id', '=', vals['driver_id'])
  62. ], limit=2)
  63. if len(employee_ids) == 1:
  64. employee = employee_ids[0].id
  65. vals['driver_employee_id'] = employee
  66. # Same for future driver
  67. if 'future_driver_employee_id' in vals:
  68. partner = False
  69. if vals['future_driver_employee_id']:
  70. employee = self.env['hr.employee'].sudo().browse(vals['future_driver_employee_id'])
  71. partner = employee.address_home_id.id
  72. vals['future_driver_id'] = partner
  73. elif 'future_driver_id' in vals:
  74. # Reverse the process if we can find a single employee
  75. employee = False
  76. if vals['future_driver_id']:
  77. # Limit to 2, we only care about the first one if he is the only one
  78. employee_ids = self.env['hr.employee'].sudo().search([
  79. ('address_home_id', '=', vals['future_driver_id'])
  80. ], limit=2)
  81. if len(employee_ids) == 1:
  82. employee = employee_ids[0].id
  83. vals['future_driver_employee_id'] = employee
  84. @api.model_create_multi
  85. def create(self, vals_list):
  86. for vals in vals_list:
  87. self._update_create_write_vals(vals)
  88. return super().create(vals_list)
  89. def write(self, vals):
  90. self._update_create_write_vals(vals)
  91. if 'driver_employee_id' in vals:
  92. for vehicle in self:
  93. if vehicle.driver_employee_id and vehicle.driver_employee_id.id != vals['driver_employee_id']:
  94. partners_to_unsubscribe = vehicle.driver_id.ids
  95. employee = vehicle.driver_employee_id
  96. if employee and employee.user_id.partner_id:
  97. partners_to_unsubscribe.append(employee.user_id.partner_id.id)
  98. vehicle.message_unsubscribe(partner_ids=partners_to_unsubscribe)
  99. return super().write(vals)
  100. def action_open_employee(self):
  101. self.ensure_one()
  102. return {
  103. 'name': _('Related Employee'),
  104. 'type': 'ir.actions.act_window',
  105. 'res_model': 'hr.employee',
  106. 'view_mode': 'form',
  107. 'res_id': self.driver_employee_id.id,
  108. }
  109. def open_assignation_logs(self):
  110. action = super().open_assignation_logs()
  111. action['views'] = [[self.env.ref('hr_fleet.fleet_vehicle_assignation_log_view_list').id, 'tree']]
  112. return action